LCOV - code coverage report
Current view: top level - dbaccess/source/core/dataaccess - definitioncontainer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 161 298 54.0 %
Date: 2012-08-25 Functions: 28 45 62.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 172 688 25.0 %

           Branch data     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 "definitioncontainer.hxx"
      22                 :            : #include "dbastrings.hrc"
      23                 :            : #include "apitools.hxx"
      24                 :            : #include "core_resource.hxx"
      25                 :            : #include "core_resource.hrc"
      26                 :            : 
      27                 :            : #include <tools/debug.hxx>
      28                 :            : #include <tools/diagnose_ex.h>
      29                 :            : #include <osl/diagnose.h>
      30                 :            : #include <comphelper/sequence.hxx>
      31                 :            : #include <comphelper/enumhelper.hxx>
      32                 :            : #include <comphelper/extract.hxx>
      33                 :            : #include <com/sun/star/lang/XComponent.hpp>
      34                 :            : #include <com/sun/star/ucb/CommandInfo.hpp>
      35                 :            : #include <com/sun/star/beans/XPropertySet.hpp>
      36                 :            : #include <com/sun/star/sdb/ErrorCondition.hpp>
      37                 :            : #include <comphelper/types.hxx>
      38                 :            : #include <ucbhelper/contentidentifier.hxx>
      39                 :            : #include <o3tl/compat_functional.hxx>
      40                 :            : 
      41                 :            : 
      42                 :            : using namespace ::com::sun::star::uno;
      43                 :            : using namespace ::com::sun::star::lang;
      44                 :            : using namespace ::com::sun::star::util;
      45                 :            : using namespace ::com::sun::star::beans;
      46                 :            : using namespace ::com::sun::star::container;
      47                 :            : using namespace ::com::sun::star::sdbcx;
      48                 :            : using namespace ::com::sun::star::sdb;
      49                 :            : using namespace ::osl;
      50                 :            : using namespace ::comphelper;
      51                 :            : using namespace ::cppu;
      52                 :            : using namespace ::com::sun::star::ucb;
      53                 :            : 
      54                 :            : namespace dbaccess
      55                 :            : {
      56                 :            : 
      57                 :            : //==========================================================================
      58                 :            : //= ODefinitionContainer_Impl
      59                 :            : //==========================================================================
      60                 :         40 : void ODefinitionContainer_Impl::erase( TContentPtr _pDefinition )
      61                 :            : {
      62 [ +  - ][ +  - ]:         40 :     NamedDefinitions::iterator aPos = find( _pDefinition );
                 [ +  - ]
      63         [ -  + ]:         40 :     if ( aPos != end() )
      64         [ #  # ]:          0 :         m_aDefinitions.erase( aPos );
      65                 :         40 : }
      66                 :            : 
      67                 :         40 : ODefinitionContainer_Impl::const_iterator ODefinitionContainer_Impl::find( TContentPtr _pDefinition ) const
      68                 :            : {
      69                 :            :     return ::std::find_if(
      70                 :            :         m_aDefinitions.begin(),
      71                 :            :         m_aDefinitions.end(),
      72                 :            :         ::o3tl::compose1(
      73                 :            :             ::std::bind2nd( ::std::equal_to< TContentPtr >(), _pDefinition ),
      74                 :            :             ::o3tl::select2nd< NamedDefinitions::value_type >()
      75                 :            :         )
      76 [ +  - ][ +  - ]:         40 :     );
         [ +  - ][ +  - ]
                 [ +  - ]
      77                 :            : }
      78                 :            : 
      79                 :         40 : ODefinitionContainer_Impl::iterator ODefinitionContainer_Impl::find( TContentPtr _pDefinition )
      80                 :            : {
      81                 :            :     return ::std::find_if(
      82                 :            :         m_aDefinitions.begin(),
      83                 :            :         m_aDefinitions.end(),
      84                 :            :         ::o3tl::compose1(
      85                 :            :             ::std::bind2nd( ::std::equal_to< TContentPtr >(), _pDefinition ),
      86                 :            :             ::o3tl::select2nd< NamedDefinitions::value_type >()
      87                 :            :         )
      88 [ +  - ][ +  - ]:         40 :     );
         [ +  - ][ +  - ]
                 [ +  - ]
      89                 :            : }
      90                 :            : 
      91                 :            : //==========================================================================
      92                 :            : //= ODefinitionContainer
      93                 :            : //==========================================================================
      94                 :            : DBG_NAME(ODefinitionContainer)
      95                 :            : 
      96                 :       1380 : ODefinitionContainer::ODefinitionContainer(   const Reference< XMultiServiceFactory >& _xORB
      97                 :            :                                             , const Reference< XInterface >&    _xParentContainer
      98                 :            :                                             , const TContentPtr& _pImpl
      99                 :            :                                             , bool _bCheckSlash
     100                 :            :                                             )
     101                 :            :     :OContentHelper(_xORB,_xParentContainer,_pImpl)
     102                 :            :     ,m_aApproveListeners(m_aMutex)
     103                 :            :     ,m_aContainerListeners(m_aMutex)
     104                 :            :     ,m_bInPropertyChange(sal_False)
     105 [ +  - ][ +  - ]:       1380 :     ,m_bCheckSlash(_bCheckSlash)
         [ +  - ][ +  - ]
                 [ +  - ]
     106                 :            : {
     107                 :       1380 :     m_pImpl->m_aProps.bIsDocument = sal_False;
     108                 :       1380 :     m_pImpl->m_aProps.bIsFolder = sal_True;
     109                 :            : 
     110         [ +  - ]:       1380 :     const ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
     111                 :       1380 :     ODefinitionContainer_Impl::const_iterator aEnd = rDefinitions.end();
     112         [ +  + ]:       1404 :     for (   ODefinitionContainer_Impl::const_iterator aDefinition = rDefinitions.begin();
     113                 :            :             aDefinition != aEnd;
     114                 :            :             ++aDefinition
     115                 :            :         )
     116                 :            :         m_aDocuments.push_back(
     117                 :            :             m_aDocumentMap.insert(
     118 [ +  - ][ +  - ]:         24 :                 Documents::value_type( aDefinition->first, Documents::mapped_type() ) ).first );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     119                 :            : 
     120                 :            :     DBG_CTOR(ODefinitionContainer, NULL);
     121                 :       1380 : }
     122                 :            : 
     123                 :       1380 : void SAL_CALL ODefinitionContainer::disposing()
     124                 :            : {
     125         [ +  - ]:       1380 :     OContentHelper::disposing();
     126                 :            : 
     127         [ +  - ]:       1380 :     MutexGuard aGuard(m_aMutex);
     128                 :            : 
     129                 :            :     // say goodbye to our listeners
     130 [ +  - ][ +  - ]:       1380 :     EventObject aEvt(*this);
     131         [ +  - ]:       1380 :     m_aApproveListeners.disposeAndClear(aEvt);
     132         [ +  - ]:       1380 :     m_aContainerListeners.disposeAndClear(aEvt);
     133                 :            : 
     134                 :            :     // dispose our elements
     135                 :       1380 :     Documents::iterator aIter = m_aDocumentMap.begin();
     136                 :       1380 :     Documents::iterator aEnd = m_aDocumentMap.end();
     137                 :            : 
     138         [ +  + ]:       1444 :     for (; aIter != aEnd; ++aIter)
     139                 :            :     {
     140         [ +  - ]:         64 :         Reference<XContent> xProp = aIter->second;
     141         [ +  + ]:         64 :         if ( xProp.is() )
     142                 :            :         {
     143         [ +  - ]:          2 :             removeObjectListener(xProp);
     144         [ +  - ]:          2 :             ::comphelper::disposeComponent(xProp);
     145                 :            :         }
     146                 :         64 :     }
     147                 :            : 
     148                 :            :     // remove our elements
     149                 :       1380 :     m_aDocuments.clear();
     150                 :            :     //  !!! do this before clearing the map which the vector elements refer to !!!
     151 [ +  - ][ +  - ]:       1380 :     m_aDocumentMap.clear();
     152                 :       1380 : }
     153                 :            : 
     154 [ +  - ][ +  - ]:       1380 : ODefinitionContainer::~ODefinitionContainer()
                 [ +  - ]
     155                 :            : {
     156                 :            :     DBG_DTOR(ODefinitionContainer, NULL);
     157         [ -  + ]:       1380 : }
     158                 :            : 
     159 [ +  + ][ +  - ]:      48214 : IMPLEMENT_FORWARD_XINTERFACE2( ODefinitionContainer,OContentHelper,ODefinitionContainer_Base)
     160 [ +  - ][ +  - ]:          2 : IMPLEMENT_TYPEPROVIDER2(ODefinitionContainer,OContentHelper,ODefinitionContainer_Base);
         [ +  - ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     161                 :            : // XServiceInfo
     162                 :          0 : ::rtl::OUString SAL_CALL ODefinitionContainer::getImplementationName(  ) throw(RuntimeException)
     163                 :            : {
     164                 :          0 :     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.ODefinitionContainer"));
     165                 :            : }
     166                 :            : 
     167                 :          0 : Sequence< ::rtl::OUString > SAL_CALL ODefinitionContainer::getSupportedServiceNames(  ) throw(RuntimeException)
     168                 :            : {
     169                 :          0 :     Sequence< ::rtl::OUString > aReturn(2);
     170 [ #  # ][ #  # ]:          0 :     aReturn.getArray()[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.DefinitionContainer"));
     171 [ #  # ][ #  # ]:          0 :     aReturn.getArray()[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ucb.Content"));
     172                 :          0 :     return aReturn;
     173                 :            : }
     174                 :            : 
     175                 :            : // XNameContainer
     176                 :         40 : void SAL_CALL ODefinitionContainer::insertByName( const ::rtl::OUString& _rName, const Any& aElement ) throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
     177                 :            : {
     178         [ +  - ]:         40 :     ResettableMutexGuard aGuard(m_aMutex);
     179                 :            : 
     180                 :            :     // approve the new object
     181         [ +  - ]:         40 :     Reference< XContent > xNewElement(aElement,UNO_QUERY);
     182         [ +  - ]:         40 :     approveNewObject( _rName, xNewElement );  // will throw if necessary
     183                 :            : 
     184 [ +  - ][ +  - ]:         40 :     notifyByName( aGuard, _rName, xNewElement, NULL, E_INSERTED, ApproveListeners );
     185         [ +  - ]:         40 :     implAppend( _rName, xNewElement );
     186 [ +  - ][ +  - ]:         40 :     notifyByName( aGuard, _rName, xNewElement, NULL, E_INSERTED, ContainerListemers );
                 [ +  - ]
     187                 :         40 : }
     188                 :            : 
     189                 :          0 : void SAL_CALL ODefinitionContainer::removeByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
     190                 :            : {
     191         [ #  # ]:          0 :     ResettableMutexGuard aGuard(m_aMutex);
     192                 :            : 
     193                 :            :     // check the arguments
     194         [ #  # ]:          0 :     if (_rName.isEmpty())
     195         [ #  # ]:          0 :         throw IllegalArgumentException();
     196                 :            : 
     197 [ #  # ][ #  # ]:          0 :     if (!checkExistence(_rName))
     198 [ #  # ][ #  # ]:          0 :         throw NoSuchElementException(_rName,*this);
     199                 :            : 
     200                 :            :     // the old element (for the notifications)
     201 [ #  # ][ #  # ]:          0 :     Reference< XContent > xOldElement = implGetByName( _rName, impl_haveAnyListeners_nothrow() );
     202                 :            : 
     203                 :            :     // do the removal
     204 [ #  # ][ #  # ]:          0 :     notifyByName( aGuard, _rName, NULL, xOldElement, E_REMOVED, ApproveListeners );
     205         [ #  # ]:          0 :     implRemove( _rName );
     206 [ #  # ][ #  # ]:          0 :     notifyByName( aGuard, _rName, NULL, xOldElement, E_REMOVED, ContainerListemers );
     207                 :            : 
     208         [ #  # ]:          0 :     removeObjectListener( xOldElement );
     209 [ #  # ][ #  # ]:          0 :     disposeComponent(xOldElement);
     210                 :          0 : }
     211                 :            : 
     212                 :            : // XNameReplace
     213                 :          0 : void SAL_CALL ODefinitionContainer::replaceByName( const ::rtl::OUString& _rName, const Any& aElement ) throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
     214                 :            : {
     215         [ #  # ]:          0 :     ResettableMutexGuard aGuard(m_aMutex);
     216                 :            : 
     217                 :            :     // let derived classes approve the new object
     218         [ #  # ]:          0 :     Reference< XContent > xNewElement(aElement,UNO_QUERY);
     219         [ #  # ]:          0 :     approveNewObject( _rName, xNewElement );    // will throw if necessary
     220                 :            : 
     221                 :            :     // the old element (for the notifications)
     222 [ #  # ][ #  # ]:          0 :     Reference< XContent > xOldElement = implGetByName( _rName, impl_haveAnyListeners_nothrow() );
     223                 :            : 
     224         [ #  # ]:          0 :     notifyByName( aGuard, _rName, xNewElement, xOldElement, E_REPLACED, ApproveListeners );
     225         [ #  # ]:          0 :     implReplace( _rName, xNewElement );
     226         [ #  # ]:          0 :     notifyByName( aGuard, _rName, xNewElement, xOldElement, E_REPLACED, ContainerListemers );
     227                 :            : 
     228                 :            :     // and dispose it
     229 [ #  # ][ #  # ]:          0 :     disposeComponent(xOldElement);
     230                 :          0 : }
     231                 :            : 
     232                 :            : namespace
     233                 :            : {
     234                 :            :     typedef Reference< XVeto > ( SAL_CALL XContainerApproveListener::*ContainerApprovalMethod )( const ContainerEvent& );
     235                 :            : 
     236                 :            :     struct RaiseExceptionFromVeto
     237                 :            :     {
     238                 :            :     private:
     239                 :            :         ContainerApprovalMethod m_pMethod;
     240                 :            :         const ContainerEvent&   m_rEvent;
     241                 :            : 
     242                 :            :     public:
     243                 :          0 :         RaiseExceptionFromVeto( ContainerApprovalMethod _pMethod, const ContainerEvent& _rEvent )
     244                 :            :             :m_pMethod( _pMethod )
     245                 :          0 :             ,m_rEvent( _rEvent )
     246                 :            :         {
     247                 :          0 :         }
     248                 :            : 
     249                 :          0 :         void operator()( const Reference< XContainerApproveListener >& _Listener ) const
     250                 :            :         {
     251 [ #  # ][ #  # ]:          0 :             Reference< XVeto > xVeto = (_Listener.get()->*m_pMethod)( m_rEvent );
                 [ #  # ]
     252         [ #  # ]:          0 :             if ( !xVeto.is() )
     253                 :          0 :                 return;
     254                 :            : 
     255 [ #  # ][ #  # ]:          0 :             Any eVetoDetails = xVeto->getDetails();
     256                 :            : 
     257         [ #  # ]:          0 :             IllegalArgumentException aIllegalArgumentError;
     258 [ #  # ][ #  # ]:          0 :             if ( eVetoDetails >>= aIllegalArgumentError )
     259         [ #  # ]:          0 :                 throw aIllegalArgumentError;
     260                 :            : 
     261         [ #  # ]:          0 :             WrappedTargetException aWrappedError;
     262 [ #  # ][ #  # ]:          0 :             if ( eVetoDetails >>= aWrappedError )
     263         [ #  # ]:          0 :                 throw aWrappedError;
     264                 :            : 
     265 [ #  # ][ #  # ]:          0 :             throw WrappedTargetException( xVeto->getReason(), _Listener.get(), eVetoDetails );
         [ #  # ][ #  # ]
                 [ #  # ]
     266                 :            :         }
     267                 :            :     };
     268                 :            : }
     269                 :            : 
     270                 :         80 : void ODefinitionContainer::notifyByName( ResettableMutexGuard& _rGuard, const ::rtl::OUString& _rName,
     271                 :            :         const Reference< XContent >& _xNewElement, const Reference< XContent >& _xOldElement,
     272                 :            :         ContainerOperation _eOperation, ListenerType _eType )
     273                 :            : {
     274                 :         80 :     bool bApprove = ( _eType == ApproveListeners );
     275                 :            : 
     276         [ +  + ]:         80 :     ::cppu::OInterfaceContainerHelper& rContainer( bApprove ? m_aApproveListeners : m_aContainerListeners );
     277 [ +  - ][ -  + ]:         80 :     if ( !rContainer.getLength() )
     278                 :         80 :         return;
     279                 :            : 
     280 [ #  # ][ #  # ]:          0 :     ContainerEvent aEvent( *this, makeAny( _rName ), makeAny( _xNewElement ), makeAny( _xOldElement ) );
         [ #  # ][ #  # ]
                 [ #  # ]
     281                 :            : 
     282         [ #  # ]:          0 :     _rGuard.clear();
     283   [ #  #  #  # ]:          0 :     switch ( _eOperation )
     284                 :            :     {
     285                 :            :         case E_INSERTED:
     286         [ #  # ]:          0 :             if ( bApprove )
     287                 :            :                 rContainer.forEach< XContainerApproveListener, RaiseExceptionFromVeto >(
     288         [ #  # ]:          0 :                     RaiseExceptionFromVeto( &XContainerApproveListener::approveInsertElement, aEvent ) );
     289                 :            :             else
     290         [ #  # ]:          0 :                 rContainer.notifyEach( &XContainerListener::elementInserted, aEvent );
     291                 :          0 :             break;
     292                 :            :         case E_REPLACED:
     293         [ #  # ]:          0 :             if ( bApprove )
     294                 :            :                 rContainer.forEach< XContainerApproveListener, RaiseExceptionFromVeto >(
     295         [ #  # ]:          0 :                     RaiseExceptionFromVeto( &XContainerApproveListener::approveReplaceElement, aEvent ) );
     296                 :            :             else
     297         [ #  # ]:          0 :                 rContainer.notifyEach( &XContainerListener::elementReplaced, aEvent );
     298                 :          0 :             break;
     299                 :            :         case E_REMOVED:
     300         [ #  # ]:          0 :             if ( bApprove )
     301                 :            :                 rContainer.forEach< XContainerApproveListener, RaiseExceptionFromVeto >(
     302         [ #  # ]:          0 :                     RaiseExceptionFromVeto( &XContainerApproveListener::approveRemoveElement, aEvent ) );
     303                 :            :             else
     304         [ #  # ]:          0 :                 rContainer.notifyEach( &XContainerListener::elementRemoved, aEvent );
     305                 :          0 :             break;
     306                 :            :     }
     307                 :            : 
     308         [ #  # ]:          0 :     if ( bApprove )
     309 [ #  # ][ #  # ]:         80 :         _rGuard.reset();
     310                 :            : }
     311                 :            : 
     312                 :         84 : void SAL_CALL ODefinitionContainer::addContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
     313                 :            : {
     314         [ +  - ]:         84 :     if (_rxListener.is())
     315                 :         84 :         m_aContainerListeners.addInterface(_rxListener);
     316                 :         84 : }
     317                 :            : 
     318                 :         84 : void SAL_CALL ODefinitionContainer::removeContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
     319                 :            : {
     320         [ +  - ]:         84 :     if (_rxListener.is())
     321                 :         84 :         m_aContainerListeners.removeInterface(_rxListener);
     322                 :         84 : }
     323                 :            : 
     324                 :         56 : void SAL_CALL ODefinitionContainer::addContainerApproveListener( const Reference< XContainerApproveListener >& _Listener ) throw (RuntimeException)
     325                 :            : {
     326         [ +  - ]:         56 :     if ( _Listener.is() )
     327                 :         56 :         m_aApproveListeners.addInterface( _Listener );
     328                 :         56 : }
     329                 :            : 
     330                 :         56 : void SAL_CALL ODefinitionContainer::removeContainerApproveListener( const Reference< XContainerApproveListener >& _Listener ) throw (RuntimeException)
     331                 :            : {
     332         [ +  - ]:         56 :     if ( _Listener.is() )
     333                 :         56 :         m_aApproveListeners.removeInterface( _Listener );
     334                 :         56 : }
     335                 :            : 
     336                 :            : // XElementAccess
     337                 :          0 : Type SAL_CALL ODefinitionContainer::getElementType( ) throw (RuntimeException)
     338                 :            : {
     339                 :          0 :     return ::getCppuType( static_cast< Reference< XContent >* >(NULL) );
     340                 :            : }
     341                 :            : 
     342                 :       1218 : sal_Bool SAL_CALL ODefinitionContainer::hasElements( ) throw (RuntimeException)
     343                 :            : {
     344         [ +  - ]:       1218 :     MutexGuard aGuard(m_aMutex);
     345         [ +  - ]:       1218 :     return !m_aDocuments.empty();
     346                 :            : }
     347                 :            : 
     348                 :            : // XEnumerationAccess
     349                 :          0 : Reference< XEnumeration > SAL_CALL ODefinitionContainer::createEnumeration(  ) throw(RuntimeException)
     350                 :            : {
     351         [ #  # ]:          0 :     MutexGuard aGuard(m_aMutex);
     352 [ #  # ][ #  # ]:          0 :     return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess*>(this));
         [ #  # ][ #  # ]
                 [ #  # ]
     353                 :            : }
     354                 :            : 
     355                 :            : // XIndexAccess
     356                 :          0 : sal_Int32 SAL_CALL ODefinitionContainer::getCount(  ) throw(RuntimeException)
     357                 :            : {
     358         [ #  # ]:          0 :     MutexGuard aGuard(m_aMutex);
     359         [ #  # ]:          0 :     return m_aDocuments.size();
     360                 :            : }
     361                 :            : 
     362                 :          0 : Any SAL_CALL ODefinitionContainer::getByIndex( sal_Int32 _nIndex ) throw(IndexOutOfBoundsException, WrappedTargetException, RuntimeException)
     363                 :            : {
     364         [ #  # ]:          0 :     MutexGuard aGuard(m_aMutex);
     365                 :            : 
     366 [ #  # ][ #  # ]:          0 :     if ((_nIndex < 0) || (_nIndex >= (sal_Int32)m_aDocuments.size()))
                 [ #  # ]
     367         [ #  # ]:          0 :         throw IndexOutOfBoundsException();
     368                 :            : 
     369                 :          0 :     Documents::iterator aPos = m_aDocuments[_nIndex];
     370         [ #  # ]:          0 :     Reference<XContent> xProp = aPos->second;
     371         [ #  # ]:          0 :     if (!xProp.is())
     372                 :            :     {   // that's the first access to the object
     373                 :            :         // -> create it
     374 [ #  # ][ #  # ]:          0 :         xProp = createObject(aPos->first);
     375 [ #  # ][ #  # ]:          0 :         aPos->second = Documents::mapped_type();
                 [ #  # ]
     376                 :            :         // and update the name-access map
     377                 :            :     }
     378                 :            : 
     379 [ #  # ][ #  # ]:          0 :     return makeAny(xProp);
     380                 :            : }
     381                 :            : 
     382                 :         40 : Any SAL_CALL ODefinitionContainer::getByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
     383                 :            : {
     384         [ +  - ]:         40 :     MutexGuard aGuard(m_aMutex);
     385                 :            : 
     386 [ +  - ][ +  - ]:         40 :     return makeAny( implGetByName( _rName, sal_True ) );
                 [ +  - ]
     387                 :            : }
     388                 :            : 
     389                 :         40 : Reference< XContent > ODefinitionContainer::implGetByName(const ::rtl::OUString& _rName, sal_Bool _bReadIfNeccessary) throw (NoSuchElementException)
     390                 :            : {
     391         [ +  - ]:         40 :     Documents::iterator aMapPos = m_aDocumentMap.find(_rName);
     392         [ -  + ]:         40 :     if (aMapPos == m_aDocumentMap.end())
     393 [ #  # ][ #  # ]:          0 :         throw NoSuchElementException(_rName,*this);
     394                 :            : 
     395         [ +  - ]:         40 :     Reference< XContent > xProp = aMapPos->second;
     396                 :            : 
     397 [ +  - ][ +  + ]:         40 :     if (_bReadIfNeccessary && !xProp.is())
                 [ +  + ]
     398                 :            :     {   // the object has never been accessed before, so we have to read it now
     399                 :            :         // (that's the expensive part)
     400                 :            : 
     401                 :            :         // create the object and insert it into the map
     402 [ +  - ][ +  - ]:         12 :         xProp = createObject(_rName);
     403         [ +  - ]:         12 :         aMapPos->second = xProp;
     404         [ +  - ]:         40 :         addObjectListener(xProp);
     405                 :            :     }
     406                 :            : 
     407                 :         40 :     return xProp;
     408                 :            : }
     409                 :            : 
     410                 :         60 : Sequence< ::rtl::OUString > SAL_CALL ODefinitionContainer::getElementNames(  ) throw(RuntimeException)
     411                 :            : {
     412         [ +  - ]:         60 :     MutexGuard aGuard(m_aMutex);
     413                 :            : 
     414         [ +  - ]:         60 :     Sequence< ::rtl::OUString > aNames(m_aDocumentMap.size());
     415         [ +  - ]:         60 :     ::rtl::OUString* pNames = aNames.getArray();
     416                 :         60 :     Documents::iterator aEnd = m_aDocumentMap.end();
     417         [ +  + ]:         64 :     for (   Documents::iterator aNameIter = m_aDocumentMap.begin();
     418                 :            :             aNameIter != aEnd;
     419                 :            :             ++pNames, ++aNameIter
     420                 :            :         )
     421                 :            :     {
     422                 :          4 :         *pNames = aNameIter->first;
     423                 :            :     }
     424                 :            : 
     425         [ +  - ]:         60 :     return aNames;
     426                 :            : }
     427                 :            : 
     428                 :         56 : sal_Bool SAL_CALL ODefinitionContainer::hasByName( const ::rtl::OUString& _rName ) throw(RuntimeException)
     429                 :            : {
     430         [ +  - ]:         56 :     MutexGuard aGuard(m_aMutex);
     431                 :            : 
     432 [ +  - ][ +  - ]:         56 :     return checkExistence(_rName);
     433                 :            : }
     434                 :            : 
     435                 :         16 : void SAL_CALL ODefinitionContainer::disposing( const EventObject& _rSource ) throw(RuntimeException)
     436                 :            : {
     437         [ +  - ]:         16 :     MutexGuard aGuard(m_aMutex);
     438         [ +  - ]:         16 :     Reference< XContent > xSource(_rSource.Source, UNO_QUERY);
     439                 :            :     // it's one of our documents ....
     440                 :         16 :     Documents::iterator aIter = m_aDocumentMap.begin();
     441                 :         16 :     Documents::iterator aEnd = m_aDocumentMap.end();
     442         [ -  + ]:         16 :     for (;aIter != aEnd;++aIter )
     443                 :            :     {
     444 [ #  # ][ #  # ]:          0 :         if ( xSource == aIter->second.get() )
                 [ #  # ]
     445                 :            :         {
     446         [ #  # ]:          0 :             removeObjectListener(xSource);
     447                 :            :             // and clear our document map/vector, so the object will be recreated on next access
     448 [ #  # ][ #  # ]:          0 :             aIter->second = Documents::mapped_type();
                 [ #  # ]
     449                 :            :         }
     450         [ +  - ]:         16 :     }
     451                 :         16 : }
     452                 :            : 
     453                 :          0 : void ODefinitionContainer::implRemove(const ::rtl::OUString& _rName)
     454                 :            : {
     455                 :            :     // from the object maps
     456         [ #  # ]:          0 :     Documents::iterator aFind = m_aDocumentMap.find(_rName);
     457         [ #  # ]:          0 :     if ( aFind != m_aDocumentMap.end() )
     458                 :            :     {
     459 [ #  # ][ #  # ]:          0 :         m_aDocuments.erase( ::std::find(m_aDocuments.begin(),m_aDocuments.end(),aFind));
     460         [ #  # ]:          0 :         m_aDocumentMap.erase(aFind);
     461                 :            : 
     462 [ #  # ][ #  # ]:          0 :         getDefinitions().erase( _rName );
     463                 :            : 
     464         [ #  # ]:          0 :         notifyDataSourceModified();
     465                 :            :     }
     466                 :          0 : }
     467                 :            : 
     468                 :            : namespace
     469                 :            : {
     470                 :         40 :     bool    lcl_ensureName( const Reference< XContent >& _rxContent, const ::rtl::OUString& _rName )
     471                 :            :     {
     472         [ -  + ]:         40 :         if ( !_rxContent.is() )
     473                 :          0 :             return true;
     474                 :            : 
     475                 :            :         // obtain the current name. If it's the same as the new one,
     476                 :            :         // don't do anything
     477                 :            :         try
     478                 :            :         {
     479         [ +  - ]:         40 :             Reference< XPropertySet > xProps( _rxContent, UNO_QUERY );
     480         [ +  - ]:         40 :             if ( xProps.is() )
     481                 :            :             {
     482                 :         40 :                 ::rtl::OUString sCurrentName;
     483 [ +  - ][ +  - ]:         40 :                 OSL_VERIFY( xProps->getPropertyValue( PROPERTY_NAME ) >>= sCurrentName );
                 [ +  - ]
     484         [ +  - ]:         40 :                 if ( sCurrentName.equals( _rName ) )
     485         [ -  + ]:         40 :                     return true;
     486 [ -  + ][ #  # ]:         40 :             }
     487                 :            :         }
     488   [ #  #  #  # ]:          0 :         catch( const Exception& )
     489                 :            :         {
     490                 :            :             OSL_FAIL( "lcl_ensureName: caught an exception while obtaining the current name!" );
     491                 :            :         }
     492                 :            : 
     493                 :            :         // set the new name
     494         [ #  # ]:          0 :         Reference< XRename > xRename( _rxContent, UNO_QUERY );
     495                 :            :         OSL_ENSURE( xRename.is(), "lcl_ensureName: invalid content (not renameable)!" );
     496         [ #  # ]:          0 :         if ( !xRename.is() )
     497                 :          0 :             return false;
     498                 :            :         try
     499                 :            :         {
     500 [ #  # ][ #  # ]:          0 :             xRename->rename( _rName );
     501                 :          0 :             return true;
     502                 :            :         }
     503         [ #  # ]:          0 :         catch( const Exception& )
     504                 :            :         {
     505                 :            :             OSL_FAIL( "lcl_ensureName: caught an exception!" );
     506                 :            :         }
     507                 :         40 :         return false;
     508                 :            :     }
     509                 :            : }
     510                 :            : 
     511                 :         40 : void ODefinitionContainer::implAppend(const ::rtl::OUString& _rName, const Reference< XContent >& _rxNewObject)
     512                 :            : {
     513         [ +  - ]:         40 :     MutexGuard aGuard(m_aMutex);
     514                 :            :     try
     515                 :            :     {
     516         [ +  - ]:         40 :         Reference<XChild> xChild(_rxNewObject,UNO_QUERY);
     517         [ +  - ]:         40 :         if ( xChild.is() )
     518 [ +  - ][ +  - ]:         40 :             xChild->setParent(static_cast<OWeakObject*>(this));
                 [ +  - ]
     519                 :            : 
     520         [ +  - ]:         40 :         ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
     521         [ +  - ]:         40 :         ODefinitionContainer_Impl::const_iterator aFind = rDefinitions.find( _rName );
     522         [ +  - ]:         40 :         if ( aFind == rDefinitions.end() )
     523                 :            :         {
     524                 :            :             // ensure that the new object has the proper name.
     525                 :            :             // Somebody could create an object with name "foo", and insert it as "bar"
     526                 :            :             // into a container. In this case, we need to ensure that the object name
     527                 :            :             // is also "bar"
     528                 :            :             // #i44786#
     529         [ +  - ]:         40 :             lcl_ensureName( _rxNewObject, _rName );
     530                 :            : 
     531         [ +  - ]:         40 :             ::rtl::Reference< OContentHelper > pContent = OContentHelper::getImplementation( _rxNewObject );
     532         [ +  - ]:         40 :             if ( pContent.is() )
     533                 :            :             {
     534         [ +  - ]:         40 :                 TContentPtr pImpl = pContent->getImpl();
     535 [ +  - ][ +  - ]:         40 :                 rDefinitions.erase( pImpl );
                 [ +  - ]
     536                 :         40 :                 pImpl->m_aProps.aTitle = _rName;
     537 [ +  - ][ +  - ]:         40 :                 rDefinitions.insert( _rName, pImpl );
         [ +  - ][ +  - ]
     538                 :         40 :             }
     539                 :            :         }
     540                 :            : 
     541                 :            : 
     542 [ +  - ][ +  - ]:         40 :         m_aDocuments.push_back(m_aDocumentMap.insert(Documents::value_type(_rName,_rxNewObject)).first);
         [ +  - ][ +  - ]
     543         [ +  - ]:         40 :         notifyDataSourceModified();
     544                 :            :         // now update our structures
     545         [ +  - ]:         40 :         if ( _rxNewObject.is() )
     546 [ +  - ][ #  # ]:         40 :             addObjectListener(_rxNewObject);
     547                 :            :     }
     548         [ #  # ]:          0 :     catch(Exception&)
     549                 :            :     {
     550                 :            :         OSL_FAIL("ODefinitionContainer::implAppend: caught something !");
     551         [ +  - ]:         40 :     }
     552                 :         40 : }
     553                 :            : 
     554                 :          0 : void ODefinitionContainer::implReplace(const ::rtl::OUString& _rName, const Reference< XContent >& _rxNewObject)
     555                 :            : {
     556                 :            :     OSL_ENSURE(checkExistence(_rName), "ODefinitionContainer::implReplace : invalid name !");
     557                 :            : 
     558         [ #  # ]:          0 :     Documents::iterator aFind = m_aDocumentMap.find(_rName);
     559 [ #  # ][ #  # ]:          0 :     removeObjectListener(aFind->second);
     560         [ #  # ]:          0 :     aFind->second = _rxNewObject;
     561 [ #  # ][ #  # ]:          0 :     addObjectListener(aFind->second);
     562                 :          0 : }
     563                 :            : 
     564                 :         40 : void ODefinitionContainer::approveNewObject(const ::rtl::OUString& _sName,const Reference< XContent >& _rxObject) const
     565                 :            : {
     566                 :            :     // check the arguments
     567         [ -  + ]:         40 :     if ( _sName.isEmpty() )
     568                 :            :         throw IllegalArgumentException(
     569                 :            :             DBA_RES( RID_STR_NAME_MUST_NOT_BE_EMPTY ),
     570                 :            :             *this,
     571 [ #  # ][ #  # ]:          0 :             0 );
                 [ #  # ]
     572                 :            : 
     573 [ -  + ][ #  # ]:         40 :     if ( m_bCheckSlash && _sName.indexOf( '/' ) != -1 )
                 [ -  + ]
     574                 :            :         throw IllegalArgumentException(
     575                 :            :             m_aErrorHelper.getErrorMessage( ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES ),
     576                 :            :             *this,
     577 [ #  # ][ #  # ]:          0 :             0 );
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     578                 :            : 
     579         [ -  + ]:         40 :     if ( !_rxObject.is() )
     580                 :            :         throw IllegalArgumentException(
     581                 :            :             DBA_RES( RID_STR_NO_NULL_OBJECTS_IN_CONTAINER ),
     582                 :            :             *this,
     583 [ #  # ][ #  # ]:          0 :             0 );
                 [ #  # ]
     584                 :            : 
     585         [ +  - ]:         40 :     const ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
     586 [ +  - ][ -  + ]:         40 :     if ( rDefinitions.find( _sName ) != rDefinitions.end() )
     587                 :            :         throw ElementExistException(
     588                 :            :             DBA_RES( RID_STR_NAME_ALREADY_USED ),
     589 [ #  # ][ #  # ]:          0 :             *this );
                 [ #  # ]
     590                 :            : 
     591         [ +  - ]:         40 :     ::rtl::Reference< OContentHelper > pContent( OContentHelper::getImplementation( _rxObject ) );
     592         [ -  + ]:         40 :     if ( !pContent.is() )
     593                 :            :         throw IllegalArgumentException(
     594                 :            :             DBA_RES( RID_STR_OBJECT_CONTAINER_MISMATCH ),
     595                 :            :             *this,
     596 [ #  # ][ #  # ]:          0 :             1 );
                 [ #  # ]
     597                 :            : 
     598 [ +  - ][ +  - ]:         40 :     if ( rDefinitions.find( pContent->getImpl() ) != rDefinitions.end() )
         [ +  - ][ -  + ]
     599                 :            :         throw ElementExistException(
     600                 :            :             DBA_RES( RID_STR_OBJECT_ALREADY_CONTAINED ),
     601 [ #  # ][ #  # ]:         40 :             *this );
                 [ #  # ]
     602                 :         40 : }
     603                 :            : 
     604                 :            : // XPropertyChangeListener
     605                 :          0 : void SAL_CALL ODefinitionContainer::propertyChange( const PropertyChangeEvent& evt ) throw (RuntimeException)
     606                 :            : {
     607         [ #  # ]:          0 :     ClearableMutexGuard aGuard(m_aMutex);
     608 [ #  # ][ #  # ]:          0 :     if( evt.PropertyName == (rtl::OUString) PROPERTY_NAME || evt.PropertyName ==  "Title" )
         [ #  # ][ #  # ]
           [ #  #  #  # ]
     609                 :            :     {
     610                 :          0 :         m_bInPropertyChange = sal_True;
     611                 :            :         try
     612                 :            :         {
     613                 :          0 :             ::rtl::OUString sNewName,sOldName;
     614                 :          0 :             evt.OldValue >>= sOldName;
     615                 :          0 :             evt.NewValue >>= sNewName;
     616         [ #  # ]:          0 :             Reference<XContent> xContent( evt.Source, UNO_QUERY );
     617         [ #  # ]:          0 :             removeObjectListener( xContent );
     618         [ #  # ]:          0 :             implRemove( sOldName );
     619         [ #  # ]:          0 :             implAppend( sNewName, xContent );
     620                 :            :         }
     621         [ #  # ]:          0 :         catch(const Exception&)
     622                 :            :         {
     623                 :            :             DBG_UNHANDLED_EXCEPTION();
     624         [ #  # ]:          0 :             throw RuntimeException();
     625                 :            :         }
     626                 :          0 :         m_bInPropertyChange = sal_False;
     627         [ #  # ]:          0 :     }
     628                 :          0 : }
     629                 :            : 
     630                 :            : // XVetoableChangeListener
     631                 :          0 : void SAL_CALL ODefinitionContainer::vetoableChange( const PropertyChangeEvent& aEvent ) throw (PropertyVetoException, RuntimeException)
     632                 :            : {
     633         [ #  # ]:          0 :     MutexGuard aGuard(m_aMutex);
     634                 :            : 
     635 [ #  # ][ #  # ]:          0 :     if( aEvent.PropertyName == (rtl::OUString) PROPERTY_NAME || aEvent.PropertyName == "Title" )
         [ #  # ][ #  # ]
           [ #  #  #  # ]
     636                 :            :     {
     637                 :          0 :         ::rtl::OUString sNewName;
     638                 :          0 :         aEvent.NewValue >>= sNewName;
     639 [ #  # ][ #  # ]:          0 :         if(hasByName(sNewName))
     640         [ #  # ]:          0 :             throw PropertyVetoException();
     641         [ #  # ]:          0 :     }
     642                 :          0 : }
     643                 :            : 
     644                 :         52 : void ODefinitionContainer::addObjectListener(const Reference< XContent >& _xNewObject)
     645                 :            : {
     646                 :            :     OSL_ENSURE(_xNewObject.is(),"ODefinitionContainer::addObjectListener: Object is null!");
     647         [ +  - ]:         52 :     Reference<XPropertySet> xProp(_xNewObject,UNO_QUERY);
     648         [ +  - ]:         52 :     if ( xProp.is() )
     649                 :            :     {
     650 [ +  - ][ +  - ]:         52 :         xProp->addPropertyChangeListener(PROPERTY_NAME, this);
         [ +  - ][ +  - ]
     651 [ +  - ][ +  - ]:         52 :         xProp->addVetoableChangeListener(PROPERTY_NAME, this);
         [ +  - ][ +  - ]
     652                 :         52 :     }
     653                 :         52 : }
     654                 :            : 
     655                 :          2 : void ODefinitionContainer::removeObjectListener(const Reference< XContent >& _xNewObject)
     656                 :            : {
     657         [ +  - ]:          2 :     Reference<XPropertySet> xProp(_xNewObject,UNO_QUERY);
     658         [ +  - ]:          2 :     if ( xProp.is() )
     659                 :            :     {
     660 [ +  - ][ +  - ]:          2 :         xProp->removePropertyChangeListener(PROPERTY_NAME, this);
         [ +  - ][ +  - ]
     661 [ +  - ][ +  - ]:          2 :         xProp->removeVetoableChangeListener(PROPERTY_NAME, this);
         [ +  - ][ +  - ]
     662                 :          2 :     }
     663                 :          2 : }
     664                 :            : 
     665                 :         56 : sal_Bool ODefinitionContainer::checkExistence(const ::rtl::OUString& _rName)
     666                 :            : {
     667         [ +  - ]:         56 :     return m_aDocumentMap.find(_rName) != m_aDocumentMap.end();
     668                 :            : }
     669                 :            : 
     670                 :            : }
     671                 :            : // namespace dbaccess
     672                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10