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<XComponentContext>& _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( comphelper::getComponentContext(_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(std::exception)
67 : {
68 0 : StringSequence aSupported = OBoundControl::getSupportedServiceNames();
69 0 : aSupported.realloc(aSupported.getLength() + 1);
70 :
71 0 : 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( comphelper::getComponentContext(_rxFactory) ));
81 : }
82 :
83 :
84 0 : Sequence<Type> ODateModel::_getTypes()
85 : {
86 0 : return OEditBaseModel::_getTypes();
87 : }
88 :
89 :
90 0 : ODateModel::ODateModel(const Reference<XComponentContext>& _rxFactory)
91 : : OEditBaseModel(_rxFactory, VCL_CONTROLMODEL_DATEFIELD,
92 : FRM_SUN_CONTROL_DATEFIELD, sal_True, sal_True)
93 : // use the old control name for compytibility reasons
94 : , OLimitedFormats(_rxFactory, FormComponentType::DATEFIELD)
95 0 : , m_bDateTimeField(false)
96 : {
97 0 : m_nClassId = FormComponentType::DATEFIELD;
98 0 : initValueProperty( PROPERTY_DATE, PROPERTY_ID_DATE );
99 :
100 0 : setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_DATEFORMAT));
101 :
102 0 : osl_atomic_increment( &m_refCount );
103 : try
104 : {
105 0 : if ( m_xAggregateSet.is() )
106 0 : m_xAggregateSet->setPropertyValue( PROPERTY_DATEMIN, makeAny(util::Date(1, 1, 1800)) );
107 : }
108 0 : catch( const Exception& )
109 : {
110 : OSL_FAIL( "ODateModel::ODateModel: caught an exception!" );
111 : }
112 0 : osl_atomic_decrement( &m_refCount );
113 0 : }
114 :
115 :
116 0 : ODateModel::ODateModel( const ODateModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
117 : : OEditBaseModel(_pOriginal, _rxFactory)
118 : , OLimitedFormats(_rxFactory, FormComponentType::DATEFIELD)
119 0 : , m_bDateTimeField(false)
120 : {
121 0 : setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_DATEFORMAT ) );
122 0 : }
123 :
124 :
125 0 : ODateModel::~ODateModel( )
126 : {
127 0 : setAggregateSet(Reference< XFastPropertySet >(), -1);
128 0 : }
129 :
130 : // XCloneable
131 :
132 0 : IMPLEMENT_DEFAULT_CLONING( ODateModel )
133 :
134 : // XServiceInfo
135 :
136 0 : StringSequence SAL_CALL ODateModel::getSupportedServiceNames() throw(std::exception)
137 : {
138 0 : StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
139 :
140 0 : sal_Int32 nOldLen = aSupported.getLength();
141 0 : aSupported.realloc( nOldLen + 8 );
142 0 : OUString* pStoreTo = aSupported.getArray() + nOldLen;
143 :
144 0 : *pStoreTo++ = BINDABLE_CONTROL_MODEL;
145 0 : *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
146 0 : *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
147 :
148 0 : *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
149 0 : *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
150 :
151 0 : *pStoreTo++ = FRM_SUN_COMPONENT_DATEFIELD;
152 0 : *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_DATEFIELD;
153 0 : *pStoreTo++ = BINDABLE_DATABASE_DATE_FIELD;
154 :
155 0 : return aSupported;
156 : }
157 :
158 :
159 0 : OUString SAL_CALL ODateModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException, std::exception)
160 : {
161 0 : return OUString(FRM_COMPONENT_DATEFIELD); // old (non-sun) name for compatibility !
162 : }
163 :
164 : // XPropertySet
165 :
166 0 : void ODateModel::describeFixedProperties( Sequence< Property >& _rProps ) const
167 : {
168 0 : BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel )
169 0 : DECL_PROP3(DEFAULT_DATE, util::Date, BOUND, MAYBEDEFAULT, MAYBEVOID);
170 0 : DECL_PROP1(TABINDEX, sal_Int16, BOUND);
171 0 : DECL_PROP1(FORMATKEY, sal_Int32, TRANSIENT);
172 0 : DECL_IFACE_PROP2(FORMATSSUPPLIER, XNumberFormatsSupplier, READONLY, TRANSIENT);
173 : END_DESCRIBE_PROPERTIES();
174 0 : }
175 :
176 :
177 0 : void SAL_CALL ODateModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const
178 : {
179 0 : switch (_nHandle)
180 : {
181 : case PROPERTY_ID_FORMATKEY:
182 0 : getFormatKeyPropertyValue(_rValue);
183 0 : break;
184 : case PROPERTY_ID_FORMATSSUPPLIER:
185 0 : _rValue <<= getFormatsSupplier();
186 0 : break;
187 : default:
188 0 : OEditBaseModel::getFastPropertyValue(_rValue, _nHandle);
189 0 : break;
190 : }
191 0 : }
192 :
193 :
194 0 : sal_Bool SAL_CALL ODateModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue,
195 : sal_Int32 _nHandle, const Any& _rValue ) throw(IllegalArgumentException)
196 : {
197 0 : if (PROPERTY_ID_FORMATKEY == _nHandle)
198 0 : return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue);
199 : else
200 0 : return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue );
201 : }
202 :
203 :
204 0 : void SAL_CALL ODateModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue) throw ( ::com::sun::star::uno::Exception, std::exception)
205 : {
206 0 : if (PROPERTY_ID_FORMATKEY == _nHandle)
207 0 : setFormatKeyPropertyValue(_rValue);
208 : else
209 0 : OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
210 0 : }
211 :
212 : // XLoadListener
213 :
214 0 : void ODateModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
215 : {
216 0 : OBoundControlModel::onConnectedDbColumn( _rxForm );
217 0 : Reference<XPropertySet> xField = getField();
218 0 : if (xField.is())
219 : {
220 0 : m_bDateTimeField = sal_False;
221 : try
222 : {
223 0 : sal_Int32 nFieldType = 0;
224 0 : xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType;
225 0 : m_bDateTimeField = (nFieldType == DataType::TIMESTAMP);
226 : }
227 0 : catch(const Exception&)
228 : {
229 : }
230 0 : }
231 0 : }
232 :
233 :
234 0 : sal_Bool ODateModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
235 : {
236 0 : Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
237 0 : if ( !compare( aControlValue, m_aSaveValue ) )
238 : {
239 0 : if ( !aControlValue.hasValue() )
240 0 : m_xColumnUpdate->updateNull();
241 : else
242 : {
243 : try
244 : {
245 0 : util::Date aDate;
246 0 : if ( !( aControlValue >>= aDate ) )
247 : {
248 0 : sal_Int32 nAsInt(0);
249 0 : aControlValue >>= nAsInt;
250 0 : aDate = DBTypeConversion::toDate(nAsInt);
251 : }
252 :
253 0 : if ( !m_bDateTimeField )
254 0 : m_xColumnUpdate->updateDate( aDate );
255 : else
256 : {
257 0 : util::DateTime aDateTime = m_xColumn->getTimestamp();
258 0 : aDateTime.Day = aDate.Day;
259 0 : aDateTime.Month = aDate.Month;
260 0 : aDateTime.Year = aDate.Year;
261 0 : m_xColumnUpdate->updateTimestamp( aDateTime );
262 : }
263 : }
264 0 : catch(const Exception&)
265 : {
266 0 : return sal_False;
267 : }
268 : }
269 0 : m_aSaveValue = aControlValue;
270 : }
271 0 : return sal_True;
272 : }
273 :
274 :
275 0 : Any ODateModel::translateControlValueToExternalValue( ) const
276 : {
277 0 : return getControlValue();
278 : }
279 :
280 :
281 0 : Any ODateModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
282 : {
283 0 : return _rExternalValue;
284 : }
285 :
286 :
287 0 : Any ODateModel::translateControlValueToValidatableValue( ) const
288 : {
289 0 : return getControlValue();
290 : }
291 :
292 :
293 0 : Any ODateModel::translateDbColumnToControlValue()
294 : {
295 0 : util::Date aDate = m_xColumn->getDate();
296 0 : if (m_xColumn->wasNull())
297 0 : m_aSaveValue.clear();
298 : else
299 0 : m_aSaveValue <<= aDate;
300 :
301 0 : return m_aSaveValue;
302 : }
303 :
304 :
305 0 : Any ODateModel::getDefaultForReset() const
306 : {
307 0 : return m_aDefault;
308 : }
309 :
310 :
311 0 : void ODateModel::resetNoBroadcast()
312 : {
313 0 : OEditBaseModel::resetNoBroadcast();
314 0 : m_aSaveValue.clear();
315 0 : }
316 :
317 :
318 0 : Sequence< Type > ODateModel::getSupportedBindingTypes()
319 : {
320 0 : return Sequence< Type >( &::getCppuType( static_cast< util::Date* >( NULL ) ), 1 );
321 : }
322 :
323 :
324 : } // namespace frm
325 :
326 :
327 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|