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