bump product version to 6.3.0.0.beta1
[LibreOffice.git] / reportdesign / source / core / sdr / formatnormalizer.cxx
blobd9dc4af14158e4a7e5273dc242fe5e8e87a632fd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "formatnormalizer.hxx"
21 #include <RptModel.hxx>
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/util/XNumberFormatTypes.hpp>
28 #include <dbaccess/dbsubcomponentcontroller.hxx>
29 #include <unotools/syslocale.hxx>
30 #include <connectivity/statementcomposer.hxx>
31 #include <connectivity/dbtools.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <i18nlangtag/languagetag.hxx>
36 namespace rptui
40 using ::com::sun::star::uno::Reference;
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;
55 //= FormatNormalizer
58 FormatNormalizer::FormatNormalizer( const OReportModel& _rModel )
59 :m_rModel( _rModel )
60 ,m_xReportDefinition( )
61 ,m_bFieldListDirty( true )
66 FormatNormalizer::~FormatNormalizer()
71 void FormatNormalizer::notifyPropertyChange( const css::beans::PropertyChangeEvent& _rEvent )
73 if ( !impl_lateInit() )
74 return;
76 if ( ( _rEvent.Source == m_xReportDefinition ) && m_xReportDefinition.is() )
78 impl_onDefinitionPropertyChange( _rEvent.PropertyName );
79 return;
82 Reference< XFormattedField > xFormatted( _rEvent.Source, UNO_QUERY );
83 if ( xFormatted.is() )
84 impl_onFormattedProperttyChange( xFormatted, _rEvent.PropertyName );
88 void FormatNormalizer::notifyElementInserted( const css::uno::Reference< css::uno::XInterface >& _rxElement )
90 if ( !impl_lateInit() )
91 return;
93 Reference< XFormattedField > xFormatted( _rxElement, UNO_QUERY );
94 if ( !xFormatted.is() )
95 return;
97 impl_adjustFormatToDataFieldType_nothrow( xFormatted );
101 bool FormatNormalizer::impl_lateInit()
103 if ( m_xReportDefinition.is() )
104 return true;
106 m_xReportDefinition = m_rModel.getReportDefinition();
107 return m_xReportDefinition.is();
111 void FormatNormalizer::impl_onDefinitionPropertyChange( const OUString& _rChangedPropName )
113 if ( _rChangedPropName != "Command" && _rChangedPropName != "CommandType" && _rChangedPropName != "EscapeProcessing" )
114 // nothing we're interested in
115 return;
116 m_bFieldListDirty = true;
120 void FormatNormalizer::impl_onFormattedProperttyChange( const Reference< XFormattedField >& _rxFormatted, const OUString& _rChangedPropName )
122 if ( _rChangedPropName != "DataField" )
123 // nothing we're interested in
124 return;
126 impl_adjustFormatToDataFieldType_nothrow( _rxFormatted );
130 namespace
132 void lcl_collectFields_throw( const Reference< XIndexAccess >& _rxColumns, FormatNormalizer::FieldList& _inout_rFields )
136 sal_Int32 nCount( _rxColumns->getCount() );
137 _inout_rFields.reserve( _inout_rFields.size() + static_cast<size_t>(nCount) );
139 Reference< XPropertySet > xColumn;
140 FormatNormalizer::Field aField;
142 for ( sal_Int32 i=0; i<nCount; ++i )
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 );
152 catch( const Exception& )
154 DBG_UNHANDLED_EXCEPTION("reportdesign");
160 bool FormatNormalizer::impl_ensureUpToDateFieldList_nothrow()
162 if ( !m_bFieldListDirty )
163 return true;
164 m_aFields.resize( 0 );
166 OSL_PRECOND( m_xReportDefinition.is(), "FormatNormalizer::impl_ensureUpToDateFieldList_nothrow: no report definition!" );
167 if ( !m_xReportDefinition.is() )
168 return false;
170 ::dbaui::DBSubComponentController* pController( m_rModel.getController() );
171 OSL_ENSURE( pController, "FormatNormalizer::impl_ensureUpToDateFieldList_nothrow: no controller? how can *this* happen?!" );
172 if ( !pController )
173 return false;
177 ::dbtools::StatementComposer aComposer( pController->getConnection(), m_xReportDefinition->getCommand(),
178 m_xReportDefinition->getCommandType(), m_xReportDefinition->getEscapeProcessing() );
180 Reference< XSingleSelectQueryComposer > xComposer( aComposer.getComposer() );
181 if ( !xComposer.is() )
182 return false;
185 Reference< XColumnsSupplier > xSuppCols( xComposer, UNO_QUERY_THROW );
186 Reference< XIndexAccess > xColumns( xSuppCols->getColumns(), UNO_QUERY_THROW );
187 lcl_collectFields_throw( xColumns, m_aFields );
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 );
193 catch( const SQLException& )
195 // silence it. This might happen for instance when the user sets an non-existent table,
196 // or things like this
198 catch( const Exception& )
200 DBG_UNHANDLED_EXCEPTION("reportdesign");
203 m_bFieldListDirty = false;
204 return true;
208 void FormatNormalizer::impl_adjustFormatToDataFieldType_nothrow( const Reference< XFormattedField >& _rxFormatted )
210 if ( !impl_ensureUpToDateFieldList_nothrow() )
211 // unable to obtain a recent field list
212 return;
216 sal_Int32 nFormatKey = _rxFormatted->getFormatKey();
217 if ( nFormatKey != 0 )
218 // it's not the "standard numeric" format -> not interested in
219 return;
221 OUString sDataField( _rxFormatted->getDataField() );
222 const OUString sFieldPrefix( "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("]") )
229 // last character is not the closing brace
230 OSL_FAIL( "FormatNormalizer::impl_adjustFormatToDataFieldType_nothrow: suspicious data field value!" );
231 return;
233 sDataField = sDataField.copy( sFieldPrefix.getLength(), sDataField.getLength() - sFieldPrefix.getLength() - 1 );
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;
241 Reference< XNumberFormatsSupplier > xSuppNumFmts( _rxFormatted->getFormatsSupplier(), css::uno::UNO_SET_THROW );
242 Reference< XNumberFormatTypes > xNumFmtTypes( xSuppNumFmts->getNumberFormats(), UNO_QUERY_THROW );
244 nFormatKey = ::dbtools::getDefaultNumberFormat( field->nDataType, field->nScale, field->bIsCurrency, xNumFmtTypes,
245 SvtSysLocale().GetLanguageTag().getLocale() );
246 _rxFormatted->setFormatKey( nFormatKey );
248 catch( const Exception& )
250 DBG_UNHANDLED_EXCEPTION("reportdesign");
255 } // namespace rptui
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */