update dev300-m58
[ooovba.git] / dbaccess / source / ui / misc / TableCopyHelper.cxx
blob4677aa9ed17c8f2f8cdca13fbbb4baf6bf362e52
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TableCopyHelper.cxx,v $
10 * $Revision: 1.11.32.2 $
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_dbaccess.hxx"
33 #ifndef DBUI_TABLECOPYHELPER_HXX
34 #include "TableCopyHelper.hxx"
35 #endif
36 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
37 #include "dbustrings.hrc"
38 #endif
39 #ifndef _DBAUI_SQLMESSAGE_HXX_
40 #include "sqlmessage.hxx"
41 #endif
42 #ifndef _SV_MSGBOX_HXX
43 #include <vcl/msgbox.hxx>
44 #endif
45 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
46 #include "WCopyTable.hxx"
47 #endif
48 #ifndef DBAUI_GENERICCONTROLLER_HXX
49 #include "genericcontroller.hxx"
50 #endif
51 #ifndef DBAUI_WIZARD_CPAGE_HXX
52 #include "WCPage.hxx"
53 #endif
54 #ifndef _COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP_
55 #include <com/sun/star/task/XInteractionHandler.hpp>
56 #endif
57 #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYCOMPOSER_HPP_
58 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
59 #endif
60 #ifndef _COM_SUN_STAR_SDB_APPLICATION_COPYTABLEOPERATION_HPP_
61 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
62 #endif
63 #ifndef _COM_SUN_STAR_SDB_APPLICATION_COPYTABLEWIZARD_HPP_
64 #include <com/sun/star/sdb/application/CopyTableWizard.hpp>
65 #endif
66 #ifndef _COM_SUN_STAR_SDB_DATAACCESSDESCRIPTORFACTORY_HPP_
67 #include <com/sun/star/sdb/DataAccessDescriptorFactory.hpp>
68 #endif
70 #ifndef DBAUI_RTFREADER_HXX
71 #include "RtfReader.hxx"
72 #endif
73 #ifndef DBAUI_HTMLREADER_HXX
74 #include "HtmlReader.hxx"
75 #endif
76 #ifndef DBAUI_TOKENWRITER_HXX
77 #include "TokenWriter.hxx"
78 #endif
79 #ifndef DBAUI_TOOLS_HXX
80 #include "UITools.hxx"
81 #endif
82 #ifndef DBAUI_DATAVIEW_HXX
83 #include "dataview.hxx"
84 #endif
85 #ifndef _DBU_RESOURCE_HRC_
86 #include "dbu_resource.hrc"
87 #endif
88 #ifndef _UNOTOOLS_UCBHELPER_HXX
89 #include <unotools/ucbhelper.hxx>
90 #endif
91 #ifndef _URLOBJ_HXX
92 #include <tools/urlobj.hxx>
93 #endif
94 #ifndef TOOLS_DIAGNOSE_EX_H
95 #include <tools/diagnose_ex.h>
96 #endif
97 #ifndef COMPHELPER_COMPONENTCONTEXT_HXX
98 #include <comphelper/componentcontext.hxx>
99 #endif
100 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
101 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
102 #endif
103 #ifndef _COM_SUN_STAR_SDBCX_XVIEWSSUPPLIER_HPP_
104 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
105 #endif
106 #ifndef _COM_SUN_STAR_SDB_XQUERYDEFINITIONSSUPPLIER_HPP_
107 #include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
108 #endif
109 #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
110 #include <com/sun/star/sdb/SQLContext.hpp>
111 #endif
112 #ifndef _COM_SUN_STAR_SDBC_XPARAMETERS_HPP_
113 #include <com/sun/star/sdbc/XParameters.hpp>
114 #endif
115 #ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATASUPPLIER_HPP_
116 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
117 #endif
118 #ifndef _COM_SUN_STAR_SDB_XQUERIESSUPPLIER_HPP_
119 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
120 #endif
121 #ifndef _COM_SUN_STAR_SDBC_XCOLUMNLOCATE_HPP_
122 #include <com/sun/star/sdbc/XColumnLocate.hpp>
123 #endif
124 #ifndef _COM_SUN_STAR_SDBCX_XROWLOCATE_HPP_
125 #include <com/sun/star/sdbcx/XRowLocate.hpp>
126 #endif
127 #ifndef _SV_WAITOBJ_HXX
128 #include <vcl/waitobj.hxx>
129 #endif
130 #ifndef _COM_SUN_STAR_SDB_XSQLQUERYCOMPOSERFACTORY_HPP_
131 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
132 #endif
133 #ifndef _UNOTOOLS_TEMPFILE_HXX
134 #include <unotools/tempfile.hxx>
135 #endif
136 #ifndef _CPPUHELPER_EXC_HLP_HXX_
137 #include <cppuhelper/exc_hlp.hxx>
138 #endif
140 #ifndef DBAUI_DBEXCHANGE_HXX
141 #include "dbexchange.hxx"
142 #endif
143 #include <rtl/logfile.hxx>
144 //........................................................................
145 namespace dbaui
147 //........................................................................
148 using namespace ::dbtools;
149 using namespace ::svx;
150 using namespace ::com::sun::star::uno;
151 using namespace ::com::sun::star::task;
152 using namespace ::com::sun::star::beans;
153 using namespace ::com::sun::star::lang;
154 using namespace ::com::sun::star::container;
155 using namespace ::com::sun::star::sdb;
156 using namespace ::com::sun::star::sdb::application;
157 using namespace ::com::sun::star::sdbc;
158 using namespace ::com::sun::star::sdbcx;
159 using namespace ::com::sun::star::frame;
160 using namespace ::com::sun::star::ucb;
162 // -----------------------------------------------------------------------------
163 OTableCopyHelper::OTableCopyHelper(OGenericUnoController* _pControler)
164 :m_pController(_pControler)
166 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OTableCopyHelper::OTableCopyHelper" );
169 // -----------------------------------------------------------------------------
170 void OTableCopyHelper::insertTable(sal_Int32 _nCommandType
171 ,const Reference<XConnection>& _xSrcConnection
172 ,const Sequence< Any >& _aSelection
173 ,sal_Bool _bBookmarkSelection
174 ,const ::rtl::OUString& _sCommand
175 ,const ::rtl::OUString& _sSrcDataSourceName
176 ,const ::rtl::OUString& _sDestDataSourceName
177 ,const Reference<XConnection>& _xDestConnection)
179 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OTableCopyHelper::insertTable" );
180 if ( CommandType::QUERY != _nCommandType && CommandType::TABLE != _nCommandType )
182 DBG_ERROR( "OTableCopyHelper::insertTable: invalid call (no supported format found)!" );
183 return;
188 Reference<XConnection> xSrcConnection( _xSrcConnection );
189 if ( _sSrcDataSourceName == _sDestDataSourceName )
190 xSrcConnection = _xDestConnection;
192 if ( !xSrcConnection.is() || !_xDestConnection.is() )
194 OSL_ENSURE( false, "OTableCopyHelper::insertTable: no connection/s!" );
195 return;
198 ::comphelper::ComponentContext aContext( m_pController->getORB() );
200 Reference< XDataAccessDescriptorFactory > xFactory( DataAccessDescriptorFactory::get( aContext.getUNOContext() ) );
202 Reference< XPropertySet > xSource( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
203 xSource->setPropertyValue( PROPERTY_COMMAND_TYPE, makeAny( _nCommandType ) );
204 xSource->setPropertyValue( PROPERTY_COMMAND, makeAny( _sCommand ) );
205 xSource->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( xSrcConnection ) );
206 xSource->setPropertyValue( PROPERTY_SELECTION, makeAny( _aSelection ) );
207 xSource->setPropertyValue( PROPERTY_BOOKMARK_SELECTION, makeAny( _bBookmarkSelection ) );
209 Reference< XPropertySet > xDest( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
210 xDest->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( _xDestConnection ) );
212 Reference< XCopyTableWizard > xWizard( CopyTableWizard::create( aContext.getUNOContext(), xSource, xDest ), UNO_SET_THROW );
214 ::rtl::OUString sTableNameForAppend( GetTableNameForAppend() );
215 xWizard->setDestinationTableName( GetTableNameForAppend() );
217 bool bAppendToExisting = ( sTableNameForAppend.getLength() != 0 );
218 xWizard->setOperation( bAppendToExisting ? CopyTableOperation::AppendData : CopyTableOperation::CopyDefinitionAndData );
220 xWizard->execute();
222 catch( const SQLException& )
224 m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
226 catch( const Exception& )
228 DBG_UNHANDLED_EXCEPTION();
232 // -----------------------------------------------------------------------------
233 void OTableCopyHelper::pasteTable( const ::svx::ODataAccessDescriptor& _rPasteData, const ::rtl::OUString& _sDestDataSourceName,
234 const SharedConnection& _xDestConnection )
236 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OTableCopyHelper::pasteTable" );
237 Reference<XConnection> xSrcConnection;
238 ::rtl::OUString sCommand,
239 sSrcDataSourceName = _rPasteData.getDataSource();
241 _rPasteData[daCommand] >>= sCommand;
242 if ( _rPasteData.has(daConnection) )
243 _rPasteData[daConnection] >>= xSrcConnection;
244 #if OSL_DEBUG_LEVEL > 0
245 if ( _rPasteData.has(daCursor) )
247 Reference< XResultSet > xSrcRs;
248 _rPasteData[daCursor] >>= xSrcRs;
249 OSL_ENSURE( !xSrcRs.is(), "OTableCopyHelper::pasteTable: source result set not supported anymore!" );
250 // There was a time where we supported passing a result set as shortcut to the source
251 // object. That is, we do not need to create an own result set we already have one.
252 // Since we UNOized the Copy Table Wizard (#i81658#), we removed this support, since it
253 // contradicted the semantics of DataAccessDescriptor.ResultSet.
255 // This shouldn't be a problem, since there seems to be no client which actually
256 // passed a result set here.
257 // However, if there still is, we probably need to introduce an (undocumented?) property
258 // at the DataAccessDescriptor, which takes this "source result set".
261 if ( _rPasteData.has( daSelection ) || _rPasteData.has( daBookmarkSelection ) )
263 OSL_ENSURE( false, "OTableCopyHelper::pasteTable: bookmark/selection not supported anymore!" );
264 // similar notes here: Selection and BookmarkSelection are not supported in the UNOized
265 // copy table wizard anymore (it doesn't make sense without support for a source result set),
266 // and there seem to be no clients which actually use it. So, instead of implementing an
267 // unused case, we dropped this here.
269 #endif
271 // paste into the tables
272 sal_Int32 nCommandType = CommandType::COMMAND;
273 if ( _rPasteData.has(daCommandType) )
274 _rPasteData[daCommandType] >>= nCommandType;
276 insertTable( nCommandType
277 ,xSrcConnection
278 ,Sequence< Any >()
279 ,sal_False
280 ,sCommand
281 ,sSrcDataSourceName
282 ,_sDestDataSourceName
283 ,_xDestConnection);
286 // -----------------------------------------------------------------------------
287 void OTableCopyHelper::pasteTable( SotFormatStringId _nFormatId
288 ,const TransferableDataHelper& _rTransData
289 ,const ::rtl::OUString& _sDestDataSourceName
290 ,const SharedConnection& _xConnection)
292 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OTableCopyHelper::pasteTable" );
293 if ( _nFormatId == SOT_FORMATSTR_ID_DBACCESS_TABLE || _nFormatId == SOT_FORMATSTR_ID_DBACCESS_QUERY )
295 if ( ODataAccessObjectTransferable::canExtractObjectDescriptor(_rTransData.GetDataFlavorExVector()) )
297 ::svx::ODataAccessDescriptor aPasteData = ODataAccessObjectTransferable::extractObjectDescriptor(_rTransData);
298 pasteTable( aPasteData,_sDestDataSourceName,_xConnection);
301 else if ( _rTransData.HasFormat(_nFormatId) )
305 DropDescriptor aTrans;
306 if ( _nFormatId != SOT_FORMAT_RTF )
307 const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SOT_FORMATSTR_ID_HTML ,aTrans.aHtmlRtfStorage);
308 else
309 const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SOT_FORMAT_RTF,aTrans.aHtmlRtfStorage);
311 aTrans.nType = E_TABLE;
312 aTrans.bHtml = SOT_FORMATSTR_ID_HTML == _nFormatId;
313 aTrans.sDefaultTableName = GetTableNameForAppend();
314 if ( !copyTagTable(aTrans,sal_False,_xConnection) )
315 m_pController->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE)),*m_pController,::rtl::OUString::createFromAscii("S1000") ,0,Any()));
317 catch(const SQLException&)
319 m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
321 catch( const Exception& )
323 DBG_UNHANDLED_EXCEPTION();
326 else
327 m_pController->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE)),*m_pController,::rtl::OUString::createFromAscii("S1000") ,0,Any()));
330 // -----------------------------------------------------------------------------
331 void OTableCopyHelper::pasteTable( const TransferableDataHelper& _rTransData
332 ,const ::rtl::OUString& _sDestDataSourceName
333 ,const SharedConnection& _xConnection)
335 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OTableCopyHelper::pasteTable" );
336 if ( _rTransData.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE) || _rTransData.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY) )
337 pasteTable( SOT_FORMATSTR_ID_DBACCESS_TABLE,_rTransData,_sDestDataSourceName,_xConnection);
338 else if ( _rTransData.HasFormat(SOT_FORMATSTR_ID_HTML) )
339 pasteTable( SOT_FORMATSTR_ID_HTML,_rTransData,_sDestDataSourceName,_xConnection);
340 else if ( _rTransData.HasFormat(SOT_FORMAT_RTF) )
341 pasteTable( SOT_FORMAT_RTF,_rTransData,_sDestDataSourceName,_xConnection);
344 // -----------------------------------------------------------------------------
345 sal_Bool OTableCopyHelper::copyTagTable(OTableCopyHelper::DropDescriptor& _rDesc, sal_Bool _bCheck,const SharedConnection& _xConnection)
347 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OTableCopyHelper::copyTagTable" );
348 Reference<XEventListener> xEvt;
349 ODatabaseImportExport* pImport = NULL;
350 if ( _rDesc.bHtml )
351 pImport = new OHTMLImportExport(_xConnection,getNumberFormatter(_xConnection,m_pController->getORB()),m_pController->getORB());
352 else
353 pImport = new ORTFImportExport(_xConnection,getNumberFormatter(_xConnection,m_pController->getORB()),m_pController->getORB());
355 xEvt = pImport;
356 SvStream* pStream = (SvStream*)(SotStorageStream*)_rDesc.aHtmlRtfStorage;
357 if ( _bCheck )
358 pImport->enableCheckOnly();
360 //dyf add 20070601
361 //set the selected tablename
362 pImport->setSTableName(_rDesc.sDefaultTableName);
363 //dyf add end
364 pImport->setStream(pStream);
365 return pImport->Read();
367 // -----------------------------------------------------------------------------
368 sal_Bool OTableCopyHelper::isTableFormat(const TransferableDataHelper& _rClipboard) const
370 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OTableCopyHelper::isTableFormat" );
371 sal_Bool bTableFormat = _rClipboard.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE)
372 || _rClipboard.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY)
373 || _rClipboard.HasFormat(SOT_FORMAT_RTF)
374 || _rClipboard.HasFormat(SOT_FORMATSTR_ID_HTML);
376 return bTableFormat;
378 // -----------------------------------------------------------------------------
379 sal_Bool OTableCopyHelper::copyTagTable(const TransferableDataHelper& _aDroppedData
380 ,DropDescriptor& _rAsyncDrop
381 ,const SharedConnection& _xConnection)
383 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OTableCopyHelper::copyTagTable" );
384 sal_Bool bRet = sal_False;
385 sal_Bool bHtml = _aDroppedData.HasFormat(SOT_FORMATSTR_ID_HTML);
386 if ( bHtml || _aDroppedData.HasFormat(SOT_FORMAT_RTF) )
388 if ( bHtml )
389 const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SOT_FORMATSTR_ID_HTML ,_rAsyncDrop.aHtmlRtfStorage);
390 else
391 const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SOT_FORMAT_RTF,_rAsyncDrop.aHtmlRtfStorage);
393 _rAsyncDrop.bHtml = bHtml;
394 _rAsyncDrop.bError = !copyTagTable(_rAsyncDrop,sal_True,_xConnection);
396 bRet = ( !_rAsyncDrop.bError && _rAsyncDrop.aHtmlRtfStorage.Is() );
397 if ( bRet )
399 // now we need to copy the stream
400 ::utl::TempFile aTmp;
401 aTmp.EnableKillingFile(sal_False);
402 _rAsyncDrop.aUrl = aTmp.GetURL();
403 SotStorageStreamRef aNew = new SotStorageStream( aTmp.GetFileName() );
404 _rAsyncDrop.aHtmlRtfStorage->Seek(STREAM_SEEK_TO_BEGIN);
405 _rAsyncDrop.aHtmlRtfStorage->CopyTo( aNew );
406 aNew->Commit();
407 _rAsyncDrop.aHtmlRtfStorage = aNew;
409 else
410 _rAsyncDrop.aHtmlRtfStorage = NULL;
412 return bRet;
414 // -----------------------------------------------------------------------------
415 void OTableCopyHelper::asyncCopyTagTable( DropDescriptor& _rDesc
416 ,const ::rtl::OUString& _sDestDataSourceName
417 ,const SharedConnection& _xConnection)
419 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OTableCopyHelper::asyncCopyTagTable" );
420 if ( _rDesc.aHtmlRtfStorage.Is() )
422 copyTagTable(_rDesc,sal_False,_xConnection);
423 _rDesc.aHtmlRtfStorage = NULL;
424 // we now have to delete the temp file created in executeDrop
425 INetURLObject aURL;
426 aURL.SetURL(_rDesc.aUrl);
427 ::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::NO_DECODE));
429 else if ( !_rDesc.bError )
430 pasteTable(_rDesc.aDroppedData,_sDestDataSourceName,_xConnection);
431 else
432 m_pController->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE)),*m_pController,::rtl::OUString::createFromAscii("S1000") ,0,Any()));
434 // -----------------------------------------------------------------------------
435 //........................................................................
436 } // namespace dbaui
437 //........................................................................