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