1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b3f79822SAndrew Rist * distributed with this work for additional information
6*b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at
10*b3f79822SAndrew Rist *
11*b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist *
13*b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist * KIND, either express or implied. See the License for the
17*b3f79822SAndrew Rist * specific language governing permissions and limitations
18*b3f79822SAndrew Rist * under the License.
19*b3f79822SAndrew Rist *
20*b3f79822SAndrew Rist *************************************************************/
21*b3f79822SAndrew Rist
22*b3f79822SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #undef SC_DLLIMPLEMENTATION
28cdf0e10cSrcweir
29cdf0e10cSrcweir
30cdf0e10cSrcweir
31cdf0e10cSrcweir //------------------------------------------------------------------
32cdf0e10cSrcweir
33cdf0e10cSrcweir #include <tools/debug.hxx>
34cdf0e10cSrcweir #include <vcl/waitobj.hxx>
35cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
36cdf0e10cSrcweir
37cdf0e10cSrcweir #include <com/sun/star/sheet/DataImportMode.hpp>
38cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
40cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp>
41cdf0e10cSrcweir #include <com/sun/star/sdb/XCompletedConnection.hpp>
42cdf0e10cSrcweir
43cdf0e10cSrcweir using namespace com::sun::star;
44cdf0e10cSrcweir
45cdf0e10cSrcweir #include "dapidata.hxx"
46cdf0e10cSrcweir #include "scresid.hxx"
47cdf0e10cSrcweir #include "sc.hrc"
48cdf0e10cSrcweir #include "dapitype.hrc"
49cdf0e10cSrcweir #include "miscuno.hxx"
50cdf0e10cSrcweir #include "dpsdbtab.hxx" // ScImportSourceDesc
51cdf0e10cSrcweir
52cdf0e10cSrcweir //-------------------------------------------------------------------------
53cdf0e10cSrcweir
54cdf0e10cSrcweir #define DP_SERVICE_DBCONTEXT "com.sun.star.sdb.DatabaseContext"
55cdf0e10cSrcweir #define SC_SERVICE_INTHANDLER "com.sun.star.task.InteractionHandler"
56cdf0e10cSrcweir
57cdf0e10cSrcweir // entries in the "type" ListBox
58cdf0e10cSrcweir #define DP_TYPELIST_TABLE 0
59cdf0e10cSrcweir #define DP_TYPELIST_QUERY 1
60cdf0e10cSrcweir #define DP_TYPELIST_SQL 2
61cdf0e10cSrcweir #define DP_TYPELIST_SQLNAT 3
62cdf0e10cSrcweir
63cdf0e10cSrcweir //-------------------------------------------------------------------------
64cdf0e10cSrcweir
ScDataPilotDatabaseDlg(Window * pParent)65cdf0e10cSrcweir ScDataPilotDatabaseDlg::ScDataPilotDatabaseDlg( Window* pParent ) :
66cdf0e10cSrcweir ModalDialog ( pParent, ScResId( RID_SCDLG_DAPIDATA ) ),
67cdf0e10cSrcweir //
68cdf0e10cSrcweir aFlFrame ( this, ScResId( FL_FRAME ) ),
69cdf0e10cSrcweir aFtDatabase ( this, ScResId( FT_DATABASE ) ),
70cdf0e10cSrcweir aLbDatabase ( this, ScResId( LB_DATABASE ) ),
71cdf0e10cSrcweir aFtObject ( this, ScResId( FT_OBJECT ) ),
72cdf0e10cSrcweir aCbObject ( this, ScResId( CB_OBJECT ) ),
73cdf0e10cSrcweir aFtType ( this, ScResId( FT_OBJTYPE ) ),
74cdf0e10cSrcweir aLbType ( this, ScResId( LB_OBJTYPE ) ),
75cdf0e10cSrcweir aBtnOk ( this, ScResId( BTN_OK ) ),
76cdf0e10cSrcweir aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
77cdf0e10cSrcweir aBtnHelp ( this, ScResId( BTN_HELP ) )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir FreeResource();
80cdf0e10cSrcweir
81cdf0e10cSrcweir WaitObject aWait( this ); // initializing the database service the first time takes a while
82cdf0e10cSrcweir
83cdf0e10cSrcweir try
84cdf0e10cSrcweir {
85cdf0e10cSrcweir // get database names
86cdf0e10cSrcweir
87cdf0e10cSrcweir uno::Reference<container::XNameAccess> xContext(
88cdf0e10cSrcweir comphelper::getProcessServiceFactory()->createInstance(
89cdf0e10cSrcweir rtl::OUString::createFromAscii( DP_SERVICE_DBCONTEXT ) ),
90cdf0e10cSrcweir uno::UNO_QUERY);
91cdf0e10cSrcweir if (xContext.is())
92cdf0e10cSrcweir {
93cdf0e10cSrcweir uno::Sequence<rtl::OUString> aNames = xContext->getElementNames();
94cdf0e10cSrcweir long nCount = aNames.getLength();
95cdf0e10cSrcweir const rtl::OUString* pArray = aNames.getConstArray();
96cdf0e10cSrcweir for (long nPos = 0; nPos < nCount; nPos++)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir String aName = pArray[nPos];
99cdf0e10cSrcweir aLbDatabase.InsertEntry( aName );
100cdf0e10cSrcweir }
101cdf0e10cSrcweir }
102cdf0e10cSrcweir }
103cdf0e10cSrcweir catch(uno::Exception&)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir DBG_ERROR("exception in database");
106cdf0e10cSrcweir }
107cdf0e10cSrcweir
108cdf0e10cSrcweir aLbDatabase.SelectEntryPos( 0 );
109cdf0e10cSrcweir aLbType.SelectEntryPos( 0 );
110cdf0e10cSrcweir
111cdf0e10cSrcweir FillObjects();
112cdf0e10cSrcweir
113cdf0e10cSrcweir aLbDatabase.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
114cdf0e10cSrcweir aLbType.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
115cdf0e10cSrcweir }
116cdf0e10cSrcweir
~ScDataPilotDatabaseDlg()117cdf0e10cSrcweir ScDataPilotDatabaseDlg::~ScDataPilotDatabaseDlg()
118cdf0e10cSrcweir {
119cdf0e10cSrcweir }
120cdf0e10cSrcweir
GetValues(ScImportSourceDesc & rDesc)121cdf0e10cSrcweir void ScDataPilotDatabaseDlg::GetValues( ScImportSourceDesc& rDesc )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir sal_uInt16 nSelect = aLbType.GetSelectEntryPos();
124cdf0e10cSrcweir
125cdf0e10cSrcweir rDesc.aDBName = aLbDatabase.GetSelectEntry();
126cdf0e10cSrcweir rDesc.aObject = aCbObject.GetText();
127cdf0e10cSrcweir
128cdf0e10cSrcweir if ( !rDesc.aDBName.Len() || !rDesc.aObject.Len() )
129cdf0e10cSrcweir rDesc.nType = sheet::DataImportMode_NONE;
130cdf0e10cSrcweir else if ( nSelect == DP_TYPELIST_TABLE )
131cdf0e10cSrcweir rDesc.nType = sheet::DataImportMode_TABLE;
132cdf0e10cSrcweir else if ( nSelect == DP_TYPELIST_QUERY )
133cdf0e10cSrcweir rDesc.nType = sheet::DataImportMode_QUERY;
134cdf0e10cSrcweir else
135cdf0e10cSrcweir rDesc.nType = sheet::DataImportMode_SQL;
136cdf0e10cSrcweir
137cdf0e10cSrcweir rDesc.bNative = ( nSelect == DP_TYPELIST_SQLNAT );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir
IMPL_LINK(ScDataPilotDatabaseDlg,SelectHdl,ListBox *,EMPTYARG)140cdf0e10cSrcweir IMPL_LINK( ScDataPilotDatabaseDlg, SelectHdl, ListBox*, EMPTYARG )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir FillObjects();
143cdf0e10cSrcweir return 0;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
FillObjects()146cdf0e10cSrcweir void ScDataPilotDatabaseDlg::FillObjects()
147cdf0e10cSrcweir {
148cdf0e10cSrcweir aCbObject.Clear();
149cdf0e10cSrcweir
150cdf0e10cSrcweir String aDatabaseName = aLbDatabase.GetSelectEntry();
151cdf0e10cSrcweir if (!aDatabaseName.Len())
152cdf0e10cSrcweir return;
153cdf0e10cSrcweir
154cdf0e10cSrcweir sal_uInt16 nSelect = aLbType.GetSelectEntryPos();
155cdf0e10cSrcweir if ( nSelect > DP_TYPELIST_QUERY )
156cdf0e10cSrcweir return; // only tables and queries
157cdf0e10cSrcweir
158cdf0e10cSrcweir try
159cdf0e10cSrcweir {
160cdf0e10cSrcweir // open connection (for tables or queries)
161cdf0e10cSrcweir
162cdf0e10cSrcweir uno::Reference<container::XNameAccess> xContext(
163cdf0e10cSrcweir comphelper::getProcessServiceFactory()->createInstance(
164cdf0e10cSrcweir rtl::OUString::createFromAscii( DP_SERVICE_DBCONTEXT ) ),
165cdf0e10cSrcweir uno::UNO_QUERY);
166cdf0e10cSrcweir if ( !xContext.is() ) return;
167cdf0e10cSrcweir
168cdf0e10cSrcweir uno::Any aSourceAny = xContext->getByName( aDatabaseName );
169cdf0e10cSrcweir uno::Reference<sdb::XCompletedConnection> xSource(
170cdf0e10cSrcweir ScUnoHelpFunctions::AnyToInterface( aSourceAny ), uno::UNO_QUERY );
171cdf0e10cSrcweir if ( !xSource.is() ) return;
172cdf0e10cSrcweir
173cdf0e10cSrcweir uno::Reference<task::XInteractionHandler> xHandler(
174cdf0e10cSrcweir comphelper::getProcessServiceFactory()->createInstance(
175cdf0e10cSrcweir rtl::OUString::createFromAscii( SC_SERVICE_INTHANDLER ) ),
176cdf0e10cSrcweir uno::UNO_QUERY);
177cdf0e10cSrcweir
178cdf0e10cSrcweir uno::Reference<sdbc::XConnection> xConnection = xSource->connectWithCompletion( xHandler );
179cdf0e10cSrcweir
180cdf0e10cSrcweir uno::Sequence<rtl::OUString> aNames;
181cdf0e10cSrcweir if ( nSelect == DP_TYPELIST_TABLE )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir // get all tables
184cdf0e10cSrcweir
185cdf0e10cSrcweir uno::Reference<sdbcx::XTablesSupplier> xTablesSupp( xConnection, uno::UNO_QUERY );
186cdf0e10cSrcweir if ( !xTablesSupp.is() ) return;
187cdf0e10cSrcweir
188cdf0e10cSrcweir uno::Reference<container::XNameAccess> xTables = xTablesSupp->getTables();
189cdf0e10cSrcweir if ( !xTables.is() ) return;
190cdf0e10cSrcweir
191cdf0e10cSrcweir aNames = xTables->getElementNames();
192cdf0e10cSrcweir }
193cdf0e10cSrcweir else
194cdf0e10cSrcweir {
195cdf0e10cSrcweir // get all queries
196cdf0e10cSrcweir
197cdf0e10cSrcweir uno::Reference<sdb::XQueriesSupplier> xQueriesSupp( xConnection, uno::UNO_QUERY );
198cdf0e10cSrcweir if ( !xQueriesSupp.is() ) return;
199cdf0e10cSrcweir
200cdf0e10cSrcweir uno::Reference<container::XNameAccess> xQueries = xQueriesSupp->getQueries();
201cdf0e10cSrcweir if ( !xQueries.is() ) return;
202cdf0e10cSrcweir
203cdf0e10cSrcweir aNames = xQueries->getElementNames();
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir // fill list
207cdf0e10cSrcweir
208cdf0e10cSrcweir long nCount = aNames.getLength();
209cdf0e10cSrcweir const rtl::OUString* pArray = aNames.getConstArray();
210cdf0e10cSrcweir for( long nPos=0; nPos<nCount; nPos++ )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir String aName = pArray[nPos];
213cdf0e10cSrcweir aCbObject.InsertEntry( aName );
214cdf0e10cSrcweir }
215cdf0e10cSrcweir }
216cdf0e10cSrcweir catch(uno::Exception&)
217cdf0e10cSrcweir {
218cdf0e10cSrcweir // #71604# this may happen if an invalid database is selected -> no DBG_ERROR
219cdf0e10cSrcweir DBG_WARNING("exception in database");
220cdf0e10cSrcweir }
221cdf0e10cSrcweir }
222cdf0e10cSrcweir
223cdf0e10cSrcweir
224cdf0e10cSrcweir
225cdf0e10cSrcweir
226