Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / core / api / querycontainer.cxx
blob59ad63ed2f6a95ed08e239990c2b22c4eb79e7a6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "querycontainer.hxx"
21 #include "dbastrings.hrc"
22 #include "query.hxx"
23 #include "objectnameapproval.hxx"
24 #include "veto.hxx"
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/container/XContainer.hpp>
28 #include <com/sun/star/container/XContainerApproveBroadcaster.hpp>
29 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
30 #include <com/sun/star/sdbc/XConnection.hpp>
31 #include <com/sun/star/sdb/QueryDefinition.hpp>
33 #include <connectivity/dbexception.hxx>
35 #include <tools/debug.hxx>
36 #include <osl/diagnose.h>
37 #include <comphelper/enumhelper.hxx>
38 #include <comphelper/uno3.hxx>
39 #include <comphelper/property.hxx>
40 #include <comphelper/sequence.hxx>
41 #include <comphelper/extract.hxx>
42 #include <cppuhelper/exc_hlp.hxx>
44 using namespace dbtools;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::ucb;
47 using namespace ::com::sun::star::lang;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::sdb;
50 using namespace ::com::sun::star::sdbc;
51 using namespace ::com::sun::star::sdbcx;
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::util;
54 using namespace ::osl;
55 using namespace ::comphelper;
56 using namespace ::cppu;
58 namespace dbaccess
61 // OQueryContainer
63 OQueryContainer::OQueryContainer(
64 const Reference< XNameContainer >& _rxCommandDefinitions
65 , const Reference< XConnection >& _rxConn
66 , const Reference< XComponentContext >& _rxORB,
67 ::dbtools::IWarningsContainer* _pWarnings)
68 :ODefinitionContainer(_rxORB,NULL,TContentPtr(new ODefinitionContainer_Impl))
69 ,m_pWarnings( _pWarnings )
70 ,m_xCommandDefinitions(_rxCommandDefinitions)
71 ,m_xConnection(_rxConn)
72 ,m_eDoingCurrently(NONE)
76 void OQueryContainer::init()
78 Reference< XContainer > xContainer( m_xCommandDefinitions, UNO_QUERY_THROW );
79 xContainer->addContainerListener( this );
81 Reference< XContainerApproveBroadcaster > xContainerApprove( m_xCommandDefinitions, UNO_QUERY_THROW );
82 xContainerApprove->addContainerApproveListener( this );
84 // fill my structures
85 ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
86 Sequence< OUString > sDefinitionNames = m_xCommandDefinitions->getElementNames();
87 const OUString* pDefinitionName = sDefinitionNames.getConstArray();
88 const OUString* pEnd = pDefinitionName + sDefinitionNames.getLength();
89 for ( ; pDefinitionName != pEnd; ++pDefinitionName )
91 rDefinitions.insert( *pDefinitionName, TContentPtr() );
92 m_aDocuments.push_back(m_aDocumentMap.insert(Documents::value_type(*pDefinitionName,Documents::mapped_type())).first);
95 setElementApproval( PContainerApprove( new ObjectNameApproval( m_xConnection, ObjectNameApproval::TypeQuery ) ) );
98 rtl::Reference<OQueryContainer> OQueryContainer::create(
99 const Reference< XNameContainer >& _rxCommandDefinitions
100 , const Reference< XConnection >& _rxConn
101 , const Reference< XComponentContext >& _rxORB,
102 ::dbtools::IWarningsContainer* _pWarnings)
104 rtl::Reference<OQueryContainer> c(
105 new OQueryContainer(
106 _rxCommandDefinitions, _rxConn, _rxORB, _pWarnings));
107 c->init();
108 return c;
111 OQueryContainer::~OQueryContainer()
113 // dispose();
114 // maybe we're already disposed, but this should be uncritical
117 IMPLEMENT_FORWARD_XINTERFACE2( OQueryContainer,ODefinitionContainer,OQueryContainer_Base)
119 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OQueryContainer,ODefinitionContainer,OQueryContainer_Base)
121 void OQueryContainer::disposing()
123 ODefinitionContainer::disposing();
124 MutexGuard aGuard(m_aMutex);
125 if ( !m_xCommandDefinitions.is() )
126 // already disposed
127 return;
129 Reference< XContainer > xContainer( m_xCommandDefinitions, UNO_QUERY );
130 xContainer->removeContainerListener( this );
131 Reference< XContainerApproveBroadcaster > xContainerApprove( m_xCommandDefinitions, UNO_QUERY );
132 xContainerApprove->removeContainerApproveListener( this );
134 m_xCommandDefinitions = NULL;
135 m_xConnection = NULL;
138 // XServiceInfo
139 IMPLEMENT_SERVICE_INFO2(OQueryContainer, "com.sun.star.sdb.dbaccess.OQueryContainer", SERVICE_SDBCX_CONTAINER, SERVICE_SDB_QUERIES)
141 // XDataDescriptorFactory
142 Reference< XPropertySet > SAL_CALL OQueryContainer::createDataDescriptor( ) throw(RuntimeException, std::exception)
144 return new OQueryDescriptor();
147 // XAppend
148 void SAL_CALL OQueryContainer::appendByDescriptor( const Reference< XPropertySet >& _rxDesc ) throw(SQLException, ElementExistException, RuntimeException, std::exception)
150 ResettableMutexGuard aGuard(m_aMutex);
151 if ( !m_xCommandDefinitions.is() )
152 throw DisposedException( OUString(), *this );
154 // first clone this object's CommandDefinition part
155 Reference< css::sdb::XQueryDefinition > xCommandDefinitionPart = css::sdb::QueryDefinition::create(m_aContext);
157 ::comphelper::copyProperties( _rxDesc, Reference<XPropertySet>(xCommandDefinitionPart, UNO_QUERY_THROW) );
158 // TODO : the columns part of the descriptor has to be copied
160 // create a wrapper for the object (*before* inserting into our command definition container)
161 Reference< XContent > xNewObject( implCreateWrapper( Reference< XContent>( xCommandDefinitionPart, UNO_QUERY_THROW ) ) );
163 OUString sNewObjectName;
164 _rxDesc->getPropertyValue(PROPERTY_NAME) >>= sNewObjectName;
168 notifyByName( aGuard, sNewObjectName, xNewObject, NULL, E_INSERTED, ApproveListeners );
170 catch (const WrappedTargetException& e)
172 disposeComponent( xNewObject );
173 disposeComponent( xCommandDefinitionPart );
174 throw WrappedTargetRuntimeException(e.Message, e.Context, e.TargetException);
176 catch (const Exception&)
178 disposeComponent( xNewObject );
179 disposeComponent( xCommandDefinitionPart );
180 throw;
183 // insert the basic object into the definition container
185 m_eDoingCurrently = INSERTING;
186 OAutoActionReset aAutoReset(this);
187 m_xCommandDefinitions->insertByName(sNewObjectName, makeAny(xCommandDefinitionPart));
190 implAppend( sNewObjectName, xNewObject );
193 notifyByName( aGuard, sNewObjectName, xNewObject, NULL, E_INSERTED, ContainerListemers );
195 catch (const WrappedTargetException& e)
197 throw WrappedTargetRuntimeException(e.Message, e.Context, e.TargetException);
201 // XDrop
202 void SAL_CALL OQueryContainer::dropByName( const OUString& _rName ) throw(SQLException, NoSuchElementException, RuntimeException, std::exception)
204 MutexGuard aGuard(m_aMutex);
205 if ( !checkExistence(_rName) )
206 throw NoSuchElementException(_rName,*this);
208 if ( !m_xCommandDefinitions.is() )
209 throw DisposedException( OUString(), *this );
211 // now simply forward the remove request to the CommandDefinition container, we're a listener for the removal
212 // and thus we do everything necessary in ::elementRemoved
213 m_xCommandDefinitions->removeByName(_rName);
216 void SAL_CALL OQueryContainer::dropByIndex( sal_Int32 _nIndex ) throw(SQLException, IndexOutOfBoundsException, RuntimeException, std::exception)
218 MutexGuard aGuard(m_aMutex);
219 if ((_nIndex<0) || (_nIndex>getCount()))
220 throw IndexOutOfBoundsException();
222 if ( !m_xCommandDefinitions.is() )
223 throw DisposedException( OUString(), *this );
225 OUString sName;
226 Reference<XPropertySet> xProp(Reference<XIndexAccess>(m_xCommandDefinitions,UNO_QUERY)->getByIndex(_nIndex),UNO_QUERY);
227 if ( xProp.is() )
228 xProp->getPropertyValue(PROPERTY_NAME) >>= sName;
230 dropByName(sName);
233 void SAL_CALL OQueryContainer::elementInserted( const ::com::sun::star::container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException, std::exception)
235 Reference< XContent > xNewElement;
236 OUString sElementName;
237 _rEvent.Accessor >>= sElementName;
239 MutexGuard aGuard(m_aMutex);
240 if (INSERTING == m_eDoingCurrently)
241 // nothing to do, we're inserting via an "appendByDescriptor"
242 return;
244 OSL_ENSURE(!sElementName.isEmpty(), "OQueryContainer::elementInserted : invalid name !");
245 OSL_ENSURE(m_aDocumentMap.find(sElementName) == m_aDocumentMap.end(), "OQueryContainer::elementInserted : oops .... we're inconsistent with our master container !");
246 if (sElementName.isEmpty() || hasByName(sElementName))
247 return;
249 // insert an own new element
250 xNewElement = implCreateWrapper(sElementName);
252 insertByName(sElementName,makeAny(xNewElement));
255 void SAL_CALL OQueryContainer::elementRemoved( const ::com::sun::star::container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException, std::exception)
257 OUString sAccessor;
258 _rEvent.Accessor >>= sAccessor;
260 OSL_ENSURE(!sAccessor.isEmpty(), "OQueryContainer::elementRemoved : invalid name !");
261 OSL_ENSURE(m_aDocumentMap.find(sAccessor) != m_aDocumentMap.end(), "OQueryContainer::elementRemoved : oops .... we're inconsistent with our master container !");
262 if ( sAccessor.isEmpty() || !hasByName(sAccessor) )
263 return;
265 removeByName(sAccessor);
268 void SAL_CALL OQueryContainer::elementReplaced( const ::com::sun::star::container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException, std::exception)
270 Reference< XPropertySet > xReplacedElement;
271 Reference< XContent > xNewElement;
272 OUString sAccessor;
273 _rEvent.Accessor >>= sAccessor;
276 MutexGuard aGuard(m_aMutex);
277 OSL_ENSURE(!sAccessor.isEmpty(), "OQueryContainer::elementReplaced : invalid name !");
278 OSL_ENSURE(m_aDocumentMap.find(sAccessor) != m_aDocumentMap.end(), "OQueryContainer::elementReplaced : oops .... we're inconsistent with our master container !");
279 if (sAccessor.isEmpty() || !hasByName(sAccessor))
280 return;
282 xNewElement = implCreateWrapper(sAccessor);
285 replaceByName(sAccessor,makeAny(xNewElement));
288 Reference< XVeto > SAL_CALL OQueryContainer::approveInsertElement( const ContainerEvent& Event ) throw (WrappedTargetException, RuntimeException, std::exception)
290 OUString sName;
291 OSL_VERIFY( Event.Accessor >>= sName );
292 Reference< XContent > xElement( Event.Element, UNO_QUERY_THROW );
294 Reference< XVeto > xReturn;
297 getElementApproval()->approveElement( sName, xElement.get() );
299 catch( const Exception& )
301 xReturn = new Veto( OUString(), ::cppu::getCaughtException() );
303 return xReturn;
306 Reference< XVeto > SAL_CALL OQueryContainer::approveReplaceElement( const ContainerEvent& /*Event*/ ) throw (WrappedTargetException, RuntimeException, std::exception)
308 return NULL;
311 Reference< XVeto > SAL_CALL OQueryContainer::approveRemoveElement( const ContainerEvent& /*Event*/ ) throw (WrappedTargetException, RuntimeException, std::exception)
313 return NULL;
316 void SAL_CALL OQueryContainer::disposing( const ::com::sun::star::lang::EventObject& _rSource ) throw(::com::sun::star::uno::RuntimeException, std::exception)
318 if (_rSource.Source.get() == Reference< XInterface >(m_xCommandDefinitions, UNO_QUERY).get())
319 { // our "master container" (with the command definitions) is being disposed
320 OSL_FAIL("OQueryContainer::disposing : nobody should dispose the CommandDefinition container before disposing my connection !");
321 dispose();
323 else
325 Reference< XContent > xSource(_rSource.Source, UNO_QUERY);
326 // it's one of our documents ....
327 Documents::iterator aIter = m_aDocumentMap.begin();
328 Documents::iterator aEnd = m_aDocumentMap.end();
329 for (;aIter != aEnd;++aIter )
331 if ( xSource == aIter->second.get() )
333 m_xCommandDefinitions->removeByName(aIter->first);
334 break;
337 ODefinitionContainer::disposing(_rSource);
341 OUString OQueryContainer::determineContentType() const
343 return OUString( "application/vnd.org.openoffice.DatabaseQueryContainer" );
346 Reference< XContent > OQueryContainer::implCreateWrapper(const OUString& _rName)
348 Reference< XContent > xObject(m_xCommandDefinitions->getByName(_rName),UNO_QUERY);
349 return implCreateWrapper(xObject);
352 Reference< XContent > OQueryContainer::implCreateWrapper(const Reference< XContent >& _rxCommandDesc)
354 Reference<XNameContainer> xContainer(_rxCommandDesc,UNO_QUERY);
355 Reference< XContent > xReturn;
356 if ( xContainer .is() )
358 xReturn = create( xContainer, m_xConnection, m_aContext, m_pWarnings ).
359 get();
361 else
363 OQuery* pNewObject = new OQuery( Reference< XPropertySet >( _rxCommandDesc, UNO_QUERY ), m_xConnection, m_aContext );
364 xReturn = pNewObject;
366 pNewObject->setWarningsContainer( m_pWarnings );
367 // pNewObject->getColumns();
368 // Why? This is expensive. If you comment this in 'cause you really need it, be sure to run the
369 // QueryInQuery test in dbaccess/qa/complex/dbaccess ...
372 return xReturn;
375 Reference< XContent > OQueryContainer::createObject( const OUString& _rName)
377 return implCreateWrapper(_rName);
380 bool OQueryContainer::checkExistence(const OUString& _rName)
382 bool bRet = false;
383 if ( !m_bInPropertyChange )
385 bRet = m_xCommandDefinitions->hasByName(_rName);
386 Documents::iterator aFind = m_aDocumentMap.find(_rName);
387 if ( !bRet && aFind != m_aDocumentMap.end() )
389 m_aDocuments.erase( ::std::find(m_aDocuments.begin(),m_aDocuments.end(),aFind));
390 m_aDocumentMap.erase(aFind);
392 else if ( bRet && aFind == m_aDocumentMap.end() )
394 implAppend(_rName,NULL);
397 return bRet;
400 sal_Bool SAL_CALL OQueryContainer::hasElements( ) throw (RuntimeException, std::exception)
402 MutexGuard aGuard(m_aMutex);
403 return m_xCommandDefinitions->hasElements();
406 sal_Int32 SAL_CALL OQueryContainer::getCount( ) throw(RuntimeException, std::exception)
408 MutexGuard aGuard(m_aMutex);
409 return Reference<XIndexAccess>(m_xCommandDefinitions,UNO_QUERY)->getCount();
412 Sequence< OUString > SAL_CALL OQueryContainer::getElementNames( ) throw(RuntimeException, std::exception)
414 MutexGuard aGuard(m_aMutex);
416 return m_xCommandDefinitions->getElementNames();
419 } // namespace dbaccess
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */