1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/sdb/XParametersSupplier.hpp>
25 #include <com/sun/star/util/XNumberFormatTypes.hpp>
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>
38 using ::com::sun::star::uno::Reference
;
39 using ::com::sun::star::report::XFormattedField
;
40 using ::com::sun::star::uno::UNO_QUERY
;
41 using ::com::sun::star::sdb::XSingleSelectQueryComposer
;
42 using ::com::sun::star::sdbcx::XColumnsSupplier
;
43 using ::com::sun::star::container::XIndexAccess
;
44 using ::com::sun::star::beans::XPropertySet
;
45 using ::com::sun::star::uno::UNO_QUERY_THROW
;
46 using ::com::sun::star::uno::Exception
;
47 using ::com::sun::star::sdb::XParametersSupplier
;
48 using ::com::sun::star::sdbc::SQLException
;
49 using ::com::sun::star::util::XNumberFormatsSupplier
;
50 using ::com::sun::star::util::XNumberFormatTypes
;
56 FormatNormalizer::FormatNormalizer( const OReportModel
& _rModel
)
58 ,m_xReportDefinition( )
59 ,m_bFieldListDirty( true )
64 FormatNormalizer::~FormatNormalizer()
69 void FormatNormalizer::notifyPropertyChange( const css::beans::PropertyChangeEvent
& _rEvent
)
71 if ( !impl_lateInit() )
74 if ( ( _rEvent
.Source
== m_xReportDefinition
) && m_xReportDefinition
.is() )
76 impl_onDefinitionPropertyChange( _rEvent
.PropertyName
);
80 Reference
< XFormattedField
> xFormatted( _rEvent
.Source
, UNO_QUERY
);
81 if ( xFormatted
.is() )
82 impl_onFormattedProperttyChange( xFormatted
, _rEvent
.PropertyName
);
86 void FormatNormalizer::notifyElementInserted( const css::uno::Reference
< css::uno::XInterface
>& _rxElement
)
88 if ( !impl_lateInit() )
91 Reference
< XFormattedField
> xFormatted( _rxElement
, UNO_QUERY
);
92 if ( !xFormatted
.is() )
95 impl_adjustFormatToDataFieldType_nothrow( xFormatted
);
99 bool FormatNormalizer::impl_lateInit()
101 if ( m_xReportDefinition
.is() )
104 m_xReportDefinition
= m_rModel
.getReportDefinition();
105 return m_xReportDefinition
.is();
109 void FormatNormalizer::impl_onDefinitionPropertyChange( const OUString
& _rChangedPropName
)
111 if ( _rChangedPropName
!= "Command" && _rChangedPropName
!= "CommandType" && _rChangedPropName
!= "EscapeProcessing" )
112 // nothing we're interested in
114 m_bFieldListDirty
= true;
118 void FormatNormalizer::impl_onFormattedProperttyChange( const Reference
< XFormattedField
>& _rxFormatted
, const OUString
& _rChangedPropName
)
120 if ( _rChangedPropName
!= "DataField" )
121 // nothing we're interested in
124 impl_adjustFormatToDataFieldType_nothrow( _rxFormatted
);
130 void lcl_collectFields_throw( const Reference
< XIndexAccess
>& _rxColumns
, FormatNormalizer::FieldList
& _inout_rFields
)
134 sal_Int32
nCount( _rxColumns
->getCount() );
135 _inout_rFields
.reserve( _inout_rFields
.size() + static_cast<size_t>(nCount
) );
137 Reference
< XPropertySet
> xColumn
;
138 FormatNormalizer::Field aField
;
140 for ( sal_Int32 i
=0; i
<nCount
; ++i
)
142 xColumn
.set( _rxColumns
->getByIndex( i
), UNO_QUERY_THROW
);
143 OSL_VERIFY( xColumn
->getPropertyValue("Name") >>= aField
.sName
);
144 OSL_VERIFY( xColumn
->getPropertyValue("Type") >>= aField
.nDataType
);
145 OSL_VERIFY( xColumn
->getPropertyValue("Scale") >>= aField
.nScale
);
146 OSL_VERIFY( xColumn
->getPropertyValue("IsCurrency") >>= aField
.bIsCurrency
);
147 _inout_rFields
.push_back( aField
);
150 catch( const Exception
& )
152 DBG_UNHANDLED_EXCEPTION("reportdesign");
158 bool FormatNormalizer::impl_ensureUpToDateFieldList_nothrow()
160 if ( !m_bFieldListDirty
)
162 m_aFields
.resize( 0 );
164 OSL_PRECOND( m_xReportDefinition
.is(), "FormatNormalizer::impl_ensureUpToDateFieldList_nothrow: no report definition!" );
165 if ( !m_xReportDefinition
.is() )
168 ::dbaui::DBSubComponentController
* pController( m_rModel
.getController() );
169 OSL_ENSURE( pController
, "FormatNormalizer::impl_ensureUpToDateFieldList_nothrow: no controller? how can *this* happen?!" );
175 ::dbtools::StatementComposer
aComposer( pController
->getConnection(), m_xReportDefinition
->getCommand(),
176 m_xReportDefinition
->getCommandType(), m_xReportDefinition
->getEscapeProcessing() );
178 Reference
< XSingleSelectQueryComposer
> xComposer( aComposer
.getComposer() );
179 if ( !xComposer
.is() )
183 Reference
< XColumnsSupplier
> xSuppCols( xComposer
, UNO_QUERY_THROW
);
184 Reference
< XIndexAccess
> xColumns( xSuppCols
->getColumns(), UNO_QUERY_THROW
);
185 lcl_collectFields_throw( xColumns
, m_aFields
);
187 Reference
< XParametersSupplier
> xSuppParams( xComposer
, UNO_QUERY_THROW
);
188 Reference
< XIndexAccess
> xParams( xSuppParams
->getParameters(), UNO_QUERY_THROW
);
189 lcl_collectFields_throw( xParams
, m_aFields
);
191 catch( const SQLException
& )
193 // silence it. This might happen for instance when the user sets an non-existent table,
194 // or things like this
196 catch( const Exception
& )
198 DBG_UNHANDLED_EXCEPTION("reportdesign");
201 m_bFieldListDirty
= false;
206 void FormatNormalizer::impl_adjustFormatToDataFieldType_nothrow( const Reference
< XFormattedField
>& _rxFormatted
)
208 if ( !impl_ensureUpToDateFieldList_nothrow() )
209 // unable to obtain a recent field list
214 sal_Int32 nFormatKey
= _rxFormatted
->getFormatKey();
215 if ( nFormatKey
!= 0 )
216 // it's not the "standard numeric" format -> not interested in
219 OUString
sDataField( _rxFormatted
->getDataField() );
220 const OUString
sFieldPrefix( "field:[" );
221 if ( sDataField
.indexOf( sFieldPrefix
) != 0 )
222 // not bound to a table field
223 // TODO: we might also do this kind of thing for functions and expressions ...
225 if ( !sDataField
.endsWith("]") )
227 // last character is not the closing brace
228 OSL_FAIL( "FormatNormalizer::impl_adjustFormatToDataFieldType_nothrow: suspicious data field value!" );
231 sDataField
= sDataField
.copy( sFieldPrefix
.getLength(), sDataField
.getLength() - sFieldPrefix
.getLength() - 1 );
233 FieldList::const_iterator field
= m_aFields
.begin();
234 for ( ; field
!= m_aFields
.end(); ++field
)
236 if ( field
->sName
== sDataField
)
239 if ( field
== m_aFields
.end() )
243 Reference
< XNumberFormatsSupplier
> xSuppNumFmts( _rxFormatted
->getFormatsSupplier(), UNO_QUERY_THROW
);
244 Reference
< XNumberFormatTypes
> xNumFmtTypes( xSuppNumFmts
->getNumberFormats(), UNO_QUERY_THROW
);
246 nFormatKey
= ::dbtools::getDefaultNumberFormat( field
->nDataType
, field
->nScale
, field
->bIsCurrency
, xNumFmtTypes
,
247 SvtSysLocale().GetLanguageTag().getLocale() );
248 _rxFormatted
->setFormatKey( nFormatKey
);
250 catch( const Exception
& )
252 DBG_UNHANDLED_EXCEPTION("reportdesign");
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */