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 "formatnormalizer.hxx"
21 : #include "RptModel.hxx"
22 :
23 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
24 : #include <com/sun/star/sdb/XParametersSupplier.hpp>
25 : #include <com/sun/star/util/XNumberFormatTypes.hpp>
26 :
27 : #include <dbaccess/dbsubcomponentcontroller.hxx>
28 : #include <unotools/syslocale.hxx>
29 : #include <connectivity/statementcomposer.hxx>
30 : #include <connectivity/dbtools.hxx>
31 : #include <tools/diagnose_ex.h>
32 :
33 : //........................................................................
34 : namespace rptui
35 : {
36 : //........................................................................
37 :
38 : /** === begin UNO using === **/
39 : using ::com::sun::star::uno::Reference;
40 : using ::com::sun::star::report::XReportDefinition;
41 : using ::com::sun::star::report::XFormattedField;
42 : using ::com::sun::star::uno::UNO_QUERY;
43 : using ::com::sun::star::sdb::XSingleSelectQueryComposer;
44 : using ::com::sun::star::sdbcx::XColumnsSupplier;
45 : using ::com::sun::star::container::XIndexAccess;
46 : using ::com::sun::star::beans::XPropertySet;
47 : using ::com::sun::star::uno::UNO_QUERY_THROW;
48 : using ::com::sun::star::uno::Exception;
49 : using ::com::sun::star::sdb::XParametersSupplier;
50 : using ::com::sun::star::sdbc::SQLException;
51 : using ::com::sun::star::util::XNumberFormatsSupplier;
52 : using ::com::sun::star::util::XNumberFormatTypes;
53 : using ::com::sun::star::uno::makeAny;
54 : /** === end UNO using === **/
55 :
56 : //====================================================================
57 : //= FormatNormalizer
58 : //====================================================================
59 : DBG_NAME(rpt_FormatNormalizer)
60 : //--------------------------------------------------------------------
61 0 : FormatNormalizer::FormatNormalizer( const OReportModel& _rModel )
62 : :m_rModel( _rModel )
63 : ,m_xReportDefinition( )
64 0 : ,m_bFieldListDirty( true )
65 : {
66 : DBG_CTOR(rpt_FormatNormalizer,NULL);
67 0 : }
68 :
69 : //--------------------------------------------------------------------
70 0 : FormatNormalizer::~FormatNormalizer()
71 : {
72 : DBG_DTOR(rpt_FormatNormalizer,NULL);
73 0 : }
74 :
75 : //--------------------------------------------------------------------
76 0 : void FormatNormalizer::notifyPropertyChange( const ::com::sun::star::beans::PropertyChangeEvent& _rEvent )
77 : {
78 0 : if ( !impl_lateInit() )
79 : return;
80 :
81 0 : if ( ( _rEvent.Source == m_xReportDefinition ) && m_xReportDefinition.is() )
82 : {
83 0 : impl_onDefinitionPropertyChange( _rEvent.PropertyName );
84 : return;
85 : }
86 :
87 0 : Reference< XFormattedField > xFormatted( _rEvent.Source, UNO_QUERY );
88 0 : if ( xFormatted.is() )
89 0 : impl_onFormattedProperttyChange( xFormatted, _rEvent.PropertyName );
90 : }
91 :
92 : //--------------------------------------------------------------------
93 0 : void FormatNormalizer::notifyElementInserted( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxElement )
94 : {
95 0 : if ( !impl_lateInit() )
96 : return;
97 :
98 0 : Reference< XFormattedField > xFormatted( _rxElement, UNO_QUERY );
99 0 : if ( !xFormatted.is() )
100 : return;
101 :
102 0 : impl_adjustFormatToDataFieldType_nothrow( xFormatted );
103 : }
104 :
105 : //--------------------------------------------------------------------
106 0 : bool FormatNormalizer::impl_lateInit()
107 : {
108 0 : if ( m_xReportDefinition.is() )
109 0 : return true;
110 :
111 0 : m_xReportDefinition = m_rModel.getReportDefinition();
112 0 : return m_xReportDefinition.is();
113 : }
114 :
115 : //--------------------------------------------------------------------
116 0 : void FormatNormalizer::impl_onDefinitionPropertyChange( const ::rtl::OUString& _rChangedPropName )
117 : {
118 0 : if ( _rChangedPropName != "Command" && _rChangedPropName != "CommandType" && _rChangedPropName != "EscapeProcessing" )
119 : // nothing we're interested in
120 0 : return;
121 0 : m_bFieldListDirty = true;
122 : }
123 :
124 : //--------------------------------------------------------------------
125 0 : void FormatNormalizer::impl_onFormattedProperttyChange( const Reference< XFormattedField >& _rxFormatted, const ::rtl::OUString& _rChangedPropName )
126 : {
127 0 : if ( _rChangedPropName != "DataField" )
128 : // nothing we're interested in
129 0 : return;
130 :
131 0 : impl_adjustFormatToDataFieldType_nothrow( _rxFormatted );
132 : }
133 :
134 : //--------------------------------------------------------------------
135 : namespace
136 : {
137 0 : void lcl_collectFields_throw( const Reference< XIndexAccess >& _rxColumns, FormatNormalizer::FieldList& _inout_rFields )
138 : {
139 : try
140 : {
141 0 : sal_Int32 nCount( _rxColumns->getCount() );
142 0 : _inout_rFields.reserve( _inout_rFields.size() + (size_t)nCount );
143 :
144 0 : Reference< XPropertySet > xColumn;
145 0 : FormatNormalizer::Field aField;
146 :
147 0 : for ( sal_Int32 i=0; i<nCount; ++i )
148 : {
149 0 : xColumn.set( _rxColumns->getByIndex( i ), UNO_QUERY_THROW );
150 0 : OSL_VERIFY( xColumn->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ) ) >>= aField.sName );
151 0 : OSL_VERIFY( xColumn->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Type" ) ) ) >>= aField.nDataType );
152 0 : OSL_VERIFY( xColumn->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Scale" ) ) ) >>= aField.nScale );
153 0 : OSL_VERIFY( xColumn->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsCurrency" ) ) ) >>= aField.bIsCurrency );
154 0 : _inout_rFields.push_back( aField );
155 0 : }
156 : }
157 0 : catch( const Exception& )
158 : {
159 : DBG_UNHANDLED_EXCEPTION();
160 : }
161 0 : }
162 : }
163 :
164 : //--------------------------------------------------------------------
165 0 : bool FormatNormalizer::impl_ensureUpToDateFieldList_nothrow()
166 : {
167 0 : if ( !m_bFieldListDirty )
168 0 : return true;
169 0 : m_aFields.resize( 0 );
170 :
171 : OSL_PRECOND( m_xReportDefinition.is(), "FormatNormalizer::impl_ensureUpToDateFieldList_nothrow: no report definition!" );
172 0 : if ( !m_xReportDefinition.is() )
173 0 : return false;
174 :
175 0 : ::dbaui::DBSubComponentController* pController( m_rModel.getController() );
176 : OSL_ENSURE( pController, "FormatNormalizer::impl_ensureUpToDateFieldList_nothrow: no controller? how can *this* happen?!" );
177 0 : if ( !pController )
178 0 : return false;
179 :
180 : try
181 : {
182 0 : ::dbtools::StatementComposer aComposer( pController->getConnection(), m_xReportDefinition->getCommand(),
183 0 : m_xReportDefinition->getCommandType(), m_xReportDefinition->getEscapeProcessing() );
184 :
185 0 : Reference< XSingleSelectQueryComposer > xComposer( aComposer.getComposer() );
186 0 : if ( !xComposer.is() )
187 0 : return false;
188 :
189 :
190 0 : Reference< XColumnsSupplier > xSuppCols( xComposer, UNO_QUERY_THROW );
191 0 : Reference< XIndexAccess > xColumns( xSuppCols->getColumns(), UNO_QUERY_THROW );
192 0 : lcl_collectFields_throw( xColumns, m_aFields );
193 :
194 0 : Reference< XParametersSupplier > xSuppParams( xComposer, UNO_QUERY_THROW );
195 0 : Reference< XIndexAccess > xParams( xSuppParams->getParameters(), UNO_QUERY_THROW );
196 0 : lcl_collectFields_throw( xParams, m_aFields );
197 : }
198 0 : catch( const SQLException& )
199 : {
200 : // silence it. This might happen for instance when the user sets an non-existent table,
201 : // or things like this
202 : }
203 0 : catch( const Exception& )
204 : {
205 : DBG_UNHANDLED_EXCEPTION();
206 : }
207 :
208 0 : m_bFieldListDirty = false;
209 0 : return true;
210 : }
211 :
212 : //--------------------------------------------------------------------
213 0 : void FormatNormalizer::impl_adjustFormatToDataFieldType_nothrow( const Reference< XFormattedField >& _rxFormatted )
214 : {
215 0 : if ( !impl_ensureUpToDateFieldList_nothrow() )
216 : // unable to obtain a recent field list
217 0 : return;
218 :
219 : try
220 : {
221 0 : sal_Int32 nFormatKey = _rxFormatted->getFormatKey();
222 0 : if ( nFormatKey != 0 )
223 : // it's not the "standard numeric" format -> not interested in
224 : return;
225 :
226 0 : ::rtl::OUString sDataField( _rxFormatted->getDataField() );
227 0 : const ::rtl::OUString sFieldPrefix( RTL_CONSTASCII_USTRINGPARAM( "field:[" ) );
228 0 : if ( sDataField.indexOf( sFieldPrefix ) != 0 )
229 : // not bound to a table field
230 : // TODO: we might also do this kind of thing for functions and expressions ...
231 : return;
232 0 : if ( sDataField.getStr()[ sDataField.getLength() - 1 ] != ']' )
233 : {
234 : // last character is not the closing brace
235 : OSL_FAIL( "FormatNormalizer::impl_adjustFormatToDataFieldType_nothrow: suspicious data field value!" );
236 : return;
237 : }
238 0 : sDataField = sDataField.copy( sFieldPrefix.getLength(), sDataField.getLength() - sFieldPrefix.getLength() - 1 );
239 :
240 0 : FieldList::const_iterator field = m_aFields.begin();
241 0 : for ( ; field != m_aFields.end(); ++field )
242 : {
243 0 : if ( field->sName == sDataField )
244 0 : break;
245 : }
246 0 : if ( field == m_aFields.end() )
247 : // unknown field
248 : return;
249 :
250 0 : Reference< XNumberFormatsSupplier > xSuppNumFmts( _rxFormatted->getFormatsSupplier(), UNO_QUERY_THROW );
251 0 : Reference< XNumberFormatTypes > xNumFmtTypes( xSuppNumFmts->getNumberFormats(), UNO_QUERY_THROW );
252 :
253 0 : nFormatKey = ::dbtools::getDefaultNumberFormat( field->nDataType, field->nScale, field->bIsCurrency, xNumFmtTypes,
254 0 : SvtSysLocale().GetLanguageTag().getLocale() );
255 0 : _rxFormatted->setFormatKey( nFormatKey );
256 : }
257 0 : catch( const Exception& )
258 : {
259 : DBG_UNHANDLED_EXCEPTION();
260 : }
261 : }
262 :
263 : //........................................................................
264 : } // namespace rptui
265 : //........................................................................
266 :
267 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|