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 "documentcontainer.hxx"
22 : #include "dbastrings.hrc"
23 : #include "documentdefinition.hxx"
24 : #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
25 : #include <com/sun/star/ucb/OpenMode.hpp>
26 : #include <tools/debug.hxx>
27 : #include <connectivity/dbtools.hxx>
28 : #include "myucp_resultset.hxx"
29 : #include <ucbhelper/cancelcommandexecution.hxx>
30 : #include <com/sun/star/ucb/UnsupportedOpenModeException.hpp>
31 : #include <com/sun/star/ucb/InsertCommandArgument.hpp>
32 : #include <com/sun/star/beans/PropertyAttribute.hpp>
33 : #include <com/sun/star/sdb/ErrorCondition.hpp>
34 : #include "datasource.hxx"
35 : #include <comphelper/classids.hxx>
36 : #include <comphelper/mimeconfighelper.hxx>
37 : #include <comphelper/processfactory.hxx>
38 : #include <connectivity/sqlerror.hxx>
39 : #include "core_resource.hxx"
40 : #include "core_resource.hrc"
41 : #include <comphelper/namedvaluecollection.hxx>
42 :
43 : #include <vcl/svapp.hxx>
44 : #include <osl/mutex.hxx>
45 :
46 : using namespace ::com::sun::star::uno;
47 : using namespace ::com::sun::star::lang;
48 : using namespace ::com::sun::star::embed;
49 : using namespace ::com::sun::star::beans;
50 : using namespace ::com::sun::star::container;
51 : using namespace ::com::sun::star::ucb;
52 : using namespace ::com::sun::star::sdbc;
53 : using namespace ::com::sun::star::sdb;
54 : using namespace ::com::sun::star::io;
55 : using namespace ::osl;
56 : using namespace ::comphelper;
57 : using namespace ::cppu;
58 :
59 : namespace dbaccess
60 : {
61 :
62 : //==========================================================================
63 : //= LocalNameApproval
64 : //==========================================================================
65 : class LocalNameApproval : public IContainerApprove
66 : {
67 : ::connectivity::SQLError m_aErrors;
68 :
69 : public:
70 0 : LocalNameApproval( const Reference< XComponentContext >& _rxContext )
71 0 : :m_aErrors( _rxContext )
72 : {
73 0 : }
74 0 : virtual ~LocalNameApproval()
75 0 : {
76 0 : }
77 :
78 : void SAL_CALL approveElement( const ::rtl::OUString& _rName, const Reference< XInterface >& _rxElement );
79 : };
80 :
81 0 : void SAL_CALL LocalNameApproval::approveElement( const ::rtl::OUString& _rName, const Reference< XInterface >& /*_rxElement*/ )
82 : {
83 0 : if ( _rName.indexOf( '/' ) != -1 )
84 : throw IllegalArgumentException(
85 : m_aErrors.getErrorMessage( ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES ),
86 : NULL,
87 : 0
88 0 : );
89 0 : }
90 :
91 : //==========================================================================
92 : //= ODocumentContainer
93 : //==========================================================================
94 : DBG_NAME(ODocumentContainer)
95 :
96 0 : ODocumentContainer::ODocumentContainer(const Reference< XMultiServiceFactory >& _xORB
97 : ,const Reference< XInterface >& _xParentContainer
98 : ,const TContentPtr& _pImpl
99 : , sal_Bool _bFormsContainer
100 : )
101 : :ODefinitionContainer(_xORB,_xParentContainer,_pImpl)
102 : ,OPropertyStateContainer(OContentHelper::rBHelper)
103 0 : ,m_bFormsContainer(_bFormsContainer)
104 : {
105 : DBG_CTOR(ODocumentContainer, NULL);
106 : registerProperty(PROPERTY_NAME, PROPERTY_ID_NAME, PropertyAttribute::BOUND | PropertyAttribute::READONLY | PropertyAttribute::CONSTRAINED,
107 0 : &m_pImpl->m_aProps.aTitle, ::getCppuType(&m_pImpl->m_aProps.aTitle));
108 :
109 0 : setElementApproval( PContainerApprove( new LocalNameApproval ( comphelper::getComponentContext(_xORB) ) ) );
110 0 : }
111 :
112 0 : ODocumentContainer::~ODocumentContainer()
113 : {
114 : DBG_DTOR(ODocumentContainer, NULL);
115 :
116 0 : if ( !OContentHelper::rBHelper.bInDispose && !OContentHelper::rBHelper.bDisposed )
117 : {
118 0 : acquire();
119 0 : dispose();
120 : }
121 0 : }
122 :
123 0 : IMPLEMENT_FORWARD_XINTERFACE3( ODocumentContainer,ODefinitionContainer,ODocumentContainer_Base,OPropertyStateContainer)
124 0 : IMPLEMENT_TYPEPROVIDER3(ODocumentContainer,ODefinitionContainer,OPropertyStateContainer,ODocumentContainer_Base);
125 0 : IMPLEMENT_SERVICE_INFO_IMPLNAME(ODocumentContainer, "com.sun.star.comp.dba.ODocumentContainer");
126 0 : IMPLEMENT_SERVICE_INFO_SUPPORTS(ODocumentContainer);
127 0 : IMPLEMENT_PROPERTYCONTAINER_DEFAULTS(ODocumentContainer)
128 :
129 0 : Sequence< ::rtl::OUString > SAL_CALL ODocumentContainer::getSupportedServiceNames( ) throw(RuntimeException)
130 : {
131 0 : Sequence< ::rtl::OUString > aSupported(1);
132 0 : aSupported[0] = m_bFormsContainer ? SERVICE_NAME_FORM_COLLECTION : SERVICE_NAME_REPORT_COLLECTION;
133 0 : return aSupported;
134 : }
135 :
136 0 : ::rtl::OUString ODocumentContainer::determineContentType() const
137 : {
138 0 : return ::rtl::OUString();
139 : }
140 :
141 0 : Reference< XContent > ODocumentContainer::createObject( const ::rtl::OUString& _rName)
142 : {
143 0 : const ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
144 0 : ODefinitionContainer_Impl::const_iterator aFind = rDefinitions.find( _rName );
145 : OSL_ENSURE( aFind != rDefinitions.end(), "ODocumentContainer::createObject:Invalid entry in map!" );
146 0 : if ( aFind->second->m_aProps.bIsFolder )
147 0 : return new ODocumentContainer( m_aContext.getLegacyServiceFactory(), *this, aFind->second, m_bFormsContainer );
148 0 : return new ODocumentDefinition( *this, m_aContext.getLegacyServiceFactory(), aFind->second, m_bFormsContainer );
149 : }
150 :
151 0 : Reference< XInterface > SAL_CALL ODocumentContainer::createInstance( const ::rtl::OUString& aServiceSpecifier ) throw (Exception, RuntimeException)
152 : {
153 0 : return createInstanceWithArguments( aServiceSpecifier, Sequence< Any >() );
154 : }
155 :
156 : namespace
157 : {
158 : template< class TYPE >
159 0 : void lcl_extractAndRemove( ::comphelper::NamedValueCollection& io_rArguments, const ::rtl::OUString& i_rName, TYPE& o_rValue )
160 : {
161 0 : if ( io_rArguments.has( i_rName ) )
162 : {
163 0 : io_rArguments.get_ensureType( i_rName, o_rValue );
164 0 : io_rArguments.remove( i_rName );
165 : }
166 0 : }
167 : }
168 :
169 0 : Reference< XInterface > SAL_CALL ODocumentContainer::createInstanceWithArguments( const ::rtl::OUString& ServiceSpecifier, const Sequence< Any >& _aArguments ) throw (Exception, RuntimeException)
170 : {
171 0 : Reference< XInterface > xRet;
172 0 : Reference< XContent > xContent;
173 0 : if ( ServiceSpecifier == SERVICE_SDB_DOCUMENTDEFINITION )
174 : {
175 0 : MutexGuard aGuard(m_aMutex);
176 :
177 : // extract known arguments
178 0 : ::rtl::OUString sName, sPersistentName, sURL, sMediaType, sDocServiceName;
179 0 : Reference< XCommandProcessor > xCopyFrom;
180 0 : Reference< XConnection > xConnection;
181 0 : sal_Bool bAsTemplate( sal_False );
182 0 : Sequence< sal_Int8 > aClassID;
183 :
184 0 : ::comphelper::NamedValueCollection aArgs( _aArguments );
185 0 : lcl_extractAndRemove( aArgs, PROPERTY_NAME, sName );
186 0 : lcl_extractAndRemove( aArgs, PROPERTY_PERSISTENT_NAME, sPersistentName );
187 0 : lcl_extractAndRemove( aArgs, PROPERTY_URL, sURL );
188 0 : lcl_extractAndRemove( aArgs, PROPERTY_EMBEDDEDOBJECT, xCopyFrom );
189 0 : lcl_extractAndRemove( aArgs, PROPERTY_ACTIVE_CONNECTION, xConnection );
190 0 : lcl_extractAndRemove( aArgs, PROPERTY_AS_TEMPLATE, bAsTemplate );
191 0 : lcl_extractAndRemove( aArgs, INFO_MEDIATYPE, sMediaType );
192 0 : lcl_extractAndRemove( aArgs, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DocumentServiceName" ) ), sDocServiceName );
193 :
194 : // ClassID has two allowed types, so a special treatment here
195 0 : Any aClassIDArg = aArgs.get( "ClassID" );
196 0 : if ( aClassIDArg.hasValue() )
197 : {
198 0 : if ( !( aClassIDArg >>= aClassID ) )
199 : {
200 : // Extended for usage also with a string
201 0 : ::rtl::OUString sClassIDString;
202 0 : if ( !( aClassIDArg >>= sClassIDString ) )
203 0 : throw IllegalArgumentException( ::rtl::OUString(), *this, 2 );
204 :
205 0 : aClassID = ::comphelper::MimeConfigurationHelper::GetSequenceClassIDRepresentation( sClassIDString );
206 : }
207 :
208 : #if OSL_DEBUG_LEVEL > 0
209 : ::rtl::OUString sClassIDString = ::comphelper::MimeConfigurationHelper::GetStringClassIDRepresentation( aClassID );
210 : (void)sClassIDString;
211 : #endif
212 0 : aArgs.remove( "ClassID" );
213 : }
214 : // Everything which now is still present in the arguments is passed to the embedded object
215 0 : const Sequence< PropertyValue > aCreationArgs( aArgs.getPropertyValues() );
216 :
217 0 : const ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
218 0 : sal_Bool bNew = sPersistentName.isEmpty();
219 0 : if ( bNew )
220 : {
221 0 : const static ::rtl::OUString sBaseName(RTL_CONSTASCII_USTRINGPARAM("Obj"));
222 :
223 0 : sPersistentName = sBaseName;
224 0 : sPersistentName += ::rtl::OUString::valueOf(sal_Int32(rDefinitions.size() + 1));
225 0 : Reference<XNameAccess> xElements(getContainerStorage(),UNO_QUERY);
226 0 : if ( xElements.is() )
227 0 : sPersistentName = ::dbtools::createUniqueName(xElements,sPersistentName);
228 :
229 0 : const bool bNeedClassID = (0 == aClassID.getLength()) && sURL.isEmpty() ;
230 0 : if ( xCopyFrom.is() )
231 : {
232 0 : Sequence<Any> aIni(2);
233 0 : aIni[0] <<= getContainerStorage();
234 0 : aIni[1] <<= sPersistentName;
235 0 : Command aCommand;
236 0 : aCommand.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("copyTo"));
237 0 : aCommand.Argument <<= aIni;
238 :
239 0 : xCopyFrom->execute(aCommand,-1,Reference< XCommandEnvironment >());
240 0 : Reference<XPropertySet> xProp(xCopyFrom,UNO_QUERY);
241 0 : if ( xProp.is() && xProp->getPropertySetInfo().is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_AS_TEMPLATE) )
242 0 : xProp->getPropertyValue(PROPERTY_AS_TEMPLATE) >>= bAsTemplate;
243 :
244 : // if we do not have an own class ID, see if we can determine one from the copy we just created
245 0 : if ( bNeedClassID )
246 0 : ODocumentDefinition::GetDocumentServiceFromMediaType( getContainerStorage(), sPersistentName, m_aContext, aClassID );
247 : }
248 : else
249 : {
250 0 : if ( bNeedClassID )
251 : {
252 0 : if ( !sMediaType.isEmpty() )
253 0 : ODocumentDefinition::GetDocumentServiceFromMediaType( sMediaType, m_aContext, aClassID );
254 0 : else if ( !sDocServiceName.isEmpty() )
255 : {
256 0 : ::comphelper::MimeConfigurationHelper aConfigHelper( m_aContext.getLegacyServiceFactory() );
257 0 : const Sequence< NamedValue > aProps( aConfigHelper.GetObjectPropsByDocumentName( sDocServiceName ) );
258 0 : const ::comphelper::NamedValueCollection aMediaTypeProps( aProps );
259 0 : aClassID = aMediaTypeProps.getOrDefault( "ClassID", Sequence< sal_Int8 >() );
260 : }
261 : }
262 0 : }
263 : }
264 :
265 0 : ODefinitionContainer_Impl::const_iterator aFind = rDefinitions.find( sName );
266 0 : TContentPtr pElementImpl;
267 0 : if ( bNew || ( aFind == rDefinitions.end() ) )
268 : {
269 0 : pElementImpl.reset( new OContentHelper_Impl );
270 0 : if ( !bNew )
271 0 : pElementImpl->m_aProps.aTitle = sName;
272 :
273 0 : pElementImpl->m_aProps.sPersistentName = sPersistentName;
274 0 : pElementImpl->m_aProps.bAsTemplate = bAsTemplate;
275 0 : pElementImpl->m_pDataSource = m_pImpl->m_pDataSource;
276 : }
277 : else
278 0 : pElementImpl = aFind->second;
279 :
280 0 : ::rtl::Reference< ODocumentDefinition > pDocDef = new ODocumentDefinition( *this, m_aContext.getLegacyServiceFactory(), pElementImpl, m_bFormsContainer );
281 0 : if ( aClassID.getLength() )
282 : {
283 0 : pDocDef->initialLoad( aClassID, aCreationArgs, xConnection );
284 : }
285 : else
286 : {
287 : OSL_ENSURE( aCreationArgs.getLength() == 0, "ODocumentContainer::createInstance: additional creation args are lost, if you do not provide a class ID." );
288 : }
289 0 : xContent = pDocDef.get();
290 :
291 0 : if ( !sURL.isEmpty() )
292 : {
293 0 : Sequence<Any> aIni(2);
294 0 : aIni[0] <<= sURL;
295 0 : Command aCommand;
296 0 : aCommand.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("insert"));
297 0 : aCommand.Argument <<= aIni;
298 0 : Reference< XCommandProcessor > xCommandProcessor(xContent,UNO_QUERY);
299 0 : if ( xContent.is() )
300 : {
301 0 : xCommandProcessor->execute(aCommand,-1,Reference< XCommandEnvironment >());
302 0 : }
303 0 : }
304 : }
305 0 : else if ( ServiceSpecifier == SERVICE_NAME_FORM_COLLECTION || SERVICE_NAME_REPORT_COLLECTION == ServiceSpecifier )
306 : {
307 0 : const Any* pBegin = _aArguments.getConstArray();
308 0 : const Any* pEnd = pBegin + _aArguments.getLength();
309 0 : PropertyValue aValue;
310 0 : ::rtl::OUString sName;
311 0 : Reference<XNameAccess> xCopyFrom;
312 0 : for(;pBegin != pEnd;++pBegin)
313 : {
314 0 : *pBegin >>= aValue;
315 0 : if ( aValue.Name.equalsAsciiL(PROPERTY_NAME.ascii, PROPERTY_NAME.length) )
316 : {
317 0 : aValue.Value >>= sName;
318 : }
319 0 : else if ( aValue.Name.equalsAsciiL(PROPERTY_EMBEDDEDOBJECT.ascii, PROPERTY_EMBEDDEDOBJECT.length) )
320 : {
321 0 : xCopyFrom.set(aValue.Value,UNO_QUERY);
322 : }
323 : }
324 : OSL_ENSURE(!sName.isEmpty(),"Invalid name for a document container!");
325 0 : const ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
326 0 : ODefinitionContainer_Impl::const_iterator aFind = rDefinitions.find( sName );
327 0 : TContentPtr pElementImpl;
328 0 : if ( aFind == rDefinitions.end() )
329 : {
330 0 : pElementImpl.reset(new ODefinitionContainer_Impl);
331 0 : pElementImpl->m_aProps.aTitle = sName;
332 0 : pElementImpl->m_pDataSource = m_pImpl->m_pDataSource;
333 : }
334 : else
335 0 : pElementImpl = aFind->second;
336 : OSL_ENSURE( pElementImpl ," Invalid entry in map!");
337 0 : xContent = new ODocumentContainer( m_aContext.getLegacyServiceFactory(), *this, pElementImpl, ServiceSpecifier == SERVICE_NAME_FORM_COLLECTION );
338 :
339 : // copy children
340 0 : if ( xCopyFrom.is() )
341 : {
342 0 : Sequence< ::rtl::OUString> aSeq = xCopyFrom->getElementNames();
343 0 : const ::rtl::OUString* elements = aSeq.getConstArray();
344 0 : const ::rtl::OUString* elementsEnd = elements + aSeq.getLength();
345 0 : Reference<XContent> xObjectToCopy;
346 :
347 0 : Reference<XMultiServiceFactory> xORB(xContent,UNO_QUERY);
348 : OSL_ENSURE(xORB.is(),"No service factory given");
349 0 : if ( xORB.is() )
350 : {
351 0 : for(;elements != elementsEnd;++elements)
352 : {
353 0 : xCopyFrom->getByName(*elements) >>= xObjectToCopy;
354 0 : Sequence< Any > aArguments(3);
355 0 : PropertyValue aArgument;
356 : // set as folder
357 0 : aArgument.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Name"));
358 0 : aArgument.Value <<= *elements;
359 0 : aArguments[0] <<= aArgument;
360 : //parent
361 0 : aArgument.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Parent"));
362 0 : aArgument.Value <<= xContent;
363 0 : aArguments[1] <<= aArgument;
364 :
365 0 : aArgument.Name = PROPERTY_EMBEDDEDOBJECT;
366 0 : aArgument.Value <<= xObjectToCopy;
367 0 : aArguments[2] <<= aArgument;
368 :
369 0 : ::rtl::OUString sServiceName;
370 0 : if ( Reference< XNameAccess >( xObjectToCopy, UNO_QUERY ).is() )
371 : {
372 0 : if ( m_bFormsContainer )
373 0 : sServiceName = SERVICE_NAME_FORM_COLLECTION;
374 : else
375 0 : sServiceName = SERVICE_NAME_REPORT_COLLECTION;
376 : }
377 : else
378 0 : sServiceName = SERVICE_SDB_DOCUMENTDEFINITION;
379 :
380 0 : Reference<XContent > xNew(xORB->createInstanceWithArguments(sServiceName,aArguments),UNO_QUERY);
381 0 : Reference<XNameContainer> xNameContainer(xContent,UNO_QUERY);
382 0 : if ( xNameContainer.is() )
383 0 : xNameContainer->insertByName(*elements,makeAny(xNew));
384 0 : }
385 0 : }
386 0 : }
387 : }
388 0 : xRet = xContent;
389 0 : return xRet;
390 : }
391 :
392 0 : Sequence< ::rtl::OUString > SAL_CALL ODocumentContainer::getAvailableServiceNames( ) throw (RuntimeException)
393 : {
394 0 : Sequence< ::rtl::OUString > aSe(3);
395 0 : aSe[0] = SERVICE_SDB_DOCUMENTDEFINITION;
396 0 : aSe[1] = SERVICE_NAME_FORM_COLLECTION;
397 0 : aSe[2] = SERVICE_NAME_REPORT_COLLECTION;
398 0 : return aSe;
399 : }
400 :
401 0 : Any SAL_CALL ODocumentContainer::execute( const Command& aCommand, sal_Int32 CommandId, const Reference< XCommandEnvironment >& Environment ) throw (Exception, CommandAbortedException, RuntimeException)
402 : {
403 0 : Any aRet;
404 0 : if ( aCommand.Name.compareToAscii( "open" ) == 0 )
405 : {
406 : //////////////////////////////////////////////////////////////////
407 : // open command for a folder content
408 : //////////////////////////////////////////////////////////////////
409 0 : OpenCommandArgument2 aOpenCommand;
410 0 : if ( !( aCommand.Argument >>= aOpenCommand ) )
411 : {
412 : OSL_FAIL( "Wrong argument type!" );
413 : ucbhelper::cancelCommandExecution(
414 : makeAny( IllegalArgumentException(
415 : rtl::OUString(),
416 : static_cast< cppu::OWeakObject * >( this ),
417 : -1 ) ),
418 0 : Environment );
419 : // Unreachable
420 : }
421 : sal_Bool bOpenFolder =
422 : ( ( aOpenCommand.Mode == OpenMode::ALL ) ||
423 : ( aOpenCommand.Mode == OpenMode::FOLDERS ) ||
424 0 : ( aOpenCommand.Mode == OpenMode::DOCUMENTS ) );
425 :
426 0 : if ( bOpenFolder )
427 : {
428 : // open as folder - return result set
429 :
430 : Reference< XDynamicResultSet > xSet
431 : = new DynamicResultSet( m_aContext.getUNOContext(),
432 : this,
433 : aOpenCommand,
434 0 : Environment );
435 0 : aRet <<= xSet;
436 : }
437 : else
438 : {
439 : // Unsupported.
440 : ucbhelper::cancelCommandExecution(
441 : makeAny( UnsupportedOpenModeException(
442 : rtl::OUString(),
443 : static_cast< cppu::OWeakObject * >( this ),
444 : sal_Int16( aOpenCommand.Mode ) ) ),
445 0 : Environment );
446 : // Unreachable
447 0 : }
448 : }
449 0 : else if ( aCommand.Name == "insert" )
450 : {
451 : //////////////////////////////////////////////////////////////////
452 : // insert
453 : //////////////////////////////////////////////////////////////////
454 :
455 0 : InsertCommandArgument arg;
456 0 : if ( !( aCommand.Argument >>= arg ) )
457 : {
458 : OSL_FAIL( "Wrong argument type!" );
459 : ucbhelper::cancelCommandExecution(
460 : makeAny( IllegalArgumentException(
461 : rtl::OUString(),
462 : static_cast< cppu::OWeakObject * >( this ),
463 : -1 ) ),
464 0 : Environment );
465 : // Unreachable
466 0 : }
467 : }
468 0 : else if ( aCommand.Name == "delete" )
469 : {
470 : //////////////////////////////////////////////////////////////////
471 : // delete
472 : //////////////////////////////////////////////////////////////////
473 0 : Sequence< ::rtl::OUString> aSeq = getElementNames();
474 0 : const ::rtl::OUString* pIter = aSeq.getConstArray();
475 0 : const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
476 0 : for(;pIter != pEnd;++pIter)
477 0 : removeByName(*pIter);
478 :
479 0 : dispose();
480 : }
481 : else
482 0 : aRet = OContentHelper::execute(aCommand,CommandId,Environment);
483 0 : return aRet;
484 : }
485 :
486 : namespace
487 : {
488 0 : sal_Bool lcl_queryContent(const ::rtl::OUString& _sName,Reference< XNameContainer >& _xNameContainer,Any& _rRet,::rtl::OUString& _sSimpleName)
489 : {
490 0 : sal_Bool bRet = sal_False;
491 0 : sal_Int32 nIndex = 0;
492 0 : ::rtl::OUString sName = _sName.getToken(0,'/',nIndex);
493 0 : bRet = _xNameContainer->hasByName(sName);
494 0 : if ( bRet )
495 : {
496 0 : _rRet = _xNameContainer->getByName(_sSimpleName = sName);
497 0 : while ( nIndex != -1 && bRet )
498 : {
499 0 : sName = _sName.getToken(0,'/',nIndex);
500 0 : _xNameContainer.set(_rRet,UNO_QUERY);
501 0 : bRet = _xNameContainer.is();
502 0 : if ( bRet )
503 : {
504 0 : bRet = _xNameContainer->hasByName(sName);
505 0 : _sSimpleName = sName;
506 0 : if ( bRet )
507 0 : _rRet = _xNameContainer->getByName(sName);
508 : }
509 : }
510 : }
511 0 : if ( nIndex == -1 )
512 0 : _sSimpleName = sName; // a content
513 : else
514 0 : _xNameContainer.clear(); // a sub folder doesn't exist
515 0 : return bRet;
516 : }
517 : }
518 :
519 0 : Reference< XComponent > SAL_CALL ODocumentContainer::loadComponentFromURL( const ::rtl::OUString& _sURL
520 : , const ::rtl::OUString& /*TargetFrameName*/
521 : , sal_Int32 /*SearchFlags*/
522 : , const Sequence< PropertyValue >& Arguments ) throw (IOException, IllegalArgumentException, RuntimeException)
523 : {
524 0 : ::SolarMutexGuard aSolarGuard;
525 :
526 0 : MutexGuard aGuard(m_aMutex);
527 0 : Reference< XComponent > xComp;
528 : try
529 : {
530 0 : Any aContent;
531 0 : Reference< XNameContainer > xNameContainer(this);
532 0 : ::rtl::OUString sName;
533 0 : if ( !lcl_queryContent(_sURL,xNameContainer,aContent,sName) )
534 : {
535 : ::rtl::OUString sMessage(
536 0 : DBA_RES(RID_STR_NAME_NOT_FOUND).replaceFirst("$name$", _sURL));
537 0 : throw IllegalArgumentException( sMessage, *this, 1 );
538 : }
539 :
540 0 : Reference< XCommandProcessor > xContent(aContent,UNO_QUERY);
541 0 : if ( xContent.is() )
542 : {
543 0 : Command aCommand;
544 :
545 0 : ::comphelper::NamedValueCollection aArgs( Arguments );
546 0 : aCommand.Name = aArgs.getOrDefault( "OpenMode", ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "open" ) ) );
547 0 : aArgs.remove( "OpenMode" );
548 :
549 0 : OpenCommandArgument2 aOpenCommand;
550 0 : aOpenCommand.Mode = OpenMode::DOCUMENT;
551 0 : aArgs.put( "OpenCommandArgument", aOpenCommand );
552 :
553 0 : aCommand.Argument <<= aArgs.getPropertyValues();
554 0 : xComp.set(xContent->execute(aCommand,xContent->createCommandIdentifier(),Reference< XCommandEnvironment >()),UNO_QUERY);
555 0 : }
556 : }
557 0 : catch(const NoSuchElementException&)
558 : {
559 0 : throw IllegalArgumentException();
560 : }
561 0 : catch(const WrappedTargetException&)
562 : {
563 0 : throw;
564 : }
565 0 : return xComp;
566 : }
567 :
568 0 : Any SAL_CALL ODocumentContainer::getByHierarchicalName( const ::rtl::OUString& _sName ) throw (NoSuchElementException, RuntimeException)
569 : {
570 0 : MutexGuard aGuard(m_aMutex);
571 0 : Any aContent;
572 0 : Reference< XNameContainer > xNameContainer(this);
573 0 : ::rtl::OUString sName;
574 0 : if ( lcl_queryContent(_sName,xNameContainer,aContent,sName) )
575 0 : return aContent;
576 0 : throw NoSuchElementException(_sName,*this);
577 : }
578 :
579 0 : sal_Bool SAL_CALL ODocumentContainer::hasByHierarchicalName( const ::rtl::OUString& _sName ) throw (RuntimeException)
580 : {
581 0 : MutexGuard aGuard(m_aMutex);
582 0 : Any aContent;
583 0 : Reference< XNameContainer > xNameContainer(this);
584 0 : ::rtl::OUString sName;
585 0 : return lcl_queryContent(_sName,xNameContainer,aContent,sName);
586 : }
587 :
588 : // XHierarchicalNameContainer
589 0 : void SAL_CALL ODocumentContainer::insertByHierarchicalName( const ::rtl::OUString& _sName, const Any& _aElement ) throw (IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
590 : {
591 0 : Reference< XContent > xContent(_aElement,UNO_QUERY);
592 0 : if ( !xContent.is() )
593 0 : throw IllegalArgumentException();
594 :
595 0 : ClearableMutexGuard aGuard(m_aMutex);
596 0 : Any aContent;
597 0 : Reference< XNameContainer > xNameContainer(this);
598 0 : ::rtl::OUString sName;
599 0 : if ( lcl_queryContent(_sName,xNameContainer,aContent,sName) )
600 0 : throw ElementExistException(_sName,*this);
601 :
602 0 : if ( !xNameContainer.is() )
603 : {
604 0 : sal_Int32 index = sName.getLength();
605 : ::rtl::OUString sMessage(
606 : DBA_RES(RID_STR_NO_SUB_FOLDER).replaceFirst("$folder$",
607 0 : _sName.getToken(0,'/',index)));
608 0 : throw IllegalArgumentException( sMessage, *this, 1 );
609 : }
610 :
611 0 : xNameContainer->insertByName(sName,_aElement);
612 0 : }
613 :
614 0 : void SAL_CALL ODocumentContainer::removeByHierarchicalName( const ::rtl::OUString& _sName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
615 : {
616 0 : if ( _sName.isEmpty() )
617 0 : throw NoSuchElementException(_sName,*this);
618 :
619 0 : ClearableMutexGuard aGuard(m_aMutex);
620 0 : Any aContent;
621 0 : ::rtl::OUString sName;
622 0 : Reference< XNameContainer > xNameContainer(this);
623 0 : if ( !lcl_queryContent(_sName,xNameContainer,aContent,sName) )
624 0 : throw NoSuchElementException(_sName,*this);
625 :
626 0 : xNameContainer->removeByName(sName);
627 0 : }
628 :
629 : // XHierarchicalNameReplace
630 0 : void SAL_CALL ODocumentContainer::replaceByHierarchicalName( const ::rtl::OUString& _sName, const Any& _aElement ) throw (IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
631 : {
632 0 : Reference< XContent > xContent(_aElement,UNO_QUERY);
633 0 : if ( !xContent.is() )
634 0 : throw IllegalArgumentException();
635 :
636 0 : ClearableMutexGuard aGuard(m_aMutex);
637 0 : Any aContent;
638 0 : ::rtl::OUString sName;
639 0 : Reference< XNameContainer > xNameContainer(this);
640 0 : if ( !lcl_queryContent(_sName,xNameContainer,aContent,sName) )
641 0 : throw NoSuchElementException(_sName,*this);
642 :
643 0 : xNameContainer->replaceByName(sName,_aElement);
644 0 : }
645 :
646 0 : ::rtl::OUString SAL_CALL ODocumentContainer::getHierarchicalName() throw (RuntimeException)
647 : {
648 0 : ::osl::MutexGuard aGuard( m_aMutex );
649 0 : return impl_getHierarchicalName( false );
650 : }
651 :
652 0 : ::rtl::OUString SAL_CALL ODocumentContainer::composeHierarchicalName( const ::rtl::OUString& i_rRelativeName ) throw (IllegalArgumentException, NoSupportException, RuntimeException)
653 : {
654 0 : ::rtl::OUStringBuffer aBuffer;
655 0 : aBuffer.append( getHierarchicalName() );
656 0 : aBuffer.append( sal_Unicode( '/' ) );
657 0 : aBuffer.append( i_rRelativeName );
658 0 : return aBuffer.makeStringAndClear();
659 : }
660 :
661 0 : ::rtl::Reference<OContentHelper> ODocumentContainer::getContent(const ::rtl::OUString& _sName) const
662 : {
663 0 : ::rtl::Reference<OContentHelper> pContent = NULL;
664 : try
665 : {
666 0 : Reference<XUnoTunnel> xUnoTunnel(const_cast<ODocumentContainer*>(this)->implGetByName( _sName, sal_True ), UNO_QUERY );
667 0 : if ( xUnoTunnel.is() )
668 0 : pContent = reinterpret_cast<OContentHelper*>(xUnoTunnel->getSomething(OContentHelper::getUnoTunnelImplementationId()));
669 : }
670 0 : catch(const Exception&)
671 : {
672 : }
673 0 : return pContent;
674 : }
675 :
676 0 : void ODocumentContainer::getPropertyDefaultByHandle( sal_Int32 /*_nHandle*/, Any& _rDefault ) const
677 : {
678 0 : _rDefault.clear();
679 0 : }
680 :
681 0 : void SAL_CALL ODocumentContainer::commit( ) throw (::com::sun::star::io::IOException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
682 : {
683 0 : MutexGuard aGuard(m_aMutex);
684 0 : Documents::iterator aIter = m_aDocumentMap.begin();
685 0 : Documents::iterator aEnd = m_aDocumentMap.end();
686 0 : for (; aIter != aEnd ; ++aIter)
687 : {
688 0 : Reference<XTransactedObject> xTrans(aIter->second.get(),UNO_QUERY);
689 0 : if ( xTrans.is() )
690 0 : xTrans->commit();
691 0 : }
692 0 : Reference<XTransactedObject> xTrans(getContainerStorage(),UNO_QUERY);
693 0 : if ( xTrans.is() )
694 0 : xTrans->commit();
695 0 : }
696 :
697 0 : void SAL_CALL ODocumentContainer::revert( ) throw (::com::sun::star::io::IOException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
698 : {
699 0 : MutexGuard aGuard(m_aMutex);
700 0 : Documents::iterator aIter = m_aDocumentMap.begin();
701 0 : Documents::iterator aEnd = m_aDocumentMap.end();
702 0 : for (; aIter != aEnd ; ++aIter)
703 : {
704 0 : Reference<XTransactedObject> xTrans(aIter->second.get(),UNO_QUERY);
705 0 : if ( xTrans.is() )
706 0 : xTrans->revert();
707 0 : }
708 0 : Reference<XTransactedObject> xTrans(getContainerStorage(),UNO_QUERY);
709 0 : if ( xTrans.is() )
710 0 : xTrans->revert();
711 0 : }
712 :
713 0 : Reference< XStorage> ODocumentContainer::getContainerStorage() const
714 : {
715 0 : return m_pImpl->m_pDataSource
716 0 : ? m_pImpl->m_pDataSource->getStorage( m_bFormsContainer ? ODatabaseModelImpl::E_FORM : ODatabaseModelImpl::E_REPORT )
717 0 : : Reference< XStorage>();
718 : }
719 :
720 0 : void SAL_CALL ODocumentContainer::removeByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
721 : {
722 0 : ResettableMutexGuard aGuard(m_aMutex);
723 :
724 : // check the arguments
725 0 : if (_rName.isEmpty())
726 0 : throw IllegalArgumentException();
727 :
728 0 : if (!checkExistence(_rName))
729 0 : throw NoSuchElementException(_rName,*this);
730 :
731 0 : Reference< XCommandProcessor > xContent( implGetByName( _rName, sal_True ), UNO_QUERY );
732 0 : if ( xContent.is() )
733 : {
734 0 : Command aCommand;
735 :
736 0 : aCommand.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("delete"));
737 0 : xContent->execute(aCommand,xContent->createCommandIdentifier(),Reference< XCommandEnvironment >());
738 : }
739 :
740 : // do the removal
741 0 : implRemove(_rName);
742 :
743 0 : notifyByName( aGuard, _rName, NULL, NULL, E_REMOVED, ContainerListemers );
744 0 : }
745 :
746 0 : void SAL_CALL ODocumentContainer::rename( const ::rtl::OUString& newName ) throw (SQLException, ElementExistException, RuntimeException)
747 : {
748 : try
749 : {
750 0 : osl::ClearableGuard< osl::Mutex > aGuard(m_aMutex);
751 0 : if ( newName.equals( m_pImpl->m_aProps.aTitle ) )
752 0 : return;
753 :
754 0 : sal_Int32 nHandle = PROPERTY_ID_NAME;
755 0 : Any aOld = makeAny(m_pImpl->m_aProps.aTitle);
756 0 : Any aNew = makeAny(newName);
757 :
758 0 : aGuard.clear();
759 0 : fire(&nHandle, &aNew, &aOld, 1, sal_True );
760 0 : m_pImpl->m_aProps.aTitle = newName;
761 0 : fire(&nHandle, &aNew, &aOld, 1, sal_False );
762 : }
763 0 : catch(const PropertyVetoException&)
764 : {
765 0 : throw ElementExistException(newName,*this);
766 : }
767 : }
768 :
769 : } // namespace dbaccess
770 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|