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