Branch data Line data Source code
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 "dbustrings.hrc"
22 : : #include "sqlmessage.hxx"
23 : : #include <vcl/msgbox.hxx>
24 : : #include "WCopyTable.hxx"
25 : : #include "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>
32 : :
33 : : #include "RtfReader.hxx"
34 : : #include "HtmlReader.hxx"
35 : : #include "TokenWriter.hxx"
36 : : #include "UITools.hxx"
37 : : #include "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/componentcontext.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>
56 : :
57 : : #include "dbexchange.hxx"
58 : : #include <rtl/logfile.hxx>
59 : : //........................................................................
60 : : namespace dbaui
61 : : {
62 : : //........................................................................
63 : : using namespace ::dbtools;
64 : : using namespace ::svx;
65 : : using namespace ::com::sun::star::uno;
66 : : using namespace ::com::sun::star::task;
67 : : using namespace ::com::sun::star::beans;
68 : : using namespace ::com::sun::star::lang;
69 : : using namespace ::com::sun::star::container;
70 : : using namespace ::com::sun::star::sdb;
71 : : using namespace ::com::sun::star::sdb::application;
72 : : using namespace ::com::sun::star::sdbc;
73 : : using namespace ::com::sun::star::sdbcx;
74 : : using namespace ::com::sun::star::frame;
75 : : using namespace ::com::sun::star::ucb;
76 : :
77 : : // -----------------------------------------------------------------------------
78 : 2 : OTableCopyHelper::OTableCopyHelper(OGenericUnoController* _pControler)
79 : 2 : :m_pController(_pControler)
80 : : {
81 : 2 : }
82 : :
83 : : // -----------------------------------------------------------------------------
84 : 0 : void OTableCopyHelper::insertTable( const ::rtl::OUString& i_rSourceDataSource, const Reference<XConnection>& i_rSourceConnection,
85 : : const ::rtl::OUString& i_rCommand, const sal_Int32 i_nCommandType,
86 : : const Reference< XResultSet >& i_rSourceRows, const Sequence< Any >& i_rSelection, const sal_Bool i_bBookmarkSelection,
87 : : const ::rtl::OUString& i_rDestDataSource, const Reference<XConnection>& i_rDestConnection)
88 : : {
89 [ # # ][ # # ]: 0 : if ( CommandType::QUERY != i_nCommandType && CommandType::TABLE != i_nCommandType )
90 : : {
91 : : OSL_FAIL( "OTableCopyHelper::insertTable: invalid call (no supported format found)!" );
92 : 0 : return;
93 : : }
94 : :
95 : : try
96 : : {
97 : 0 : Reference<XConnection> xSrcConnection( i_rSourceConnection );
98 [ # # ]: 0 : if ( i_rSourceDataSource == i_rDestDataSource )
99 [ # # ]: 0 : xSrcConnection = i_rDestConnection;
100 : :
101 [ # # ][ # # ]: 0 : if ( !xSrcConnection.is() || !i_rDestConnection.is() )
[ # # ]
102 : : {
103 : : OSL_FAIL( "OTableCopyHelper::insertTable: no connection/s!" );
104 : : return;
105 : : }
106 : :
107 [ # # ][ # # ]: 0 : ::comphelper::ComponentContext aContext( m_pController->getORB() );
108 : :
109 [ # # ][ # # ]: 0 : Reference< XDataAccessDescriptorFactory > xFactory( DataAccessDescriptorFactory::get( aContext.getUNOContext() ) );
110 : :
111 [ # # ][ # # ]: 0 : Reference< XPropertySet > xSource( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
[ # # ]
112 [ # # ][ # # ]: 0 : xSource->setPropertyValue( PROPERTY_COMMAND_TYPE, makeAny( i_nCommandType ) );
[ # # ][ # # ]
113 [ # # ][ # # ]: 0 : xSource->setPropertyValue( PROPERTY_COMMAND, makeAny( i_rCommand ) );
[ # # ][ # # ]
114 [ # # ][ # # ]: 0 : xSource->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( xSrcConnection ) );
[ # # ][ # # ]
115 [ # # ][ # # ]: 0 : xSource->setPropertyValue( PROPERTY_RESULT_SET, makeAny( i_rSourceRows ) );
[ # # ][ # # ]
116 [ # # ][ # # ]: 0 : xSource->setPropertyValue( PROPERTY_SELECTION, makeAny( i_rSelection ) );
[ # # ][ # # ]
117 [ # # ][ # # ]: 0 : xSource->setPropertyValue( PROPERTY_BOOKMARK_SELECTION, makeAny( i_bBookmarkSelection ) );
[ # # ][ # # ]
118 : :
119 [ # # ][ # # ]: 0 : Reference< XPropertySet > xDest( xFactory->createDataAccessDescriptor(), UNO_SET_THROW );
[ # # ]
120 [ # # ][ # # ]: 0 : xDest->setPropertyValue( PROPERTY_ACTIVE_CONNECTION, makeAny( i_rDestConnection ) );
[ # # ][ # # ]
121 : :
122 [ # # ][ # # ]: 0 : Reference< XCopyTableWizard > xWizard( CopyTableWizard::create( aContext.getUNOContext(), xSource, xDest ), UNO_SET_THROW );
[ # # ]
123 : :
124 : 0 : ::rtl::OUString sTableNameForAppend( GetTableNameForAppend() );
125 [ # # ][ # # ]: 0 : xWizard->setDestinationTableName( GetTableNameForAppend() );
126 : :
127 : 0 : bool bAppendToExisting = !sTableNameForAppend.isEmpty();
128 [ # # ][ # # ]: 0 : xWizard->setOperation( bAppendToExisting ? CopyTableOperation::AppendData : CopyTableOperation::CopyDefinitionAndData );
[ # # ]
129 : :
130 [ # # ][ # # ]: 0 : xWizard->execute();
[ # # ][ # # ]
131 : : }
132 [ # # # ]: 0 : catch( const SQLException& )
133 : : {
134 [ # # # # : 0 : m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
# # # # ]
135 : : }
136 : 0 : catch( const Exception& )
137 : : {
138 : : DBG_UNHANDLED_EXCEPTION();
139 : : }
140 : : }
141 : :
142 : : // -----------------------------------------------------------------------------
143 : 0 : void OTableCopyHelper::pasteTable( const ::svx::ODataAccessDescriptor& _rPasteData, const ::rtl::OUString& i_rDestDataSourceName,
144 : : const SharedConnection& i_rDestConnection )
145 : : {
146 [ # # ]: 0 : ::rtl::OUString sSrcDataSourceName = _rPasteData.getDataSource();
147 : :
148 : 0 : ::rtl::OUString sCommand;
149 [ # # ]: 0 : _rPasteData[ daCommand ] >>= sCommand;
150 : :
151 : 0 : Reference<XConnection> xSrcConnection;
152 [ # # ][ # # ]: 0 : if ( _rPasteData.has(daConnection) )
153 : : {
154 [ # # ][ # # ]: 0 : OSL_VERIFY( _rPasteData[daConnection] >>= xSrcConnection );
155 : : }
156 : :
157 : 0 : Reference< XResultSet > xResultSet;
158 [ # # ][ # # ]: 0 : if ( _rPasteData.has(daCursor) )
159 : : {
160 [ # # ][ # # ]: 0 : OSL_VERIFY( _rPasteData[ daCursor ] >>= xResultSet );
161 : : }
162 : :
163 [ # # ]: 0 : Sequence< Any > aSelection;
164 [ # # ][ # # ]: 0 : if ( _rPasteData.has( daSelection ) )
165 : : {
166 [ # # ][ # # ]: 0 : OSL_VERIFY( _rPasteData[ daSelection ] >>= aSelection );
167 : : OSL_ENSURE( _rPasteData.has( daBookmarkSelection ), "OTableCopyHelper::pasteTable: you should specify BookmarkSelection, too, to be on the safe side!" );
168 : : }
169 : :
170 : :
171 : 0 : sal_Bool bBookmarkSelection( sal_True );
172 [ # # ][ # # ]: 0 : if ( _rPasteData.has( daBookmarkSelection ) )
173 : : {
174 [ # # ]: 0 : OSL_VERIFY( _rPasteData[ daBookmarkSelection ] >>= bBookmarkSelection );
175 : : }
176 : : OSL_ENSURE( bBookmarkSelection, "OTableCopyHelper::pasteTable: working with selection-indicies (instead of bookmarks) is error-prone, and thus deprecated!" );
177 : :
178 : 0 : sal_Int32 nCommandType = CommandType::COMMAND;
179 [ # # ][ # # ]: 0 : if ( _rPasteData.has(daCommandType) )
180 [ # # ]: 0 : _rPasteData[daCommandType] >>= nCommandType;
181 : :
182 : : insertTable( sSrcDataSourceName, xSrcConnection, sCommand, nCommandType,
183 : : xResultSet, aSelection, bBookmarkSelection,
184 [ # # ][ # # ]: 0 : i_rDestDataSourceName, i_rDestConnection );
185 : 0 : }
186 : :
187 : : // -----------------------------------------------------------------------------
188 : 0 : void OTableCopyHelper::pasteTable( SotFormatStringId _nFormatId
189 : : ,const TransferableDataHelper& _rTransData
190 : : ,const ::rtl::OUString& i_rDestDataSource
191 : : ,const SharedConnection& _xConnection)
192 : : {
193 [ # # ][ # # ]: 0 : if ( _nFormatId == SOT_FORMATSTR_ID_DBACCESS_TABLE || _nFormatId == SOT_FORMATSTR_ID_DBACCESS_QUERY )
194 : : {
195 [ # # ]: 0 : if ( ODataAccessObjectTransferable::canExtractObjectDescriptor(_rTransData.GetDataFlavorExVector()) )
196 : : {
197 [ # # ]: 0 : ::svx::ODataAccessDescriptor aPasteData = ODataAccessObjectTransferable::extractObjectDescriptor(_rTransData);
198 [ # # ][ # # ]: 0 : pasteTable( aPasteData,i_rDestDataSource,_xConnection);
199 : 0 : }
200 : : }
201 [ # # ]: 0 : else if ( _rTransData.HasFormat(_nFormatId) )
202 : : {
203 : : try
204 : : {
205 [ # # ]: 0 : DropDescriptor aTrans;
206 [ # # ]: 0 : if ( _nFormatId != SOT_FORMAT_RTF )
207 [ # # ]: 0 : const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SOT_FORMATSTR_ID_HTML ,aTrans.aHtmlRtfStorage);
208 : : else
209 [ # # ]: 0 : const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SOT_FORMAT_RTF,aTrans.aHtmlRtfStorage);
210 : :
211 : 0 : aTrans.nType = E_TABLE;
212 : 0 : aTrans.bHtml = SOT_FORMATSTR_ID_HTML == _nFormatId;
213 : 0 : aTrans.sDefaultTableName = GetTableNameForAppend();
214 [ # # ][ # # ]: 0 : if ( !copyTagTable(aTrans,sal_False,_xConnection) )
215 [ # # ][ # # ]: 0 : m_pController->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE)),*m_pController,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")),0,Any()));
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
216 : : }
217 [ # # # ]: 0 : catch(const SQLException&)
218 : : {
219 [ # # # # : 0 : m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
# # # # ]
220 : : }
221 : 0 : catch( const Exception& )
222 : : {
223 : : DBG_UNHANDLED_EXCEPTION();
224 : : }
225 : : }
226 : : else
227 [ # # ][ # # ]: 0 : m_pController->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE)),*m_pController,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")),0,Any()));
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
228 : 0 : }
229 : :
230 : : // -----------------------------------------------------------------------------
231 : 0 : void OTableCopyHelper::pasteTable( const TransferableDataHelper& _rTransData
232 : : ,const ::rtl::OUString& i_rDestDataSource
233 : : ,const SharedConnection& _xConnection)
234 : : {
235 [ # # ][ # # ]: 0 : if ( _rTransData.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE) || _rTransData.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY) )
[ # # ]
236 : 0 : pasteTable( SOT_FORMATSTR_ID_DBACCESS_TABLE,_rTransData,i_rDestDataSource,_xConnection);
237 [ # # ]: 0 : else if ( _rTransData.HasFormat(SOT_FORMATSTR_ID_HTML) )
238 : 0 : pasteTable( SOT_FORMATSTR_ID_HTML,_rTransData,i_rDestDataSource,_xConnection);
239 [ # # ]: 0 : else if ( _rTransData.HasFormat(SOT_FORMAT_RTF) )
240 : 0 : pasteTable( SOT_FORMAT_RTF,_rTransData,i_rDestDataSource,_xConnection);
241 : 0 : }
242 : :
243 : : // -----------------------------------------------------------------------------
244 : 0 : sal_Bool OTableCopyHelper::copyTagTable(OTableCopyHelper::DropDescriptor& _rDesc, sal_Bool _bCheck,const SharedConnection& _xConnection)
245 : : {
246 : 0 : Reference<XEventListener> xEvt;
247 : 0 : ODatabaseImportExport* pImport = NULL;
248 [ # # ]: 0 : if ( _rDesc.bHtml )
249 [ # # ][ # # ]: 0 : pImport = new OHTMLImportExport(_xConnection,getNumberFormatter(_xConnection,m_pController->getORB()),m_pController->getORB());
[ # # ][ # # ]
250 : : else
251 [ # # ][ # # ]: 0 : pImport = new ORTFImportExport(_xConnection,getNumberFormatter(_xConnection,m_pController->getORB()),m_pController->getORB());
[ # # ][ # # ]
252 : :
253 [ # # ][ # # ]: 0 : xEvt = pImport;
254 : 0 : SvStream* pStream = (SvStream*)(SotStorageStream*)_rDesc.aHtmlRtfStorage;
255 [ # # ]: 0 : if ( _bCheck )
256 : 0 : pImport->enableCheckOnly();
257 : :
258 : : //set the selected tablename
259 : 0 : pImport->setSTableName(_rDesc.sDefaultTableName);
260 : :
261 : 0 : pImport->setStream(pStream);
262 [ # # ]: 0 : return pImport->Read();
263 : : }
264 : : // -----------------------------------------------------------------------------
265 : 0 : sal_Bool OTableCopyHelper::isTableFormat(const TransferableDataHelper& _rClipboard) const
266 : : {
267 : 0 : sal_Bool bTableFormat = _rClipboard.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE)
268 : 0 : || _rClipboard.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY)
269 : 0 : || _rClipboard.HasFormat(SOT_FORMAT_RTF)
270 [ # # ][ # # : 0 : || _rClipboard.HasFormat(SOT_FORMATSTR_ID_HTML);
# # # # ]
271 : :
272 : 0 : return bTableFormat;
273 : : }
274 : : // -----------------------------------------------------------------------------
275 : 0 : sal_Bool OTableCopyHelper::copyTagTable(const TransferableDataHelper& _aDroppedData
276 : : ,DropDescriptor& _rAsyncDrop
277 : : ,const SharedConnection& _xConnection)
278 : : {
279 : 0 : sal_Bool bRet = sal_False;
280 : 0 : sal_Bool bHtml = _aDroppedData.HasFormat(SOT_FORMATSTR_ID_HTML);
281 [ # # ][ # # ]: 0 : if ( bHtml || _aDroppedData.HasFormat(SOT_FORMAT_RTF) )
[ # # ]
282 : : {
283 [ # # ]: 0 : if ( bHtml )
284 : 0 : const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SOT_FORMATSTR_ID_HTML ,_rAsyncDrop.aHtmlRtfStorage);
285 : : else
286 : 0 : const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SOT_FORMAT_RTF,_rAsyncDrop.aHtmlRtfStorage);
287 : :
288 : 0 : _rAsyncDrop.bHtml = bHtml;
289 : 0 : _rAsyncDrop.bError = !copyTagTable(_rAsyncDrop,sal_True,_xConnection);
290 : :
291 [ # # ][ # # ]: 0 : bRet = ( !_rAsyncDrop.bError && _rAsyncDrop.aHtmlRtfStorage.Is() );
292 [ # # ]: 0 : if ( bRet )
293 : : {
294 : : // now we need to copy the stream
295 [ # # ]: 0 : ::utl::TempFile aTmp;
296 : 0 : aTmp.EnableKillingFile(sal_False);
297 [ # # ][ # # ]: 0 : _rAsyncDrop.aUrl = aTmp.GetURL();
[ # # ]
298 [ # # ][ # # ]: 0 : SotStorageStreamRef aNew = new SotStorageStream( aTmp.GetFileName() );
[ # # ][ # # ]
299 [ # # ]: 0 : _rAsyncDrop.aHtmlRtfStorage->Seek(STREAM_SEEK_TO_BEGIN);
300 [ # # ]: 0 : _rAsyncDrop.aHtmlRtfStorage->CopyTo( aNew );
301 [ # # ]: 0 : aNew->Commit();
302 [ # # ][ # # ]: 0 : _rAsyncDrop.aHtmlRtfStorage = aNew;
[ # # ]
303 : : }
304 : : else
305 : 0 : _rAsyncDrop.aHtmlRtfStorage = NULL;
306 : : }
307 : 0 : return bRet;
308 : : }
309 : : // -----------------------------------------------------------------------------
310 : 0 : void OTableCopyHelper::asyncCopyTagTable( DropDescriptor& _rDesc
311 : : ,const ::rtl::OUString& i_rDestDataSource
312 : : ,const SharedConnection& _xConnection)
313 : : {
314 [ # # ]: 0 : if ( _rDesc.aHtmlRtfStorage.Is() )
315 : : {
316 [ # # ]: 0 : copyTagTable(_rDesc,sal_False,_xConnection);
317 [ # # ]: 0 : _rDesc.aHtmlRtfStorage = NULL;
318 : : // we now have to delete the temp file created in executeDrop
319 [ # # ]: 0 : INetURLObject aURL;
320 [ # # ][ # # ]: 0 : aURL.SetURL(_rDesc.aUrl);
321 [ # # ][ # # ]: 0 : ::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::NO_DECODE));
[ # # ]
322 : : }
323 [ # # ]: 0 : else if ( !_rDesc.bError )
324 : 0 : pasteTable(_rDesc.aDroppedData,i_rDestDataSource,_xConnection);
325 : : else
326 [ # # ][ # # ]: 0 : m_pController->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE)),*m_pController,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")),0,Any()));
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
327 : 0 : }
328 : : // -----------------------------------------------------------------------------
329 : : //........................................................................
330 : : } // namespace dbaui
331 : : //........................................................................
332 : :
333 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|