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