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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBUI_TABLECOPYHELPER_HXX
31 #include "TableCopyHelper.hxx"
32 #endif
33 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
34 #include "dbustrings.hrc"
35 #endif
36 #ifndef _DBAUI_SQLMESSAGE_HXX_
37 #include "sqlmessage.hxx"
38 #endif
39 #ifndef _SV_MSGBOX_HXX
40 #include <vcl/msgbox.hxx>
41 #endif
42 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
43 #include "WCopyTable.hxx"
44 #endif
45 #ifndef DBAUI_GENERICCONTROLLER_HXX
46 #include "genericcontroller.hxx"
47 #endif
48 #ifndef DBAUI_WIZARD_CPAGE_HXX
49 #include "WCPage.hxx"
50 #endif
51 #ifndef _COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP_
52 #include <com/sun/star/task/XInteractionHandler.hpp>
53 #endif
54 #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYCOMPOSER_HPP_
55 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
56 #endif
57 #ifndef _COM_SUN_STAR_SDB_APPLICATION_COPYTABLEOPERATION_HPP_
58 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
59 #endif
60 #ifndef _COM_SUN_STAR_SDB_APPLICATION_COPYTABLEWIZARD_HPP_
61 #include <com/sun/star/sdb/application/CopyTableWizard.hpp>
62 #endif
63 #ifndef _COM_SUN_STAR_SDB_DATAACCESSDESCRIPTORFACTORY_HPP_
64 #include <com/sun/star/sdb/DataAccessDescriptorFactory.hpp>
65 #endif
66 
67 #ifndef DBAUI_RTFREADER_HXX
68 #include "RtfReader.hxx"
69 #endif
70 #ifndef DBAUI_HTMLREADER_HXX
71 #include "HtmlReader.hxx"
72 #endif
73 #ifndef DBAUI_TOKENWRITER_HXX
74 #include "TokenWriter.hxx"
75 #endif
76 #ifndef DBAUI_TOOLS_HXX
77 #include "UITools.hxx"
78 #endif
79 #ifndef DBAUI_DATAVIEW_HXX
80 #include "dataview.hxx"
81 #endif
82 #ifndef _DBU_RESOURCE_HRC_
83 #include "dbu_resource.hrc"
84 #endif
85 #ifndef _UNOTOOLS_UCBHELPER_HXX
86 #include <unotools/ucbhelper.hxx>
87 #endif
88 #ifndef _URLOBJ_HXX
89 #include <tools/urlobj.hxx>
90 #endif
91 #ifndef TOOLS_DIAGNOSE_EX_H
92 #include <tools/diagnose_ex.h>
93 #endif
94 #ifndef COMPHELPER_COMPONENTCONTEXT_HXX
95 #include <comphelper/componentcontext.hxx>
96 #endif
97 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
98 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
99 #endif
100 #ifndef _COM_SUN_STAR_SDBCX_XVIEWSSUPPLIER_HPP_
101 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
102 #endif
103 #ifndef _COM_SUN_STAR_SDB_XQUERYDEFINITIONSSUPPLIER_HPP_
104 #include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
105 #endif
106 #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
107 #include <com/sun/star/sdb/SQLContext.hpp>
108 #endif
109 #ifndef _COM_SUN_STAR_SDBC_XPARAMETERS_HPP_
110 #include <com/sun/star/sdbc/XParameters.hpp>
111 #endif
112 #ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATASUPPLIER_HPP_
113 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
114 #endif
115 #ifndef _COM_SUN_STAR_SDB_XQUERIESSUPPLIER_HPP_
116 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
117 #endif
118 #ifndef _COM_SUN_STAR_SDBC_XCOLUMNLOCATE_HPP_
119 #include <com/sun/star/sdbc/XColumnLocate.hpp>
120 #endif
121 #ifndef _COM_SUN_STAR_SDBCX_XROWLOCATE_HPP_
122 #include <com/sun/star/sdbcx/XRowLocate.hpp>
123 #endif
124 #ifndef _SV_WAITOBJ_HXX
125 #include <vcl/waitobj.hxx>
126 #endif
127 #ifndef _COM_SUN_STAR_SDB_XSQLQUERYCOMPOSERFACTORY_HPP_
128 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
129 #endif
130 #ifndef _UNOTOOLS_TEMPFILE_HXX
131 #include <unotools/tempfile.hxx>
132 #endif
133 #ifndef _CPPUHELPER_EXC_HLP_HXX_
134 #include <cppuhelper/exc_hlp.hxx>
135 #endif
136 
137 #ifndef DBAUI_DBEXCHANGE_HXX
138 #include "dbexchange.hxx"
139 #endif
140 #include <rtl/logfile.hxx>
141 //........................................................................
142 namespace dbaui
143 {
144 //........................................................................
145 using namespace ::dbtools;
146 using namespace ::svx;
147 using namespace ::com::sun::star::uno;
148 using namespace ::com::sun::star::task;
149 using namespace ::com::sun::star::beans;
150 using namespace ::com::sun::star::lang;
151 using namespace ::com::sun::star::container;
152 using namespace ::com::sun::star::sdb;
153 using namespace ::com::sun::star::sdb::application;
154 using namespace ::com::sun::star::sdbc;
155 using namespace ::com::sun::star::sdbcx;
156 using namespace ::com::sun::star::frame;
157 using namespace ::com::sun::star::ucb;
158 
159 // -----------------------------------------------------------------------------
160 OTableCopyHelper::OTableCopyHelper(OGenericUnoController* _pControler)
161     :m_pController(_pControler)
162 {
163 }
164 
165 // -----------------------------------------------------------------------------
166 void OTableCopyHelper::insertTable( const ::rtl::OUString& i_rSourceDataSource, const Reference<XConnection>& i_rSourceConnection,
167         const ::rtl::OUString& i_rCommand, const sal_Int32 i_nCommandType,
168         const Reference< XResultSet >& i_rSourceRows, const Sequence< Any >& i_rSelection, const sal_Bool i_bBookmarkSelection,
169         const ::rtl::OUString& i_rDestDataSource, const Reference<XConnection>& i_rDestConnection)
170 {
171     if ( CommandType::QUERY != i_nCommandType && CommandType::TABLE != i_nCommandType )
172     {
173 		DBG_ERROR( "OTableCopyHelper::insertTable: invalid call (no supported format found)!" );
174         return;
175     }
176 
177 	try
178 	{
179         Reference<XConnection> xSrcConnection( i_rSourceConnection );
180         if ( i_rSourceDataSource == i_rDestDataSource )
181             xSrcConnection = i_rDestConnection;
182 
183         if ( !xSrcConnection.is() || !i_rDestConnection.is() )
184         {
185             OSL_ENSURE( false, "OTableCopyHelper::insertTable: no connection/s!" );
186             return;
187         }
188 
189         ::comphelper::ComponentContext aContext( m_pController->getORB() );
190 
191         Reference< XDataAccessDescriptorFactory > xFactory( DataAccessDescriptorFactory::get( aContext.getUNOContext() ) );
192 
193         Reference< XPropertySet > xSource( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
194         xSource->setPropertyValue( PROPERTY_COMMAND_TYPE, makeAny( i_nCommandType ) );
195         xSource->setPropertyValue( PROPERTY_COMMAND, makeAny( i_rCommand ) );
196         xSource->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( xSrcConnection ) );
197         xSource->setPropertyValue( PROPERTY_RESULT_SET, makeAny( i_rSourceRows ) );
198         xSource->setPropertyValue( PROPERTY_SELECTION, makeAny( i_rSelection ) );
199         xSource->setPropertyValue( PROPERTY_BOOKMARK_SELECTION, makeAny( i_bBookmarkSelection ) );
200 
201         Reference< XPropertySet > xDest( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
202         xDest->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( i_rDestConnection ) );
203 
204         Reference< XCopyTableWizard > xWizard( CopyTableWizard::create( aContext.getUNOContext(), xSource, xDest ), UNO_SET_THROW );
205 
206         ::rtl::OUString sTableNameForAppend( GetTableNameForAppend() );
207         xWizard->setDestinationTableName( GetTableNameForAppend() );
208 
209         bool bAppendToExisting = ( sTableNameForAppend.getLength() != 0 );
210         xWizard->setOperation( bAppendToExisting ? CopyTableOperation::AppendData : CopyTableOperation::CopyDefinitionAndData );
211 
212         xWizard->execute();
213 	}
214 	catch( const SQLException& )
215     {
216         m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
217     }
218 	catch( const Exception& )
219 	{
220         DBG_UNHANDLED_EXCEPTION();
221 	}
222 }
223 
224 // -----------------------------------------------------------------------------
225 void OTableCopyHelper::pasteTable( const ::svx::ODataAccessDescriptor& _rPasteData, const ::rtl::OUString& i_rDestDataSourceName,
226                                   const SharedConnection& i_rDestConnection )
227 {
228     ::rtl::OUString sSrcDataSourceName = _rPasteData.getDataSource();
229 
230     ::rtl::OUString sCommand;
231     _rPasteData[ daCommand ] >>= sCommand;
232 
233     Reference<XConnection> xSrcConnection;
234     if ( _rPasteData.has(daConnection) )
235     {
236         OSL_VERIFY( _rPasteData[daConnection] >>= xSrcConnection );
237     }
238 
239     Reference< XResultSet > xResultSet;
240     if ( _rPasteData.has(daCursor) )
241     {
242         OSL_VERIFY( _rPasteData[ daCursor ] >>= xResultSet );
243     }
244 
245     Sequence< Any > aSelection;
246     if ( _rPasteData.has( daSelection ) )
247     {
248         OSL_VERIFY( _rPasteData[ daSelection ] >>= aSelection );
249         OSL_ENSURE( _rPasteData.has( daBookmarkSelection ), "OTableCopyHelper::pasteTable: you should specify BookmarkSelection, too, to be on the safe side!" );
250     }
251 
252 
253     sal_Bool bBookmarkSelection( sal_True );
254     if ( _rPasteData.has( daBookmarkSelection ) )
255     {
256         OSL_VERIFY( _rPasteData[ daBookmarkSelection ] >>= bBookmarkSelection );
257     }
258     OSL_ENSURE( bBookmarkSelection, "OTableCopyHelper::pasteTable: working with selection-indicies (instead of bookmarks) is error-prone, and thus deprecated!" );
259 
260     sal_Int32 nCommandType = CommandType::COMMAND;
261     if ( _rPasteData.has(daCommandType) )
262         _rPasteData[daCommandType] >>= nCommandType;
263 
264     insertTable( sSrcDataSourceName, xSrcConnection, sCommand, nCommandType,
265                  xResultSet, aSelection, bBookmarkSelection,
266                  i_rDestDataSourceName, i_rDestConnection );
267 }
268 
269 // -----------------------------------------------------------------------------
270 void OTableCopyHelper::pasteTable( SotFormatStringId _nFormatId
271                                   ,const TransferableDataHelper& _rTransData
272                                   ,const ::rtl::OUString& i_rDestDataSource
273                                   ,const SharedConnection& _xConnection)
274 {
275     if ( _nFormatId == SOT_FORMATSTR_ID_DBACCESS_TABLE || _nFormatId == SOT_FORMATSTR_ID_DBACCESS_QUERY )
276 	{
277 		if ( ODataAccessObjectTransferable::canExtractObjectDescriptor(_rTransData.GetDataFlavorExVector()) )
278 		{
279 			::svx::ODataAccessDescriptor aPasteData = ODataAccessObjectTransferable::extractObjectDescriptor(_rTransData);
280 			pasteTable( aPasteData,i_rDestDataSource,_xConnection);
281 		}
282 	}
283 	else if ( _rTransData.HasFormat(_nFormatId) )
284 	{
285 		try
286 		{
287 			DropDescriptor aTrans;
288 			if ( _nFormatId != SOT_FORMAT_RTF )
289 				const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SOT_FORMATSTR_ID_HTML ,aTrans.aHtmlRtfStorage);
290 			else
291 				const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SOT_FORMAT_RTF,aTrans.aHtmlRtfStorage);
292 
293 			aTrans.nType			= E_TABLE;
294 			aTrans.bHtml			= SOT_FORMATSTR_ID_HTML == _nFormatId;
295 			aTrans.sDefaultTableName = GetTableNameForAppend();
296 			if ( !copyTagTable(aTrans,sal_False,_xConnection) )
297 				m_pController->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE)),*m_pController,::rtl::OUString::createFromAscii("S1000") ,0,Any()));
298 		}
299         catch(const SQLException&)
300         {
301             m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
302         }
303         catch( const Exception& )
304         {
305             DBG_UNHANDLED_EXCEPTION();
306         }
307 	}
308 	else
309 		m_pController->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE)),*m_pController,::rtl::OUString::createFromAscii("S1000") ,0,Any()));
310 }
311 
312 // -----------------------------------------------------------------------------
313 void OTableCopyHelper::pasteTable( const TransferableDataHelper& _rTransData
314                                   ,const ::rtl::OUString& i_rDestDataSource
315                                   ,const SharedConnection& _xConnection)
316 {
317 	if ( _rTransData.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE) || _rTransData.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY) )
318 		pasteTable( SOT_FORMATSTR_ID_DBACCESS_TABLE,_rTransData,i_rDestDataSource,_xConnection);
319 	else if ( _rTransData.HasFormat(SOT_FORMATSTR_ID_HTML) )
320 		pasteTable( SOT_FORMATSTR_ID_HTML,_rTransData,i_rDestDataSource,_xConnection);
321 	else if ( _rTransData.HasFormat(SOT_FORMAT_RTF) )
322 		pasteTable( SOT_FORMAT_RTF,_rTransData,i_rDestDataSource,_xConnection);
323 }
324 
325 // -----------------------------------------------------------------------------
326 sal_Bool OTableCopyHelper::copyTagTable(OTableCopyHelper::DropDescriptor& _rDesc, sal_Bool _bCheck,const SharedConnection& _xConnection)
327 {
328     Reference<XEventListener> xEvt;
329 	ODatabaseImportExport* pImport = NULL;
330 	if ( _rDesc.bHtml )
331 		pImport = new OHTMLImportExport(_xConnection,getNumberFormatter(_xConnection,m_pController->getORB()),m_pController->getORB());
332 	else
333 		pImport = new ORTFImportExport(_xConnection,getNumberFormatter(_xConnection,m_pController->getORB()),m_pController->getORB());
334 
335 	xEvt = pImport;
336 	SvStream* pStream = (SvStream*)(SotStorageStream*)_rDesc.aHtmlRtfStorage;
337 	if ( _bCheck )
338 		pImport->enableCheckOnly();
339 
340 	//dyf add 20070601
341 	//set the selected tablename
342 	pImport->setSTableName(_rDesc.sDefaultTableName);
343 	//dyf add end
344 	pImport->setStream(pStream);
345 	return pImport->Read();
346 }
347 // -----------------------------------------------------------------------------
348 sal_Bool OTableCopyHelper::isTableFormat(const TransferableDataHelper& _rClipboard)  const
349 {
350 	sal_Bool bTableFormat	=	_rClipboard.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE)
351 				||	_rClipboard.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY)
352 				||	_rClipboard.HasFormat(SOT_FORMAT_RTF)
353 				||	_rClipboard.HasFormat(SOT_FORMATSTR_ID_HTML);
354 
355 	return bTableFormat;
356 }
357 // -----------------------------------------------------------------------------
358 sal_Bool OTableCopyHelper::copyTagTable(const TransferableDataHelper& _aDroppedData
359                                         ,DropDescriptor& _rAsyncDrop
360                                         ,const SharedConnection& _xConnection)
361 {
362     sal_Bool bRet = sal_False;
363     sal_Bool bHtml = _aDroppedData.HasFormat(SOT_FORMATSTR_ID_HTML);
364 	if ( bHtml || _aDroppedData.HasFormat(SOT_FORMAT_RTF) )
365 	{
366 		if ( bHtml )
367 			const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SOT_FORMATSTR_ID_HTML ,_rAsyncDrop.aHtmlRtfStorage);
368 		else
369 			const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SOT_FORMAT_RTF,_rAsyncDrop.aHtmlRtfStorage);
370 
371 		_rAsyncDrop.bHtml			= bHtml;
372 		_rAsyncDrop.bError			= !copyTagTable(_rAsyncDrop,sal_True,_xConnection);
373 
374         bRet = ( !_rAsyncDrop.bError && _rAsyncDrop.aHtmlRtfStorage.Is() );
375 		if ( bRet )
376 		{
377 			// now we need to copy the stream
378 			::utl::TempFile aTmp;
379 			aTmp.EnableKillingFile(sal_False);
380 			_rAsyncDrop.aUrl = aTmp.GetURL();
381 			SotStorageStreamRef aNew = new SotStorageStream( aTmp.GetFileName() );
382 			_rAsyncDrop.aHtmlRtfStorage->Seek(STREAM_SEEK_TO_BEGIN);
383 			_rAsyncDrop.aHtmlRtfStorage->CopyTo( aNew );
384 			aNew->Commit();
385 			_rAsyncDrop.aHtmlRtfStorage = aNew;
386 		}
387 		else
388 			_rAsyncDrop.aHtmlRtfStorage = NULL;
389 	}
390     return bRet;
391 }
392 // -----------------------------------------------------------------------------
393 void OTableCopyHelper::asyncCopyTagTable(  DropDescriptor& _rDesc
394                                 ,const ::rtl::OUString& i_rDestDataSource
395                                 ,const SharedConnection& _xConnection)
396 {
397     if ( _rDesc.aHtmlRtfStorage.Is() )
398 	{
399 		copyTagTable(_rDesc,sal_False,_xConnection);
400 		_rDesc.aHtmlRtfStorage = NULL;
401 		// we now have to delete the temp file created in executeDrop
402 		INetURLObject aURL;
403 		aURL.SetURL(_rDesc.aUrl);
404 		::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::NO_DECODE));
405 	}
406 	else if ( !_rDesc.bError )
407 		pasteTable(_rDesc.aDroppedData,i_rDestDataSource,_xConnection);
408 	else
409 		m_pController->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE)),*m_pController,::rtl::OUString::createFromAscii("S1000") ,0,Any()));
410 }
411 // -----------------------------------------------------------------------------
412 //........................................................................
413 }	// namespace dbaui
414 //........................................................................
415 
416