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