xref: /trunk/main/sc/source/ui/dbgui/dapidata.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_sc.hxx"
30 
31 #undef SC_DLLIMPLEMENTATION
32 
33 
34 
35 //------------------------------------------------------------------
36 
37 #include <tools/debug.hxx>
38 #include <vcl/waitobj.hxx>
39 #include <comphelper/processfactory.hxx>
40 
41 #include <com/sun/star/sheet/DataImportMode.hpp>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
44 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
45 #include <com/sun/star/sdb/XCompletedConnection.hpp>
46 
47 using namespace com::sun::star;
48 
49 #include "dapidata.hxx"
50 #include "scresid.hxx"
51 #include "sc.hrc"
52 #include "dapitype.hrc"
53 #include "miscuno.hxx"
54 #include "dpsdbtab.hxx"         // ScImportSourceDesc
55 
56 //-------------------------------------------------------------------------
57 
58 #define DP_SERVICE_DBCONTEXT        "com.sun.star.sdb.DatabaseContext"
59 #define SC_SERVICE_INTHANDLER       "com.sun.star.task.InteractionHandler"
60 
61 //  entries in the "type" ListBox
62 #define DP_TYPELIST_TABLE   0
63 #define DP_TYPELIST_QUERY   1
64 #define DP_TYPELIST_SQL     2
65 #define DP_TYPELIST_SQLNAT  3
66 
67 //-------------------------------------------------------------------------
68 
69 ScDataPilotDatabaseDlg::ScDataPilotDatabaseDlg( Window* pParent ) :
70     ModalDialog     ( pParent, ScResId( RID_SCDLG_DAPIDATA ) ),
71     //
72     aFlFrame        ( this, ScResId( FL_FRAME ) ),
73     aFtDatabase     ( this, ScResId( FT_DATABASE ) ),
74     aLbDatabase     ( this, ScResId( LB_DATABASE ) ),
75     aFtObject       ( this, ScResId( FT_OBJECT ) ),
76     aCbObject       ( this, ScResId( CB_OBJECT ) ),
77     aFtType         ( this, ScResId( FT_OBJTYPE ) ),
78     aLbType         ( this, ScResId( LB_OBJTYPE ) ),
79     aBtnOk          ( this, ScResId( BTN_OK ) ),
80     aBtnCancel      ( this, ScResId( BTN_CANCEL ) ),
81     aBtnHelp        ( this, ScResId( BTN_HELP ) )
82 {
83     FreeResource();
84 
85     WaitObject aWait( this );       // initializing the database service the first time takes a while
86 
87     try
88     {
89         //  get database names
90 
91         uno::Reference<container::XNameAccess> xContext(
92                 comphelper::getProcessServiceFactory()->createInstance(
93                     rtl::OUString::createFromAscii( DP_SERVICE_DBCONTEXT ) ),
94                 uno::UNO_QUERY);
95         if (xContext.is())
96         {
97             uno::Sequence<rtl::OUString> aNames = xContext->getElementNames();
98             long nCount = aNames.getLength();
99             const rtl::OUString* pArray = aNames.getConstArray();
100             for (long nPos = 0; nPos < nCount; nPos++)
101             {
102                 String aName = pArray[nPos];
103                 aLbDatabase.InsertEntry( aName );
104             }
105         }
106     }
107     catch(uno::Exception&)
108     {
109         DBG_ERROR("exception in database");
110     }
111 
112     aLbDatabase.SelectEntryPos( 0 );
113     aLbType.SelectEntryPos( 0 );
114 
115     FillObjects();
116 
117     aLbDatabase.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
118     aLbType.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
119 }
120 
121 ScDataPilotDatabaseDlg::~ScDataPilotDatabaseDlg()
122 {
123 }
124 
125 void ScDataPilotDatabaseDlg::GetValues( ScImportSourceDesc& rDesc )
126 {
127     sal_uInt16 nSelect = aLbType.GetSelectEntryPos();
128 
129     rDesc.aDBName = aLbDatabase.GetSelectEntry();
130     rDesc.aObject = aCbObject.GetText();
131 
132     if ( !rDesc.aDBName.Len() || !rDesc.aObject.Len() )
133         rDesc.nType = sheet::DataImportMode_NONE;
134     else if ( nSelect == DP_TYPELIST_TABLE )
135         rDesc.nType = sheet::DataImportMode_TABLE;
136     else if ( nSelect == DP_TYPELIST_QUERY )
137         rDesc.nType = sheet::DataImportMode_QUERY;
138     else
139         rDesc.nType = sheet::DataImportMode_SQL;
140 
141     rDesc.bNative = ( nSelect == DP_TYPELIST_SQLNAT );
142 }
143 
144 IMPL_LINK( ScDataPilotDatabaseDlg, SelectHdl, ListBox*, EMPTYARG )
145 {
146     FillObjects();
147     return 0;
148 }
149 
150 void ScDataPilotDatabaseDlg::FillObjects()
151 {
152     aCbObject.Clear();
153 
154     String aDatabaseName = aLbDatabase.GetSelectEntry();
155     if (!aDatabaseName.Len())
156         return;
157 
158     sal_uInt16 nSelect = aLbType.GetSelectEntryPos();
159     if ( nSelect > DP_TYPELIST_QUERY )
160         return;                                 // only tables and queries
161 
162     try
163     {
164         //  open connection (for tables or queries)
165 
166         uno::Reference<container::XNameAccess> xContext(
167                 comphelper::getProcessServiceFactory()->createInstance(
168                     rtl::OUString::createFromAscii( DP_SERVICE_DBCONTEXT ) ),
169                 uno::UNO_QUERY);
170         if ( !xContext.is() ) return;
171 
172         uno::Any aSourceAny = xContext->getByName( aDatabaseName );
173         uno::Reference<sdb::XCompletedConnection> xSource(
174                 ScUnoHelpFunctions::AnyToInterface( aSourceAny ), uno::UNO_QUERY );
175         if ( !xSource.is() ) return;
176 
177         uno::Reference<task::XInteractionHandler> xHandler(
178                 comphelper::getProcessServiceFactory()->createInstance(
179                     rtl::OUString::createFromAscii( SC_SERVICE_INTHANDLER ) ),
180                 uno::UNO_QUERY);
181 
182         uno::Reference<sdbc::XConnection> xConnection = xSource->connectWithCompletion( xHandler );
183 
184         uno::Sequence<rtl::OUString> aNames;
185         if ( nSelect == DP_TYPELIST_TABLE )
186         {
187             //  get all tables
188 
189             uno::Reference<sdbcx::XTablesSupplier> xTablesSupp( xConnection, uno::UNO_QUERY );
190             if ( !xTablesSupp.is() ) return;
191 
192             uno::Reference<container::XNameAccess> xTables = xTablesSupp->getTables();
193             if ( !xTables.is() ) return;
194 
195             aNames = xTables->getElementNames();
196         }
197         else
198         {
199             //  get all queries
200 
201             uno::Reference<sdb::XQueriesSupplier> xQueriesSupp( xConnection, uno::UNO_QUERY );
202             if ( !xQueriesSupp.is() ) return;
203 
204             uno::Reference<container::XNameAccess> xQueries = xQueriesSupp->getQueries();
205             if ( !xQueries.is() ) return;
206 
207             aNames = xQueries->getElementNames();
208         }
209 
210         //  fill list
211 
212         long nCount = aNames.getLength();
213         const rtl::OUString* pArray = aNames.getConstArray();
214         for( long nPos=0; nPos<nCount; nPos++ )
215         {
216             String aName = pArray[nPos];
217             aCbObject.InsertEntry( aName );
218         }
219     }
220     catch(uno::Exception&)
221     {
222         //  #71604# this may happen if an invalid database is selected -> no DBG_ERROR
223         DBG_WARNING("exception in database");
224     }
225 }
226 
227 
228 
229 
230