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