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