calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / dbaccess / source / core / api / querycontainer.cxx
blob583cab373b015e205d98fd7b530257eeb3a1d05b
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 "query.hxx"
22 #include <strings.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/IndexOutOfBoundsException.hpp>
30 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
31 #include <com/sun/star/sdbc/XConnection.hpp>
32 #include <com/sun/star/sdb/QueryDefinition.hpp>
34 #include <osl/diagnose.h>
35 #include <comphelper/uno3.hxx>
36 #include <comphelper/property.hxx>
37 #include <comphelper/types.hxx>
38 #include <cppuhelper/exc_hlp.hxx>
40 using namespace dbtools;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::ucb;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::sdb;
46 using namespace ::com::sun::star::sdbc;
47 using namespace ::com::sun::star::sdbcx;
48 using namespace ::com::sun::star::container;
49 using namespace ::com::sun::star::util;
50 using namespace ::osl;
51 using namespace ::comphelper;
52 using namespace ::cppu;
54 namespace dbaccess
57 // OQueryContainer
59 OQueryContainer::OQueryContainer(
60 const Reference< XNameContainer >& _rxCommandDefinitions
61 , const Reference< XConnection >& _rxConn
62 , const Reference< XComponentContext >& _rxORB,
63 ::dbtools::WarningsContainer* _pWarnings)
64 :ODefinitionContainer(_rxORB,nullptr,std::make_shared<ODefinitionContainer_Impl>())
65 ,m_pWarnings( _pWarnings )
66 ,m_xCommandDefinitions(_rxCommandDefinitions)
67 ,m_xConnection(_rxConn)
68 ,m_eDoingCurrently(AggregateAction::NONE)
72 void OQueryContainer::init()
74 Reference< XContainer > xContainer( m_xCommandDefinitions, UNO_QUERY_THROW );
75 xContainer->addContainerListener( this );
77 Reference< XContainerApproveBroadcaster > xContainerApprove( m_xCommandDefinitions, UNO_QUERY_THROW );
78 xContainerApprove->addContainerApproveListener( this );
80 // fill my structures
81 ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
82 Sequence< OUString > sDefinitionNames = m_xCommandDefinitions->getElementNames();
83 const OUString* pDefinitionName = sDefinitionNames.getConstArray();
84 const OUString* pEnd = pDefinitionName + sDefinitionNames.getLength();
85 for ( ; pDefinitionName != pEnd; ++pDefinitionName )
87 rDefinitions.insert( *pDefinitionName, TContentPtr() );
88 m_aDocuments.push_back(m_aDocumentMap.emplace( *pDefinitionName,Documents::mapped_type()).first);
91 setElementApproval( std::make_shared<ObjectNameApproval>( m_xConnection, ObjectNameApproval::TypeQuery ) );
94 rtl::Reference<OQueryContainer> OQueryContainer::create(
95 const Reference< XNameContainer >& _rxCommandDefinitions
96 , const Reference< XConnection >& _rxConn
97 , const Reference< XComponentContext >& _rxORB,
98 ::dbtools::WarningsContainer* _pWarnings)
100 rtl::Reference c(
101 new OQueryContainer(
102 _rxCommandDefinitions, _rxConn, _rxORB, _pWarnings));
103 c->init();
104 return c;
107 OQueryContainer::~OQueryContainer()
109 // dispose();
110 // maybe we're already disposed, but this should be uncritical
113 IMPLEMENT_FORWARD_XINTERFACE2( OQueryContainer,ODefinitionContainer,OQueryContainer_Base)
115 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OQueryContainer,ODefinitionContainer,OQueryContainer_Base)
117 void OQueryContainer::disposing()
119 ODefinitionContainer::disposing();
120 MutexGuard aGuard(m_aMutex);
121 if ( !m_xCommandDefinitions.is() )
122 // already disposed
123 return;
125 Reference< XContainer > xContainer( m_xCommandDefinitions, UNO_QUERY );
126 xContainer->removeContainerListener( this );
127 Reference< XContainerApproveBroadcaster > xContainerApprove( m_xCommandDefinitions, UNO_QUERY );
128 xContainerApprove->removeContainerApproveListener( this );
130 m_xCommandDefinitions = nullptr;
131 m_xConnection = nullptr;
134 // XServiceInfo
135 OUString SAL_CALL OQueryContainer::getImplementationName()
137 return "com.sun.star.sdb.dbaccess.OQueryContainer";
139 sal_Bool SAL_CALL OQueryContainer::supportsService(const OUString& _rServiceName)
141 const css::uno::Sequence< OUString > aSupported(getSupportedServiceNames());
142 for (const OUString& s : aSupported)
143 if (s == _rServiceName)
144 return true;
146 return false;
148 css::uno::Sequence< OUString > SAL_CALL OQueryContainer::getSupportedServiceNames()
150 return { SERVICE_SDBCX_CONTAINER, SERVICE_SDB_QUERIES };
153 // XDataDescriptorFactory
154 Reference< XPropertySet > SAL_CALL OQueryContainer::createDataDescriptor( )
156 return new OQueryDescriptor();
159 // XAppend
160 void SAL_CALL OQueryContainer::appendByDescriptor( const Reference< XPropertySet >& _rxDesc )
162 ResettableMutexGuard aGuard(m_aMutex);
163 if ( !m_xCommandDefinitions.is() )
164 throw DisposedException( OUString(), *this );
166 // first clone this object's CommandDefinition part
167 Reference< css::sdb::XQueryDefinition > xCommandDefinitionPart = css::sdb::QueryDefinition::create(m_aContext);
169 ::comphelper::copyProperties( _rxDesc, Reference<XPropertySet>(xCommandDefinitionPart, UNO_QUERY_THROW) );
170 // TODO : the columns part of the descriptor has to be copied
172 // create a wrapper for the object (*before* inserting into our command definition container)
173 Reference< XContent > xNewObject( implCreateWrapper( Reference< XContent>( xCommandDefinitionPart, UNO_QUERY_THROW ) ) );
175 OUString sNewObjectName;
176 _rxDesc->getPropertyValue(PROPERTY_NAME) >>= sNewObjectName;
180 notifyByName( aGuard, sNewObjectName, xNewObject, nullptr, E_INSERTED, ApproveListeners );
182 catch (const WrappedTargetException& e)
184 disposeComponent( xNewObject );
185 disposeComponent( xCommandDefinitionPart );
186 throw WrappedTargetRuntimeException(e.Message, e.Context, e.TargetException);
188 catch (const Exception&)
190 disposeComponent( xNewObject );
191 disposeComponent( xCommandDefinitionPart );
192 throw;
195 // insert the basic object into the definition container
197 m_eDoingCurrently = AggregateAction::Inserting;
198 OAutoActionReset aAutoReset(*this);
199 m_xCommandDefinitions->insertByName(sNewObjectName, Any(xCommandDefinitionPart));
202 implAppend( sNewObjectName, xNewObject );
205 notifyByName( aGuard, sNewObjectName, xNewObject, nullptr, E_INSERTED, ContainerListemers );
207 catch (const WrappedTargetException& e)
209 throw WrappedTargetRuntimeException(e.Message, e.Context, e.TargetException);
213 // XDrop
214 void SAL_CALL OQueryContainer::dropByName( const OUString& _rName )
216 MutexGuard aGuard(m_aMutex);
217 if ( !checkExistence(_rName) )
218 throw NoSuchElementException(_rName,*this);
220 if ( !m_xCommandDefinitions.is() )
221 throw DisposedException( OUString(), *this );
223 // now simply forward the remove request to the CommandDefinition container, we're a listener for the removal
224 // and thus we do everything necessary in ::elementRemoved
225 m_xCommandDefinitions->removeByName(_rName);
228 void SAL_CALL OQueryContainer::dropByIndex( sal_Int32 _nIndex )
230 MutexGuard aGuard(m_aMutex);
231 if ((_nIndex<0) || (_nIndex>getCount()))
232 throw IndexOutOfBoundsException();
234 if ( !m_xCommandDefinitions.is() )
235 throw DisposedException( OUString(), *this );
237 OUString sName;
238 Reference<XPropertySet> xProp(Reference<XIndexAccess>(m_xCommandDefinitions,UNO_QUERY_THROW)->getByIndex(_nIndex),UNO_QUERY);
239 if ( xProp.is() )
240 xProp->getPropertyValue(PROPERTY_NAME) >>= sName;
242 dropByName(sName);
245 void SAL_CALL OQueryContainer::elementInserted( const css::container::ContainerEvent& _rEvent )
247 Reference< XContent > xNewElement;
248 OUString sElementName;
249 _rEvent.Accessor >>= sElementName;
251 MutexGuard aGuard(m_aMutex);
252 if (AggregateAction::Inserting == m_eDoingCurrently)
253 // nothing to do, we're inserting via an "appendByDescriptor"
254 return;
256 OSL_ENSURE(!sElementName.isEmpty(), "OQueryContainer::elementInserted : invalid name !");
257 OSL_ENSURE(m_aDocumentMap.find(sElementName) == m_aDocumentMap.end(), "OQueryContainer::elementInserted : oops... we're inconsistent with our master container !");
258 if (sElementName.isEmpty() || hasByName(sElementName))
259 return;
261 // insert an own new element
262 xNewElement = implCreateWrapper(sElementName);
264 insertByName(sElementName,Any(xNewElement));
267 void SAL_CALL OQueryContainer::elementRemoved( const css::container::ContainerEvent& _rEvent )
269 OUString sAccessor;
270 _rEvent.Accessor >>= sAccessor;
272 OSL_ENSURE(!sAccessor.isEmpty(), "OQueryContainer::elementRemoved : invalid name !");
273 OSL_ENSURE(m_aDocumentMap.find(sAccessor) != m_aDocumentMap.end(), "OQueryContainer::elementRemoved : oops... we're inconsistent with our master container !");
274 if ( sAccessor.isEmpty() || !hasByName(sAccessor) )
275 return;
277 removeByName(sAccessor);
280 void SAL_CALL OQueryContainer::elementReplaced( const css::container::ContainerEvent& _rEvent )
282 Reference< XContent > xNewElement;
283 OUString sAccessor;
284 _rEvent.Accessor >>= sAccessor;
287 MutexGuard aGuard(m_aMutex);
288 OSL_ENSURE(!sAccessor.isEmpty(), "OQueryContainer::elementReplaced : invalid name !");
289 OSL_ENSURE(m_aDocumentMap.find(sAccessor) != m_aDocumentMap.end(), "OQueryContainer::elementReplaced : oops... we're inconsistent with our master container !");
290 if (sAccessor.isEmpty() || !hasByName(sAccessor))
291 return;
293 xNewElement = implCreateWrapper(sAccessor);
296 replaceByName(sAccessor,Any(xNewElement));
299 Reference< XVeto > SAL_CALL OQueryContainer::approveInsertElement( const ContainerEvent& Event )
301 OUString sName;
302 OSL_VERIFY( Event.Accessor >>= sName );
303 Reference< XContent > xElement( Event.Element, UNO_QUERY_THROW );
305 Reference< XVeto > xReturn;
308 getElementApproval()->approveElement( sName );
310 catch( const Exception& )
312 xReturn = new Veto( ::cppu::getCaughtException() );
314 return xReturn;
317 Reference< XVeto > SAL_CALL OQueryContainer::approveReplaceElement( const ContainerEvent& /*Event*/ )
319 return nullptr;
322 Reference< XVeto > SAL_CALL OQueryContainer::approveRemoveElement( const ContainerEvent& /*Event*/ )
324 return nullptr;
327 void SAL_CALL OQueryContainer::disposing( const css::lang::EventObject& _rSource )
329 if (_rSource.Source.get() == Reference< XInterface >(m_xCommandDefinitions, UNO_QUERY).get())
330 { // our "master container" (with the command definitions) is being disposed
331 OSL_FAIL("OQueryContainer::disposing : nobody should dispose the CommandDefinition container before disposing my connection !");
332 dispose();
334 else
336 Reference< XContent > xSource(_rSource.Source, UNO_QUERY);
337 // it's one of our documents...
338 for (auto const& document : m_aDocumentMap)
340 if ( xSource == document.second.get() )
342 m_xCommandDefinitions->removeByName(document.first);
343 break;
346 ODefinitionContainer::disposing(_rSource);
350 OUString OQueryContainer::determineContentType() const
352 return "application/vnd.org.openoffice.DatabaseQueryContainer";
355 Reference< XContent > OQueryContainer::implCreateWrapper(const OUString& _rName)
357 Reference< XContent > xObject(m_xCommandDefinitions->getByName(_rName),UNO_QUERY);
358 return implCreateWrapper(xObject);
361 Reference< XContent > OQueryContainer::implCreateWrapper(const Reference< XContent >& _rxCommandDesc)
363 Reference<XNameContainer> xContainer(_rxCommandDesc,UNO_QUERY);
364 Reference< XContent > xReturn;
365 if ( xContainer .is() )
367 xReturn = create( xContainer, m_xConnection, m_aContext, m_pWarnings ).
368 get();
370 else
372 rtl::Reference<OQuery> pNewObject = new OQuery( Reference< XPropertySet >( _rxCommandDesc, UNO_QUERY ), m_xConnection, m_aContext );
373 xReturn = pNewObject;
375 pNewObject->setWarningsContainer( m_pWarnings );
376 // pNewObject->getColumns();
377 // Why? This is expensive. If you comment this in 'cause you really need it, be sure to run the
378 // QueryInQuery test in dbaccess/qa/complex/dbaccess ...
381 return xReturn;
384 Reference< XContent > OQueryContainer::createObject( const OUString& _rName)
386 return implCreateWrapper(_rName);
389 bool OQueryContainer::checkExistence(const OUString& _rName)
391 bool bRet = false;
392 if ( !m_bInPropertyChange )
394 bRet = m_xCommandDefinitions->hasByName(_rName);
395 Documents::const_iterator aFind = m_aDocumentMap.find(_rName);
396 if ( !bRet && aFind != m_aDocumentMap.end() )
398 m_aDocuments.erase( std::find(m_aDocuments.begin(),m_aDocuments.end(),aFind));
399 m_aDocumentMap.erase(aFind);
401 else if ( bRet && aFind == m_aDocumentMap.end() )
403 implAppend(_rName,nullptr);
406 return bRet;
409 sal_Bool SAL_CALL OQueryContainer::hasElements( )
411 MutexGuard aGuard(m_aMutex);
412 return m_xCommandDefinitions->hasElements();
415 sal_Int32 SAL_CALL OQueryContainer::getCount( )
417 MutexGuard aGuard(m_aMutex);
418 return Reference<XIndexAccess>(m_xCommandDefinitions,UNO_QUERY_THROW)->getCount();
421 Sequence< OUString > SAL_CALL OQueryContainer::getElementNames( )
423 MutexGuard aGuard(m_aMutex);
425 return m_xCommandDefinitions->getElementNames();
428 } // namespace dbaccess
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */