1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*96de5490SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*96de5490SAndrew Rist * distributed with this work for additional information
6*96de5490SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*96de5490SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*96de5490SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*96de5490SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist * software distributed under the License is distributed on an
15*96de5490SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist * KIND, either express or implied. See the License for the
17*96de5490SAndrew Rist * specific language governing permissions and limitations
18*96de5490SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*96de5490SAndrew Rist *************************************************************/
21*96de5490SAndrew Rist
22*96de5490SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir #ifndef DBA_CONTAINERMEDIATOR_HXX
27cdf0e10cSrcweir #include "ContainerMediator.hxx"
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
30cdf0e10cSrcweir #include "dbastrings.hrc"
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #ifndef DBA_PROPERTYSETFORWARD_HXX
33cdf0e10cSrcweir #include "PropertyForward.hxx"
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir
36cdf0e10cSrcweir #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBUTE_HPP_
37cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
40cdf0e10cSrcweir #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
41cdf0e10cSrcweir #endif
42cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
43cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir #include <com/sun/star/sdbcx/XRename.hpp>
46cdf0e10cSrcweir #ifndef _CONNECTIVITY_DBTOOLS_HXX_
47cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
48cdf0e10cSrcweir #endif
49cdf0e10cSrcweir #ifndef _COMPHELPER_PROPERTY_HXX_
50cdf0e10cSrcweir #include <comphelper/property.hxx>
51cdf0e10cSrcweir #endif
52cdf0e10cSrcweir #ifndef _TOOLS_DEBUG_HXX
53cdf0e10cSrcweir #include <tools/debug.hxx>
54cdf0e10cSrcweir #endif
55cdf0e10cSrcweir #ifndef TOOLS_DIAGNOSE_EX_H
56cdf0e10cSrcweir #include <tools/diagnose_ex.h>
57cdf0e10cSrcweir #endif
58cdf0e10cSrcweir
59cdf0e10cSrcweir
60cdf0e10cSrcweir //........................................................................
61cdf0e10cSrcweir namespace dbaccess
62cdf0e10cSrcweir {
63cdf0e10cSrcweir //........................................................................
64cdf0e10cSrcweir using namespace ::com::sun::star::uno;
65cdf0e10cSrcweir using namespace ::com::sun::star::lang;
66cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
67cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
68cdf0e10cSrcweir using namespace ::com::sun::star::beans;
69cdf0e10cSrcweir using namespace ::com::sun::star::container;
70cdf0e10cSrcweir
DBG_NAME(OContainerMediator)71cdf0e10cSrcweir DBG_NAME(OContainerMediator)
72cdf0e10cSrcweir OContainerMediator::OContainerMediator( const Reference< XContainer >& _xContainer, const Reference< XNameAccess >& _xSettings,
73cdf0e10cSrcweir const Reference< XConnection >& _rxConnection, ContainerType _eType )
74cdf0e10cSrcweir : m_xSettings( _xSettings )
75cdf0e10cSrcweir , m_xContainer( _xContainer )
76cdf0e10cSrcweir , m_aConnection( _rxConnection )
77cdf0e10cSrcweir , m_eType( _eType )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir DBG_CTOR(OContainerMediator,NULL);
80cdf0e10cSrcweir
81cdf0e10cSrcweir if ( _xSettings.is() && _xContainer.is() )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir osl_incrementInterlockedCount(&m_refCount);
84cdf0e10cSrcweir try
85cdf0e10cSrcweir {
86cdf0e10cSrcweir m_xContainer->addContainerListener(this);
87cdf0e10cSrcweir Reference< XContainer > xContainer(_xSettings, UNO_QUERY);
88cdf0e10cSrcweir if ( xContainer.is() )
89cdf0e10cSrcweir xContainer->addContainerListener(this);
90cdf0e10cSrcweir }
91cdf0e10cSrcweir catch(Exception&)
92cdf0e10cSrcweir {
93cdf0e10cSrcweir OSL_ENSURE(sal_False, "OContainerMediator::OContainerMediator: caught an exception!");
94cdf0e10cSrcweir }
95cdf0e10cSrcweir osl_decrementInterlockedCount( &m_refCount );
96cdf0e10cSrcweir }
97cdf0e10cSrcweir else
98cdf0e10cSrcweir {
99cdf0e10cSrcweir m_xSettings.clear();
100cdf0e10cSrcweir m_xContainer.clear();
101cdf0e10cSrcweir }
102cdf0e10cSrcweir }
103cdf0e10cSrcweir // -----------------------------------------------------------------------------
~OContainerMediator()104cdf0e10cSrcweir OContainerMediator::~OContainerMediator()
105cdf0e10cSrcweir {
106cdf0e10cSrcweir DBG_DTOR(OContainerMediator,NULL);
107cdf0e10cSrcweir acquire();
108cdf0e10cSrcweir impl_cleanup_nothrow();
109cdf0e10cSrcweir }
110cdf0e10cSrcweir
111cdf0e10cSrcweir // -----------------------------------------------------------------------------
impl_cleanup_nothrow()112cdf0e10cSrcweir void OContainerMediator::impl_cleanup_nothrow()
113cdf0e10cSrcweir {
114cdf0e10cSrcweir try
115cdf0e10cSrcweir {
116cdf0e10cSrcweir Reference< XContainer > xContainer( m_xSettings, UNO_QUERY );
117cdf0e10cSrcweir if ( xContainer.is() )
118cdf0e10cSrcweir xContainer->removeContainerListener( this );
119cdf0e10cSrcweir m_xSettings.clear();
120cdf0e10cSrcweir
121cdf0e10cSrcweir xContainer = m_xContainer;
122cdf0e10cSrcweir if ( xContainer.is() )
123cdf0e10cSrcweir xContainer->removeContainerListener( this );
124cdf0e10cSrcweir m_xContainer.clear();
125cdf0e10cSrcweir
126cdf0e10cSrcweir m_aForwardList.clear();
127cdf0e10cSrcweir }
128cdf0e10cSrcweir catch( const Exception& )
129cdf0e10cSrcweir {
130cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
131cdf0e10cSrcweir }
132cdf0e10cSrcweir }
133cdf0e10cSrcweir
134cdf0e10cSrcweir // -----------------------------------------------------------------------------
elementInserted(const ContainerEvent & _rEvent)135cdf0e10cSrcweir void SAL_CALL OContainerMediator::elementInserted( const ContainerEvent& _rEvent ) throw(RuntimeException)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex);
138cdf0e10cSrcweir if ( _rEvent.Source == m_xSettings && m_xSettings.is() )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir ::rtl::OUString sElementName;
141cdf0e10cSrcweir _rEvent.Accessor >>= sElementName;
142cdf0e10cSrcweir PropertyForwardList::iterator aFind = m_aForwardList.find(sElementName);
143cdf0e10cSrcweir if ( aFind != m_aForwardList.end() )
144cdf0e10cSrcweir {
145cdf0e10cSrcweir Reference< XPropertySet> xDest(_rEvent.Element,UNO_QUERY);
146cdf0e10cSrcweir aFind->second->setDefinition( xDest );
147cdf0e10cSrcweir }
148cdf0e10cSrcweir }
149cdf0e10cSrcweir }
150cdf0e10cSrcweir // -----------------------------------------------------------------------------
elementRemoved(const ContainerEvent & _rEvent)151cdf0e10cSrcweir void SAL_CALL OContainerMediator::elementRemoved( const ContainerEvent& _rEvent ) throw(RuntimeException)
152cdf0e10cSrcweir {
153cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex);
154cdf0e10cSrcweir Reference< XContainer > xContainer = m_xContainer;
155cdf0e10cSrcweir if ( _rEvent.Source == xContainer && xContainer.is() )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir ::rtl::OUString sElementName;
158cdf0e10cSrcweir _rEvent.Accessor >>= sElementName;
159cdf0e10cSrcweir m_aForwardList.erase(sElementName);
160cdf0e10cSrcweir try
161cdf0e10cSrcweir {
162cdf0e10cSrcweir Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY );
163cdf0e10cSrcweir if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
164cdf0e10cSrcweir xNameContainer->removeByName( sElementName );
165cdf0e10cSrcweir }
166cdf0e10cSrcweir catch( const Exception& )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
169cdf0e10cSrcweir }
170cdf0e10cSrcweir }
171cdf0e10cSrcweir }
172cdf0e10cSrcweir // -----------------------------------------------------------------------------
elementReplaced(const ContainerEvent & _rEvent)173cdf0e10cSrcweir void SAL_CALL OContainerMediator::elementReplaced( const ContainerEvent& _rEvent ) throw(RuntimeException)
174cdf0e10cSrcweir {
175cdf0e10cSrcweir Reference< XContainer > xContainer = m_xContainer;
176cdf0e10cSrcweir if ( _rEvent.Source == xContainer && xContainer.is() )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir ::rtl::OUString sElementName;
179cdf0e10cSrcweir _rEvent.ReplacedElement >>= sElementName;
180cdf0e10cSrcweir
181cdf0e10cSrcweir PropertyForwardList::iterator aFind = m_aForwardList.find(sElementName);
182cdf0e10cSrcweir if ( aFind != m_aForwardList.end() )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir ::rtl::OUString sNewName;
185cdf0e10cSrcweir _rEvent.Accessor >>= sNewName;
186cdf0e10cSrcweir try
187cdf0e10cSrcweir {
188cdf0e10cSrcweir Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY_THROW );
189cdf0e10cSrcweir if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir Reference<XRename> xSource(m_xSettings->getByName(sElementName),UNO_QUERY_THROW);
192cdf0e10cSrcweir xSource->rename(sNewName);
193cdf0e10cSrcweir }
194cdf0e10cSrcweir }
195cdf0e10cSrcweir catch( const Exception& )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
198cdf0e10cSrcweir }
199cdf0e10cSrcweir
200cdf0e10cSrcweir aFind->second->setName(sNewName);
201cdf0e10cSrcweir }
202cdf0e10cSrcweir }
203cdf0e10cSrcweir }
204cdf0e10cSrcweir
205cdf0e10cSrcweir // -----------------------------------------------------------------------------
disposing(const EventObject &)206cdf0e10cSrcweir void SAL_CALL OContainerMediator::disposing( const EventObject& /*Source*/ ) throw(RuntimeException)
207cdf0e10cSrcweir {
208cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex);
209cdf0e10cSrcweir
210cdf0e10cSrcweir impl_cleanup_nothrow();
211cdf0e10cSrcweir }
212cdf0e10cSrcweir
213cdf0e10cSrcweir // -----------------------------------------------------------------------------
impl_initSettings_nothrow(const::rtl::OUString & _rName,const Reference<XPropertySet> & _rxDestination)214cdf0e10cSrcweir void OContainerMediator::impl_initSettings_nothrow( const ::rtl::OUString& _rName, const Reference< XPropertySet >& _rxDestination )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir try
217cdf0e10cSrcweir {
218cdf0e10cSrcweir if ( m_xSettings.is() && m_xSettings->hasByName( _rName ) )
219cdf0e10cSrcweir {
220cdf0e10cSrcweir Reference< XPropertySet > xSettings( m_xSettings->getByName( _rName ), UNO_QUERY_THROW );
221cdf0e10cSrcweir ::comphelper::copyProperties( xSettings, _rxDestination );
222cdf0e10cSrcweir }
223cdf0e10cSrcweir }
224cdf0e10cSrcweir catch( const Exception& )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
227cdf0e10cSrcweir }
228cdf0e10cSrcweir }
229cdf0e10cSrcweir
230cdf0e10cSrcweir // -----------------------------------------------------------------------------
notifyElementCreated(const::rtl::OUString & _sName,const Reference<XPropertySet> & _xDest)231cdf0e10cSrcweir void OContainerMediator::notifyElementCreated( const ::rtl::OUString& _sName, const Reference< XPropertySet >& _xDest )
232cdf0e10cSrcweir {
233cdf0e10cSrcweir if ( !m_xSettings.is() )
234cdf0e10cSrcweir return;
235cdf0e10cSrcweir
236cdf0e10cSrcweir PropertyForwardList::iterator aFind = m_aForwardList.find( _sName );
237cdf0e10cSrcweir if ( aFind != m_aForwardList.end()
238cdf0e10cSrcweir && aFind->second->getDefinition().is()
239cdf0e10cSrcweir )
240cdf0e10cSrcweir {
241cdf0e10cSrcweir OSL_ENSURE( false, "OContainerMediator::notifyElementCreated: is this really a valid case?" );
242cdf0e10cSrcweir return;
243cdf0e10cSrcweir }
244cdf0e10cSrcweir
245cdf0e10cSrcweir ::std::vector< ::rtl::OUString > aPropertyList;
246cdf0e10cSrcweir try
247cdf0e10cSrcweir {
248cdf0e10cSrcweir // initially copy from the settings object (if existent) to the newly created object
249cdf0e10cSrcweir impl_initSettings_nothrow( _sName, _xDest );
250cdf0e10cSrcweir
251cdf0e10cSrcweir // collect the to-be-monitored properties
252cdf0e10cSrcweir Reference< XPropertySetInfo > xPSI( _xDest->getPropertySetInfo(), UNO_QUERY_THROW );
253cdf0e10cSrcweir Sequence< Property > aProperties( xPSI->getProperties() );
254cdf0e10cSrcweir const Property* property = aProperties.getConstArray();
255cdf0e10cSrcweir const Property* propertyEnd = aProperties.getConstArray() + aProperties.getLength();
256cdf0e10cSrcweir for ( ; property != propertyEnd; ++property )
257cdf0e10cSrcweir {
258cdf0e10cSrcweir if ( ( property->Attributes & PropertyAttribute::READONLY ) != 0 )
259cdf0e10cSrcweir continue;
260cdf0e10cSrcweir if ( ( property->Attributes & PropertyAttribute::BOUND ) == 0 )
261cdf0e10cSrcweir continue;
262cdf0e10cSrcweir
263cdf0e10cSrcweir aPropertyList.push_back( property->Name );
264cdf0e10cSrcweir }
265cdf0e10cSrcweir }
266cdf0e10cSrcweir catch( const Exception& )
267cdf0e10cSrcweir {
268cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
269cdf0e10cSrcweir }
270cdf0e10cSrcweir
271cdf0e10cSrcweir ::rtl::Reference< OPropertyForward > pForward( new OPropertyForward( _xDest, m_xSettings, _sName, aPropertyList ) );
272cdf0e10cSrcweir m_aForwardList[ _sName ] = pForward;
273cdf0e10cSrcweir }
274cdf0e10cSrcweir // -----------------------------------------------------------------------------
275cdf0e10cSrcweir //........................................................................
276cdf0e10cSrcweir } // namespace dbaccess
277cdf0e10cSrcweir //........................................................................
278