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