xref: /trunk/main/dbaccess/source/ui/inc/WCopyTable.hxx (revision cdf0e10c)
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 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
29 #define DBAUI_WIZ_COPYTABLEDIALOG_HXX
30 
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/sdbc/XConnection.hpp>
33 #include <com/sun/star/sdbc/XResultSet.hpp>
34 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
35 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <comphelper/stl_types.hxx>
38 #include "TypeInfo.hxx"
39 #include <vcl/button.hxx>
40 #include <svtools/wizdlg.hxx>
41 #include "DExport.hxx"
42 #include "WTabPage.hxx"
43 #include "FieldDescriptions.hxx"
44 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
45 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
46 #include <com/sun/star/task/XInteractionHandler.hpp>
47 #include <vcl/lstbox.hxx>
48 #include <functional>
49 
50 namespace dbaui
51 {
52 
53 	typedef ::std::unary_function< ::rtl::OUString,bool> TColumnFindFunctorType;
54 	class TColumnFindFunctor : public TColumnFindFunctorType
55 	{
56 	public:
57 		virtual bool operator()(const ::rtl::OUString& _sColumnName) const = 0;
58 	};
59 
60 	class TExportColumnFindFunctor : public TColumnFindFunctor
61 	{
62 		ODatabaseExport::TColumns* m_pColumns;
63 	public:
64 		TExportColumnFindFunctor(ODatabaseExport::TColumns* _pColumns)
65 		{
66 			m_pColumns = _pColumns;
67 		}
68 		inline bool operator()(const ::rtl::OUString& _sColumnName)	const
69 		{
70 			return m_pColumns->find(_sColumnName) != m_pColumns->end();
71 		}
72 	};
73 
74 	class TMultiListBoxEntryFindFunctor : public TColumnFindFunctor
75 	{
76 		::comphelper::TStringMixEqualFunctor m_aCase;
77 		::std::vector< ::rtl::OUString>* m_pVector;
78 	public:
79 		TMultiListBoxEntryFindFunctor(::std::vector< ::rtl::OUString>* _pVector,
80 									const ::comphelper::TStringMixEqualFunctor& _aCase)
81 			:m_aCase(_aCase)
82 			,m_pVector(_pVector)
83 		{
84 		}
85 		inline bool operator()(const ::rtl::OUString& _sColumnName)	const
86 		{
87 			return ::std::find_if(m_pVector->begin(),m_pVector->end(),
88 				::std::bind2nd(m_aCase, _sColumnName)) != m_pVector->end();
89 		}
90     };
91 
92 	// ========================================================
93 	// ICopyTableSourceObject
94 	// ========================================================
95     /** interface to an object to copy to another DB, using the OCopyTableWizard
96 
97         when the wizard is used to copy an object to another DB, it usually requires
98         a sdbcx-level or sdb-level object (a css.sdbcx.Table or css.sdb.Query, that is).
99 
100         However, to also support copying tables from sdbc-level connections, we allow to
101         work with the object name only. This implies some less features (like copying the
102         UI settings of a table is not done), but still allows to copy definition and data.
103     */
104     class ICopyTableSourceObject
105     {
106     public:
107         /// retrieves the fully qualified name of the object to copy
108         virtual ::rtl::OUString     getQualifiedObjectName() const = 0;
109         /// determines whether the object is a view
110         virtual bool                isView() const = 0;
111         /** copies the UI settings of the object to the given target object. Might be
112             ignored by implementations which do not have Ui settings.
113         */
114         virtual void                copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const = 0;
115         /// retrieves the column names of the to-be-copied object
116         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
117                                     getColumnNames() const = 0;
118         /// retrieves the names of the primary keys of the to-be-copied object
119         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
120                                     getPrimaryKeyColumnNames() const = 0;
121         /// creates a OFieldDescription for the given column of the to-be-copied object
122         virtual OFieldDescription*  createFieldDescription( const ::rtl::OUString& _rColumnName ) const = 0;
123         /// returns the SELECT statement which can be used to retrieve the data of the to-be-copied object
124         virtual ::rtl::OUString     getSelectStatement() const = 0;
125 
126         /** copies the filter and sorting
127         *
128         * \return
129         */
130         virtual void                copyFilterAndSortingTo(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const = 0;
131 
132         /** returns the prepared statement which can be used to retrieve the data of the to-be-copied object
133 
134             The default implementation of this method will simply prepare a statement with the return value
135             of ->getSelectStatement.
136         */
137         virtual ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
138                                     getPreparedSelectStatement() const = 0;
139 
140         virtual ~ICopyTableSourceObject();
141     };
142 
143 	// ========================================================
144 	// ObjectCopySource
145 	// ========================================================
146     class ObjectCopySource : public ICopyTableSourceObject
147     {
148     private:
149         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >         m_xConnection;
150         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData >   m_xMetaData;
151         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >       m_xObject;
152         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo >   m_xObjectPSI;
153         ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >    m_xObjectColumns;
154 
155     public:
156         ObjectCopySource(
157             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
158             const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject
159         );
160 
161         // ICopyTableSourceObject overridables
162         virtual ::rtl::OUString     getQualifiedObjectName() const;
163         virtual bool                isView() const;
164         virtual void                copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const;
165         virtual void                copyFilterAndSortingTo(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const;
166         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
167                                     getColumnNames() const;
168         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
169                                     getPrimaryKeyColumnNames() const;
170         virtual OFieldDescription*  createFieldDescription( const ::rtl::OUString& _rColumnName ) const;
171         virtual ::rtl::OUString     getSelectStatement() const;
172         virtual ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
173                                     getPreparedSelectStatement() const;
174     };
175 
176 	// ========================================================
177 	// NamedTableCopySource
178 	// ========================================================
179     class NamedTableCopySource : public ICopyTableSourceObject
180     {
181     private:
182         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >         m_xConnection;
183         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData >   m_xMetaData;
184         ::rtl::OUString                                                                 m_sTableName;
185         ::rtl::OUString                                                                 m_sTableCatalog;
186         ::rtl::OUString                                                                 m_sTableSchema;
187         ::rtl::OUString                                                                 m_sTableBareName;
188         ::std::vector< OFieldDescription >                                              m_aColumnInfo;
189         ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >         m_xStatement;
190 
191     public:
192         NamedTableCopySource(
193             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
194             const ::rtl::OUString& _rTableName
195         );
196 
197         // ICopyTableSourceObject overridables
198         virtual ::rtl::OUString     getQualifiedObjectName() const;
199         virtual bool                isView() const;
200         virtual void                copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const;
201         virtual void                copyFilterAndSortingTo(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const;
202         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
203                                     getColumnNames() const;
204         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
205                                     getPrimaryKeyColumnNames() const;
206         virtual OFieldDescription*  createFieldDescription( const ::rtl::OUString& _rColumnName ) const;
207         virtual ::rtl::OUString     getSelectStatement() const;
208         virtual ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
209                                     getPreparedSelectStatement() const;
210 
211     private:
212         void    impl_ensureColumnInfo_throw();
213         ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
214                 impl_ensureStatement_throw();
215     };
216 
217 	// ========================================================
218 	// Wizard Dialog
219 	// ========================================================
220 	class OCopyTableWizard : public WizardDialog
221 	{
222 		friend class		OWizColumnSelect;
223 		friend class		OWizTypeSelect;
224 		friend class		OWizTypeSelectControl;
225 		friend class		OCopyTable;
226 		friend class		OWizNameMatching;
227 
228     public:
229 		DECLARE_STL_MAP(::rtl::OUString,::rtl::OUString,::comphelper::UStringMixLess,TNameMapping);
230 
231 		enum Wizard_Button_Style
232 		{
233 			WIZARD_NEXT,
234 			WIZARD_PREV,
235 			WIZARD_FINISH,
236 
237             WIZARD_NONE
238 		};
239 
240     private:
241 		ODatabaseExport::TColumns		m_vDestColumns; // contains the columns
242 		ODatabaseExport::TColumnVector	m_aDestVec;		// the order to insert the columns
243 		ODatabaseExport::TColumns		m_vSourceColumns;
244 		ODatabaseExport::TColumnVector	m_vSourceVec;
245 
246         HelpButton              m_pbHelp;
247 		CancelButton			m_pbCancel;
248 		PushButton				m_pbPrev;
249 		PushButton				m_pbNext;
250 		OKButton				m_pbFinish;
251 
252 		OTypeInfoMap				            m_aTypeInfo;
253 		::std::vector<OTypeInfoMap::iterator>   m_aTypeInfoIndex;
254 		OTypeInfoMap				            m_aDestTypeInfo;
255 		::std::vector<OTypeInfoMap::iterator>   m_aDestTypeInfoIndex;
256 		TNameMapping				            m_mNameMapping;
257 
258 		ODatabaseExport::TPositions	            m_vColumnPos;
259 		::std::vector<sal_Int32>	            m_vColumnTypes;
260 
261 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >			m_xDestConnection;
262 
263 		const ICopyTableSourceObject&                                                   m_rSourceObject;
264 
265 		::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >	m_xFormatter;
266 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory> m_xFactory;
267         ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler>  m_xInteractionHandler;
268 
269 		String					m_sTypeNames;		// these type names are the ones out of the resource file
270 		sal_uInt32				m_nPageCount;
271 		sal_Bool				m_bDeleteSourceColumns;
272         bool                    m_bInterConnectionCopy;    // are we copying between different connections?
273 
274         ::com::sun::star::lang::Locale	m_aLocale;
275 		::rtl::OUString					m_sName;	// for a table the name is composed
276 		::rtl::OUString 				m_sSourceName;
277 		::rtl::OUString					m_aKeyName;
278 		TOTypeInfoSP					m_pTypeInfo; // default type
279 		sal_Bool						m_bAddPKFirstTime;
280         sal_Int16                       m_nOperation;
281 		Wizard_Button_Style	            m_ePressed;
282 		sal_Bool			            m_bCreatePrimaryKeyColumn;
283         sal_Bool                        m_bUseHeaderLine;
284 
285     private:
286 		DECL_LINK( ImplPrevHdl	, PushButton* );
287 		DECL_LINK( ImplNextHdl	, PushButton* );
288 		DECL_LINK( ImplOKHdl	, OKButton* );
289 		DECL_LINK( ImplActivateHdl, WizardDialog* );
290 		sal_Bool CheckColumns(sal_Int32& _rnBreakPos);
291         void loadData( const ICopyTableSourceObject& _rSourceObject,
292                        ODatabaseExport::TColumns& _rColumns,
293                        ODatabaseExport::TColumnVector& _rColVector );
294         void construct();
295 		// need for table creation
296 		void appendColumns( ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XColumnsSupplier>& _rxColSup, const ODatabaseExport::TColumnVector* _pVec, sal_Bool _bKeyColumns = sal_False ) const;
297 		void appendKey(::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XKeysSupplier>& _rxSup,const ODatabaseExport::TColumnVector* _pVec) const;
298 		// checks if the type is supported in the destination database
299 		sal_Bool supportsType(sal_Int32 _nDataType,sal_Int32& _rNewDataType);
300 
301 		void    impl_loadSourceData();
302 
303 	public:
304 		// used for copy tables or queries
305 		OCopyTableWizard(
306             Window * pParent,
307             const ::rtl::OUString& _rDefaultName,
308             sal_Int16 _nOperation,
309 			const ICopyTableSourceObject&                                                           _rSourceObject,
310 			const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >&			_xSourceConnection,
311 			const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >&			_xConnection,
312             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB,
313             const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler>&   _xInteractionHandler
314         );
315 
316 		// used for importing rtf/html sources
317 		OCopyTableWizard(
318             Window* pParent,
319             const ::rtl::OUString& _rDefaultName,
320             sal_Int16 _nOperation,
321             const ODatabaseExport::TColumns& _rDestColumns,
322             const ODatabaseExport::TColumnVector& _rSourceColVec,
323             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
324             const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >&	_xFormatter,
325             TypeSelectionPageFactory _pTypeSelectionPageFactory,
326             SvStream& _rTypeSelectionPageArg,
327             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM
328         );
329 
330 		virtual ~OCopyTableWizard();
331 
332 		virtual long		DeactivatePage();
333 		OKButton&			GetOKButton() { return m_pbFinish; }
334 		Wizard_Button_Style GetPressedButton() const { return m_ePressed; }
335 		void				EnableButton(Wizard_Button_Style eStyle,sal_Bool bEnable);
336 		void				AddWizardPage(OWizardPage* pPage); // Page wird von OCopyTableWizard gel�scht
337 		void				RemoveWizardPage(OWizardPage* pPage); // Page goes again to user
338 		void				CheckButtons(); // checks which button can be disabled, enabled
339 
340 		// returns a vector where the position of a column and if the column is in the selection
341 		// when not the value is COLUMN_POSITION_NOT_FOUND == (sal_uInt32)-1
342 		ODatabaseExport::TPositions	GetColumnPositions()	const { return m_vColumnPos; }
343 		::std::vector<sal_Int32>	GetColumnTypes()		const { return m_vColumnTypes; }
344         sal_Bool                    UseHeaderLine()         const { return m_bUseHeaderLine; }
345         void                        setUseHeaderLine(sal_Bool _bUseHeaderLine) { m_bUseHeaderLine = _bUseHeaderLine; }
346 
347 		void insertColumn(sal_Int32 _nPos,OFieldDescription* _pField);
348 
349 		/** replaces a field description with another one. The name must not be known so far.
350 			@param	_nPos
351 				The pos inside the vector, 0 based.
352 			@param	_pField
353 				The field to set.
354 			@param	_sOldName
355 				The name of column to be replaced.
356 		*/
357 		void replaceColumn(sal_Int32 _nPos,OFieldDescription* _pField,const ::rtl::OUString& _sOldName);
358 
359 		/** returns whether a primary key should be created in the target database
360 		*/
361 		sal_Bool		shouldCreatePrimaryKey() const;
362         void            setCreatePrimaryKey( bool _bDoCreate, const ::rtl::OUString& _rSuggestedName );
363 
364         static bool     supportsPrimaryKey( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection );
365         bool            supportsPrimaryKey() const { return supportsPrimaryKey( m_xDestConnection ); }
366 
367         static bool     supportsViews( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection );
368         bool            supportsViews() const { return supportsViews( m_xDestConnection ); }
369 
370 		/** returns the name of the primary key
371 			@return
372 				The name of the primary key.
373 		*/
374 		::rtl::OUString	getPrimaryKeyName() const { return m_aKeyName; }
375 
376 		TOTypeInfoSP		getTypeInfo(sal_Int32 _nPos)		const { return m_aTypeInfoIndex[_nPos]->second; }
377 		const OTypeInfoMap*	getTypeInfo()						const { return &m_aTypeInfo; }
378 
379 		TOTypeInfoSP		getDestTypeInfo(sal_Int32 _nPos)	const { return m_aDestTypeInfoIndex[_nPos]->second; }
380 		const OTypeInfoMap*	getDestTypeInfo()					const { return &m_aDestTypeInfo; }
381 
382 		::com::sun::star::lang::Locale	GetLocale() const { return m_aLocale; }
383 		::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > GetFormatter() const { return m_xFormatter; }
384 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory> GetFactory() const { return m_xFactory; }
385 
386 		const ODatabaseExport::TColumns*		getSourceColumns() const{ return &m_vSourceColumns; }
387 		const ODatabaseExport::TColumnVector*	getSrcVector() const	{ return &m_vSourceVec; }
388 		ODatabaseExport::TColumns*				getDestColumns()		{ return &m_vDestColumns; }
389 		const ODatabaseExport::TColumnVector*	getDestVector() const	{ return &m_aDestVec; }
390 		::rtl::OUString	getName() const { return m_sName; }
391 
392 		/** clears the dest vectors
393 		*/
394 		void clearDestColumns();
395 
396 		::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > createTable();
397 		::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > createView() const;
398 		sal_Int32 getMaxColumnNameLength() const;
399 
400 		void setOperation( const sal_Int16 _nOperation );
401 		sal_Int16 getOperation() const;
402 
403 		::rtl::OUString convertColumnName(	const TColumnFindFunctor&	_rCmpFunctor,
404 											const ::rtl::OUString&	_sColumnName,
405 											const ::rtl::OUString&	_sExtraChars,
406 											sal_Int32				_nMaxNameLen);
407 		TOTypeInfoSP convertType(const TOTypeInfoSP&_pType,sal_Bool& _bNotConvert);
408 
409 		::rtl::OUString createUniqueName(const ::rtl::OUString& _sName);
410 
411 		// displays a error message that a column type is not supported
412 		void showColumnTypeNotSupported(const ::rtl::OUString& _rColumnName);
413 
414 		void removeColumnNameFromNameMap(const ::rtl::OUString& _sName);
415         void showError(const ::rtl::OUString& _sErrorMesage);
416         void showError(const ::com::sun::star::uno::Any& _aError);
417 	};
418 }
419 
420 #endif // DBAUI_WIZ_COPYTABLEDIALOG_HXX
421