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