Update ooo320-m1
[ooovba.git] / sw / source / ui / shells / textsh2.cxx
blob6fa11897efb0077777d5e68a2fa1534dbf2d15a0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: textsh2.cxx,v $
10 * $Revision: 1.28 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
36 #if STLPORT_VERSION>=321
37 #include <cstdarg>
38 #endif
40 #include <svtools/svmedit.hxx>
41 #include <svtools/eitem.hxx>
42 #include <svtools/whiter.hxx>
43 #include <sfx2/event.hxx>
44 #include <sfx2/dispatch.hxx>
45 #include <sfx2/viewfrm.hxx>
46 #ifndef _MSGBOX_HXX //autogen
47 #include <vcl/msgbox.hxx>
48 #endif
49 #include <svtools/stritem.hxx>
50 #include <svtools/itemset.hxx>
51 #include <sfx2/request.hxx>
52 #include <com/sun/star/sdb/CommandType.hpp>
53 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
54 #include <com/sun/star/container/XNameAccess.hpp>
55 #include <com/sun/star/sdbc/XDataSource.hpp>
56 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
57 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
58 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
59 #include <com/sun/star/sdb/XDatabaseAccess.hpp>
60 #include <com/sun/star/beans/XPropertySet.hpp>
61 #include <com/sun/star/container/XChild.hpp>
62 #include <comphelper/processfactory.hxx>
63 #include <com/sun/star/sdbc/XRowSet.hpp>
64 #include <sfx2/frame.hxx>
65 #include <fldmgr.hxx>
66 #include <fldbas.hxx>
67 #include "dbmgr.hxx"
68 #include <comphelper/uno3.hxx>
69 #include <svx/dataaccessdescriptor.hxx>
70 #include <memory>
72 #include <vcl/svapp.hxx>
74 #include "view.hxx"
75 #include "wrtsh.hxx"
76 #include "swtypes.hxx"
77 #include "cmdid.h"
78 #include "swevent.hxx"
79 #include "shells.hrc"
80 #include "textsh.hxx"
81 #include "swabstdlg.hxx"
82 #include "dbui.hrc"
84 #include <unomid.h>
86 using namespace ::svx;
87 using namespace ::com::sun::star;
88 using namespace ::com::sun::star::uno;
89 using namespace ::com::sun::star::container;
90 using namespace ::com::sun::star::lang;
91 using namespace ::com::sun::star::sdb;
92 using namespace ::com::sun::star::sdbc;
93 using namespace ::com::sun::star::sdbcx;
94 using namespace ::com::sun::star::beans;
97 #define DB_DD_DELIM 0x0b
99 struct DBTextStruct_Impl
101 SwDBData aDBData;
102 Sequence<Any> aSelection;
103 Reference<XResultSet> xCursor;
104 Reference<XConnection> xConnection;
106 inline void AddSelList( List& rLst, long nRow )
108 rLst.Insert( (void*)nRow , LIST_APPEND );
110 void SwTextShell::ExecDB(SfxRequest &rReq)
112 const SfxItemSet *pArgs = rReq.GetArgs();
113 SwNewDBMgr* pNewDBMgr = GetShell().GetNewDBMgr();
114 USHORT nSlot = rReq.GetSlot();
115 ::rtl::OUString sSourceArg, sCommandArg;
116 sal_Int32 nCommandTypeArg = 0;
118 const SfxPoolItem* pSourceItem = 0;
119 const SfxPoolItem* pCursorItem = 0;
120 const SfxPoolItem* pConnectionItem = 0;
121 const SfxPoolItem* pCommandItem = 0;
122 const SfxPoolItem* pCommandTypeItem = 0;
123 const SfxPoolItem* pSelectionItem = 0;
125 // first get the selection of rows to be inserted
126 pArgs->GetItemState(FN_DB_DATA_SELECTION_ANY, FALSE, &pSelectionItem);
128 Sequence<Any> aSelection;
129 if(pSelectionItem)
130 ((SfxUsrAnyItem*)pSelectionItem)->GetValue() >>= aSelection;
132 // get the data source name
133 pArgs->GetItemState(FN_DB_DATA_SOURCE_ANY, FALSE, &pSourceItem);
134 if(pSourceItem)
135 ((const SfxUsrAnyItem*)pSourceItem)->GetValue() >>= sSourceArg;
137 // get the command
138 pArgs->GetItemState(FN_DB_DATA_COMMAND_ANY, FALSE, &pCommandItem);
139 if(pCommandItem)
140 ((const SfxUsrAnyItem*)pCommandItem)->GetValue() >>= sCommandArg;
142 // get the command type
143 pArgs->GetItemState(FN_DB_DATA_COMMAND_TYPE_ANY, FALSE, &pCommandTypeItem);
144 if(pCommandTypeItem)
145 ((const SfxUsrAnyItem*)pCommandTypeItem)->GetValue() >>= nCommandTypeArg;
147 Reference<XConnection> xConnection;
148 pArgs->GetItemState(FN_DB_CONNECTION_ANY, FALSE, &pConnectionItem);
149 if ( pConnectionItem )
150 ((const SfxUsrAnyItem*)pConnectionItem)->GetValue() >>= xConnection;
151 // may be we even get no connection
152 if ( !xConnection.is() )
154 Reference<XDataSource> xSource;
155 xConnection = pNewDBMgr->GetConnection(sSourceArg, xSource);
157 if(!xConnection.is())
158 return ;
160 // get the cursor, we use to travel, may be NULL
161 Reference<XResultSet> xCursor;
162 pArgs->GetItemState(FN_DB_DATA_CURSOR_ANY, FALSE, &pCursorItem);
163 if ( pCursorItem )
164 ((const SfxUsrAnyItem*)pCursorItem)->GetValue() >>= xCursor;
166 switch (nSlot)
168 case FN_QRY_INSERT:
170 if(pSourceItem && pCommandItem && pCommandTypeItem)
172 DBTextStruct_Impl* pNew = new DBTextStruct_Impl;
173 pNew->aDBData.sDataSource = sSourceArg;
174 pNew->aDBData.sCommand = sCommandArg;
175 pNew->aDBData.nCommandType = nCommandTypeArg;
176 pNew->aSelection = aSelection;
177 //if the cursor is NULL, it must be created inside InsertDBTextHdl
178 // because it called via a PostUserEvent
179 pNew->xCursor = xCursor;
180 pNew->xConnection = xConnection;
182 Application::PostUserEvent( STATIC_LINK( this, SwBaseShell,
183 InsertDBTextHdl ), pNew );
184 // the pNew will be removed in InsertDBTextHdl !!
187 break;
189 case FN_QRY_MERGE_FIELD:
191 // we don't get any cursor, so we must create our own
192 BOOL bDisposeResultSet = FALSE;
193 if ( !xCursor.is() )
195 xCursor = SwNewDBMgr::createCursor(sSourceArg,sCommandArg,nCommandTypeArg,xConnection);
196 bDisposeResultSet = xCursor.is();
199 ODataAccessDescriptor aDescriptor;
200 aDescriptor.setDataSource(sSourceArg);
201 aDescriptor[daCommand] <<= sCommandArg;
202 aDescriptor[daCursor] <<= xCursor;
203 aDescriptor[daSelection] <<= aSelection;
204 aDescriptor[daCommandType] <<= nCommandTypeArg;
206 SwMergeDescriptor aMergeDesc( DBMGR_MERGE, *GetShellPtr(), aDescriptor );
207 pNewDBMgr->MergeNew(aMergeDesc);
209 if ( bDisposeResultSet )
210 ::comphelper::disposeComponent(xCursor);
212 break;
214 case FN_QRY_INSERT_FIELD:
216 const SfxPoolItem* pColumnItem = 0;
217 const SfxPoolItem* pColumnNameItem = 0;
219 pArgs->GetItemState(FN_DB_COLUMN_ANY, FALSE, &pColumnItem);
220 pArgs->GetItemState(FN_DB_DATA_COLUMN_NAME_ANY, FALSE, &pColumnNameItem);
222 ::rtl::OUString sColumnName;
223 if(pColumnNameItem)
224 ((SfxUsrAnyItem*)pColumnNameItem)->GetValue() >>= sColumnName;
225 String sDBName = sSourceArg;
226 sDBName += DB_DELIM;
227 sDBName += (String)sCommandArg;
228 sDBName += DB_DELIM;
229 sDBName += String::CreateFromInt32(nCommandTypeArg);
230 sDBName += DB_DELIM;
231 sDBName += (String)sColumnName;
233 SwFldMgr aFldMgr(GetShellPtr());
234 SwInsertFld_Data aData(TYP_DBFLD, 0, sDBName, aEmptyStr, 0, FALSE, TRUE);
235 if(pConnectionItem)
236 aData.aDBConnection = ((SfxUsrAnyItem*)pConnectionItem)->GetValue();
237 if(pColumnItem)
238 aData.aDBColumn = ((SfxUsrAnyItem*)pColumnItem)->GetValue();
239 aFldMgr.InsertFld(aData);
240 SfxViewFrame* pViewFrame = GetView().GetViewFrame();
241 uno::Reference< frame::XDispatchRecorder > xRecorder =
242 pViewFrame->GetBindings().GetRecorder();
243 if ( xRecorder.is() )
245 SfxRequest aReq( pViewFrame, FN_INSERT_DBFIELD );
246 aReq.AppendItem( SfxUInt16Item(FN_PARAM_FIELD_TYPE, TYP_DBFLD));
247 aReq.AppendItem( SfxStringItem( FN_INSERT_DBFIELD, sDBName ));
248 aReq.AppendItem( SfxStringItem( FN_PARAM_1, sCommandArg ));
249 aReq.AppendItem( SfxStringItem( FN_PARAM_2, sColumnName ));
250 aReq.AppendItem( SfxInt32Item( FN_PARAM_3, nCommandTypeArg));
251 aReq.Done();
254 break;
256 default:
257 ASSERT(!this, falscher Dispatcher);
258 return;
262 /*--------------------------------------------------------------------
263 Beschreibung:
264 --------------------------------------------------------------------*/
266 IMPL_STATIC_LINK( SwBaseShell, InsertDBTextHdl, DBTextStruct_Impl*, pDBStruct )
268 if( pDBStruct )
270 sal_Bool bDispose = sal_False;
271 Reference< sdbc::XConnection> xConnection = pDBStruct->xConnection;
272 Reference<XDataSource> xSource = SwNewDBMgr::getDataSourceAsParent(xConnection,pDBStruct->aDBData.sDataSource);
273 // #111987# the connection is disposed an so no parent has been found
274 if(xConnection.is() && !xSource.is())
275 return 0;
277 if ( !xConnection.is() )
279 xConnection = SwNewDBMgr::GetConnection(pDBStruct->aDBData.sDataSource, xSource);
280 bDispose = sal_True;
283 Reference< XColumnsSupplier> xColSupp;
284 if(xConnection.is())
285 xColSupp = SwNewDBMgr::GetColumnSupplier(xConnection,
286 pDBStruct->aDBData.sCommand,
287 pDBStruct->aDBData.nCommandType == CommandType::QUERY ?
288 SW_DB_SELECT_QUERY : SW_DB_SELECT_TABLE);
290 if( xColSupp.is() )
292 SwDBData aDBData = pDBStruct->aDBData;
293 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
294 DBG_ASSERT(pFact, "SwAbstractDialogFactory fail!");
295 ::std::auto_ptr<AbstractSwInsertDBColAutoPilot>pDlg (pFact->CreateSwInsertDBColAutoPilot( pThis->GetView(),
296 xSource,
297 xColSupp,
298 aDBData,
299 DLG_AP_INSERT_DB_SEL ));
300 if( RET_OK == pDlg->Execute() )
302 Reference <XResultSet> xResSet = pDBStruct->xCursor;
303 pDlg->DataToDoc( pDBStruct->aSelection, xSource, xConnection, xResSet);
306 if ( bDispose )
307 ::comphelper::disposeComponent(xConnection);
310 delete pDBStruct;
311 return 0;