LibreOffice Module dbaccess (master) 1
TableCopyHelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <TableCopyHelper.hxx>
21#include <core_resource.hxx>
22#include <strings.hrc>
23#include <strings.hxx>
25#include <com/sun/star/task/InteractionHandler.hpp>
26#include <com/sun/star/task/XInteractionHandler.hpp>
27#include <com/sun/star/sdb/application/CopyTableOperation.hpp>
28#include <com/sun/star/sdb/application/CopyTableWizard.hpp>
29#include <com/sun/star/sdb/DataAccessDescriptorFactory.hpp>
30#include <com/sun/star/sdb/CommandType.hpp>
31
32#include <TokenWriter.hxx>
33#include <UITools.hxx>
34#include <dbaccess/dataview.hxx>
35#include <svx/dbaexchange.hxx>
37#include <tools/urlobj.hxx>
39#include <sal/log.hxx>
41#include <unotools/tempfile.hxx>
43
44namespace dbaui
45{
46using namespace ::dbtools;
47using namespace ::svx;
48using namespace ::com::sun::star::uno;
49using namespace ::com::sun::star::task;
50using namespace ::com::sun::star::beans;
51using namespace ::com::sun::star::lang;
52using namespace ::com::sun::star::container;
53using namespace ::com::sun::star::sdb;
54using namespace ::com::sun::star::sdb::application;
55using namespace ::com::sun::star::sdbc;
56using namespace ::com::sun::star::sdbcx;
57using namespace ::com::sun::star::ucb;
58
60 :m_pController(_pController)
61{
62}
63
64void OTableCopyHelper::insertTable( std::u16string_view i_rSourceDataSource, const Reference<XConnection>& i_rSourceConnection,
65 const OUString& i_rCommand, const sal_Int32 i_nCommandType,
66 const Reference< XResultSet >& i_rSourceRows, const Sequence< Any >& i_rSelection, const bool i_bBookmarkSelection,
67 std::u16string_view i_rDestDataSource, const Reference<XConnection>& i_rDestConnection)
68{
69 if ( CommandType::QUERY != i_nCommandType && CommandType::TABLE != i_nCommandType )
70 {
71 SAL_WARN("dbaccess.ui", "OTableCopyHelper::insertTable: invalid call (no supported format found)!" );
72 return;
73 }
74
75 try
76 {
77 Reference<XConnection> xSrcConnection( i_rSourceConnection );
78 if ( i_rSourceDataSource == i_rDestDataSource )
79 xSrcConnection = i_rDestConnection;
80
81 if ( !xSrcConnection.is() || !i_rDestConnection.is() )
82 {
83 SAL_WARN("dbaccess.ui", "OTableCopyHelper::insertTable: no connection/s!" );
84 return;
85 }
86
88
89 Reference< XDataAccessDescriptorFactory > xFactory( DataAccessDescriptorFactory::get( aContext ) );
90
91 Reference< XPropertySet > xSource( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
92 xSource->setPropertyValue( PROPERTY_COMMAND_TYPE, Any( i_nCommandType ) );
93 xSource->setPropertyValue( PROPERTY_COMMAND, Any( i_rCommand ) );
94 xSource->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, Any( xSrcConnection ) );
95 xSource->setPropertyValue( PROPERTY_RESULT_SET, Any( i_rSourceRows ) );
96 xSource->setPropertyValue( PROPERTY_SELECTION, Any( i_rSelection ) );
97 xSource->setPropertyValue( PROPERTY_BOOKMARK_SELECTION, Any( i_bBookmarkSelection ) );
98
99 Reference< XPropertySet > xDest( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
100 xDest->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, Any( i_rDestConnection ) );
101
102 auto xInteractionHandler = InteractionHandler::createWithParent(aContext, VCLUnoHelper::GetInterface(m_pController->getView()));
103
104 Reference<XCopyTableWizard> xWizard(CopyTableWizard::createWithInteractionHandler(aContext, xSource, xDest, xInteractionHandler), UNO_SET_THROW);
105
106 OUString sTableNameForAppend( GetTableNameForAppend() );
107 xWizard->setDestinationTableName( GetTableNameForAppend() );
108
109 bool bAppendToExisting = !sTableNameForAppend.isEmpty();
110 xWizard->setOperation( bAppendToExisting ? CopyTableOperation::AppendData : CopyTableOperation::CopyDefinitionAndData );
111
112 xWizard->execute();
113 }
114 catch( const SQLException& )
115 {
116 m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
117 }
118 catch( const Exception& )
119 {
120 DBG_UNHANDLED_EXCEPTION("dbaccess");
121 }
122}
123
124void OTableCopyHelper::pasteTable( const svx::ODataAccessDescriptor& _rPasteData, std::u16string_view i_rDestDataSourceName,
125 const SharedConnection& i_rDestConnection )
126{
127 OUString sSrcDataSourceName = _rPasteData.getDataSource();
128
129 OUString sCommand;
130 _rPasteData[ DataAccessDescriptorProperty::Command ] >>= sCommand;
131
132 Reference<XConnection> xSrcConnection;
133 if ( _rPasteData.has(DataAccessDescriptorProperty::Connection) )
134 {
135 OSL_VERIFY( _rPasteData[DataAccessDescriptorProperty::Connection] >>= xSrcConnection );
136 }
137
138 Reference< XResultSet > xResultSet;
139 if ( _rPasteData.has(DataAccessDescriptorProperty::Cursor) )
140 {
141 OSL_VERIFY( _rPasteData[ DataAccessDescriptorProperty::Cursor ] >>= xResultSet );
142 }
143
144 Sequence< Any > aSelection;
145 if ( _rPasteData.has( DataAccessDescriptorProperty::Selection ) )
146 {
147 OSL_VERIFY( _rPasteData[ DataAccessDescriptorProperty::Selection ] >>= aSelection );
148 OSL_ENSURE( _rPasteData.has( DataAccessDescriptorProperty::BookmarkSelection ), "OTableCopyHelper::pasteTable: you should specify BookmarkSelection, too, to be on the safe side!" );
149 }
150
151 bool bBookmarkSelection( true );
152 if ( _rPasteData.has( DataAccessDescriptorProperty::BookmarkSelection ) )
153 {
154 OSL_VERIFY( _rPasteData[ DataAccessDescriptorProperty::BookmarkSelection ] >>= bBookmarkSelection );
155 }
156 OSL_ENSURE( bBookmarkSelection, "OTableCopyHelper::pasteTable: working with selection-indices (instead of bookmarks) is error-prone, and thus deprecated!" );
157
158 sal_Int32 nCommandType = CommandType::COMMAND;
159 if ( _rPasteData.has(DataAccessDescriptorProperty::CommandType) )
160 _rPasteData[DataAccessDescriptorProperty::CommandType] >>= nCommandType;
161
162 insertTable( sSrcDataSourceName, xSrcConnection, sCommand, nCommandType,
163 xResultSet, aSelection, bBookmarkSelection,
164 i_rDestDataSourceName, i_rDestConnection );
165}
166
168 ,const TransferableDataHelper& _rTransData
169 ,std::u16string_view i_rDestDataSource
170 ,const SharedConnection& _xConnection)
171{
172 if ( _nFormatId == SotClipboardFormatId::DBACCESS_TABLE || _nFormatId == SotClipboardFormatId::DBACCESS_QUERY )
173 {
174 if ( ODataAccessObjectTransferable::canExtractObjectDescriptor(_rTransData.GetDataFlavorExVector()) )
175 {
176 svx::ODataAccessDescriptor aPasteData = ODataAccessObjectTransferable::extractObjectDescriptor(_rTransData);
177 pasteTable( aPasteData,i_rDestDataSource,_xConnection);
178 }
179 }
180 else if ( _rTransData.HasFormat(_nFormatId) )
181 {
182 try
183 {
184 DropDescriptor aTrans;
185 bool bOk;
186 if ( _nFormatId != SotClipboardFormatId::RTF )
187 bOk = _rTransData.GetSotStorageStream(SotClipboardFormatId::HTML ,aTrans.aHtmlRtfStorage);
188 else
189 bOk = _rTransData.GetSotStorageStream(SotClipboardFormatId::RTF,aTrans.aHtmlRtfStorage);
190
191 aTrans.nType = E_TABLE;
192 aTrans.bHtml = SotClipboardFormatId::HTML == _nFormatId;
194 if ( !bOk || !copyTagTable(aTrans,false,_xConnection) )
195 m_pController->showError(SQLException(DBA_RES(STR_NO_TABLE_FORMAT_INSIDE), *m_pController, "S1000", 0, Any()));
196 }
197 catch(const SQLException&)
198 {
199 m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
200 }
201 catch( const Exception& )
202 {
203 DBG_UNHANDLED_EXCEPTION("dbaccess");
204 }
205 }
206 else
207 m_pController->showError(SQLException(DBA_RES(STR_NO_TABLE_FORMAT_INSIDE), *m_pController, "S1000", 0, Any()));
208}
209
211 ,std::u16string_view i_rDestDataSource
212 ,const SharedConnection& _xConnection)
213{
214 if ( _rTransData.HasFormat(SotClipboardFormatId::DBACCESS_TABLE) || _rTransData.HasFormat(SotClipboardFormatId::DBACCESS_QUERY) )
215 pasteTable( SotClipboardFormatId::DBACCESS_TABLE,_rTransData,i_rDestDataSource,_xConnection);
216 else if ( _rTransData.HasFormat(SotClipboardFormatId::HTML) )
217 pasteTable( SotClipboardFormatId::HTML,_rTransData,i_rDestDataSource,_xConnection);
218 else if ( _rTransData.HasFormat(SotClipboardFormatId::RTF) )
219 pasteTable( SotClipboardFormatId::RTF,_rTransData,i_rDestDataSource,_xConnection);
220}
221
222bool OTableCopyHelper::copyTagTable(OTableCopyHelper::DropDescriptor const & _rDesc, bool _bCheck, const SharedConnection& _xConnection)
223{
225 if ( _rDesc.bHtml )
226 pImport = new OHTMLImportExport(_xConnection,getNumberFormatter(_xConnection, m_pController->getORB()),m_pController->getORB());
227 else
228 pImport = new ORTFImportExport(_xConnection,getNumberFormatter(_xConnection, m_pController->getORB()),m_pController->getORB());
229
230 SvStream* pStream = _rDesc.aHtmlRtfStorage.get();
231 if ( _bCheck )
232 pImport->enableCheckOnly();
233
234 //set the selected tablename
235 pImport->setSTableName(_rDesc.sDefaultTableName);
236
237 pImport->setStream(pStream);
238 return pImport->Read();
239}
240
242{
243 bool bTableFormat = _rClipboard.HasFormat(SotClipboardFormatId::DBACCESS_TABLE)
244 || _rClipboard.HasFormat(SotClipboardFormatId::DBACCESS_QUERY)
245 || _rClipboard.HasFormat(SotClipboardFormatId::RTF)
246 || _rClipboard.HasFormat(SotClipboardFormatId::HTML);
247
248 return bTableFormat;
249}
250
252 ,DropDescriptor& _rAsyncDrop
253 ,const SharedConnection& _xConnection)
254{
255 bool bRet = false;
256 bool bHtml = _aDroppedData.HasFormat(SotClipboardFormatId::HTML);
257 if ( bHtml || _aDroppedData.HasFormat(SotClipboardFormatId::RTF) )
258 {
259 bool bOk;
260 if ( bHtml )
261 bOk = _aDroppedData.GetSotStorageStream(SotClipboardFormatId::HTML ,_rAsyncDrop.aHtmlRtfStorage);
262 else
263 bOk = _aDroppedData.GetSotStorageStream(SotClipboardFormatId::RTF,_rAsyncDrop.aHtmlRtfStorage);
264
265 _rAsyncDrop.bHtml = bHtml;
266 _rAsyncDrop.bError = !copyTagTable(_rAsyncDrop,true,_xConnection);
267
268 bRet = ( !_rAsyncDrop.bError && bOk && _rAsyncDrop.aHtmlRtfStorage.is() );
269 if ( bRet )
270 {
271 // now we need to copy the stream
273 _rAsyncDrop.aUrl = aTmp.GetURL();
275 _rAsyncDrop.aHtmlRtfStorage->Seek(STREAM_SEEK_TO_BEGIN);
276 _rAsyncDrop.aHtmlRtfStorage->CopyTo( aNew.get() );
277 _rAsyncDrop.aHtmlRtfStorage = aNew;
278 }
279 else
280 _rAsyncDrop.aHtmlRtfStorage = nullptr;
281 }
282 return bRet;
283}
284
286 ,std::u16string_view i_rDestDataSource
287 ,const SharedConnection& _xConnection)
288{
289 if ( _rDesc.aHtmlRtfStorage.is() )
290 {
291 copyTagTable(_rDesc,false,_xConnection);
292 _rDesc.aHtmlRtfStorage = nullptr;
293 // we now have to delete the temp file created in executeDrop
295 aURL.SetURL(_rDesc.aUrl);
296 ::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE));
297 }
298 else if ( !_rDesc.bError )
299 pasteTable(_rDesc.aDroppedData,i_rDestDataSource,_xConnection);
300 else
301 m_pController->showError(SQLException(DBA_RES(STR_NO_TABLE_FORMAT_INSIDE), *m_pController, "S1000", 0, Any()));
302}
303
304} // namespace dbaui
305
306/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const DataFlavorExVector & GetDataFlavorExVector() const
bool GetSotStorageStream(SotClipboardFormatId nFormat, tools::SvRef< SotTempStream > &rStreamRef) const
bool HasFormat(SotClipboardFormatId nFormat) const
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
void showError(const ::dbtools::SQLExceptionInfo &_rInfo)
const css::uno::Reference< css::uno::XComponentContext > & getORB() const
static bool isTableFormat(const TransferableDataHelper &_rClipboard)
returns <TRUE> if the clipboard supports a table format, otherwise <FALSE>.
OGenericUnoController * m_pController
void pasteTable(const TransferableDataHelper &_rTransData, std::u16string_view _sDestDataSourceName, const SharedConnection &_xConnection)
pastes a table into the data source
bool copyTagTable(DropDescriptor const &_rDesc, bool _bCheck, const SharedConnection &_xConnection)
copies a table which was constructed by tags like HTML or RTF
void asyncCopyTagTable(DropDescriptor &_rDesc, std::u16string_view _sDestDataSourceName, const SharedConnection &_xConnection)
copies a table which was constructed by tags like HTML or RTF
OTableCopyHelper(OGenericUnoController *_pController)
const OUString & GetTableNameForAppend() const
void insertTable(std::u16string_view i_rSourceDataSource, const css::uno::Reference< css::sdbc::XConnection > &i_rSourceConnection, const OUString &i_rCommand, const sal_Int32 i_nCommandType, const css::uno::Reference< css::sdbc::XResultSet > &i_rSourceRows, const css::uno::Sequence< css::uno::Any > &i_rSelection, const bool i_bBookmarkSelection, std::u16string_view i_rDestDataSource, const css::uno::Reference< css::sdbc::XConnection > &i_rDestConnection)
insert a table into the data source.
bool has(DataAccessDescriptorProperty _eWhich) const
OUString getDataSource() const
T * get() const
bool is() const
OUString const & GetURL() const
OUString GetFileName() const
#define DBA_RES(id)
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XSingleServiceFactory > xFactory
SotClipboardFormatId
URL aURL
Definition: intercept.cxx:87
#define SAL_WARN(area, stream)
@ Exception
css::uno::Reference< css::util::XNumberFormatter > getNumberFormatter(const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
creates a number formatter
#define STREAM_SEEK_TO_BEGIN
constexpr OUStringLiteral PROPERTY_COMMAND(u"Command")
constexpr OUStringLiteral PROPERTY_BOOKMARK_SELECTION(u"BookmarkSelection")
constexpr OUStringLiteral PROPERTY_RESULT_SET(u"ResultSet")
constexpr OUStringLiteral PROPERTY_SELECTION(u"Selection")
constexpr OUStringLiteral PROPERTY_ACTIVE_CONNECTION(u"ActiveConnection")
constexpr OUStringLiteral PROPERTY_COMMAND_TYPE(u"CommandType")
svx::ODataAccessDescriptor aDroppedData
tools::SvRef< SotTempStream > aHtmlRtfStorage