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