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