1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dapidata.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #undef SC_DLLIMPLEMENTATION
38 //------------------------------------------------------------------
40 #include <tools/debug.hxx>
41 #include <vcl/waitobj.hxx>
42 #include <comphelper/processfactory.hxx>
44 #include <com/sun/star/sheet/DataImportMode.hpp>
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
47 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
48 #include <com/sun/star/sdb/XCompletedConnection.hpp>
50 using namespace com::sun::star
;
52 #include "dapidata.hxx"
53 #include "scresid.hxx"
55 #include "dapitype.hrc"
56 #include "miscuno.hxx"
57 #include "dpsdbtab.hxx" // ScImportSourceDesc
59 //-------------------------------------------------------------------------
61 #define DP_SERVICE_DBCONTEXT "com.sun.star.sdb.DatabaseContext"
62 #define SC_SERVICE_INTHANDLER "com.sun.star.sdb.InteractionHandler"
64 // entries in the "type" ListBox
65 #define DP_TYPELIST_TABLE 0
66 #define DP_TYPELIST_QUERY 1
67 #define DP_TYPELIST_SQL 2
68 #define DP_TYPELIST_SQLNAT 3
70 //-------------------------------------------------------------------------
72 ScDataPilotDatabaseDlg::ScDataPilotDatabaseDlg( Window
* pParent
) :
73 ModalDialog ( pParent
, ScResId( RID_SCDLG_DAPIDATA
) ),
75 aFlFrame ( this, ScResId( FL_FRAME
) ),
76 aFtDatabase ( this, ScResId( FT_DATABASE
) ),
77 aLbDatabase ( this, ScResId( LB_DATABASE
) ),
78 aFtObject ( this, ScResId( FT_OBJECT
) ),
79 aCbObject ( this, ScResId( CB_OBJECT
) ),
80 aFtType ( this, ScResId( FT_OBJTYPE
) ),
81 aLbType ( this, ScResId( LB_OBJTYPE
) ),
82 aBtnOk ( this, ScResId( BTN_OK
) ),
83 aBtnCancel ( this, ScResId( BTN_CANCEL
) ),
84 aBtnHelp ( this, ScResId( BTN_HELP
) )
88 WaitObject
aWait( this ); // initializing the database service the first time takes a while
94 uno::Reference
<container::XNameAccess
> xContext(
95 comphelper::getProcessServiceFactory()->createInstance(
96 rtl::OUString::createFromAscii( DP_SERVICE_DBCONTEXT
) ),
100 uno::Sequence
<rtl::OUString
> aNames
= xContext
->getElementNames();
101 long nCount
= aNames
.getLength();
102 const rtl::OUString
* pArray
= aNames
.getConstArray();
103 for (long nPos
= 0; nPos
< nCount
; nPos
++)
105 String aName
= pArray
[nPos
];
106 aLbDatabase
.InsertEntry( aName
);
110 catch(uno::Exception
&)
112 DBG_ERROR("exception in database");
115 aLbDatabase
.SelectEntryPos( 0 );
116 aLbType
.SelectEntryPos( 0 );
120 aLbDatabase
.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg
, SelectHdl
) );
121 aLbType
.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg
, SelectHdl
) );
124 ScDataPilotDatabaseDlg::~ScDataPilotDatabaseDlg()
128 void ScDataPilotDatabaseDlg::GetValues( ScImportSourceDesc
& rDesc
)
130 USHORT nSelect
= aLbType
.GetSelectEntryPos();
132 rDesc
.aDBName
= aLbDatabase
.GetSelectEntry();
133 rDesc
.aObject
= aCbObject
.GetText();
135 if ( !rDesc
.aDBName
.Len() || !rDesc
.aObject
.Len() )
136 rDesc
.nType
= sheet::DataImportMode_NONE
;
137 else if ( nSelect
== DP_TYPELIST_TABLE
)
138 rDesc
.nType
= sheet::DataImportMode_TABLE
;
139 else if ( nSelect
== DP_TYPELIST_QUERY
)
140 rDesc
.nType
= sheet::DataImportMode_QUERY
;
142 rDesc
.nType
= sheet::DataImportMode_SQL
;
144 rDesc
.bNative
= ( nSelect
== DP_TYPELIST_SQLNAT
);
147 IMPL_LINK( ScDataPilotDatabaseDlg
, SelectHdl
, ListBox
*, EMPTYARG
)
153 void ScDataPilotDatabaseDlg::FillObjects()
157 String aDatabaseName
= aLbDatabase
.GetSelectEntry();
158 if (!aDatabaseName
.Len())
161 USHORT nSelect
= aLbType
.GetSelectEntryPos();
162 if ( nSelect
> DP_TYPELIST_QUERY
)
163 return; // only tables and queries
167 // open connection (for tables or queries)
169 uno::Reference
<container::XNameAccess
> xContext(
170 comphelper::getProcessServiceFactory()->createInstance(
171 rtl::OUString::createFromAscii( DP_SERVICE_DBCONTEXT
) ),
173 if ( !xContext
.is() ) return;
175 uno::Any aSourceAny
= xContext
->getByName( aDatabaseName
);
176 uno::Reference
<sdb::XCompletedConnection
> xSource(
177 ScUnoHelpFunctions::AnyToInterface( aSourceAny
), uno::UNO_QUERY
);
178 if ( !xSource
.is() ) return;
180 uno::Reference
<task::XInteractionHandler
> xHandler(
181 comphelper::getProcessServiceFactory()->createInstance(
182 rtl::OUString::createFromAscii( SC_SERVICE_INTHANDLER
) ),
185 uno::Reference
<sdbc::XConnection
> xConnection
= xSource
->connectWithCompletion( xHandler
);
187 uno::Sequence
<rtl::OUString
> aNames
;
188 if ( nSelect
== DP_TYPELIST_TABLE
)
192 uno::Reference
<sdbcx::XTablesSupplier
> xTablesSupp( xConnection
, uno::UNO_QUERY
);
193 if ( !xTablesSupp
.is() ) return;
195 uno::Reference
<container::XNameAccess
> xTables
= xTablesSupp
->getTables();
196 if ( !xTables
.is() ) return;
198 aNames
= xTables
->getElementNames();
204 uno::Reference
<sdb::XQueriesSupplier
> xQueriesSupp( xConnection
, uno::UNO_QUERY
);
205 if ( !xQueriesSupp
.is() ) return;
207 uno::Reference
<container::XNameAccess
> xQueries
= xQueriesSupp
->getQueries();
208 if ( !xQueries
.is() ) return;
210 aNames
= xQueries
->getElementNames();
215 long nCount
= aNames
.getLength();
216 const rtl::OUString
* pArray
= aNames
.getConstArray();
217 for( long nPos
=0; nPos
<nCount
; nPos
++ )
219 String aName
= pArray
[nPos
];
220 aCbObject
.InsertEntry( aName
);
223 catch(uno::Exception
&)
225 // #71604# this may happen if an invalid database is selected -> no DBG_ERROR
226 DBG_WARNING("exception in database");