1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: datasourceconnector.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef _DBAUI_DATASOURCECONNECTOR_HXX_
35 #include "datasourceconnector.hxx"
37 #ifndef _OSL_DIAGNOSE_H_
38 #include <osl/diagnose.h>
40 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
41 #include "dbustrings.hrc"
43 #ifndef _COM_SUN_STAR_SDBC_XWARNINGSSUPPLIER_HPP_
44 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
46 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
47 #include <com/sun/star/beans/XPropertySet.hpp>
49 #ifndef _COM_SUN_STAR_SDB_XCOMPLETEDCONNECTION_HPP_
50 #include <com/sun/star/sdb/XCompletedConnection.hpp>
52 #ifndef _COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP_
53 #include <com/sun/star/task/XInteractionHandler.hpp>
55 #ifndef _COM_SUN_STAR_FRAME_XMODEL_HPP_
56 #include <com/sun/star/frame/XModel.hpp>
58 #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
59 #include <com/sun/star/sdb/SQLContext.hpp>
61 #ifndef _COM_SUN_STAR_SDBC_SQLWARNING_HPP_
62 #include <com/sun/star/sdbc/SQLWarning.hpp>
64 #ifndef _OSL_THREAD_H_
65 #include <osl/thread.h>
67 #ifndef _COMPHELPER_EXTRACT_HXX_
68 #include <comphelper/extract.hxx>
70 #ifndef COMPHELPER_NAMEDVALUECOLLECTION_HXX
71 #include <comphelper/namedvaluecollection.hxx>
73 #ifndef _DBHELPER_DBEXCEPTION_HXX_
74 #include <connectivity/dbexception.hxx>
76 #ifndef _COM_SUN_STAR_SDBC_XDATASOURCE_HPP_
77 #include <com/sun/star/sdbc/XDataSource.hpp>
79 #ifndef DBAUI_TOOLS_HXX
80 #include "UITools.hxx"
82 #ifndef _VCL_STDTEXT_HXX
83 #include <vcl/stdtext.hxx>
85 #ifndef _SV_BUTTON_HXX
86 #include <vcl/button.hxx>
88 #ifndef SVTOOLS_FILENOTATION_HXX
89 #include <svtools/filenotation.hxx>
91 #ifndef TOOLS_DIAGNOSE_EX_H
92 #include <tools/diagnose_ex.h>
94 #ifndef _CPPUHELPER_EXC_HLP_HXX_
95 #include <cppuhelper/exc_hlp.hxx>
97 #ifndef _DBU_MISC_HRC_
98 #include "dbu_misc.hrc"
100 #include "moduledbu.hxx"
102 //.........................................................................
105 //.........................................................................
107 using namespace ::com::sun::star::uno
;
108 using namespace ::com::sun::star::lang
;
109 using namespace ::com::sun::star::sdb
;
110 using namespace ::com::sun::star::sdbc
;
111 using namespace ::com::sun::star::task
;
112 using namespace ::com::sun::star::beans
;
113 using namespace ::com::sun::star::container
;
114 using namespace ::com::sun::star::frame
;
115 using namespace ::dbtools
;
116 using ::svt::OFileNotation
;
118 //=====================================================================
119 //= ODatasourceConnector
120 //=====================================================================
121 //---------------------------------------------------------------------
122 ODatasourceConnector::ODatasourceConnector(const Reference
< XMultiServiceFactory
>& _rxORB
, Window
* _pMessageParent
)
123 :m_pErrorMessageParent(_pMessageParent
)
128 //---------------------------------------------------------------------
129 ODatasourceConnector::ODatasourceConnector( const Reference
< XMultiServiceFactory
>& _rxORB
, Window
* _pMessageParent
,
130 const ::rtl::OUString
& _rContextInformation
)
131 :m_pErrorMessageParent(_pMessageParent
)
133 ,m_sContextInformation( _rContextInformation
)
137 //---------------------------------------------------------------------
138 Reference
< XConnection
> ODatasourceConnector::connect( const ::rtl::OUString
& _rDataSourceName
,
139 ::dbtools::SQLExceptionInfo
* _pErrorInfo
) const
141 Reference
< XConnection
> xConnection
;
143 OSL_ENSURE(isValid(), "ODatasourceConnector::connect: invalid object!");
147 // get the data source
148 Reference
< XDataSource
> xDatasource(
149 getDataSourceByName( _rDataSourceName
, m_pErrorMessageParent
, m_xORB
, _pErrorInfo
),
153 if ( xDatasource
.is() )
154 xConnection
= connect( xDatasource
, _pErrorInfo
);
158 //---------------------------------------------------------------------
159 Reference
< XConnection
> ODatasourceConnector::connect(const Reference
< XDataSource
>& _xDataSource
,
160 ::dbtools::SQLExceptionInfo
* _pErrorInfo
) const
162 Reference
< XConnection
> xConnection
;
164 OSL_ENSURE( isValid() && _xDataSource
.is(), "ODatasourceConnector::connect: invalid object or argument!" );
165 if ( !isValid() || !_xDataSource
.is() )
169 ::rtl::OUString sPassword
, sUser
;
170 sal_Bool bPwdRequired
= sal_False
;
171 Reference
<XPropertySet
> xProp(_xDataSource
,UNO_QUERY
);
174 xProp
->getPropertyValue(PROPERTY_PASSWORD
) >>= sPassword
;
175 xProp
->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED
) >>= bPwdRequired
;
176 xProp
->getPropertyValue(PROPERTY_USER
) >>= sUser
;
180 DBG_UNHANDLED_EXCEPTION();
184 SQLExceptionInfo aInfo
;
187 if (bPwdRequired
&& !sPassword
.getLength())
188 { // password required, but empty -> connect using an interaction handler
189 Reference
< XCompletedConnection
> xConnectionCompletion( _xDataSource
, UNO_QUERY_THROW
);
191 Reference
< XModel
> xModel( getDataSourceOrModel( _xDataSource
), UNO_QUERY_THROW
);
192 ::comphelper::NamedValueCollection
aArgs( xModel
->getArgs() );
193 Reference
< XInteractionHandler
> xHandler( aArgs
.getOrDefault( "InteractionHandler", Reference
< XInteractionHandler
>() ) );
195 if ( !xHandler
.is() )
197 // instantiate the default SDB interaction handler
198 xHandler
= Reference
< XInteractionHandler
>( m_xORB
->createInstance( SERVICE_SDB_INTERACTION_HANDLER
), UNO_QUERY
);
199 if ( !xHandler
.is() )
200 ShowServiceNotAvailableError(m_pErrorMessageParent
, String(SERVICE_SDB_INTERACTION_HANDLER
), sal_True
);
205 xConnection
= xConnectionCompletion
->connectWithCompletion(xHandler
);
210 xConnection
= _xDataSource
->getConnection(sUser
, sPassword
);
213 catch( const SQLException
& )
215 aInfo
= ::cppu::getCaughtException();
217 catch(const Exception
&)
219 DBG_UNHANDLED_EXCEPTION();
222 if ( !aInfo
.isValid() )
224 // there was no error during connecting, but perhaps a warning?
225 Reference
< XWarningsSupplier
> xConnectionWarnings( xConnection
, UNO_QUERY
);
226 if ( xConnectionWarnings
.is() )
230 Any
aWarnings( xConnectionWarnings
->getWarnings() );
231 if ( aWarnings
.hasValue() )
233 String
sMessage( ModuleRes( STR_WARNINGS_DURING_CONNECT
) );
234 sMessage
.SearchAndReplaceAscii( "$buttontext$", Button::GetStandardText( BUTTON_MORE
) );
235 sMessage
= OutputDevice::GetNonMnemonicString( sMessage
);
238 aContext
.Message
= sMessage
;
239 aContext
.NextException
= aWarnings
;
242 xConnectionWarnings
->clearWarnings();
244 catch( const Exception
& )
246 DBG_UNHANDLED_EXCEPTION();
252 if ( m_sContextInformation
.getLength() )
255 aError
.Message
= m_sContextInformation
;
256 aError
.NextException
= aInfo
.get();
262 // was there an error?
263 if ( aInfo
.isValid() )
267 *_pErrorInfo
= aInfo
;
271 showError( aInfo
, m_pErrorMessageParent
, m_xORB
);
277 //.........................................................................
279 //.........................................................................