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