xref: /AOO41X/main/dbaccess/source/core/misc/sdbcoretools.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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 #include "sdbcoretools.hxx"
32 #include "dbastrings.hrc"
33 
34 /** === begin UNO includes === **/
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #include <com/sun/star/container/XChild.hpp>
38 #include <com/sun/star/util/XModifiable.hpp>
39 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
40 #include <com/sun/star/task/XInteractionRequestStringResolver.hpp>
41 #include <com/sun/star/embed/XTransactedObject.hpp>
42 #include <com/sun/star/embed/ElementModes.hpp>
43 /** === end UNO includes === **/
44 
45 #include <tools/diagnose_ex.h>
46 #include <tools/debug.hxx>
47 #include <comphelper/componentcontext.hxx>
48 #include <comphelper/interaction.hxx>
49 #include <rtl/ref.hxx>
50 #include <rtl/ustrbuf.hxx>
51 
52 //.........................................................................
53 namespace dbaccess
54 {
55 //.........................................................................
56 
57 	using namespace ::com::sun::star::uno;
58 	using namespace ::com::sun::star::lang;
59 	using namespace ::com::sun::star::util;
60 	using namespace ::com::sun::star::io;
61 	using namespace ::com::sun::star::sdbc;
62 	using namespace ::com::sun::star::sdb;
63 	using namespace ::com::sun::star::beans;
64 	using namespace ::com::sun::star::task;
65 	using namespace ::com::sun::star::embed;
66 	using namespace ::com::sun::star::container;
67 
68     // =========================================================================
69     // -------------------------------------------------------------------------
70     void notifyDataSourceModified(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxObject,sal_Bool _bModified)
71     {
72         Reference< XInterface > xDs = getDataSource( _rxObject );
73 		Reference<XDocumentDataSource> xDocumentDataSource(xDs,UNO_QUERY);
74 		if ( xDocumentDataSource.is() )
75             xDs = xDocumentDataSource->getDatabaseDocument();
76         Reference< XModifiable > xModi( xDs, UNO_QUERY );
77 		if ( xModi.is() )
78 			xModi->setModified(_bModified);
79     }
80 
81     // -------------------------------------------------------------------------
82     Reference< XInterface > getDataSource( const Reference< XInterface >& _rxDependentObject )
83     {
84 	    Reference< XInterface > xParent = _rxDependentObject;
85 	    Reference< XInterface > xReturn;
86 	    while( xParent.is() )
87 	    {
88 		    xReturn = xParent;
89 		    Reference<XChild> xChild(xParent,UNO_QUERY);
90 		    xParent.set(xChild.is() ? xChild->getParent() : Reference< XInterface >(),UNO_QUERY);
91 	    }
92         return xReturn;
93     }
94 
95 // -----------------------------------------------------------------------------
96     ::rtl::OUString extractExceptionMessage( const ::comphelper::ComponentContext& _rContext, const Any& _rError )
97     {
98         ::rtl::OUString sDisplayMessage;
99 
100         try
101         {
102             Reference< XInteractionRequestStringResolver > xStringResolver;
103             if ( _rContext.createComponent( "com.sun.star.task.InteractionRequestStringResolver", xStringResolver ) )
104             {
105                 ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( _rError ) );
106                 ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
107                 pRequest->addContinuation( pApprove.get() );
108                 Optional< ::rtl::OUString > aMessage = xStringResolver->getStringFromInformationalRequest( pRequest.get() );
109                 if ( aMessage.IsPresent )
110                     sDisplayMessage = aMessage.Value;
111             }
112         }
113         catch( const Exception& )
114         {
115             DBG_UNHANDLED_EXCEPTION();
116         }
117 
118         if ( !sDisplayMessage.getLength() )
119         {
120             Exception aExcept;
121             _rError >>= aExcept;
122 
123             ::rtl::OUStringBuffer aBuffer;
124             aBuffer.append( _rError.getValueTypeName() );
125             aBuffer.appendAscii( ":\n" );
126             aBuffer.append( aExcept.Message );
127 
128             sDisplayMessage = aBuffer.makeStringAndClear();
129         }
130 
131         return sDisplayMessage;
132     }
133 
134     namespace tools { namespace stor {
135 
136     // -----------------------------------------------------------------------------
137     bool storageIsWritable_nothrow( const Reference< XStorage >& _rxStorage )
138     {
139         if ( !_rxStorage.is() )
140             return false;
141 
142         sal_Int32 nMode = ElementModes::READ;
143         try
144         {
145             Reference< XPropertySet > xStorageProps( _rxStorage, UNO_QUERY_THROW );
146 			xStorageProps->getPropertyValue(
147                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OpenMode" ) ) ) >>= nMode;
148         }
149         catch( const Exception& )
150         {
151             DBG_UNHANDLED_EXCEPTION();
152         }
153         return ( nMode & ElementModes::WRITE ) != 0;
154     }
155 
156     // -----------------------------------------------------------------------------
157     bool commitStorageIfWriteable( const Reference< XStorage >& _rxStorage ) SAL_THROW(( IOException, WrappedTargetException, RuntimeException ))
158     {
159         bool bSuccess = false;
160 	    Reference< XTransactedObject > xTrans( _rxStorage, UNO_QUERY );
161 	    if ( xTrans.is() )
162         {
163             if ( storageIsWritable_nothrow( _rxStorage ) )
164                 xTrans->commit();
165             bSuccess = true;
166         }
167         return bSuccess;
168     }
169 
170     } } // tools::stor
171 
172 //.........................................................................
173 }	// namespace dbaccess
174 //.........................................................................
175 
176