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