xref: /AOO41X/main/dbaccess/source/core/misc/sdbcoretools.cxx (revision 96de54900b79e13b861fbc62cbf36018b54e21b7)
1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sdbcoretools.hxx"
28cdf0e10cSrcweir #include "dbastrings.hrc"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir /** === begin UNO includes === **/
31cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
32cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
33cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
34cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp>
35cdf0e10cSrcweir #include <com/sun/star/sdb/XDocumentDataSource.hpp>
36cdf0e10cSrcweir #include <com/sun/star/task/XInteractionRequestStringResolver.hpp>
37cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
38cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
39cdf0e10cSrcweir /** === end UNO includes === **/
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <tools/diagnose_ex.h>
42cdf0e10cSrcweir #include <tools/debug.hxx>
43cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
44cdf0e10cSrcweir #include <comphelper/interaction.hxx>
45cdf0e10cSrcweir #include <rtl/ref.hxx>
46cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //.........................................................................
49cdf0e10cSrcweir namespace dbaccess
50cdf0e10cSrcweir {
51cdf0e10cSrcweir //.........................................................................
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
54cdf0e10cSrcweir 	using namespace ::com::sun::star::lang;
55cdf0e10cSrcweir 	using namespace ::com::sun::star::util;
56cdf0e10cSrcweir 	using namespace ::com::sun::star::io;
57cdf0e10cSrcweir 	using namespace ::com::sun::star::sdbc;
58cdf0e10cSrcweir 	using namespace ::com::sun::star::sdb;
59cdf0e10cSrcweir 	using namespace ::com::sun::star::beans;
60cdf0e10cSrcweir 	using namespace ::com::sun::star::task;
61cdf0e10cSrcweir 	using namespace ::com::sun::star::embed;
62cdf0e10cSrcweir 	using namespace ::com::sun::star::container;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     // =========================================================================
65cdf0e10cSrcweir     // -------------------------------------------------------------------------
notifyDataSourceModified(const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _rxObject,sal_Bool _bModified)66cdf0e10cSrcweir     void notifyDataSourceModified(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxObject,sal_Bool _bModified)
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         Reference< XInterface > xDs = getDataSource( _rxObject );
69cdf0e10cSrcweir 		Reference<XDocumentDataSource> xDocumentDataSource(xDs,UNO_QUERY);
70cdf0e10cSrcweir 		if ( xDocumentDataSource.is() )
71cdf0e10cSrcweir             xDs = xDocumentDataSource->getDatabaseDocument();
72cdf0e10cSrcweir         Reference< XModifiable > xModi( xDs, UNO_QUERY );
73cdf0e10cSrcweir 		if ( xModi.is() )
74cdf0e10cSrcweir 			xModi->setModified(_bModified);
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     // -------------------------------------------------------------------------
getDataSource(const Reference<XInterface> & _rxDependentObject)78cdf0e10cSrcweir     Reference< XInterface > getDataSource( const Reference< XInterface >& _rxDependentObject )
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir 	    Reference< XInterface > xParent = _rxDependentObject;
81cdf0e10cSrcweir 	    Reference< XInterface > xReturn;
82cdf0e10cSrcweir 	    while( xParent.is() )
83cdf0e10cSrcweir 	    {
84cdf0e10cSrcweir 		    xReturn = xParent;
85cdf0e10cSrcweir 		    Reference<XChild> xChild(xParent,UNO_QUERY);
86cdf0e10cSrcweir 		    xParent.set(xChild.is() ? xChild->getParent() : Reference< XInterface >(),UNO_QUERY);
87cdf0e10cSrcweir 	    }
88cdf0e10cSrcweir         return xReturn;
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir // -----------------------------------------------------------------------------
extractExceptionMessage(const::comphelper::ComponentContext & _rContext,const Any & _rError)92cdf0e10cSrcweir     ::rtl::OUString extractExceptionMessage( const ::comphelper::ComponentContext& _rContext, const Any& _rError )
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         ::rtl::OUString sDisplayMessage;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         try
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             Reference< XInteractionRequestStringResolver > xStringResolver;
99cdf0e10cSrcweir             if ( _rContext.createComponent( "com.sun.star.task.InteractionRequestStringResolver", xStringResolver ) )
100cdf0e10cSrcweir             {
101cdf0e10cSrcweir                 ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( _rError ) );
102cdf0e10cSrcweir                 ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
103cdf0e10cSrcweir                 pRequest->addContinuation( pApprove.get() );
104cdf0e10cSrcweir                 Optional< ::rtl::OUString > aMessage = xStringResolver->getStringFromInformationalRequest( pRequest.get() );
105cdf0e10cSrcweir                 if ( aMessage.IsPresent )
106cdf0e10cSrcweir                     sDisplayMessage = aMessage.Value;
107cdf0e10cSrcweir             }
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir         catch( const Exception& )
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         if ( !sDisplayMessage.getLength() )
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             Exception aExcept;
117cdf0e10cSrcweir             _rError >>= aExcept;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             ::rtl::OUStringBuffer aBuffer;
120cdf0e10cSrcweir             aBuffer.append( _rError.getValueTypeName() );
121cdf0e10cSrcweir             aBuffer.appendAscii( ":\n" );
122cdf0e10cSrcweir             aBuffer.append( aExcept.Message );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             sDisplayMessage = aBuffer.makeStringAndClear();
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         return sDisplayMessage;
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     namespace tools { namespace stor {
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     // -----------------------------------------------------------------------------
storageIsWritable_nothrow(const Reference<XStorage> & _rxStorage)133cdf0e10cSrcweir     bool storageIsWritable_nothrow( const Reference< XStorage >& _rxStorage )
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         if ( !_rxStorage.is() )
136cdf0e10cSrcweir             return false;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         sal_Int32 nMode = ElementModes::READ;
139cdf0e10cSrcweir         try
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             Reference< XPropertySet > xStorageProps( _rxStorage, UNO_QUERY_THROW );
142cdf0e10cSrcweir 			xStorageProps->getPropertyValue(
143cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OpenMode" ) ) ) >>= nMode;
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir         catch( const Exception& )
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir         return ( nMode & ElementModes::WRITE ) != 0;
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     // -----------------------------------------------------------------------------
commitStorageIfWriteable(const Reference<XStorage> & _rxStorage)153cdf0e10cSrcweir     bool commitStorageIfWriteable( const Reference< XStorage >& _rxStorage ) SAL_THROW(( IOException, WrappedTargetException, RuntimeException ))
154cdf0e10cSrcweir     {
155cdf0e10cSrcweir         bool bSuccess = false;
156cdf0e10cSrcweir 	    Reference< XTransactedObject > xTrans( _rxStorage, UNO_QUERY );
157cdf0e10cSrcweir 	    if ( xTrans.is() )
158cdf0e10cSrcweir         {
159cdf0e10cSrcweir             if ( storageIsWritable_nothrow( _rxStorage ) )
160cdf0e10cSrcweir                 xTrans->commit();
161cdf0e10cSrcweir             bSuccess = true;
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         return bSuccess;
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     } } // tools::stor
167cdf0e10cSrcweir 
168cdf0e10cSrcweir //.........................................................................
169cdf0e10cSrcweir }	// namespace dbaccess
170cdf0e10cSrcweir //.........................................................................
171cdf0e10cSrcweir 
172