1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "TableCopyHelper.hxx"
30 #include "dbustrings.hrc"
31 #include "sqlmessage.hxx"
32 #include <vcl/msgbox.hxx>
33 #include "WCopyTable.hxx"
34 #include "genericcontroller.hxx"
36 #include <com/sun/star/task/XInteractionHandler.hpp>
37 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
38 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
39 #include <com/sun/star/sdb/application/CopyTableWizard.hpp>
40 #include <com/sun/star/sdb/DataAccessDescriptorFactory.hpp>
42 #include "RtfReader.hxx"
43 #include "HtmlReader.hxx"
44 #include "TokenWriter.hxx"
45 #include "UITools.hxx"
46 #include "dataview.hxx"
47 #include "dbu_resource.hrc"
48 #include <unotools/ucbhelper.hxx>
49 #include <tools/urlobj.hxx>
50 #include <tools/diagnose_ex.h>
51 #include <comphelper/componentcontext.hxx>
52 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
53 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
54 #include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
55 #include <com/sun/star/sdb/SQLContext.hpp>
56 #include <com/sun/star/sdbc/XParameters.hpp>
57 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
58 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
59 #include <com/sun/star/sdbc/XColumnLocate.hpp>
60 #include <com/sun/star/sdbcx/XRowLocate.hpp>
61 #include <vcl/waitobj.hxx>
62 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
63 #include <unotools/tempfile.hxx>
64 #include <cppuhelper/exc_hlp.hxx>
66 #include "dbexchange.hxx"
67 #include <rtl/logfile.hxx>
68 //........................................................................
71 //........................................................................
72 using namespace ::dbtools
;
73 using namespace ::svx
;
74 using namespace ::com::sun::star::uno
;
75 using namespace ::com::sun::star::task
;
76 using namespace ::com::sun::star::beans
;
77 using namespace ::com::sun::star::lang
;
78 using namespace ::com::sun::star::container
;
79 using namespace ::com::sun::star::sdb
;
80 using namespace ::com::sun::star::sdb::application
;
81 using namespace ::com::sun::star::sdbc
;
82 using namespace ::com::sun::star::sdbcx
;
83 using namespace ::com::sun::star::frame
;
84 using namespace ::com::sun::star::ucb
;
86 // -----------------------------------------------------------------------------
87 OTableCopyHelper::OTableCopyHelper(OGenericUnoController
* _pControler
)
88 :m_pController(_pControler
)
92 // -----------------------------------------------------------------------------
93 void OTableCopyHelper::insertTable( const ::rtl::OUString
& i_rSourceDataSource
, const Reference
<XConnection
>& i_rSourceConnection
,
94 const ::rtl::OUString
& i_rCommand
, const sal_Int32 i_nCommandType
,
95 const Reference
< XResultSet
>& i_rSourceRows
, const Sequence
< Any
>& i_rSelection
, const sal_Bool i_bBookmarkSelection
,
96 const ::rtl::OUString
& i_rDestDataSource
, const Reference
<XConnection
>& i_rDestConnection
)
98 if ( CommandType::QUERY
!= i_nCommandType
&& CommandType::TABLE
!= i_nCommandType
)
100 OSL_FAIL( "OTableCopyHelper::insertTable: invalid call (no supported format found)!" );
106 Reference
<XConnection
> xSrcConnection( i_rSourceConnection
);
107 if ( i_rSourceDataSource
== i_rDestDataSource
)
108 xSrcConnection
= i_rDestConnection
;
110 if ( !xSrcConnection
.is() || !i_rDestConnection
.is() )
112 OSL_FAIL( "OTableCopyHelper::insertTable: no connection/s!" );
116 ::comphelper::ComponentContext
aContext( m_pController
->getORB() );
118 Reference
< XDataAccessDescriptorFactory
> xFactory( DataAccessDescriptorFactory::get( aContext
.getUNOContext() ) );
120 Reference
< XPropertySet
> xSource( xFactory
->createDataAccessDescriptor(), UNO_SET_THROW
);
121 xSource
->setPropertyValue( PROPERTY_COMMAND_TYPE
, makeAny( i_nCommandType
) );
122 xSource
->setPropertyValue( PROPERTY_COMMAND
, makeAny( i_rCommand
) );
123 xSource
->setPropertyValue( PROPERTY_ACTIVE_CONNECTION
, makeAny( xSrcConnection
) );
124 xSource
->setPropertyValue( PROPERTY_RESULT_SET
, makeAny( i_rSourceRows
) );
125 xSource
->setPropertyValue( PROPERTY_SELECTION
, makeAny( i_rSelection
) );
126 xSource
->setPropertyValue( PROPERTY_BOOKMARK_SELECTION
, makeAny( i_bBookmarkSelection
) );
128 Reference
< XPropertySet
> xDest( xFactory
->createDataAccessDescriptor(), UNO_SET_THROW
);
129 xDest
->setPropertyValue( PROPERTY_ACTIVE_CONNECTION
, makeAny( i_rDestConnection
) );
131 Reference
< XCopyTableWizard
> xWizard( CopyTableWizard::create( aContext
.getUNOContext(), xSource
, xDest
), UNO_SET_THROW
);
133 ::rtl::OUString
sTableNameForAppend( GetTableNameForAppend() );
134 xWizard
->setDestinationTableName( GetTableNameForAppend() );
136 bool bAppendToExisting
= !sTableNameForAppend
.isEmpty();
137 xWizard
->setOperation( bAppendToExisting
? CopyTableOperation::AppendData
: CopyTableOperation::CopyDefinitionAndData
);
141 catch( const SQLException
& )
143 m_pController
->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
145 catch( const Exception
& )
147 DBG_UNHANDLED_EXCEPTION();
151 // -----------------------------------------------------------------------------
152 void OTableCopyHelper::pasteTable( const ::svx::ODataAccessDescriptor
& _rPasteData
, const ::rtl::OUString
& i_rDestDataSourceName
,
153 const SharedConnection
& i_rDestConnection
)
155 ::rtl::OUString sSrcDataSourceName
= _rPasteData
.getDataSource();
157 ::rtl::OUString sCommand
;
158 _rPasteData
[ daCommand
] >>= sCommand
;
160 Reference
<XConnection
> xSrcConnection
;
161 if ( _rPasteData
.has(daConnection
) )
163 OSL_VERIFY( _rPasteData
[daConnection
] >>= xSrcConnection
);
166 Reference
< XResultSet
> xResultSet
;
167 if ( _rPasteData
.has(daCursor
) )
169 OSL_VERIFY( _rPasteData
[ daCursor
] >>= xResultSet
);
172 Sequence
< Any
> aSelection
;
173 if ( _rPasteData
.has( daSelection
) )
175 OSL_VERIFY( _rPasteData
[ daSelection
] >>= aSelection
);
176 OSL_ENSURE( _rPasteData
.has( daBookmarkSelection
), "OTableCopyHelper::pasteTable: you should specify BookmarkSelection, too, to be on the safe side!" );
180 sal_Bool
bBookmarkSelection( sal_True
);
181 if ( _rPasteData
.has( daBookmarkSelection
) )
183 OSL_VERIFY( _rPasteData
[ daBookmarkSelection
] >>= bBookmarkSelection
);
185 OSL_ENSURE( bBookmarkSelection
, "OTableCopyHelper::pasteTable: working with selection-indicies (instead of bookmarks) is error-prone, and thus deprecated!" );
187 sal_Int32 nCommandType
= CommandType::COMMAND
;
188 if ( _rPasteData
.has(daCommandType
) )
189 _rPasteData
[daCommandType
] >>= nCommandType
;
191 insertTable( sSrcDataSourceName
, xSrcConnection
, sCommand
, nCommandType
,
192 xResultSet
, aSelection
, bBookmarkSelection
,
193 i_rDestDataSourceName
, i_rDestConnection
);
196 // -----------------------------------------------------------------------------
197 void OTableCopyHelper::pasteTable( SotFormatStringId _nFormatId
198 ,const TransferableDataHelper
& _rTransData
199 ,const ::rtl::OUString
& i_rDestDataSource
200 ,const SharedConnection
& _xConnection
)
202 if ( _nFormatId
== SOT_FORMATSTR_ID_DBACCESS_TABLE
|| _nFormatId
== SOT_FORMATSTR_ID_DBACCESS_QUERY
)
204 if ( ODataAccessObjectTransferable::canExtractObjectDescriptor(_rTransData
.GetDataFlavorExVector()) )
206 ::svx::ODataAccessDescriptor aPasteData
= ODataAccessObjectTransferable::extractObjectDescriptor(_rTransData
);
207 pasteTable( aPasteData
,i_rDestDataSource
,_xConnection
);
210 else if ( _rTransData
.HasFormat(_nFormatId
) )
214 DropDescriptor aTrans
;
215 if ( _nFormatId
!= SOT_FORMAT_RTF
)
216 const_cast<TransferableDataHelper
&>(_rTransData
).GetSotStorageStream(SOT_FORMATSTR_ID_HTML
,aTrans
.aHtmlRtfStorage
);
218 const_cast<TransferableDataHelper
&>(_rTransData
).GetSotStorageStream(SOT_FORMAT_RTF
,aTrans
.aHtmlRtfStorage
);
220 aTrans
.nType
= E_TABLE
;
221 aTrans
.bHtml
= SOT_FORMATSTR_ID_HTML
== _nFormatId
;
222 aTrans
.sDefaultTableName
= GetTableNameForAppend();
223 if ( !copyTagTable(aTrans
,sal_False
,_xConnection
) )
224 m_pController
->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE
)),*m_pController
,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")),0,Any()));
226 catch(const SQLException
&)
228 m_pController
->showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
230 catch( const Exception
& )
232 DBG_UNHANDLED_EXCEPTION();
236 m_pController
->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE
)),*m_pController
,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")),0,Any()));
239 // -----------------------------------------------------------------------------
240 void OTableCopyHelper::pasteTable( const TransferableDataHelper
& _rTransData
241 ,const ::rtl::OUString
& i_rDestDataSource
242 ,const SharedConnection
& _xConnection
)
244 if ( _rTransData
.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE
) || _rTransData
.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY
) )
245 pasteTable( SOT_FORMATSTR_ID_DBACCESS_TABLE
,_rTransData
,i_rDestDataSource
,_xConnection
);
246 else if ( _rTransData
.HasFormat(SOT_FORMATSTR_ID_HTML
) )
247 pasteTable( SOT_FORMATSTR_ID_HTML
,_rTransData
,i_rDestDataSource
,_xConnection
);
248 else if ( _rTransData
.HasFormat(SOT_FORMAT_RTF
) )
249 pasteTable( SOT_FORMAT_RTF
,_rTransData
,i_rDestDataSource
,_xConnection
);
252 // -----------------------------------------------------------------------------
253 sal_Bool
OTableCopyHelper::copyTagTable(OTableCopyHelper::DropDescriptor
& _rDesc
, sal_Bool _bCheck
,const SharedConnection
& _xConnection
)
255 Reference
<XEventListener
> xEvt
;
256 ODatabaseImportExport
* pImport
= NULL
;
258 pImport
= new OHTMLImportExport(_xConnection
,getNumberFormatter(_xConnection
,m_pController
->getORB()),m_pController
->getORB());
260 pImport
= new ORTFImportExport(_xConnection
,getNumberFormatter(_xConnection
,m_pController
->getORB()),m_pController
->getORB());
263 SvStream
* pStream
= (SvStream
*)(SotStorageStream
*)_rDesc
.aHtmlRtfStorage
;
265 pImport
->enableCheckOnly();
267 //set the selected tablename
268 pImport
->setSTableName(_rDesc
.sDefaultTableName
);
270 pImport
->setStream(pStream
);
271 return pImport
->Read();
273 // -----------------------------------------------------------------------------
274 sal_Bool
OTableCopyHelper::isTableFormat(const TransferableDataHelper
& _rClipboard
) const
276 sal_Bool bTableFormat
= _rClipboard
.HasFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE
)
277 || _rClipboard
.HasFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY
)
278 || _rClipboard
.HasFormat(SOT_FORMAT_RTF
)
279 || _rClipboard
.HasFormat(SOT_FORMATSTR_ID_HTML
);
283 // -----------------------------------------------------------------------------
284 sal_Bool
OTableCopyHelper::copyTagTable(const TransferableDataHelper
& _aDroppedData
285 ,DropDescriptor
& _rAsyncDrop
286 ,const SharedConnection
& _xConnection
)
288 sal_Bool bRet
= sal_False
;
289 sal_Bool bHtml
= _aDroppedData
.HasFormat(SOT_FORMATSTR_ID_HTML
);
290 if ( bHtml
|| _aDroppedData
.HasFormat(SOT_FORMAT_RTF
) )
293 const_cast<TransferableDataHelper
&>(_aDroppedData
).GetSotStorageStream(SOT_FORMATSTR_ID_HTML
,_rAsyncDrop
.aHtmlRtfStorage
);
295 const_cast<TransferableDataHelper
&>(_aDroppedData
).GetSotStorageStream(SOT_FORMAT_RTF
,_rAsyncDrop
.aHtmlRtfStorage
);
297 _rAsyncDrop
.bHtml
= bHtml
;
298 _rAsyncDrop
.bError
= !copyTagTable(_rAsyncDrop
,sal_True
,_xConnection
);
300 bRet
= ( !_rAsyncDrop
.bError
&& _rAsyncDrop
.aHtmlRtfStorage
.Is() );
303 // now we need to copy the stream
304 ::utl::TempFile aTmp
;
305 aTmp
.EnableKillingFile(sal_False
);
306 _rAsyncDrop
.aUrl
= aTmp
.GetURL();
307 SotStorageStreamRef aNew
= new SotStorageStream( aTmp
.GetFileName() );
308 _rAsyncDrop
.aHtmlRtfStorage
->Seek(STREAM_SEEK_TO_BEGIN
);
309 _rAsyncDrop
.aHtmlRtfStorage
->CopyTo( aNew
);
311 _rAsyncDrop
.aHtmlRtfStorage
= aNew
;
314 _rAsyncDrop
.aHtmlRtfStorage
= NULL
;
318 // -----------------------------------------------------------------------------
319 void OTableCopyHelper::asyncCopyTagTable( DropDescriptor
& _rDesc
320 ,const ::rtl::OUString
& i_rDestDataSource
321 ,const SharedConnection
& _xConnection
)
323 if ( _rDesc
.aHtmlRtfStorage
.Is() )
325 copyTagTable(_rDesc
,sal_False
,_xConnection
);
326 _rDesc
.aHtmlRtfStorage
= NULL
;
327 // we now have to delete the temp file created in executeDrop
329 aURL
.SetURL(_rDesc
.aUrl
);
330 ::utl::UCBContentHelper::Kill(aURL
.GetMainURL(INetURLObject::NO_DECODE
));
332 else if ( !_rDesc
.bError
)
333 pasteTable(_rDesc
.aDroppedData
,i_rDestDataSource
,_xConnection
);
335 m_pController
->showError(SQLException(String(ModuleRes(STR_NO_TABLE_FORMAT_INSIDE
)),*m_pController
,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")),0,Any()));
337 // -----------------------------------------------------------------------------
338 //........................................................................
340 //........................................................................
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */