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