LCOV - code coverage report
Current view: top level - reportdesign/source/core/sdr - formatnormalizer.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 3 98 3.1 %
Date: 2014-04-11 Functions: 1 10 10.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10