Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "Date.hxx"
21 : #include <tools/debug.hxx>
22 : #include <tools/date.hxx>
23 : #include <connectivity/dbconversion.hxx>
24 : #include <com/sun/star/sdbc/DataType.hpp>
25 : #include <comphelper/processfactory.hxx>
26 :
27 : using namespace dbtools;
28 :
29 : //.........................................................................
30 : namespace frm
31 : {
32 : //.........................................................................
33 :
34 : using namespace ::com::sun::star;
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::com::sun::star::sdb;
37 : using namespace ::com::sun::star::sdbc;
38 : using namespace ::com::sun::star::sdbcx;
39 : using namespace ::com::sun::star::beans;
40 : using namespace ::com::sun::star::util;
41 : using namespace ::com::sun::star::container;
42 : using namespace ::com::sun::star::form;
43 : using namespace ::com::sun::star::awt;
44 : using namespace ::com::sun::star::io;
45 : using namespace ::com::sun::star::lang;
46 :
47 : //------------------------------------------------------------------
48 0 : ODateControl::ODateControl(const Reference<XMultiServiceFactory>& _rxFactory)
49 0 : :OBoundControl(_rxFactory, VCL_CONTROL_DATEFIELD)
50 : {
51 0 : }
52 :
53 : //------------------------------------------------------------------
54 0 : InterfaceRef SAL_CALL ODateControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
55 : {
56 0 : return *(new ODateControl(_rxFactory));
57 : }
58 :
59 : //------------------------------------------------------------------------------
60 0 : Sequence<Type> ODateControl::_getTypes()
61 : {
62 0 : return OBoundControl::_getTypes();
63 : }
64 :
65 : //------------------------------------------------------------------------------
66 0 : StringSequence SAL_CALL ODateControl::getSupportedServiceNames() throw()
67 : {
68 0 : StringSequence aSupported = OBoundControl::getSupportedServiceNames();
69 0 : aSupported.realloc(aSupported.getLength() + 1);
70 :
71 0 : ::rtl::OUString*pArray = aSupported.getArray();
72 0 : pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_DATEFIELD;
73 0 : return aSupported;
74 : }
75 :
76 : /*************************************************************************/
77 : //------------------------------------------------------------------
78 0 : InterfaceRef SAL_CALL ODateModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
79 : {
80 0 : return *(new ODateModel(_rxFactory));
81 : }
82 :
83 : //------------------------------------------------------------------------------
84 0 : Sequence<Type> ODateModel::_getTypes()
85 : {
86 0 : return OEditBaseModel::_getTypes();
87 : }
88 :
89 : //------------------------------------------------------------------
90 : DBG_NAME( ODateModel )
91 : //------------------------------------------------------------------
92 0 : ODateModel::ODateModel(const Reference<XMultiServiceFactory>& _rxFactory)
93 : :OEditBaseModel( _rxFactory, VCL_CONTROLMODEL_DATEFIELD, FRM_SUN_CONTROL_DATEFIELD, sal_True, sal_True )
94 : // use the old control name for compytibility reasons
95 0 : ,OLimitedFormats( comphelper::getComponentContext(_rxFactory), FormComponentType::DATEFIELD )
96 : {
97 : DBG_CTOR( ODateModel, NULL );
98 :
99 0 : m_nClassId = FormComponentType::DATEFIELD;
100 0 : initValueProperty( PROPERTY_DATE, PROPERTY_ID_DATE );
101 :
102 0 : setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_DATEFORMAT));
103 :
104 0 : osl_atomic_increment( &m_refCount );
105 : try
106 : {
107 0 : if ( m_xAggregateSet.is() )
108 0 : m_xAggregateSet->setPropertyValue( PROPERTY_DATEMIN, makeAny( (sal_Int32)( ::Date( 1, 1, 1800 ).GetDate() ) ) );
109 : }
110 0 : catch( const Exception& )
111 : {
112 : OSL_FAIL( "ODateModel::ODateModel: caught an exception!" );
113 : }
114 0 : osl_atomic_decrement( &m_refCount );
115 0 : }
116 :
117 : //------------------------------------------------------------------------------
118 0 : ODateModel::ODateModel( const ODateModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
119 : :OEditBaseModel( _pOriginal, _rxFactory )
120 0 : ,OLimitedFormats( comphelper::getComponentContext(_rxFactory), FormComponentType::DATEFIELD )
121 : {
122 : DBG_CTOR( ODateModel, NULL );
123 :
124 0 : setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_DATEFORMAT ) );
125 0 : }
126 :
127 : //------------------------------------------------------------------------------
128 0 : ODateModel::~ODateModel( )
129 : {
130 0 : setAggregateSet(Reference< XFastPropertySet >(), -1);
131 : DBG_DTOR( ODateModel, NULL );
132 0 : }
133 :
134 : // XCloneable
135 : //------------------------------------------------------------------------------
136 0 : IMPLEMENT_DEFAULT_CLONING( ODateModel )
137 :
138 : // XServiceInfo
139 : //------------------------------------------------------------------------------
140 0 : StringSequence SAL_CALL ODateModel::getSupportedServiceNames() throw()
141 : {
142 0 : StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
143 :
144 0 : sal_Int32 nOldLen = aSupported.getLength();
145 0 : aSupported.realloc( nOldLen + 8 );
146 0 : ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
147 :
148 0 : *pStoreTo++ = BINDABLE_CONTROL_MODEL;
149 0 : *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
150 0 : *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
151 :
152 0 : *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
153 0 : *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
154 :
155 0 : *pStoreTo++ = FRM_SUN_COMPONENT_DATEFIELD;
156 0 : *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_DATEFIELD;
157 0 : *pStoreTo++ = BINDABLE_DATABASE_DATE_FIELD;
158 :
159 0 : return aSupported;
160 : }
161 :
162 : //------------------------------------------------------------------------------
163 0 : ::rtl::OUString SAL_CALL ODateModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException)
164 : {
165 0 : return FRM_COMPONENT_DATEFIELD; // old (non-sun) name for compatibility !
166 : }
167 :
168 : // XPropertySet
169 : //------------------------------------------------------------------------------
170 0 : void ODateModel::describeFixedProperties( Sequence< Property >& _rProps ) const
171 : {
172 0 : BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel )
173 0 : DECL_PROP3(DEFAULT_DATE, sal_Int32, BOUND, MAYBEDEFAULT, MAYBEVOID);
174 0 : DECL_PROP1(TABINDEX, sal_Int16, BOUND);
175 0 : DECL_PROP1(FORMATKEY, sal_Int32, TRANSIENT);
176 0 : DECL_IFACE_PROP2(FORMATSSUPPLIER, XNumberFormatsSupplier, READONLY, TRANSIENT);
177 : END_DESCRIBE_PROPERTIES();
178 0 : }
179 :
180 : //------------------------------------------------------------------------------
181 0 : void SAL_CALL ODateModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const
182 : {
183 0 : switch (_nHandle)
184 : {
185 : case PROPERTY_ID_FORMATKEY:
186 0 : getFormatKeyPropertyValue(_rValue);
187 0 : break;
188 : case PROPERTY_ID_FORMATSSUPPLIER:
189 0 : _rValue <<= getFormatsSupplier();
190 0 : break;
191 : default:
192 0 : OEditBaseModel::getFastPropertyValue(_rValue, _nHandle);
193 0 : break;
194 : }
195 0 : }
196 :
197 : //------------------------------------------------------------------------------
198 0 : sal_Bool SAL_CALL ODateModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue,
199 : sal_Int32 _nHandle, const Any& _rValue ) throw(IllegalArgumentException)
200 : {
201 0 : if (PROPERTY_ID_FORMATKEY == _nHandle)
202 0 : return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue);
203 : else
204 0 : return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue );
205 : }
206 :
207 : //------------------------------------------------------------------------------
208 0 : void SAL_CALL ODateModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue) throw ( ::com::sun::star::uno::Exception)
209 : {
210 0 : if (PROPERTY_ID_FORMATKEY == _nHandle)
211 0 : setFormatKeyPropertyValue(_rValue);
212 : else
213 0 : OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
214 0 : }
215 :
216 : // XLoadListener
217 : //------------------------------------------------------------------------------
218 0 : void ODateModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
219 : {
220 0 : OBoundControlModel::onConnectedDbColumn( _rxForm );
221 0 : Reference<XPropertySet> xField = getField();
222 0 : if (xField.is())
223 : {
224 0 : m_bDateTimeField = sal_False;
225 : try
226 : {
227 0 : sal_Int32 nFieldType = 0;
228 0 : xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType;
229 0 : m_bDateTimeField = (nFieldType == DataType::TIMESTAMP);
230 : }
231 0 : catch(const Exception&)
232 : {
233 : }
234 0 : }
235 0 : }
236 :
237 : //------------------------------------------------------------------------------
238 0 : sal_Bool ODateModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
239 : {
240 0 : Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
241 0 : if ( !compare( aControlValue, m_aSaveValue ) )
242 : {
243 0 : if ( !aControlValue.hasValue() )
244 0 : m_xColumnUpdate->updateNull();
245 : else
246 : {
247 : try
248 : {
249 0 : util::Date aDate;
250 0 : if ( !( aControlValue >>= aDate ) )
251 : {
252 0 : sal_Int32 nAsInt(0);
253 0 : aControlValue >>= nAsInt;
254 0 : aDate = DBTypeConversion::toDate(nAsInt);
255 : }
256 :
257 0 : if ( !m_bDateTimeField )
258 0 : m_xColumnUpdate->updateDate( aDate );
259 : else
260 : {
261 0 : util::DateTime aDateTime = m_xColumn->getTimestamp();
262 0 : aDateTime.Day = aDate.Day;
263 0 : aDateTime.Month = aDate.Month;
264 0 : aDateTime.Year = aDate.Year;
265 0 : m_xColumnUpdate->updateTimestamp( aDateTime );
266 : }
267 : }
268 0 : catch(const Exception&)
269 : {
270 0 : return sal_False;
271 : }
272 : }
273 0 : m_aSaveValue = aControlValue;
274 : }
275 0 : return sal_True;
276 : }
277 :
278 : //------------------------------------------------------------------------------
279 0 : void ODateModel::impl_translateControlValueToUNODate( Any& _rUNOValue ) const
280 : {
281 0 : _rUNOValue = getControlValue();
282 0 : if ( _rUNOValue.hasValue() )
283 : {
284 0 : sal_Int32 nDate = 0;
285 0 : OSL_VERIFY( _rUNOValue >>= nDate );
286 0 : _rUNOValue <<= DBTypeConversion::toDate( nDate );
287 : }
288 0 : }
289 :
290 : //------------------------------------------------------------------------------
291 0 : Any ODateModel::translateControlValueToExternalValue( ) const
292 : {
293 0 : Any aExternalValue;
294 0 : impl_translateControlValueToUNODate( aExternalValue );
295 0 : return aExternalValue;
296 : }
297 :
298 : //------------------------------------------------------------------------------
299 0 : Any ODateModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
300 : {
301 0 : Any aControlValue;
302 0 : if ( _rExternalValue.hasValue() )
303 : {
304 0 : util::Date aDate;
305 0 : OSL_VERIFY( _rExternalValue >>= aDate );
306 0 : aControlValue <<= DBTypeConversion::toINT32( aDate );
307 : }
308 0 : return aControlValue;
309 : }
310 :
311 : //------------------------------------------------------------------------------
312 0 : Any ODateModel::translateControlValueToValidatableValue( ) const
313 : {
314 0 : Any aValidatableValue;
315 0 : impl_translateControlValueToUNODate( aValidatableValue );
316 0 : return aValidatableValue;
317 : }
318 :
319 : //------------------------------------------------------------------------------
320 0 : Any ODateModel::translateDbColumnToControlValue()
321 : {
322 0 : util::Date aDate = m_xColumn->getDate();
323 0 : if (m_xColumn->wasNull())
324 0 : m_aSaveValue.clear();
325 : else
326 : // the aggregated set expects an Int32 as value ...
327 0 : m_aSaveValue <<= DBTypeConversion::toINT32(aDate);
328 :
329 0 : return m_aSaveValue;
330 : }
331 :
332 : //------------------------------------------------------------------------------
333 0 : Any ODateModel::getDefaultForReset() const
334 : {
335 0 : return m_aDefault;
336 : }
337 :
338 : //------------------------------------------------------------------------------
339 0 : void ODateModel::resetNoBroadcast()
340 : {
341 0 : OEditBaseModel::resetNoBroadcast();
342 0 : m_aSaveValue.clear();
343 0 : }
344 :
345 : //------------------------------------------------------------------------------
346 0 : Sequence< Type > ODateModel::getSupportedBindingTypes()
347 : {
348 0 : return Sequence< Type >( &::getCppuType( static_cast< util::Date* >( NULL ) ), 1 );
349 : }
350 :
351 : //.........................................................................
352 : } // namespace frm
353 : //.........................................................................
354 :
355 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|