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