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

Generated by: LCOV version 1.10