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