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