LCOV - code coverage report
Current view: top level - connectivity/source/commontools - formattedcolumnvalue.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 67 88 76.1 %
Date: 2015-06-13 12:38:46 Functions: 13 14 92.9 %
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             : 
      21             : #include <connectivity/formattedcolumnvalue.hxx>
      22             : #include <connectivity/dbtools.hxx>
      23             : #include <connectivity/dbconversion.hxx>
      24             : 
      25             : #include <com/sun/star/util/NumberFormatter.hpp>
      26             : #include <com/sun/star/util/Date.hpp>
      27             : #include <com/sun/star/sdbc/XConnection.hpp>
      28             : #include <com/sun/star/util/XNumberFormatTypes.hpp>
      29             : #include <com/sun/star/util/NumberFormat.hpp>
      30             : #include <com/sun/star/sdbc/DataType.hpp>
      31             : 
      32             : #include <tools/diagnose_ex.h>
      33             : #include <i18nlangtag/mslangid.hxx>
      34             : #include <i18nlangtag/languagetag.hxx>
      35             : #include <comphelper/numbers.hxx>
      36             : #include <unotools/sharedunocomponent.hxx>
      37             : 
      38             : 
      39             : namespace dbtools
      40             : {
      41             : 
      42             : 
      43             :     using ::com::sun::star::uno::Reference;
      44             :     using ::com::sun::star::uno::UNO_QUERY;
      45             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      46             :     using ::com::sun::star::uno::UNO_SET_THROW;
      47             :     using ::com::sun::star::uno::Exception;
      48             :     using ::com::sun::star::uno::RuntimeException;
      49             :     using ::com::sun::star::uno::Any;
      50             :     using ::com::sun::star::uno::makeAny;
      51             :     using ::com::sun::star::uno::XComponentContext;
      52             :     using ::com::sun::star::sdbc::XRowSet;
      53             :     using ::com::sun::star::beans::XPropertySet;
      54             :     using ::com::sun::star::util::NumberFormatter;
      55             :     using ::com::sun::star::util::XNumberFormatter;
      56             :     using ::com::sun::star::util::Date;
      57             :     using ::com::sun::star::sdbc::XConnection;
      58             :     using ::com::sun::star::util::XNumberFormatsSupplier;
      59             :     using ::com::sun::star::beans::XPropertySetInfo;
      60             :     using ::com::sun::star::lang::Locale;
      61             :     using ::com::sun::star::util::XNumberFormatTypes;
      62             :     using ::com::sun::star::sdb::XColumn;
      63             :     using ::com::sun::star::sdb::XColumnUpdate;
      64             :     using ::com::sun::star::lang::XComponent;
      65             : 
      66             :     namespace DataType = ::com::sun::star::sdbc::DataType;
      67             :     namespace NumberFormat = ::com::sun::star::util::NumberFormat;
      68             : 
      69         113 :     struct FormattedColumnValue_Data
      70             :     {
      71             :         Reference< XNumberFormatter >   m_xFormatter;
      72             :         Date                            m_aNullDate;
      73             :         sal_Int32                       m_nFormatKey;
      74             :         sal_Int32                       m_nFieldType;
      75             :         sal_Int16                       m_nKeyType;
      76             :         bool                            m_bNumericField;
      77             : 
      78             :         Reference< XColumn >            m_xColumn;
      79             :         Reference< XColumnUpdate >      m_xColumnUpdate;
      80             : 
      81         113 :         FormattedColumnValue_Data()
      82             :             :m_xFormatter()
      83             :             ,m_aNullDate( DBTypeConversion::getStandardDate() )
      84             :             ,m_nFormatKey( 0 )
      85             :             ,m_nFieldType( DataType::OTHER )
      86             :             ,m_nKeyType( NumberFormat::UNDEFINED )
      87             :             ,m_bNumericField( false )
      88             :             ,m_xColumn()
      89         113 :             ,m_xColumnUpdate()
      90             :         {
      91         113 :         }
      92             :     };
      93             : 
      94             : 
      95             :     namespace
      96             :     {
      97             : 
      98         226 :         void lcl_clear_nothrow( FormattedColumnValue_Data& _rData )
      99             :         {
     100         226 :             _rData.m_xFormatter.clear();
     101         226 :             _rData.m_nFormatKey = 0;
     102         226 :             _rData.m_nFieldType = DataType::OTHER;
     103         226 :             _rData.m_nKeyType = NumberFormat::UNDEFINED;
     104         226 :             _rData.m_bNumericField = false;
     105             : 
     106         226 :             _rData.m_xColumn.clear();
     107         226 :             _rData.m_xColumnUpdate.clear();
     108         226 :         }
     109             : 
     110             : 
     111         113 :         void lcl_initColumnDataValue_nothrow( FormattedColumnValue_Data& _rData,
     112             :             const Reference< XNumberFormatter >& i_rNumberFormatter, const Reference< XPropertySet >& _rxColumn )
     113             :         {
     114         113 :             lcl_clear_nothrow( _rData );
     115             : 
     116             :             OSL_PRECOND( i_rNumberFormatter.is(), "lcl_initColumnDataValue_nothrow: no number formats -> no formatted values!" );
     117         113 :             if ( !i_rNumberFormatter.is() )
     118         113 :                 return;
     119             : 
     120             :             try
     121             :             {
     122         113 :                 Reference< XNumberFormatsSupplier > xNumberFormatsSupp( i_rNumberFormatter->getNumberFormatsSupplier(), UNO_SET_THROW );
     123             : 
     124             :                 // remember the column
     125         113 :                 _rData.m_xColumn.set( _rxColumn, UNO_QUERY_THROW );
     126         113 :                 _rData.m_xColumnUpdate.set( _rxColumn, UNO_QUERY );
     127             : 
     128             :                 // determine the field type, and whether it's a numeric field
     129         113 :                 OSL_VERIFY( _rxColumn->getPropertyValue("Type") >>= _rData.m_nFieldType );
     130             : 
     131         113 :                 switch ( _rData.m_nFieldType )
     132             :                 {
     133             :                     case DataType::DATE:
     134             :                     case DataType::TIME:
     135             :                     case DataType::TIMESTAMP:
     136             :                     case DataType::BIT:
     137             :                     case DataType::BOOLEAN:
     138             :                     case DataType::TINYINT:
     139             :                     case DataType::SMALLINT:
     140             :                     case DataType::INTEGER:
     141             :                     case DataType::REAL:
     142             :                     case DataType::BIGINT:
     143             :                     case DataType::DOUBLE:
     144             :                     case DataType::NUMERIC:
     145             :                     case DataType::DECIMAL:
     146           0 :                         _rData.m_bNumericField = true;
     147           0 :                         break;
     148             :                     default:
     149         113 :                         _rData.m_bNumericField = false;
     150         113 :                         break;
     151             :                 }
     152             : 
     153             :                 // get the format key of our bound field
     154         113 :                 Reference< XPropertySetInfo > xPSI( _rxColumn->getPropertySetInfo(), UNO_QUERY_THROW );
     155         113 :                 bool bHaveFieldFormat = false;
     156         226 :                 const OUString sFormatKeyProperty( "FormatKey" );
     157         113 :                 if ( xPSI->hasPropertyByName( sFormatKeyProperty ) )
     158             :                 {
     159         113 :                     bHaveFieldFormat = ( _rxColumn->getPropertyValue( sFormatKeyProperty ) >>= _rData.m_nFormatKey );
     160             :                 }
     161         113 :                 if ( !bHaveFieldFormat )
     162             :                 {
     163             :                     // fall back to a format key as indicated by the field type
     164           0 :                     Locale aSystemLocale( LanguageTag( MsLangId::getSystemLanguage() ).getLocale() );
     165           0 :                     Reference< XNumberFormatTypes > xNumTypes( xNumberFormatsSupp->getNumberFormats(), UNO_QUERY_THROW );
     166           0 :                     _rData.m_nFormatKey = getDefaultNumberFormat( _rxColumn, xNumTypes, aSystemLocale );
     167             :                 }
     168             : 
     169             :                 // some more formatter settings
     170         113 :                 _rData.m_nKeyType  = ::comphelper::getNumberFormatType( xNumberFormatsSupp->getNumberFormats(), _rData.m_nFormatKey );
     171         226 :                 Reference< XPropertySet > xFormatSettings( xNumberFormatsSupp->getNumberFormatSettings(), UNO_QUERY_THROW );
     172         113 :                 OSL_VERIFY( xFormatSettings->getPropertyValue("NullDate") >>= _rData.m_aNullDate );
     173             : 
     174             :                 // remember the formatter
     175         226 :                 _rData.m_xFormatter = i_rNumberFormatter;
     176             :             }
     177           0 :             catch( const Exception& )
     178             :             {
     179             :                 DBG_UNHANDLED_EXCEPTION();
     180             :             }
     181             :         }
     182             : 
     183             : 
     184          17 :         void lcl_initColumnDataValue_nothrow( const Reference<XComponentContext>& i_rContext, FormattedColumnValue_Data& i_rData,
     185             :             const Reference< XRowSet >& i_rRowSet, const Reference< XPropertySet >& i_rColumn )
     186             :         {
     187             :             OSL_PRECOND( i_rRowSet.is(), "lcl_initColumnDataValue_nothrow: no row set!" );
     188          17 :             if ( !i_rRowSet.is() )
     189          17 :                 return;
     190             : 
     191          17 :             Reference< XNumberFormatter > xNumberFormatter;
     192             :             try
     193             :             {
     194             :                 // get the number formats supplier of the connection of the form
     195          17 :                 Reference< XConnection > xConnection( getConnection( i_rRowSet ), UNO_QUERY_THROW );
     196          34 :                 Reference< XNumberFormatsSupplier > xSupplier( getNumberFormats( xConnection, true, i_rContext ), UNO_SET_THROW );
     197             : 
     198             :                 // create a number formatter for it
     199          17 :                 xNumberFormatter.set( NumberFormatter::create( i_rContext ), UNO_QUERY_THROW );
     200          34 :                 xNumberFormatter->attachNumberFormatsSupplier( xSupplier );
     201             :             }
     202           0 :             catch( const Exception& )
     203             :             {
     204             :                 DBG_UNHANDLED_EXCEPTION();
     205             :             }
     206             : 
     207          17 :             lcl_initColumnDataValue_nothrow( i_rData, xNumberFormatter, i_rColumn );
     208             :         }
     209             :     }
     210             : 
     211          17 :     FormattedColumnValue::FormattedColumnValue( const Reference< XComponentContext >& _rxContext,
     212             :             const Reference< XRowSet >& _rxRowSet, const Reference< XPropertySet >& i_rColumn )
     213          17 :         :m_pData( new FormattedColumnValue_Data )
     214             :     {
     215          17 :         lcl_initColumnDataValue_nothrow( _rxContext, *m_pData, _rxRowSet, i_rColumn );
     216          17 :     }
     217             : 
     218             : 
     219          96 :     FormattedColumnValue::FormattedColumnValue( const Reference< XNumberFormatter >& i_rNumberFormatter,
     220             :             const Reference< XPropertySet >& _rxColumn )
     221          96 :         :m_pData( new FormattedColumnValue_Data )
     222             :     {
     223          96 :         lcl_initColumnDataValue_nothrow( *m_pData, i_rNumberFormatter, _rxColumn );
     224          96 :     }
     225             : 
     226             : 
     227         113 :     void FormattedColumnValue::clear()
     228             :     {
     229         113 :         lcl_clear_nothrow( *m_pData );
     230         113 :     }
     231             : 
     232             : 
     233         243 :     FormattedColumnValue::~FormattedColumnValue()
     234             :     {
     235         113 :         clear();
     236         130 :     }
     237             : 
     238          17 :     sal_Int16 FormattedColumnValue::getKeyType() const
     239             :     {
     240          17 :         return m_pData->m_nKeyType;
     241             :     }
     242             : 
     243             : 
     244          26 :     const Reference< XColumn >& FormattedColumnValue::getColumn() const
     245             :     {
     246          26 :         return m_pData->m_xColumn;
     247             :     }
     248             : 
     249           0 :     bool FormattedColumnValue::setFormattedValue( const OUString& _rFormattedStringValue ) const
     250             :     {
     251             :         OSL_PRECOND( m_pData->m_xColumnUpdate.is(), "FormattedColumnValue::setFormattedValue: no column!" );
     252           0 :         if ( !m_pData->m_xColumnUpdate.is() )
     253           0 :             return false;
     254             : 
     255             :         try
     256             :         {
     257           0 :             if ( m_pData->m_bNumericField )
     258             :             {
     259           0 :                 ::dbtools::DBTypeConversion::setValue( m_pData->m_xColumnUpdate, m_pData->m_xFormatter, m_pData->m_aNullDate,
     260           0 :                     _rFormattedStringValue, m_pData->m_nFormatKey, ::sal::static_int_cast< sal_Int16 >( m_pData->m_nFieldType ),
     261           0 :                     m_pData->m_nKeyType );
     262             :             }
     263             :             else
     264             :             {
     265           0 :                 m_pData->m_xColumnUpdate->updateString( _rFormattedStringValue );
     266             :             }
     267             :         }
     268           0 :         catch( const Exception& )
     269             :         {
     270           0 :             return false;
     271             :         }
     272           0 :         return true;
     273             :     }
     274             : 
     275             : 
     276         113 :     OUString FormattedColumnValue::getFormattedValue() const
     277             :     {
     278             :         OSL_PRECOND( m_pData->m_xColumn.is(), "FormattedColumnValue::setFormattedValue: no column!" );
     279             : 
     280         113 :         OUString sStringValue;
     281         113 :         if ( m_pData->m_xColumn.is() )
     282             :         {
     283         113 :             if ( m_pData->m_bNumericField )
     284             :             {
     285           0 :                 sStringValue = DBTypeConversion::getFormattedValue(
     286           0 :                     m_pData->m_xColumn, m_pData->m_xFormatter, m_pData->m_aNullDate, m_pData->m_nFormatKey, m_pData->m_nKeyType
     287           0 :                 );
     288             :             }
     289             :             else
     290             :             {
     291         113 :                 sStringValue = m_pData->m_xColumn->getString();
     292             :             }
     293             :         }
     294         113 :         return sStringValue;
     295             :     }
     296             : 
     297             : 
     298             : } // namespace dbtools
     299             : 
     300             : 
     301             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11