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 <cstdarg>
21 :
22 : #include <svtools/svmedit.hxx>
23 : #include <svl/eitem.hxx>
24 : #include <svl/whiter.hxx>
25 : #include <sfx2/event.hxx>
26 : #include <sfx2/dispatch.hxx>
27 : #include <sfx2/viewfrm.hxx>
28 : #include <vcl/msgbox.hxx>
29 : #include <svl/stritem.hxx>
30 : #include <svl/itemset.hxx>
31 : #include <sfx2/request.hxx>
32 : #include <com/sun/star/sdb/CommandType.hpp>
33 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 : #include <com/sun/star/container/XNameAccess.hpp>
35 : #include <com/sun/star/sdbc/XDataSource.hpp>
36 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
37 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
38 : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
39 : #include <com/sun/star/sdb/XDatabaseAccess.hpp>
40 : #include <com/sun/star/beans/XPropertySet.hpp>
41 : #include <com/sun/star/container/XChild.hpp>
42 : #include <comphelper/processfactory.hxx>
43 : #include <com/sun/star/sdbc/XRowSet.hpp>
44 : #include <sfx2/frame.hxx>
45 : #include <fldmgr.hxx>
46 : #include <fldbas.hxx>
47 : #include "dbmgr.hxx"
48 : #include <comphelper/uno3.hxx>
49 : #include <svx/dataaccessdescriptor.hxx>
50 :
51 : #include <vcl/svapp.hxx>
52 :
53 : #include "view.hxx"
54 : #include "wrtsh.hxx"
55 : #include "swtypes.hxx"
56 : #include "cmdid.h"
57 : #include "swevent.hxx"
58 : #include "shells.hrc"
59 : #include "textsh.hxx"
60 : #include "swabstdlg.hxx"
61 : #include "dbui.hrc"
62 :
63 : #include <unomid.h>
64 :
65 : #include <boost/scoped_ptr.hpp>
66 :
67 : using namespace ::svx;
68 : using namespace ::com::sun::star;
69 : using namespace ::com::sun::star::uno;
70 : using namespace ::com::sun::star::container;
71 : using namespace ::com::sun::star::lang;
72 : using namespace ::com::sun::star::sdb;
73 : using namespace ::com::sun::star::sdbc;
74 : using namespace ::com::sun::star::sdbcx;
75 : using namespace ::com::sun::star::beans;
76 : using namespace ::com::sun::star::frame;
77 :
78 0 : struct DBTextStruct_Impl
79 : {
80 : SwDBData aDBData;
81 : Sequence<Any> aSelection;
82 : Reference<XResultSet> xCursor;
83 : Reference<XConnection> xConnection;
84 : };
85 :
86 0 : void SwTextShell::ExecDB(SfxRequest &rReq)
87 : {
88 0 : const SfxItemSet *pArgs = rReq.GetArgs();
89 0 : SwNewDBMgr* pNewDBMgr = GetShell().GetNewDBMgr();
90 0 : sal_uInt16 nSlot = rReq.GetSlot();
91 0 : OUString sSourceArg, sCommandArg;
92 0 : sal_Int32 nCommandTypeArg = 0;
93 :
94 0 : const SfxPoolItem* pSourceItem = 0;
95 0 : const SfxPoolItem* pCursorItem = 0;
96 0 : const SfxPoolItem* pConnectionItem = 0;
97 0 : const SfxPoolItem* pCommandItem = 0;
98 0 : const SfxPoolItem* pCommandTypeItem = 0;
99 0 : const SfxPoolItem* pSelectionItem = 0;
100 :
101 : // first get the selection of rows to be inserted
102 0 : pArgs->GetItemState(FN_DB_DATA_SELECTION_ANY, false, &pSelectionItem);
103 :
104 0 : Sequence<Any> aSelection;
105 0 : if(pSelectionItem)
106 0 : ((SfxUsrAnyItem*)pSelectionItem)->GetValue() >>= aSelection;
107 :
108 : // get the data source name
109 0 : pArgs->GetItemState(FN_DB_DATA_SOURCE_ANY, false, &pSourceItem);
110 0 : if(pSourceItem)
111 0 : ((const SfxUsrAnyItem*)pSourceItem)->GetValue() >>= sSourceArg;
112 :
113 : // get the command
114 0 : pArgs->GetItemState(FN_DB_DATA_COMMAND_ANY, false, &pCommandItem);
115 0 : if(pCommandItem)
116 0 : ((const SfxUsrAnyItem*)pCommandItem)->GetValue() >>= sCommandArg;
117 :
118 : // get the command type
119 0 : pArgs->GetItemState(FN_DB_DATA_COMMAND_TYPE_ANY, false, &pCommandTypeItem);
120 0 : if(pCommandTypeItem)
121 0 : ((const SfxUsrAnyItem*)pCommandTypeItem)->GetValue() >>= nCommandTypeArg;
122 :
123 0 : Reference<XConnection> xConnection;
124 0 : pArgs->GetItemState(FN_DB_CONNECTION_ANY, false, &pConnectionItem);
125 0 : if ( pConnectionItem )
126 0 : ((const SfxUsrAnyItem*)pConnectionItem)->GetValue() >>= xConnection;
127 : // may be we even get no connection
128 0 : if ( !xConnection.is() )
129 : {
130 0 : Reference<XDataSource> xSource;
131 0 : xConnection = pNewDBMgr->GetConnection(sSourceArg, xSource);
132 : }
133 0 : if(!xConnection.is())
134 0 : return ;
135 :
136 : // get the cursor, we use to travel, may be NULL
137 0 : Reference<XResultSet> xCursor;
138 0 : pArgs->GetItemState(FN_DB_DATA_CURSOR_ANY, false, &pCursorItem);
139 0 : if ( pCursorItem )
140 0 : ((const SfxUsrAnyItem*)pCursorItem)->GetValue() >>= xCursor;
141 :
142 0 : switch (nSlot)
143 : {
144 : case FN_QRY_INSERT:
145 : {
146 0 : if(pSourceItem && pCommandItem && pCommandTypeItem)
147 : {
148 0 : DBTextStruct_Impl* pNew = new DBTextStruct_Impl;
149 0 : pNew->aDBData.sDataSource = sSourceArg;
150 0 : pNew->aDBData.sCommand = sCommandArg;
151 0 : pNew->aDBData.nCommandType = nCommandTypeArg;
152 0 : pNew->aSelection = aSelection;
153 : //if the cursor is NULL, it must be created inside InsertDBTextHdl
154 : // because it called via a PostUserEvent
155 0 : pNew->xCursor = xCursor;
156 0 : pNew->xConnection = xConnection;
157 :
158 : Application::PostUserEvent( STATIC_LINK( this, SwBaseShell,
159 0 : InsertDBTextHdl ), pNew );
160 : // the pNew will be removed in InsertDBTextHdl !!
161 : }
162 : }
163 0 : break;
164 :
165 : case FN_QRY_MERGE_FIELD:
166 : {
167 : // we don't get any cursor, so we must create our own
168 0 : sal_Bool bDisposeResultSet = sal_False;
169 0 : if ( !xCursor.is() )
170 : {
171 0 : xCursor = SwNewDBMgr::createCursor(sSourceArg,sCommandArg,nCommandTypeArg,xConnection);
172 0 : bDisposeResultSet = xCursor.is();
173 : }
174 :
175 0 : ODataAccessDescriptor aDescriptor;
176 0 : aDescriptor.setDataSource(sSourceArg);
177 0 : aDescriptor[daCommand] <<= sCommandArg;
178 0 : aDescriptor[daCursor] <<= xCursor;
179 0 : aDescriptor[daSelection] <<= aSelection;
180 0 : aDescriptor[daCommandType] <<= nCommandTypeArg;
181 :
182 0 : SwMergeDescriptor aMergeDesc( DBMGR_MERGE, *GetShellPtr(), aDescriptor );
183 0 : pNewDBMgr->MergeNew(aMergeDesc);
184 :
185 0 : if ( bDisposeResultSet )
186 0 : ::comphelper::disposeComponent(xCursor);
187 : }
188 0 : break;
189 :
190 : case FN_QRY_INSERT_FIELD:
191 : {
192 0 : const SfxPoolItem* pColumnItem = 0;
193 0 : const SfxPoolItem* pColumnNameItem = 0;
194 :
195 0 : pArgs->GetItemState(FN_DB_COLUMN_ANY, false, &pColumnItem);
196 0 : pArgs->GetItemState(FN_DB_DATA_COLUMN_NAME_ANY, false, &pColumnNameItem);
197 :
198 0 : OUString sColumnName;
199 0 : if(pColumnNameItem)
200 0 : ((SfxUsrAnyItem*)pColumnNameItem)->GetValue() >>= sColumnName;
201 0 : OUString sDBName = sSourceArg;
202 0 : sDBName += OUString(DB_DELIM);
203 0 : sDBName += sCommandArg;
204 0 : sDBName += OUString(DB_DELIM);
205 0 : sDBName += OUString::number(nCommandTypeArg);
206 0 : sDBName += OUString(DB_DELIM);
207 0 : sDBName += sColumnName;
208 :
209 0 : SwFldMgr aFldMgr(GetShellPtr());
210 0 : SwInsertFld_Data aData(TYP_DBFLD, 0, sDBName, OUString(), 0);
211 0 : if(pConnectionItem)
212 0 : aData.aDBConnection = ((SfxUsrAnyItem*)pConnectionItem)->GetValue();
213 0 : if(pColumnItem)
214 0 : aData.aDBColumn = ((SfxUsrAnyItem*)pColumnItem)->GetValue();
215 0 : aFldMgr.InsertFld(aData);
216 0 : SfxViewFrame* pViewFrame = GetView().GetViewFrame();
217 : uno::Reference< XDispatchRecorder > xRecorder =
218 0 : pViewFrame->GetBindings().GetRecorder();
219 0 : if ( xRecorder.is() )
220 : {
221 0 : SfxRequest aReq( pViewFrame, FN_INSERT_DBFIELD );
222 0 : aReq.AppendItem( SfxUInt16Item(FN_PARAM_FIELD_TYPE, TYP_DBFLD));
223 0 : aReq.AppendItem( SfxStringItem( FN_INSERT_DBFIELD, sDBName ));
224 0 : aReq.AppendItem( SfxStringItem( FN_PARAM_1, sCommandArg ));
225 0 : aReq.AppendItem( SfxStringItem( FN_PARAM_2, sColumnName ));
226 0 : aReq.AppendItem( SfxInt32Item( FN_PARAM_3, nCommandTypeArg));
227 0 : aReq.Done();
228 0 : }
229 : }
230 0 : break;
231 :
232 : default:
233 : OSL_ENSURE(!this, "wrong dispatcher");
234 0 : return;
235 0 : }
236 : }
237 :
238 0 : IMPL_STATIC_LINK( SwBaseShell, InsertDBTextHdl, DBTextStruct_Impl*, pDBStruct )
239 : {
240 0 : if( pDBStruct )
241 : {
242 0 : bool bDispose = false;
243 0 : Reference< sdbc::XConnection> xConnection = pDBStruct->xConnection;
244 0 : Reference<XDataSource> xSource = SwNewDBMgr::getDataSourceAsParent(xConnection,pDBStruct->aDBData.sDataSource);
245 : // #111987# the connection is disposed an so no parent has been found
246 0 : if(xConnection.is() && !xSource.is())
247 0 : return 0;
248 :
249 0 : if ( !xConnection.is() )
250 : {
251 0 : xConnection = SwNewDBMgr::GetConnection(pDBStruct->aDBData.sDataSource, xSource);
252 0 : bDispose = true;
253 : }
254 :
255 0 : Reference< XColumnsSupplier> xColSupp;
256 0 : if(xConnection.is())
257 0 : xColSupp = SwNewDBMgr::GetColumnSupplier(xConnection,
258 : pDBStruct->aDBData.sCommand,
259 0 : pDBStruct->aDBData.nCommandType == CommandType::QUERY ?
260 0 : SW_DB_SELECT_QUERY : SW_DB_SELECT_TABLE);
261 :
262 0 : if( xColSupp.is() )
263 : {
264 0 : SwDBData aDBData = pDBStruct->aDBData;
265 0 : SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
266 : OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
267 0 : boost::scoped_ptr<AbstractSwInsertDBColAutoPilot>pDlg (pFact->CreateSwInsertDBColAutoPilot(pThis->GetView(),
268 : xSource,
269 : xColSupp,
270 0 : aDBData));
271 0 : if( RET_OK == pDlg->Execute() )
272 : {
273 0 : Reference <XResultSet> xResSet = pDBStruct->xCursor;
274 0 : pDlg->DataToDoc( pDBStruct->aSelection, xSource, xConnection, xResSet);
275 0 : }
276 : }
277 0 : if ( bDispose )
278 0 : ::comphelper::disposeComponent(xConnection);
279 : }
280 :
281 0 : delete pDBStruct;
282 0 : return 0;
283 0 : }
284 :
285 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|