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 0 : OTableCopyHelper::OTableCopyHelper(OGenericUnoController* _pControler)
75 0 : :m_pController(_pControler)
76 : {
77 0 : }
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 sal_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 : sal_Bool bBookmarkSelection( sal_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 0 : if ( _nFormatId != SOT_FORMAT_RTF )
199 0 : const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SOT_FORMATSTR_ID_HTML ,aTrans.aHtmlRtfStorage);
200 : else
201 0 : const_cast<TransferableDataHelper&>(_rTransData).GetSotStorageStream(SOT_FORMAT_RTF,aTrans.aHtmlRtfStorage);
202 :
203 0 : aTrans.nType = E_TABLE;
204 0 : aTrans.bHtml = SOT_FORMATSTR_ID_HTML == _nFormatId;
205 0 : aTrans.sDefaultTableName = GetTableNameForAppend();
206 0 : if ( !copyTagTable(aTrans,sal_False,_xConnection) )
207 0 : m_pController->showError(SQLException(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE), *m_pController, OUString("S1000"), 0, Any()));
208 : }
209 0 : catch(const SQLException&)
210 : {
211 0 : m_pController->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
212 : }
213 0 : catch( const Exception& )
214 : {
215 : DBG_UNHANDLED_EXCEPTION();
216 : }
217 : }
218 : else
219 0 : m_pController->showError(SQLException(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE), *m_pController, OUString("S1000"), 0, Any()));
220 0 : }
221 :
222 0 : void OTableCopyHelper::pasteTable( const TransferableDataHelper& _rTransData
223 : ,const OUString& i_rDestDataSource
224 : ,const SharedConnection& _xConnection)
225 : {
226 0 : if ( _rTransData.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE) || _rTransData.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY) )
227 0 : pasteTable( SOT_FORMATSTR_ID_DBACCESS_TABLE,_rTransData,i_rDestDataSource,_xConnection);
228 0 : else if ( _rTransData.HasFormat(SOT_FORMATSTR_ID_HTML) )
229 0 : pasteTable( SOT_FORMATSTR_ID_HTML,_rTransData,i_rDestDataSource,_xConnection);
230 0 : else if ( _rTransData.HasFormat(SOT_FORMAT_RTF) )
231 0 : pasteTable( SOT_FORMAT_RTF,_rTransData,i_rDestDataSource,_xConnection);
232 0 : }
233 :
234 0 : sal_Bool OTableCopyHelper::copyTagTable(OTableCopyHelper::DropDescriptor& _rDesc, sal_Bool _bCheck,const SharedConnection& _xConnection)
235 : {
236 0 : Reference<XEventListener> xEvt;
237 0 : ODatabaseImportExport* pImport = NULL;
238 0 : if ( _rDesc.bHtml )
239 0 : pImport = new OHTMLImportExport(_xConnection,getNumberFormatter(_xConnection, m_pController->getORB()),m_pController->getORB());
240 : else
241 0 : pImport = new ORTFImportExport(_xConnection,getNumberFormatter(_xConnection, m_pController->getORB()),m_pController->getORB());
242 :
243 0 : xEvt = pImport;
244 0 : SvStream* pStream = (SvStream*)(SotStorageStream*)_rDesc.aHtmlRtfStorage;
245 0 : if ( _bCheck )
246 0 : pImport->enableCheckOnly();
247 :
248 : //set the selected tablename
249 0 : pImport->setSTableName(_rDesc.sDefaultTableName);
250 :
251 0 : pImport->setStream(pStream);
252 0 : return pImport->Read();
253 : }
254 :
255 0 : sal_Bool OTableCopyHelper::isTableFormat(const TransferableDataHelper& _rClipboard) const
256 : {
257 0 : sal_Bool bTableFormat = _rClipboard.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE)
258 0 : || _rClipboard.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY)
259 0 : || _rClipboard.HasFormat(SOT_FORMAT_RTF)
260 0 : || _rClipboard.HasFormat(SOT_FORMATSTR_ID_HTML);
261 :
262 0 : return bTableFormat;
263 : }
264 :
265 0 : sal_Bool OTableCopyHelper::copyTagTable(const TransferableDataHelper& _aDroppedData
266 : ,DropDescriptor& _rAsyncDrop
267 : ,const SharedConnection& _xConnection)
268 : {
269 0 : sal_Bool bRet = sal_False;
270 0 : sal_Bool bHtml = _aDroppedData.HasFormat(SOT_FORMATSTR_ID_HTML);
271 0 : if ( bHtml || _aDroppedData.HasFormat(SOT_FORMAT_RTF) )
272 : {
273 0 : if ( bHtml )
274 0 : const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SOT_FORMATSTR_ID_HTML ,_rAsyncDrop.aHtmlRtfStorage);
275 : else
276 0 : const_cast<TransferableDataHelper&>(_aDroppedData).GetSotStorageStream(SOT_FORMAT_RTF,_rAsyncDrop.aHtmlRtfStorage);
277 :
278 0 : _rAsyncDrop.bHtml = bHtml;
279 0 : _rAsyncDrop.bError = !copyTagTable(_rAsyncDrop,sal_True,_xConnection);
280 :
281 0 : bRet = ( !_rAsyncDrop.bError && _rAsyncDrop.aHtmlRtfStorage.Is() );
282 0 : if ( bRet )
283 : {
284 : // now we need to copy the stream
285 0 : ::utl::TempFile aTmp;
286 0 : aTmp.EnableKillingFile(false);
287 0 : _rAsyncDrop.aUrl = aTmp.GetURL();
288 0 : SotStorageStreamRef aNew = new SotStorageStream( aTmp.GetFileName() );
289 0 : _rAsyncDrop.aHtmlRtfStorage->Seek(STREAM_SEEK_TO_BEGIN);
290 0 : _rAsyncDrop.aHtmlRtfStorage->CopyTo( aNew );
291 0 : aNew->Commit();
292 0 : _rAsyncDrop.aHtmlRtfStorage = aNew;
293 : }
294 : else
295 0 : _rAsyncDrop.aHtmlRtfStorage = NULL;
296 : }
297 0 : return bRet;
298 : }
299 :
300 0 : void OTableCopyHelper::asyncCopyTagTable( DropDescriptor& _rDesc
301 : ,const OUString& i_rDestDataSource
302 : ,const SharedConnection& _xConnection)
303 : {
304 0 : if ( _rDesc.aHtmlRtfStorage.Is() )
305 : {
306 0 : copyTagTable(_rDesc,sal_False,_xConnection);
307 0 : _rDesc.aHtmlRtfStorage = NULL;
308 : // we now have to delete the temp file created in executeDrop
309 0 : INetURLObject aURL;
310 0 : aURL.SetURL(_rDesc.aUrl);
311 0 : ::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::NO_DECODE));
312 : }
313 0 : else if ( !_rDesc.bError )
314 0 : pasteTable(_rDesc.aDroppedData,i_rDestDataSource,_xConnection);
315 : else
316 0 : m_pController->showError(SQLException(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE), *m_pController, OUString("S1000"), 0, Any()));
317 0 : }
318 :
319 : } // namespace dbaui
320 :
321 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|