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