Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / TableCopyHelper.cxx
blob1b14e462b7c929fcd90896d73726824dd0eff5e3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "TableCopyHelper.hxx"
21 #include "dbustrings.hrc"
22 #include "sqlmessage.hxx"
23 #include <vcl/msgbox.hxx>
24 #include "WCopyTable.hxx"
25 #include <dbaccess/genericcontroller.hxx>
26 #include "WCPage.hxx"
27 #include <com/sun/star/task/XInteractionHandler.hpp>
28 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
29 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
30 #include <com/sun/star/sdb/application/CopyTableWizard.hpp>
31 #include <com/sun/star/sdb/DataAccessDescriptorFactory.hpp>
33 #include "RtfReader.hxx"
34 #include "HtmlReader.hxx"
35 #include "TokenWriter.hxx"
36 #include "UITools.hxx"
37 #include <dbaccess/dataview.hxx>
38 #include "dbu_resource.hrc"
39 #include <unotools/ucbhelper.hxx>
40 #include <tools/urlobj.hxx>
41 #include <tools/diagnose_ex.h>
42 #include <comphelper/processfactory.hxx>
43 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
44 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
45 #include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
46 #include <com/sun/star/sdb/SQLContext.hpp>
47 #include <com/sun/star/sdbc/XParameters.hpp>
48 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
49 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
50 #include <com/sun/star/sdbc/XColumnLocate.hpp>
51 #include <com/sun/star/sdbcx/XRowLocate.hpp>
52 #include <vcl/waitobj.hxx>
53 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
54 #include <unotools/tempfile.hxx>
55 #include <cppuhelper/exc_hlp.hxx>
57 #include "dbexchange.hxx"
58 namespace dbaui
60 using namespace ::dbtools;
61 using namespace ::svx;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::task;
64 using namespace ::com::sun::star::beans;
65 using namespace ::com::sun::star::lang;
66 using namespace ::com::sun::star::container;
67 using namespace ::com::sun::star::sdb;
68 using namespace ::com::sun::star::sdb::application;
69 using namespace ::com::sun::star::sdbc;
70 using namespace ::com::sun::star::sdbcx;
71 using namespace ::com::sun::star::frame;
72 using namespace ::com::sun::star::ucb;
74 OTableCopyHelper::OTableCopyHelper(OGenericUnoController* _pControler)
75 :m_pController(_pControler)
79 void OTableCopyHelper::insertTable( const OUString& i_rSourceDataSource, const Reference<XConnection>& i_rSourceConnection,
80 const OUString& i_rCommand, const sal_Int32 i_nCommandType,
81 const Reference< XResultSet >& i_rSourceRows, const Sequence< Any >& i_rSelection, const bool i_bBookmarkSelection,
82 const OUString& i_rDestDataSource, const Reference<XConnection>& i_rDestConnection)
84 if ( CommandType::QUERY != i_nCommandType && CommandType::TABLE != i_nCommandType )
86 SAL_WARN("dbaccess.ui", "OTableCopyHelper::insertTable: invalid call (no supported format found)!" );
87 return;
90 try
92 Reference<XConnection> xSrcConnection( i_rSourceConnection );
93 if ( i_rSourceDataSource == i_rDestDataSource )
94 xSrcConnection = i_rDestConnection;
96 if ( !xSrcConnection.is() || !i_rDestConnection.is() )
98 SAL_WARN("dbaccess.ui", "OTableCopyHelper::insertTable: no connection/s!" );
99 return;
102 Reference<XComponentContext> aContext( m_pController->getORB() );
104 Reference< XDataAccessDescriptorFactory > xFactory( DataAccessDescriptorFactory::get( aContext ) );
106 Reference< XPropertySet > xSource( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
107 xSource->setPropertyValue( PROPERTY_COMMAND_TYPE, makeAny( i_nCommandType ) );
108 xSource->setPropertyValue( PROPERTY_COMMAND, makeAny( i_rCommand ) );
109 xSource->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( xSrcConnection ) );
110 xSource->setPropertyValue( PROPERTY_RESULT_SET, makeAny( i_rSourceRows ) );
111 xSource->setPropertyValue( PROPERTY_SELECTION, makeAny( i_rSelection ) );
112 xSource->setPropertyValue( PROPERTY_BOOKMARK_SELECTION, makeAny( i_bBookmarkSelection ) );
114 Reference< XPropertySet > xDest( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
115 xDest->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( i_rDestConnection ) );
117 Reference< XCopyTableWizard > xWizard( CopyTableWizard::create( aContext, xSource, xDest ), UNO_SET_THROW );
119 OUString sTableNameForAppend( GetTableNameForAppend() );
120 xWizard->setDestinationTableName( GetTableNameForAppend() );
122 bool bAppendToExisting = !sTableNameForAppend.isEmpty();
123 xWizard->setOperation( bAppendToExisting ? CopyTableOperation::AppendData : CopyTableOperation::CopyDefinitionAndData );
125 xWizard->execute();
127 catch( const SQLException& )
129 m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
131 catch( const Exception& )
133 DBG_UNHANDLED_EXCEPTION();
137 void OTableCopyHelper::pasteTable( const svx::ODataAccessDescriptor& _rPasteData, const OUString& i_rDestDataSourceName,
138 const SharedConnection& i_rDestConnection )
140 OUString sSrcDataSourceName = _rPasteData.getDataSource();
142 OUString sCommand;
143 _rPasteData[ daCommand ] >>= sCommand;
145 Reference<XConnection> xSrcConnection;
146 if ( _rPasteData.has(daConnection) )
148 OSL_VERIFY( _rPasteData[daConnection] >>= xSrcConnection );
151 Reference< XResultSet > xResultSet;
152 if ( _rPasteData.has(daCursor) )
154 OSL_VERIFY( _rPasteData[ daCursor ] >>= xResultSet );
157 Sequence< Any > aSelection;
158 if ( _rPasteData.has( daSelection ) )
160 OSL_VERIFY( _rPasteData[ daSelection ] >>= aSelection );
161 OSL_ENSURE( _rPasteData.has( daBookmarkSelection ), "OTableCopyHelper::pasteTable: you should specify BookmarkSelection, too, to be on the safe side!" );
164 bool bBookmarkSelection( true );
165 if ( _rPasteData.has( daBookmarkSelection ) )
167 OSL_VERIFY( _rPasteData[ daBookmarkSelection ] >>= bBookmarkSelection );
169 OSL_ENSURE( bBookmarkSelection, "OTableCopyHelper::pasteTable: working with selection-indicies (instead of bookmarks) is error-prone, and thus deprecated!" );
171 sal_Int32 nCommandType = CommandType::COMMAND;
172 if ( _rPasteData.has(daCommandType) )
173 _rPasteData[daCommandType] >>= nCommandType;
175 insertTable( sSrcDataSourceName, xSrcConnection, sCommand, nCommandType,
176 xResultSet, aSelection, bBookmarkSelection,
177 i_rDestDataSourceName, i_rDestConnection );
180 void OTableCopyHelper::pasteTable( SotClipboardFormatId _nFormatId
181 ,const TransferableDataHelper& _rTransData
182 ,const OUString& i_rDestDataSource
183 ,const SharedConnection& _xConnection)
185 if ( _nFormatId == SotClipboardFormatId::DBACCESS_TABLE || _nFormatId == SotClipboardFormatId::DBACCESS_QUERY )
187 if ( ODataAccessObjectTransferable::canExtractObjectDescriptor(_rTransData.GetDataFlavorExVector()) )
189 svx::ODataAccessDescriptor aPasteData = ODataAccessObjectTransferable::extractObjectDescriptor(_rTransData);
190 pasteTable( aPasteData,i_rDestDataSource,_xConnection);
193 else if ( _rTransData.HasFormat(_nFormatId) )
197 DropDescriptor aTrans;
198 bool bOk;
199 if ( _nFormatId != SotClipboardFormatId::RTF )
200 bOk = const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SotClipboardFormatId::HTML ,aTrans.aHtmlRtfStorage);
201 else
202 bOk = const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SotClipboardFormatId::RTF,aTrans.aHtmlRtfStorage);
204 aTrans.nType = E_TABLE;
205 aTrans.bHtml = SotClipboardFormatId::HTML == _nFormatId;
206 aTrans.sDefaultTableName = GetTableNameForAppend();
207 if ( !bOk || !copyTagTable(aTrans,false,_xConnection) )
208 m_pController->showError(SQLException(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE), *m_pController, OUString("S1000"), 0, Any()));
210 catch(const SQLException&)
212 m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
214 catch( const Exception& )
216 DBG_UNHANDLED_EXCEPTION();
219 else
220 m_pController->showError(SQLException(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE), *m_pController, OUString("S1000"), 0, Any()));
223 void OTableCopyHelper::pasteTable( const TransferableDataHelper& _rTransData
224 ,const OUString& i_rDestDataSource
225 ,const SharedConnection& _xConnection)
227 if ( _rTransData.HasFormat(SotClipboardFormatId::DBACCESS_TABLE) || _rTransData.HasFormat(SotClipboardFormatId::DBACCESS_QUERY) )
228 pasteTable( SotClipboardFormatId::DBACCESS_TABLE,_rTransData,i_rDestDataSource,_xConnection);
229 else if ( _rTransData.HasFormat(SotClipboardFormatId::HTML) )
230 pasteTable( SotClipboardFormatId::HTML,_rTransData,i_rDestDataSource,_xConnection);
231 else if ( _rTransData.HasFormat(SotClipboardFormatId::RTF) )
232 pasteTable( SotClipboardFormatId::RTF,_rTransData,i_rDestDataSource,_xConnection);
235 bool OTableCopyHelper::copyTagTable(OTableCopyHelper::DropDescriptor& _rDesc, bool _bCheck, const SharedConnection& _xConnection)
237 Reference<XEventListener> xEvt;
238 ODatabaseImportExport* pImport = nullptr;
239 if ( _rDesc.bHtml )
240 pImport = new OHTMLImportExport(_xConnection,getNumberFormatter(_xConnection, m_pController->getORB()),m_pController->getORB());
241 else
242 pImport = new ORTFImportExport(_xConnection,getNumberFormatter(_xConnection, m_pController->getORB()),m_pController->getORB());
244 xEvt = pImport;
245 SvStream* pStream = static_cast<SvStream*>(static_cast<SotStorageStream*>(_rDesc.aHtmlRtfStorage));
246 if ( _bCheck )
247 pImport->enableCheckOnly();
249 //set the selected tablename
250 pImport->setSTableName(_rDesc.sDefaultTableName);
252 pImport->setStream(pStream);
253 return pImport->Read();
256 bool OTableCopyHelper::isTableFormat(const TransferableDataHelper& _rClipboard)
258 bool bTableFormat = _rClipboard.HasFormat(SotClipboardFormatId::DBACCESS_TABLE)
259 || _rClipboard.HasFormat(SotClipboardFormatId::DBACCESS_QUERY)
260 || _rClipboard.HasFormat(SotClipboardFormatId::RTF)
261 || _rClipboard.HasFormat(SotClipboardFormatId::HTML);
263 return bTableFormat;
266 bool OTableCopyHelper::copyTagTable(const TransferableDataHelper& _aDroppedData
267 ,DropDescriptor& _rAsyncDrop
268 ,const SharedConnection& _xConnection)
270 bool bRet = false;
271 bool bHtml = _aDroppedData.HasFormat(SotClipboardFormatId::HTML);
272 if ( bHtml || _aDroppedData.HasFormat(SotClipboardFormatId::RTF) )
274 bool bOk;
275 if ( bHtml )
276 bOk = const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SotClipboardFormatId::HTML ,_rAsyncDrop.aHtmlRtfStorage);
277 else
278 bOk = const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SotClipboardFormatId::RTF,_rAsyncDrop.aHtmlRtfStorage);
280 _rAsyncDrop.bHtml = bHtml;
281 _rAsyncDrop.bError = !copyTagTable(_rAsyncDrop,true,_xConnection);
283 bRet = ( !_rAsyncDrop.bError && bOk && _rAsyncDrop.aHtmlRtfStorage.Is() );
284 if ( bRet )
286 // now we need to copy the stream
287 ::utl::TempFile aTmp;
288 _rAsyncDrop.aUrl = aTmp.GetURL();
289 ::tools::SvRef<SotStorageStream> aNew = new SotStorageStream( aTmp.GetFileName() );
290 _rAsyncDrop.aHtmlRtfStorage->Seek(STREAM_SEEK_TO_BEGIN);
291 _rAsyncDrop.aHtmlRtfStorage->CopyTo( aNew );
292 aNew->Commit();
293 _rAsyncDrop.aHtmlRtfStorage = aNew;
295 else
296 _rAsyncDrop.aHtmlRtfStorage = nullptr;
298 return bRet;
301 void OTableCopyHelper::asyncCopyTagTable( DropDescriptor& _rDesc
302 ,const OUString& i_rDestDataSource
303 ,const SharedConnection& _xConnection)
305 if ( _rDesc.aHtmlRtfStorage.Is() )
307 copyTagTable(_rDesc,false,_xConnection);
308 _rDesc.aHtmlRtfStorage = nullptr;
309 // we now have to delete the temp file created in executeDrop
310 INetURLObject aURL;
311 aURL.SetURL(_rDesc.aUrl);
312 ::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::NO_DECODE));
314 else if ( !_rDesc.bError )
315 pasteTable(_rDesc.aDroppedData,i_rDestDataSource,_xConnection);
316 else
317 m_pController->showError(SQLException(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE), *m_pController, OUString("S1000"), 0, Any()));
320 } // namespace dbaui
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */