LCOV - code coverage report
Current view: top level - connectivity/source/commontools - formattedcolumnvalue.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 63 94 67.0 %
Date: 2014-04-11 Functions: 12 17 70.6 %
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             : 
      70             :     //= FormattedColumnValue_Data
      71             : 
      72          17 :     struct FormattedColumnValue_Data
      73             :     {
      74             :         Reference< XNumberFormatter >   m_xFormatter;
      75             :         Date                            m_aNullDate;
      76             :         sal_Int32                       m_nFormatKey;
      77             :         sal_Int32                       m_nFieldType;
      78             :         sal_Int16                       m_nKeyType;
      79             :         bool                            m_bNumericField;
      80             : 
      81             :         Reference< XColumn >            m_xColumn;
      82             :         Reference< XColumnUpdate >      m_xColumnUpdate;
      83             : 
      84          17 :         FormattedColumnValue_Data()
      85             :             :m_xFormatter()
      86             :             ,m_aNullDate( DBTypeConversion::getStandardDate() )
      87             :             ,m_nFormatKey( 0 )
      88             :             ,m_nFieldType( DataType::OTHER )
      89             :             ,m_nKeyType( NumberFormat::UNDEFINED )
      90             :             ,m_bNumericField( false )
      91             :             ,m_xColumn()
      92          17 :             ,m_xColumnUpdate()
      93             :         {
      94          17 :         }
      95             :     };
      96             : 
      97             : 
      98             :     namespace
      99             :     {
     100             : 
     101          34 :         void lcl_clear_nothrow( FormattedColumnValue_Data& _rData )
     102             :         {
     103          34 :             _rData.m_xFormatter.clear();
     104          34 :             _rData.m_nFormatKey = 0;
     105          34 :             _rData.m_nFieldType = DataType::OTHER;
     106          34 :             _rData.m_nKeyType = NumberFormat::UNDEFINED;
     107          34 :             _rData.m_bNumericField = false;
     108             : 
     109          34 :             _rData.m_xColumn.clear();
     110          34 :             _rData.m_xColumnUpdate.clear();
     111          34 :         }
     112             : 
     113             : 
     114          17 :         void lcl_initColumnDataValue_nothrow( FormattedColumnValue_Data& _rData,
     115             :             const Reference< XNumberFormatter >& i_rNumberFormatter, const Reference< XPropertySet >& _rxColumn )
     116             :         {
     117          17 :             lcl_clear_nothrow( _rData );
     118             : 
     119             :             OSL_PRECOND( i_rNumberFormatter.is(), "lcl_initColumnDataValue_nothrow: no number formats -> no formatted values!" );
     120          17 :             if ( !i_rNumberFormatter.is() )
     121          17 :                 return;
     122             : 
     123             :             try
     124             :             {
     125          17 :                 Reference< XNumberFormatsSupplier > xNumberFormatsSupp( i_rNumberFormatter->getNumberFormatsSupplier(), UNO_SET_THROW );
     126             : 
     127             :                 // remember the column
     128          17 :                 _rData.m_xColumn.set( _rxColumn, UNO_QUERY_THROW );
     129          17 :                 _rData.m_xColumnUpdate.set( _rxColumn, UNO_QUERY );
     130             : 
     131             :                 // determine the field type, and whether it's a numeric field
     132          17 :                 OSL_VERIFY( _rxColumn->getPropertyValue("Type") >>= _rData.m_nFieldType );
     133             : 
     134          17 :                 switch ( _rData.m_nFieldType )
     135             :                 {
     136             :                     case DataType::DATE:
     137             :                     case DataType::TIME:
     138             :                     case DataType::TIMESTAMP:
     139             :                     case DataType::BIT:
     140             :                     case DataType::BOOLEAN:
     141             :                     case DataType::TINYINT:
     142             :                     case DataType::SMALLINT:
     143             :                     case DataType::INTEGER:
     144             :                     case DataType::REAL:
     145             :                     case DataType::BIGINT:
     146             :                     case DataType::DOUBLE:
     147             :                     case DataType::NUMERIC:
     148             :                     case DataType::DECIMAL:
     149           0 :                         _rData.m_bNumericField = true;
     150           0 :                         break;
     151             :                     default:
     152          17 :                         _rData.m_bNumericField = false;
     153          17 :                         break;
     154             :                 }
     155             : 
     156             :                 // get the format key of our bound field
     157          17 :                 Reference< XPropertySetInfo > xPSI( _rxColumn->getPropertySetInfo(), UNO_QUERY_THROW );
     158          17 :                 bool bHaveFieldFormat = false;
     159          34 :                 const OUString sFormatKeyProperty( "FormatKey" );
     160          17 :                 if ( xPSI->hasPropertyByName( sFormatKeyProperty ) )
     161             :                 {
     162          17 :                     bHaveFieldFormat = ( _rxColumn->getPropertyValue( sFormatKeyProperty ) >>= _rData.m_nFormatKey );
     163             :                 }
     164          17 :                 if ( !bHaveFieldFormat )
     165             :                 {
     166             :                     // fall back to a format key as indicated by the field type
     167           0 :                     Locale aSystemLocale( LanguageTag( MsLangId::getSystemLanguage() ).getLocale() );
     168           0 :                     Reference< XNumberFormatTypes > xNumTypes( xNumberFormatsSupp->getNumberFormats(), UNO_QUERY_THROW );
     169           0 :                     _rData.m_nFormatKey = getDefaultNumberFormat( _rxColumn, xNumTypes, aSystemLocale );
     170             :                 }
     171             : 
     172             :                 // some more formatter settings
     173          17 :                 _rData.m_nKeyType  = ::comphelper::getNumberFormatType( xNumberFormatsSupp->getNumberFormats(), _rData.m_nFormatKey );
     174          34 :                 Reference< XPropertySet > xFormatSettings( xNumberFormatsSupp->getNumberFormatSettings(), UNO_QUERY_THROW );
     175          17 :                 OSL_VERIFY( xFormatSettings->getPropertyValue("NullDate") >>= _rData.m_aNullDate );
     176             : 
     177             :                 // remember the formatter
     178          34 :                 _rData.m_xFormatter = i_rNumberFormatter;
     179             :             }
     180           0 :             catch( const Exception& )
     181             :             {
     182             :                 DBG_UNHANDLED_EXCEPTION();
     183             :             }
     184             :         }
     185             : 
     186             : 
     187          17 :         void lcl_initColumnDataValue_nothrow( const Reference<XComponentContext>& i_rContext, FormattedColumnValue_Data& i_rData,
     188             :             const Reference< XRowSet >& i_rRowSet, const Reference< XPropertySet >& i_rColumn )
     189             :         {
     190             :             OSL_PRECOND( i_rRowSet.is(), "lcl_initColumnDataValue_nothrow: no row set!" );
     191          17 :             if ( !i_rRowSet.is() )
     192          17 :                 return;
     193             : 
     194          17 :             Reference< XNumberFormatter > xNumberFormatter;
     195             :             try
     196             :             {
     197             :                 // get the number formats supplier of the connection of the form
     198          17 :                 Reference< XConnection > xConnection( getConnection( i_rRowSet ), UNO_QUERY_THROW );
     199          34 :                 Reference< XNumberFormatsSupplier > xSupplier( getNumberFormats( xConnection, true, i_rContext ), UNO_SET_THROW );
     200             : 
     201             :                 // create a number formatter for it
     202          17 :                 xNumberFormatter.set( NumberFormatter::create( i_rContext ), UNO_QUERY_THROW );
     203          34 :                 xNumberFormatter->attachNumberFormatsSupplier( xSupplier );
     204             :             }
     205           0 :             catch( const Exception& )
     206             :             {
     207             :                 DBG_UNHANDLED_EXCEPTION();
     208             :             }
     209             : 
     210          17 :             lcl_initColumnDataValue_nothrow( i_rData, xNumberFormatter, i_rColumn );
     211             :         }
     212             :     }
     213             : 
     214             : 
     215             :     //= FormattedColumnValue
     216             : 
     217             : 
     218          17 :     FormattedColumnValue::FormattedColumnValue( const Reference< XComponentContext >& _rxContext,
     219             :             const Reference< XRowSet >& _rxRowSet, const Reference< XPropertySet >& i_rColumn )
     220          17 :         :m_pData( new FormattedColumnValue_Data )
     221             :     {
     222          17 :         lcl_initColumnDataValue_nothrow( _rxContext, *m_pData, _rxRowSet, i_rColumn );
     223          17 :     }
     224             : 
     225             : 
     226           0 :     FormattedColumnValue::FormattedColumnValue( const Reference< XNumberFormatter >& i_rNumberFormatter,
     227             :             const Reference< XPropertySet >& _rxColumn )
     228           0 :         :m_pData( new FormattedColumnValue_Data )
     229             :     {
     230           0 :         lcl_initColumnDataValue_nothrow( *m_pData, i_rNumberFormatter, _rxColumn );
     231           0 :     }
     232             : 
     233             : 
     234          17 :     void FormattedColumnValue::clear()
     235             :     {
     236          17 :         lcl_clear_nothrow( *m_pData );
     237          17 :     }
     238             : 
     239             : 
     240          51 :     FormattedColumnValue::~FormattedColumnValue()
     241             :     {
     242          17 :         clear();
     243          34 :     }
     244             : 
     245             : 
     246           0 :     sal_Int32 FormattedColumnValue::getFormatKey() const
     247             :     {
     248           0 :         return m_pData->m_nFormatKey;
     249             :     }
     250             : 
     251             : 
     252           0 :     sal_Int32 FormattedColumnValue::getFieldType() const
     253             :     {
     254           0 :         return m_pData->m_nFieldType;
     255             :     }
     256             : 
     257             : 
     258          17 :     sal_Int16 FormattedColumnValue::getKeyType() const
     259             :     {
     260          17 :         return m_pData->m_nKeyType;
     261             :     }
     262             : 
     263             : 
     264          52 :     const Reference< XColumn >& FormattedColumnValue::getColumn() const
     265             :     {
     266          52 :         return m_pData->m_xColumn;
     267             :     }
     268             : 
     269             : 
     270           0 :     const Reference< XColumnUpdate >& FormattedColumnValue::getColumnUpdate() const
     271             :     {
     272           0 :         return m_pData->m_xColumnUpdate;
     273             :     }
     274             : 
     275             : 
     276           0 :     bool FormattedColumnValue::setFormattedValue( const OUString& _rFormattedStringValue ) const
     277             :     {
     278             :         OSL_PRECOND( m_pData->m_xColumnUpdate.is(), "FormattedColumnValue::setFormattedValue: no column!" );
     279           0 :         if ( !m_pData->m_xColumnUpdate.is() )
     280           0 :             return false;
     281             : 
     282             :         try
     283             :         {
     284           0 :             if ( m_pData->m_bNumericField )
     285             :             {
     286           0 :                 ::dbtools::DBTypeConversion::setValue( m_pData->m_xColumnUpdate, m_pData->m_xFormatter, m_pData->m_aNullDate,
     287           0 :                     _rFormattedStringValue, m_pData->m_nFormatKey, ::sal::static_int_cast< sal_Int16 >( m_pData->m_nFieldType ),
     288           0 :                     m_pData->m_nKeyType );
     289             :             }
     290             :             else
     291             :             {
     292           0 :                 m_pData->m_xColumnUpdate->updateString( _rFormattedStringValue );
     293             :             }
     294             :         }
     295           0 :         catch( const Exception& )
     296             :         {
     297           0 :             return false;
     298             :         }
     299           0 :         return true;
     300             :     }
     301             : 
     302             : 
     303          34 :     OUString FormattedColumnValue::getFormattedValue() const
     304             :     {
     305             :         OSL_PRECOND( m_pData->m_xColumn.is(), "FormattedColumnValue::setFormattedValue: no column!" );
     306             : 
     307          34 :         OUString sStringValue;
     308          34 :         if ( m_pData->m_xColumn.is() )
     309             :         {
     310          34 :             if ( m_pData->m_bNumericField )
     311             :             {
     312           0 :                 sStringValue = DBTypeConversion::getFormattedValue(
     313           0 :                     m_pData->m_xColumn, m_pData->m_xFormatter, m_pData->m_aNullDate, m_pData->m_nFormatKey, m_pData->m_nKeyType
     314           0 :                 );
     315             :             }
     316             :             else
     317             :             {
     318          34 :                 sStringValue = m_pData->m_xColumn->getString();
     319             :             }
     320             :         }
     321          34 :         return sStringValue;
     322             :     }
     323             : 
     324             : 
     325             : } // namespace dbtools
     326             : 
     327             : 
     328             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10