xref: /aoo41x/main/sc/source/ui/dbgui/dapidata.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sc.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #undef SC_DLLIMPLEMENTATION
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir //------------------------------------------------------------------
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <tools/debug.hxx>
38*cdf0e10cSrcweir #include <vcl/waitobj.hxx>
39*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <com/sun/star/sheet/DataImportMode.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/sdb/XCompletedConnection.hpp>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using namespace com::sun::star;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include "dapidata.hxx"
50*cdf0e10cSrcweir #include "scresid.hxx"
51*cdf0e10cSrcweir #include "sc.hrc"
52*cdf0e10cSrcweir #include "dapitype.hrc"
53*cdf0e10cSrcweir #include "miscuno.hxx"
54*cdf0e10cSrcweir #include "dpsdbtab.hxx"			// ScImportSourceDesc
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir //-------------------------------------------------------------------------
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir #define DP_SERVICE_DBCONTEXT		"com.sun.star.sdb.DatabaseContext"
59*cdf0e10cSrcweir #define SC_SERVICE_INTHANDLER		"com.sun.star.task.InteractionHandler"
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir //	entries in the "type" ListBox
62*cdf0e10cSrcweir #define DP_TYPELIST_TABLE	0
63*cdf0e10cSrcweir #define DP_TYPELIST_QUERY	1
64*cdf0e10cSrcweir #define DP_TYPELIST_SQL		2
65*cdf0e10cSrcweir #define DP_TYPELIST_SQLNAT	3
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir //-------------------------------------------------------------------------
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir ScDataPilotDatabaseDlg::ScDataPilotDatabaseDlg( Window* pParent ) :
70*cdf0e10cSrcweir 	ModalDialog     ( pParent, ScResId( RID_SCDLG_DAPIDATA ) ),
71*cdf0e10cSrcweir 	//
72*cdf0e10cSrcweir     aFlFrame        ( this, ScResId( FL_FRAME ) ),
73*cdf0e10cSrcweir 	aFtDatabase     ( this, ScResId( FT_DATABASE ) ),
74*cdf0e10cSrcweir 	aLbDatabase     ( this, ScResId( LB_DATABASE ) ),
75*cdf0e10cSrcweir 	aFtObject       ( this, ScResId( FT_OBJECT ) ),
76*cdf0e10cSrcweir 	aCbObject       ( this, ScResId( CB_OBJECT ) ),
77*cdf0e10cSrcweir 	aFtType         ( this, ScResId( FT_OBJTYPE ) ),
78*cdf0e10cSrcweir 	aLbType         ( this, ScResId( LB_OBJTYPE ) ),
79*cdf0e10cSrcweir 	aBtnOk          ( this, ScResId( BTN_OK ) ),
80*cdf0e10cSrcweir 	aBtnCancel      ( this, ScResId( BTN_CANCEL ) ),
81*cdf0e10cSrcweir 	aBtnHelp        ( this, ScResId( BTN_HELP ) )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir 	FreeResource();
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	WaitObject aWait( this );		// initializing the database service the first time takes a while
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 	try
88*cdf0e10cSrcweir 	{
89*cdf0e10cSrcweir 		//	get database names
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		uno::Reference<container::XNameAccess> xContext(
92*cdf0e10cSrcweir 				comphelper::getProcessServiceFactory()->createInstance(
93*cdf0e10cSrcweir 					rtl::OUString::createFromAscii( DP_SERVICE_DBCONTEXT ) ),
94*cdf0e10cSrcweir 				uno::UNO_QUERY);
95*cdf0e10cSrcweir 		if (xContext.is())
96*cdf0e10cSrcweir 		{
97*cdf0e10cSrcweir 			uno::Sequence<rtl::OUString> aNames = xContext->getElementNames();
98*cdf0e10cSrcweir 			long nCount = aNames.getLength();
99*cdf0e10cSrcweir 			const rtl::OUString* pArray = aNames.getConstArray();
100*cdf0e10cSrcweir 			for (long nPos = 0; nPos < nCount; nPos++)
101*cdf0e10cSrcweir 			{
102*cdf0e10cSrcweir 				String aName = pArray[nPos];
103*cdf0e10cSrcweir 				aLbDatabase.InsertEntry( aName );
104*cdf0e10cSrcweir 			}
105*cdf0e10cSrcweir 		}
106*cdf0e10cSrcweir 	}
107*cdf0e10cSrcweir 	catch(uno::Exception&)
108*cdf0e10cSrcweir 	{
109*cdf0e10cSrcweir 		DBG_ERROR("exception in database");
110*cdf0e10cSrcweir 	}
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	aLbDatabase.SelectEntryPos( 0 );
113*cdf0e10cSrcweir 	aLbType.SelectEntryPos( 0 );
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	FillObjects();
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	aLbDatabase.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
118*cdf0e10cSrcweir 	aLbType.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir ScDataPilotDatabaseDlg::~ScDataPilotDatabaseDlg()
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir void ScDataPilotDatabaseDlg::GetValues( ScImportSourceDesc& rDesc )
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir 	sal_uInt16 nSelect = aLbType.GetSelectEntryPos();
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 	rDesc.aDBName = aLbDatabase.GetSelectEntry();
130*cdf0e10cSrcweir 	rDesc.aObject = aCbObject.GetText();
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	if ( !rDesc.aDBName.Len() || !rDesc.aObject.Len() )
133*cdf0e10cSrcweir 		rDesc.nType = sheet::DataImportMode_NONE;
134*cdf0e10cSrcweir 	else if ( nSelect == DP_TYPELIST_TABLE )
135*cdf0e10cSrcweir 		rDesc.nType = sheet::DataImportMode_TABLE;
136*cdf0e10cSrcweir 	else if ( nSelect == DP_TYPELIST_QUERY )
137*cdf0e10cSrcweir 		rDesc.nType = sheet::DataImportMode_QUERY;
138*cdf0e10cSrcweir 	else
139*cdf0e10cSrcweir 		rDesc.nType = sheet::DataImportMode_SQL;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 	rDesc.bNative = ( nSelect == DP_TYPELIST_SQLNAT );
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir IMPL_LINK( ScDataPilotDatabaseDlg, SelectHdl, ListBox*, EMPTYARG )
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir 	FillObjects();
147*cdf0e10cSrcweir 	return 0;
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir void ScDataPilotDatabaseDlg::FillObjects()
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir 	aCbObject.Clear();
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	String aDatabaseName = aLbDatabase.GetSelectEntry();
155*cdf0e10cSrcweir 	if (!aDatabaseName.Len())
156*cdf0e10cSrcweir 		return;
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 	sal_uInt16 nSelect = aLbType.GetSelectEntryPos();
159*cdf0e10cSrcweir 	if ( nSelect > DP_TYPELIST_QUERY )
160*cdf0e10cSrcweir 		return;									// only tables and queries
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	try
163*cdf0e10cSrcweir 	{
164*cdf0e10cSrcweir 		//	open connection (for tables or queries)
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 		uno::Reference<container::XNameAccess> xContext(
167*cdf0e10cSrcweir 				comphelper::getProcessServiceFactory()->createInstance(
168*cdf0e10cSrcweir 					rtl::OUString::createFromAscii( DP_SERVICE_DBCONTEXT ) ),
169*cdf0e10cSrcweir 				uno::UNO_QUERY);
170*cdf0e10cSrcweir 		if ( !xContext.is() ) return;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 		uno::Any aSourceAny = xContext->getByName( aDatabaseName );
173*cdf0e10cSrcweir 		uno::Reference<sdb::XCompletedConnection> xSource(
174*cdf0e10cSrcweir 				ScUnoHelpFunctions::AnyToInterface( aSourceAny ), uno::UNO_QUERY );
175*cdf0e10cSrcweir 		if ( !xSource.is() ) return;
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 		uno::Reference<task::XInteractionHandler> xHandler(
178*cdf0e10cSrcweir 				comphelper::getProcessServiceFactory()->createInstance(
179*cdf0e10cSrcweir 					rtl::OUString::createFromAscii( SC_SERVICE_INTHANDLER ) ),
180*cdf0e10cSrcweir 				uno::UNO_QUERY);
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 		uno::Reference<sdbc::XConnection> xConnection = xSource->connectWithCompletion( xHandler );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 		uno::Sequence<rtl::OUString> aNames;
185*cdf0e10cSrcweir 		if ( nSelect == DP_TYPELIST_TABLE )
186*cdf0e10cSrcweir 		{
187*cdf0e10cSrcweir 			//	get all tables
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 			uno::Reference<sdbcx::XTablesSupplier> xTablesSupp( xConnection, uno::UNO_QUERY );
190*cdf0e10cSrcweir 			if ( !xTablesSupp.is() ) return;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 			uno::Reference<container::XNameAccess> xTables = xTablesSupp->getTables();
193*cdf0e10cSrcweir 			if ( !xTables.is() ) return;
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 			aNames = xTables->getElementNames();
196*cdf0e10cSrcweir 		}
197*cdf0e10cSrcweir 		else
198*cdf0e10cSrcweir 		{
199*cdf0e10cSrcweir 			//	get all queries
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 			uno::Reference<sdb::XQueriesSupplier> xQueriesSupp( xConnection, uno::UNO_QUERY );
202*cdf0e10cSrcweir 			if ( !xQueriesSupp.is() ) return;
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 			uno::Reference<container::XNameAccess> xQueries = xQueriesSupp->getQueries();
205*cdf0e10cSrcweir 			if ( !xQueries.is() ) return;
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 			aNames = xQueries->getElementNames();
208*cdf0e10cSrcweir 		}
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 		//	fill list
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 		long nCount = aNames.getLength();
213*cdf0e10cSrcweir 		const rtl::OUString* pArray = aNames.getConstArray();
214*cdf0e10cSrcweir 		for( long nPos=0; nPos<nCount; nPos++ )
215*cdf0e10cSrcweir 		{
216*cdf0e10cSrcweir 			String aName = pArray[nPos];
217*cdf0e10cSrcweir 			aCbObject.InsertEntry( aName );
218*cdf0e10cSrcweir 		}
219*cdf0e10cSrcweir 	}
220*cdf0e10cSrcweir 	catch(uno::Exception&)
221*cdf0e10cSrcweir 	{
222*cdf0e10cSrcweir 		//	#71604# this may happen if an invalid database is selected -> no DBG_ERROR
223*cdf0e10cSrcweir 		DBG_WARNING("exception in database");
224*cdf0e10cSrcweir 	}
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 
230