Branch data 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 : :
21 : : #include "connectivity/formattedcolumnvalue.hxx"
22 : : #include "connectivity/dbtools.hxx"
23 : : #include "connectivity/dbconversion.hxx"
24 : :
25 : : #include <com/sun/star/util/XNumberFormatter.hpp>
26 : : #include <com/sun/star/util/Date.hpp>
27 : : #include <com/sun/star/sdbc/XConnection.hpp>
28 : : #include <com/sun/star/util/XNumberFormatTypes.hpp>
29 : : #include <com/sun/star/util/NumberFormat.hpp>
30 : : #include <com/sun/star/sdbc/DataType.hpp>
31 : :
32 : : #include <tools/diagnose_ex.h>
33 : : #include <i18npool/mslangid.hxx>
34 : : #include <comphelper/numbers.hxx>
35 : : #include <comphelper/componentcontext.hxx>
36 : : #include <unotools/sharedunocomponent.hxx>
37 : :
38 : : //........................................................................
39 : : namespace dbtools
40 : : {
41 : : //........................................................................
42 : :
43 : : /** === begin UNO using === **/
44 : : using ::com::sun::star::uno::Reference;
45 : : using ::com::sun::star::uno::UNO_QUERY;
46 : : using ::com::sun::star::uno::UNO_QUERY_THROW;
47 : : using ::com::sun::star::uno::UNO_SET_THROW;
48 : : using ::com::sun::star::uno::Exception;
49 : : using ::com::sun::star::uno::RuntimeException;
50 : : using ::com::sun::star::uno::Any;
51 : : using ::com::sun::star::uno::makeAny;
52 : : using ::com::sun::star::sdbc::XRowSet;
53 : : using ::com::sun::star::beans::XPropertySet;
54 : : using ::com::sun::star::util::XNumberFormatter;
55 : : using ::com::sun::star::util::Date;
56 : : using ::com::sun::star::sdbc::XConnection;
57 : : using ::com::sun::star::util::XNumberFormatsSupplier;
58 : : using ::com::sun::star::beans::XPropertySetInfo;
59 : : using ::com::sun::star::lang::Locale;
60 : : using ::com::sun::star::util::XNumberFormatTypes;
61 : : using ::com::sun::star::sdb::XColumn;
62 : : using ::com::sun::star::sdb::XColumnUpdate;
63 : : using ::com::sun::star::lang::XComponent;
64 : : /** === end UNO using === **/
65 : : namespace DataType = ::com::sun::star::sdbc::DataType;
66 : : namespace NumberFormat = ::com::sun::star::util::NumberFormat;
67 : :
68 : : //====================================================================
69 : : //= FormattedColumnValue_Data
70 : : //====================================================================
71 : 34 : struct FormattedColumnValue_Data
72 : : {
73 : : Reference< XNumberFormatter > m_xFormatter;
74 : : Date m_aNullDate;
75 : : sal_Int32 m_nFormatKey;
76 : : sal_Int32 m_nFieldType;
77 : : sal_Int16 m_nKeyType;
78 : : bool m_bNumericField;
79 : :
80 : : Reference< XColumn > m_xColumn;
81 : : Reference< XColumnUpdate > m_xColumnUpdate;
82 : :
83 : 34 : FormattedColumnValue_Data()
84 : : :m_xFormatter()
85 : : ,m_aNullDate( DBTypeConversion::getStandardDate() )
86 : : ,m_nFormatKey( 0 )
87 : : ,m_nFieldType( DataType::OTHER )
88 : : ,m_nKeyType( NumberFormat::UNDEFINED )
89 : : ,m_bNumericField( false )
90 : : ,m_xColumn()
91 [ + - ]: 34 : ,m_xColumnUpdate()
92 : : {
93 : 34 : }
94 : : };
95 : :
96 : : //--------------------------------------------------------------------
97 : : namespace
98 : : {
99 : : //................................................................
100 : 68 : void lcl_clear_nothrow( FormattedColumnValue_Data& _rData )
101 : : {
102 : 68 : _rData.m_xFormatter.clear();
103 : 68 : _rData.m_nFormatKey = 0;
104 : 68 : _rData.m_nFieldType = DataType::OTHER;
105 : 68 : _rData.m_nKeyType = NumberFormat::UNDEFINED;
106 : 68 : _rData.m_bNumericField = false;
107 : :
108 : 68 : _rData.m_xColumn.clear();
109 : 68 : _rData.m_xColumnUpdate.clear();
110 : 68 : }
111 : :
112 : : //................................................................
113 : 34 : void lcl_initColumnDataValue_nothrow( FormattedColumnValue_Data& _rData,
114 : : const Reference< XNumberFormatter >& i_rNumberFormatter, const Reference< XPropertySet >& _rxColumn )
115 : : {
116 : 34 : lcl_clear_nothrow( _rData );
117 : :
118 : : OSL_PRECOND( i_rNumberFormatter.is(), "lcl_initColumnDataValue_nothrow: no number formats -> no formatted values!" );
119 [ - + ]: 34 : if ( !i_rNumberFormatter.is() )
120 : 34 : return;
121 : :
122 : : try
123 : : {
124 [ + - ][ + - ]: 34 : Reference< XNumberFormatsSupplier > xNumberFormatsSupp( i_rNumberFormatter->getNumberFormatsSupplier(), UNO_SET_THROW );
[ + - ]
125 : :
126 : : // remember the column
127 [ + - ]: 34 : _rData.m_xColumn.set( _rxColumn, UNO_QUERY_THROW );
128 [ + - ]: 34 : _rData.m_xColumnUpdate.set( _rxColumn, UNO_QUERY );
129 : :
130 : : // determine the field type, and whether it's a numeric field
131 [ + - ][ + - ]: 34 : OSL_VERIFY( _rxColumn->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Type" ) ) ) >>= _rData.m_nFieldType );
[ + - ]
132 : :
133 [ - + ]: 34 : switch ( _rData.m_nFieldType )
134 : : {
135 : : case DataType::DATE:
136 : : case DataType::TIME:
137 : : case DataType::TIMESTAMP:
138 : : case DataType::BIT:
139 : : case DataType::BOOLEAN:
140 : : case DataType::TINYINT:
141 : : case DataType::SMALLINT:
142 : : case DataType::INTEGER:
143 : : case DataType::REAL:
144 : : case DataType::BIGINT:
145 : : case DataType::DOUBLE:
146 : : case DataType::NUMERIC:
147 : : case DataType::DECIMAL:
148 : 0 : _rData.m_bNumericField = true;
149 : 0 : break;
150 : : default:
151 : 34 : _rData.m_bNumericField = false;
152 : 34 : break;
153 : : }
154 : :
155 : : // get the format key of our bound field
156 [ + - ][ + - ]: 34 : Reference< XPropertySetInfo > xPSI( _rxColumn->getPropertySetInfo(), UNO_QUERY_THROW );
[ + - ]
157 : 34 : bool bHaveFieldFormat = false;
158 [ + - ]: 34 : const ::rtl::OUString sFormatKeyProperty( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FormatKey" ) ) );
159 [ + - ][ + - ]: 34 : if ( xPSI->hasPropertyByName( sFormatKeyProperty ) )
[ + - ]
160 : : {
161 [ + - ][ + - ]: 34 : bHaveFieldFormat = ( _rxColumn->getPropertyValue( sFormatKeyProperty ) >>= _rData.m_nFormatKey );
162 : : }
163 [ - + ]: 34 : if ( !bHaveFieldFormat )
164 : : {
165 : : // fall back to a format key as indicated by the field type
166 : 0 : Locale aSystemLocale;
167 [ # # ][ # # ]: 0 : MsLangId::convertLanguageToLocale( MsLangId::getSystemLanguage(), aSystemLocale );
168 [ # # ][ # # ]: 0 : Reference< XNumberFormatTypes > xNumTypes( xNumberFormatsSupp->getNumberFormats(), UNO_QUERY_THROW );
[ # # ]
169 [ # # ]: 0 : _rData.m_nFormatKey = getDefaultNumberFormat( _rxColumn, xNumTypes, aSystemLocale );
170 : : }
171 : :
172 : : // some more formatter settings
173 [ + - ][ + - ]: 34 : _rData.m_nKeyType = ::comphelper::getNumberFormatType( xNumberFormatsSupp->getNumberFormats(), _rData.m_nFormatKey );
[ + - ]
174 [ + - ][ + - ]: 34 : Reference< XPropertySet > xFormatSettings( xNumberFormatsSupp->getNumberFormatSettings(), UNO_QUERY_THROW );
[ + - ]
175 [ + - ][ + - ]: 34 : OSL_VERIFY( xFormatSettings->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "NullDate" )) ) >>= _rData.m_aNullDate );
[ + - ][ + - ]
176 : :
177 : : // remember the formatter
178 [ # # ][ + - ]: 34 : _rData.m_xFormatter = i_rNumberFormatter;
179 : : }
180 : 0 : catch( const Exception& )
181 : : {
182 : : DBG_UNHANDLED_EXCEPTION();
183 : : }
184 : : }
185 : :
186 : : //................................................................
187 : 34 : void lcl_initColumnDataValue_nothrow( const ::comphelper::ComponentContext& i_rContext, FormattedColumnValue_Data& i_rData,
188 : : const Reference< XRowSet >& i_rRowSet, const Reference< XPropertySet >& i_rColumn )
189 : : {
190 : : OSL_PRECOND( i_rRowSet.is(), "lcl_initColumnDataValue_nothrow: no row set!" );
191 [ + - ]: 34 : if ( !i_rRowSet.is() )
192 : 34 : return;
193 : :
194 : 34 : Reference< XNumberFormatter > xNumberFormatter;
195 : : try
196 : : {
197 : : // get the number formats supplier of the connection of the form
198 [ + - ][ + - ]: 34 : Reference< XConnection > xConnection( getConnection( i_rRowSet ), UNO_QUERY_THROW );
199 [ + - ][ + - ]: 34 : Reference< XNumberFormatsSupplier > xSupplier( getNumberFormats( xConnection, sal_True, i_rContext.getLegacyServiceFactory() ), UNO_SET_THROW );
[ + - ]
200 : :
201 : : // create a number formatter for it
202 [ + - ][ + - ]: 34 : xNumberFormatter.set( i_rContext.createComponent( "com.sun.star.util.NumberFormatter" ), UNO_QUERY_THROW );
203 [ + - ][ # # ]: 34 : xNumberFormatter->attachNumberFormatsSupplier( xSupplier );
[ + - ]
204 : : }
205 [ # # ]: 0 : catch( const Exception& )
206 : : {
207 : : DBG_UNHANDLED_EXCEPTION();
208 : : }
209 : :
210 [ + - ]: 34 : lcl_initColumnDataValue_nothrow( i_rData, xNumberFormatter, i_rColumn );
211 : : }
212 : : }
213 : :
214 : : //====================================================================
215 : : //= FormattedColumnValue
216 : : //====================================================================
217 : : //--------------------------------------------------------------------
218 : 34 : FormattedColumnValue::FormattedColumnValue( const ::comphelper::ComponentContext& i_rContext,
219 : : const Reference< XRowSet >& _rxRowSet, const Reference< XPropertySet >& i_rColumn )
220 [ + - ][ + - ]: 34 : :m_pData( new FormattedColumnValue_Data )
221 : : {
222 [ + - ]: 34 : lcl_initColumnDataValue_nothrow( i_rContext, *m_pData, _rxRowSet, i_rColumn );
223 : 34 : }
224 : :
225 : : //--------------------------------------------------------------------
226 : 0 : FormattedColumnValue::FormattedColumnValue( const Reference< XNumberFormatter >& i_rNumberFormatter,
227 : : const Reference< XPropertySet >& _rxColumn )
228 [ # # ][ # # ]: 0 : :m_pData( new FormattedColumnValue_Data )
229 : : {
230 [ # # ]: 0 : lcl_initColumnDataValue_nothrow( *m_pData, i_rNumberFormatter, _rxColumn );
231 : 0 : }
232 : :
233 : : //--------------------------------------------------------------------
234 : 34 : void FormattedColumnValue::clear()
235 : : {
236 : 34 : lcl_clear_nothrow( *m_pData );
237 : 34 : }
238 : :
239 : : //--------------------------------------------------------------------
240 : 34 : FormattedColumnValue::~FormattedColumnValue()
241 : : {
242 [ + - ]: 34 : clear();
243 [ - + ]: 68 : }
244 : :
245 : : //--------------------------------------------------------------------
246 : 0 : sal_Int32 FormattedColumnValue::getFormatKey() const
247 : : {
248 : 0 : return m_pData->m_nFormatKey;
249 : : }
250 : :
251 : : //--------------------------------------------------------------------
252 : 0 : sal_Int32 FormattedColumnValue::getFieldType() const
253 : : {
254 : 0 : return m_pData->m_nFieldType;
255 : : }
256 : :
257 : : //--------------------------------------------------------------------
258 : 34 : sal_Int16 FormattedColumnValue::getKeyType() const
259 : : {
260 : 34 : return m_pData->m_nKeyType;
261 : : }
262 : :
263 : : //--------------------------------------------------------------------
264 : 0 : bool FormattedColumnValue::isNumericField() const
265 : : {
266 : 0 : return m_pData->m_bNumericField;
267 : : }
268 : :
269 : : //--------------------------------------------------------------------
270 : 104 : const Reference< XColumn >& FormattedColumnValue::getColumn() const
271 : : {
272 : 104 : return m_pData->m_xColumn;
273 : : }
274 : :
275 : : //--------------------------------------------------------------------
276 : 0 : const Reference< XColumnUpdate >& FormattedColumnValue::getColumnUpdate() const
277 : : {
278 : 0 : return m_pData->m_xColumnUpdate;
279 : : }
280 : :
281 : : //--------------------------------------------------------------------
282 : 0 : bool FormattedColumnValue::setFormattedValue( const ::rtl::OUString& _rFormattedStringValue ) const
283 : : {
284 : : OSL_PRECOND( m_pData->m_xColumnUpdate.is(), "FormattedColumnValue::setFormattedValue: no column!" );
285 [ # # ]: 0 : if ( !m_pData->m_xColumnUpdate.is() )
286 : 0 : return false;
287 : :
288 : : try
289 : : {
290 [ # # ]: 0 : if ( m_pData->m_bNumericField )
291 : : {
292 : 0 : ::dbtools::DBTypeConversion::setValue( m_pData->m_xColumnUpdate, m_pData->m_xFormatter, m_pData->m_aNullDate,
293 : 0 : _rFormattedStringValue, m_pData->m_nFormatKey, ::sal::static_int_cast< sal_Int16 >( m_pData->m_nFieldType ),
294 [ # # ]: 0 : m_pData->m_nKeyType );
295 : : }
296 : : else
297 : : {
298 [ # # ][ # # ]: 0 : m_pData->m_xColumnUpdate->updateString( _rFormattedStringValue );
299 : : }
300 : : }
301 : 0 : catch( const Exception& )
302 : : {
303 : 0 : return false;
304 : : }
305 [ # # ]: 0 : return true;
306 : : }
307 : :
308 : : //--------------------------------------------------------------------
309 : 68 : ::rtl::OUString FormattedColumnValue::getFormattedValue() const
310 : : {
311 : : OSL_PRECOND( m_pData->m_xColumn.is(), "FormattedColumnValue::setFormattedValue: no column!" );
312 : :
313 : 68 : ::rtl::OUString sStringValue;
314 [ + - ]: 68 : if ( m_pData->m_xColumn.is() )
315 : : {
316 [ - + ]: 68 : if ( m_pData->m_bNumericField )
317 : : {
318 : : sStringValue = DBTypeConversion::getFormattedValue(
319 : 0 : m_pData->m_xColumn, m_pData->m_xFormatter, m_pData->m_aNullDate, m_pData->m_nFormatKey, m_pData->m_nKeyType
320 [ # # ]: 0 : );
321 : : }
322 : : else
323 : : {
324 [ + - ][ + - ]: 68 : sStringValue = m_pData->m_xColumn->getString();
325 : : }
326 : : }
327 : 68 : return sStringValue;
328 : : }
329 : :
330 : : //........................................................................
331 : : } // namespace dbtools
332 : : //........................................................................
333 : :
334 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|