1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: formattedcolumnvalue.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #include "connectivity/formattedcolumnvalue.hxx"
35 #include "connectivity/dbtools.hxx"
36 #include "connectivity/dbconversion.hxx"
38 /** === begin UNO includes === **/
39 #include <com/sun/star/util/XNumberFormatter.hpp>
40 #include <com/sun/star/util/Date.hpp>
41 #include <com/sun/star/sdbc/XConnection.hpp>
42 #include <com/sun/star/util/XNumberFormatTypes.hpp>
43 #include <com/sun/star/util/NumberFormat.hpp>
44 #include <com/sun/star/sdbc/DataType.hpp>
45 /** === end UNO includes === **/
47 #include <svtools/syslocale.hxx>
49 #include <tools/diagnose_ex.h>
51 #include <i18npool/mslangid.hxx>
53 #include <comphelper/numbers.hxx>
54 #include <comphelper/componentcontext.hxx>
56 //........................................................................
59 //........................................................................
61 /** === begin UNO using === **/
62 using ::com::sun::star::uno::Reference
;
63 using ::com::sun::star::uno::UNO_QUERY
;
64 using ::com::sun::star::uno::UNO_QUERY_THROW
;
65 using ::com::sun::star::uno::Exception
;
66 using ::com::sun::star::uno::RuntimeException
;
67 using ::com::sun::star::uno::Any
;
68 using ::com::sun::star::uno::makeAny
;
69 using ::com::sun::star::sdbc::XRowSet
;
70 using ::com::sun::star::beans::XPropertySet
;
71 using ::com::sun::star::util::XNumberFormatter
;
72 using ::com::sun::star::util::Date
;
73 using ::com::sun::star::sdbc::XConnection
;
74 using ::com::sun::star::util::XNumberFormatsSupplier
;
75 using ::com::sun::star::beans::XPropertySetInfo
;
76 using ::com::sun::star::lang::Locale
;
77 using ::com::sun::star::util::XNumberFormatTypes
;
78 using ::com::sun::star::sdb::XColumn
;
79 using ::com::sun::star::sdb::XColumnUpdate
;
80 using ::com::sun::star::lang::XComponent
;
81 /** === end UNO using === **/
82 namespace DataType
= ::com::sun::star::sdbc::DataType
;
83 namespace NumberFormat
= ::com::sun::star::util::NumberFormat
;
85 //====================================================================
86 //= FormattedColumnValue_Data
87 //====================================================================
88 struct FormattedColumnValue_Data
90 Reference
< XNumberFormatter
> m_xFormatter
;
92 sal_Int32 m_nFormatKey
;
93 sal_Int32 m_nFieldType
;
97 Reference
< XColumn
> m_xColumn
;
98 Reference
< XColumnUpdate
> m_xColumnUpdate
;
100 FormattedColumnValue_Data()
102 ,m_aNullDate( DBTypeConversion::getStandardDate() )
104 ,m_nFieldType( DataType::OTHER
)
105 ,m_nKeyType( NumberFormat::UNDEFINED
)
106 ,m_bNumericField( false )
113 //--------------------------------------------------------------------
116 //................................................................
117 void lcl_clear_nothrow( FormattedColumnValue_Data
& _rData
)
119 if ( _rData
.m_xFormatter
.is() )
123 Reference
< XComponent
> xFormatterComp( _rData
.m_xFormatter
, UNO_QUERY
);
124 if ( xFormatterComp
.is() )
125 xFormatterComp
->dispose();
127 catch( const Exception
& )
129 DBG_UNHANDLED_EXCEPTION();
131 _rData
.m_xFormatter
.clear();
134 _rData
.m_nFormatKey
= 0;
135 _rData
.m_nFieldType
= DataType::OTHER
;
136 _rData
.m_nKeyType
= NumberFormat::UNDEFINED
;
137 _rData
.m_bNumericField
= false;
139 _rData
.m_xColumn
.clear();
140 _rData
.m_xColumnUpdate
.clear();
143 //................................................................
144 void lcl_initColumnDataValue_nothrow( const ::comphelper::ComponentContext
& _rContext
, FormattedColumnValue_Data
& _rData
,
145 const Reference
< XRowSet
>& _rxRowSet
, const Reference
< XPropertySet
>& _rxColumn
)
147 lcl_clear_nothrow( _rData
);
149 OSL_PRECOND( _rxRowSet
.is(), "lcl_initColumnDataValue_nothrow: no row set!" );
150 OSL_PRECOND( _rxColumn
.is(), "lcl_initColumnDataValue_nothrow: no column!" );
151 if ( !_rxRowSet
.is() || !_rxColumn
.is() )
156 _rData
.m_xColumn
.set( _rxColumn
, UNO_QUERY_THROW
);
157 _rData
.m_xColumnUpdate
.set( _rxColumn
, UNO_QUERY
);
159 OSL_VERIFY( _rxColumn
->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Type" ) ) ) >>= _rData
.m_nFieldType
);
161 switch ( _rData
.m_nFieldType
)
165 case DataType::TIMESTAMP
:
167 case DataType::BOOLEAN
:
168 case DataType::TINYINT
:
169 case DataType::SMALLINT
:
170 case DataType::INTEGER
:
172 case DataType::BIGINT
:
173 case DataType::DOUBLE
:
174 case DataType::NUMERIC
:
175 case DataType::DECIMAL
:
176 _rData
.m_bNumericField
= true;
179 _rData
.m_bNumericField
= false;
183 // get the number formats supplier of the connection of the form
184 Reference
< XConnection
> xConnection( getConnection( _rxRowSet
), UNO_QUERY_THROW
);
185 Reference
< XNumberFormatsSupplier
> xSupplier( getNumberFormats( xConnection
, sal_False
, _rContext
.getLegacyServiceFactory() ), UNO_QUERY_THROW
);
187 // get the format key of our bound field
188 Reference
< XPropertySetInfo
> xPSI( _rxColumn
->getPropertySetInfo(), UNO_QUERY_THROW
);
189 bool bHaveFieldFormat
= false;
190 const ::rtl::OUString
sFormatKeyProperty( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FormatKey" ) ) );
191 if ( xPSI
->hasPropertyByName( sFormatKeyProperty
) )
193 bHaveFieldFormat
= ( _rxColumn
->getPropertyValue( sFormatKeyProperty
) >>= _rData
.m_nFormatKey
);
195 if ( !bHaveFieldFormat
)
197 // fall back to a format key as indicated by the field type
198 Locale aSystemLocale
;
199 MsLangId::convertLanguageToLocale( MsLangId::getSystemLanguage(), aSystemLocale
);
200 Reference
< XNumberFormatTypes
> xNumTypes( xSupplier
->getNumberFormats(), UNO_QUERY_THROW
);
201 _rData
.m_nFormatKey
= getDefaultNumberFormat( _rxColumn
, xNumTypes
, aSystemLocale
);
204 // some more formatter settings
205 _rData
.m_nKeyType
= ::comphelper::getNumberFormatType( xSupplier
->getNumberFormats(), _rData
.m_nFormatKey
);
206 Reference
< XPropertySet
> xFormatSettings( xSupplier
->getNumberFormatSettings(), UNO_QUERY_THROW
);
207 OSL_VERIFY( xFormatSettings
->getPropertyValue( ::rtl::OUString::createFromAscii( "NullDate" ) ) >>= _rData
.m_aNullDate
);
209 // create a formatter working with the connection's number format supplier
210 _rData
.m_xFormatter
.set( _rContext
.createComponent( "com.sun.star.util.NumberFormatter" ), UNO_QUERY_THROW
);
211 _rData
.m_xFormatter
->attachNumberFormatsSupplier( xSupplier
);
213 catch( const Exception
& )
215 DBG_UNHANDLED_EXCEPTION();
220 //====================================================================
221 //= FormattedColumnValue
222 //====================================================================
223 //--------------------------------------------------------------------
224 FormattedColumnValue::FormattedColumnValue( const ::comphelper::ComponentContext
& _rContext
,
225 const Reference
< XRowSet
>& _rxRowSet
, const Reference
< XPropertySet
>& _rxColumn
)
226 :m_pData( new FormattedColumnValue_Data
)
228 lcl_initColumnDataValue_nothrow( _rContext
, *m_pData
, _rxRowSet
, _rxColumn
);
231 //--------------------------------------------------------------------
232 void FormattedColumnValue::clear()
234 lcl_clear_nothrow( *m_pData
);
237 //--------------------------------------------------------------------
238 FormattedColumnValue::~FormattedColumnValue()
243 //--------------------------------------------------------------------
244 sal_Int32
FormattedColumnValue::getFormatKey() const
246 return m_pData
->m_nFormatKey
;
249 //--------------------------------------------------------------------
250 sal_Int32
FormattedColumnValue::getFieldType() const
252 return m_pData
->m_nFieldType
;
255 //--------------------------------------------------------------------
256 sal_Int16
FormattedColumnValue::getKeyType() const
258 return m_pData
->m_nKeyType
;
261 //--------------------------------------------------------------------
262 bool FormattedColumnValue::isNumericField() const
264 return m_pData
->m_bNumericField
;
267 //--------------------------------------------------------------------
268 const Reference
< XColumn
>& FormattedColumnValue::getColumn() const
270 return m_pData
->m_xColumn
;
273 //--------------------------------------------------------------------
274 const Reference
< XColumnUpdate
>& FormattedColumnValue::getColumnUpdate() const
276 return m_pData
->m_xColumnUpdate
;
279 //--------------------------------------------------------------------
280 bool FormattedColumnValue::setFormattedValue( const ::rtl::OUString
& _rFormattedStringValue
) const
282 OSL_PRECOND( m_pData
->m_xColumnUpdate
.is(), "FormattedColumnValue::setFormattedValue: no column!" );
283 if ( !m_pData
->m_xColumnUpdate
.is() )
288 if ( m_pData
->m_bNumericField
)
290 ::dbtools::DBTypeConversion::setValue( m_pData
->m_xColumnUpdate
, m_pData
->m_xFormatter
, m_pData
->m_aNullDate
,
291 _rFormattedStringValue
, m_pData
->m_nFormatKey
, ::sal::static_int_cast
< sal_Int16
>( m_pData
->m_nFieldType
),
292 m_pData
->m_nKeyType
);
296 m_pData
->m_xColumnUpdate
->updateString( _rFormattedStringValue
);
299 catch( const Exception
& )
306 //--------------------------------------------------------------------
307 ::rtl::OUString
FormattedColumnValue::getFormattedValue() const
309 OSL_PRECOND( m_pData
->m_xColumn
.is(), "FormattedColumnValue::setFormattedValue: no column!" );
311 ::rtl::OUString sStringValue
;
312 if ( m_pData
->m_xColumn
.is() )
314 sStringValue
= DBTypeConversion::getValue(
315 m_pData
->m_xColumn
, m_pData
->m_xFormatter
, m_pData
->m_aNullDate
, m_pData
->m_nFormatKey
, m_pData
->m_nKeyType
321 //........................................................................
322 } // namespace dbtools
323 //........................................................................