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 "controlwizard.hxx"
21 : #include <tools/debug.hxx>
22 : #include <com/sun/star/container/XNameAccess.hpp>
23 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24 : #include <com/sun/star/sdb/DatabaseContext.hpp>
25 : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
26 : #include <com/sun/star/sdbc/XPreparedStatement.hpp>
27 : #include <com/sun/star/container/XChild.hpp>
28 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
29 : #include <com/sun/star/frame/XModel.hpp>
30 : #include <com/sun/star/sheet/XSpreadsheetView.hpp>
31 : #include <com/sun/star/drawing/XDrawView.hpp>
32 : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
33 : #include <com/sun/star/sdb/CommandType.hpp>
34 : #include <com/sun/star/sdbc/SQLWarning.hpp>
35 : #include <com/sun/star/sdb/SQLContext.hpp>
36 : #include <com/sun/star/task/InteractionHandler.hpp>
37 : #include <comphelper/processfactory.hxx>
38 : #include <comphelper/types.hxx>
39 : #include <connectivity/dbtools.hxx>
40 : #include <vcl/msgbox.hxx>
41 : #include <comphelper/interaction.hxx>
42 : #include <vcl/stdtext.hxx>
43 : #include <svtools/localresaccess.hxx>
44 : #include <connectivity/conncleanup.hxx>
45 : #include <com/sun/star/sdbc/DataType.hpp>
46 : #include <tools/urlobj.hxx>
47 : #include <vcl/layout.hxx>
48 :
49 : namespace dbp
50 : {
51 : using namespace ::com::sun::star::uno;
52 : using namespace ::com::sun::star::awt;
53 : using namespace ::com::sun::star::lang;
54 : using namespace ::com::sun::star::sdb;
55 : using namespace ::com::sun::star::sdbc;
56 : using namespace ::com::sun::star::sdbcx;
57 : using namespace ::com::sun::star::beans;
58 : using namespace ::com::sun::star::container;
59 : using namespace ::com::sun::star::drawing;
60 : using namespace ::com::sun::star::frame;
61 : using namespace ::com::sun::star::sheet;
62 : using namespace ::com::sun::star::form;
63 : using namespace ::com::sun::star::task;
64 : using namespace ::svt;
65 : using namespace ::comphelper;
66 : using namespace ::dbtools;
67 :
68 : struct OAccessRegulator
69 : {
70 : friend class OControlWizardPage;
71 :
72 : protected:
73 0 : OAccessRegulator() { }
74 : };
75 :
76 0 : OControlWizardPage::OControlWizardPage( OControlWizard* _pParent, const OString& rID, const OUString& rUIXMLDescription )
77 : :OControlWizardPage_Base( _pParent, rID, rUIXMLDescription )
78 : ,m_pFormDatasourceLabel(NULL)
79 : ,m_pFormDatasource(NULL)
80 : ,m_pFormContentTypeLabel(NULL)
81 : ,m_pFormContentType(NULL)
82 : ,m_pFormTableLabel(NULL)
83 0 : ,m_pFormTable(NULL)
84 : {
85 0 : }
86 :
87 0 : OControlWizardPage::~OControlWizardPage()
88 : {
89 0 : disposeOnce();
90 0 : }
91 :
92 0 : void OControlWizardPage::dispose()
93 : {
94 0 : m_pFormDatasourceLabel.clear();
95 0 : m_pFormDatasource.clear();
96 0 : m_pFormContentTypeLabel.clear();
97 0 : m_pFormContentType.clear();
98 0 : m_pFormTableLabel.clear();
99 0 : m_pFormTable.clear();
100 0 : OControlWizardPage_Base::dispose();
101 0 : }
102 :
103 0 : OControlWizard* OControlWizardPage::getDialog()
104 : {
105 0 : return static_cast< OControlWizard* >(GetParent());
106 : }
107 :
108 0 : const OControlWizard* OControlWizardPage::getDialog() const
109 : {
110 0 : return static_cast< OControlWizard* >(GetParent());
111 : }
112 :
113 :
114 0 : bool OControlWizardPage::updateContext()
115 : {
116 0 : return getDialog()->updateContext(OAccessRegulator());
117 : }
118 :
119 :
120 0 : Reference< XConnection > OControlWizardPage::getFormConnection() const
121 : {
122 0 : return getDialog()->getFormConnection(OAccessRegulator());
123 : }
124 :
125 :
126 0 : void OControlWizardPage::setFormConnection( const Reference< XConnection >& _rxConn, bool _bAutoDispose )
127 : {
128 0 : getDialog()->setFormConnection( OAccessRegulator(), _rxConn, _bAutoDispose );
129 0 : }
130 :
131 :
132 0 : const OControlWizardContext& OControlWizardPage::getContext()
133 : {
134 0 : return getDialog()->getContext();
135 : }
136 :
137 :
138 0 : void OControlWizardPage::fillListBox(ListBox& _rList, const Sequence< OUString >& _rItems, bool _bClear)
139 : {
140 0 : if (_bClear)
141 0 : _rList.Clear();
142 0 : const OUString* pItems = _rItems.getConstArray();
143 0 : const OUString* pEnd = pItems + _rItems.getLength();
144 : ::svt::WizardTypes::WizardState nPos;
145 0 : sal_Int32 nIndex = 0;
146 0 : for (;pItems < pEnd; ++pItems, ++nIndex)
147 : {
148 0 : nPos = _rList.InsertEntry(*pItems);
149 0 : _rList.SetEntryData(nPos, reinterpret_cast<void*>(nIndex));
150 : }
151 0 : }
152 :
153 :
154 0 : void OControlWizardPage::fillListBox(ComboBox& _rList, const Sequence< OUString >& _rItems, bool _bClear)
155 : {
156 0 : if (_bClear)
157 0 : _rList.Clear();
158 0 : const OUString* pItems = _rItems.getConstArray();
159 0 : const OUString* pEnd = pItems + _rItems.getLength();
160 : ::svt::WizardTypes::WizardState nPos;
161 0 : sal_Int32 nIndex = 0;
162 0 : for (;pItems < pEnd; ++pItems)
163 : {
164 0 : nPos = _rList.InsertEntry(*pItems);
165 0 : _rList.SetEntryData(nPos, reinterpret_cast<void*>(nIndex));
166 : }
167 0 : }
168 :
169 :
170 0 : void OControlWizardPage::enableFormDatasourceDisplay()
171 : {
172 0 : if (m_pFormContentType)
173 : // nothing to do
174 0 : return;
175 :
176 0 : VclFrame *_pFrame = get<VclFrame>("sourceframe");
177 0 : _pFrame->Show();
178 0 : get(m_pFormContentType,"contenttype");
179 0 : get(m_pFormContentTypeLabel,"contenttypelabel");
180 0 : get(m_pFormDatasource, "datasource");
181 0 : get(m_pFormDatasourceLabel, "datasourcelabel");
182 0 : get(m_pFormTable,"formtable");
183 0 : get(m_pFormTableLabel,"formtablelabel");
184 :
185 0 : const OControlWizardContext& rContext = getContext();
186 0 : if ( rContext.bEmbedded )
187 : {
188 0 : m_pFormDatasourceLabel->Hide();
189 0 : m_pFormDatasource->Hide();
190 0 : m_pFormContentTypeLabel->SetPosPixel(m_pFormDatasourceLabel->GetPosPixel());
191 0 : m_pFormContentType->SetPosPixel(m_pFormDatasource->GetPosPixel());
192 0 : m_pFormTableLabel->SetPosPixel(::Point(m_pFormDatasourceLabel->GetPosPixel().X(),m_pFormTableLabel->GetPosPixel().Y()));
193 0 : m_pFormTable->SetPosPixel(::Point(m_pFormDatasource->GetPosPixel().X(),m_pFormTable->GetPosPixel().Y()));
194 : }
195 : }
196 :
197 0 : void OControlWizardPage::initializePage()
198 : {
199 0 : if (m_pFormDatasource && m_pFormContentTypeLabel && m_pFormTable)
200 : {
201 0 : const OControlWizardContext& rContext = getContext();
202 0 : OUString sDataSource;
203 0 : OUString sCommand;
204 0 : sal_Int32 nCommandType = CommandType::COMMAND;
205 : try
206 : {
207 0 : rContext.xForm->getPropertyValue("DataSourceName") >>= sDataSource;
208 0 : rContext.xForm->getPropertyValue("Command") >>= sCommand;
209 0 : rContext.xForm->getPropertyValue("CommandType") >>= nCommandType;
210 : }
211 0 : catch(const Exception&)
212 : {
213 : OSL_FAIL("OControlWizardPage::initializePage: caught an exception!");
214 : }
215 :
216 0 : INetURLObject aURL( sDataSource );
217 0 : if( aURL.GetProtocol() != INetProtocol::NotValid )
218 0 : sDataSource = aURL.GetName(INetURLObject::DECODE_WITH_CHARSET);
219 0 : m_pFormDatasource->SetText(sDataSource);
220 0 : m_pFormTable->SetText(sCommand);
221 :
222 0 : ::svt::WizardTypes::WizardState nCommandTypeResourceId = 0;
223 0 : switch (nCommandType)
224 : {
225 : case CommandType::TABLE:
226 0 : nCommandTypeResourceId = RID_STR_TYPE_TABLE;
227 0 : break;
228 :
229 : case CommandType::QUERY:
230 0 : nCommandTypeResourceId = RID_STR_TYPE_QUERY;
231 0 : break;
232 :
233 : default:
234 0 : nCommandTypeResourceId = RID_STR_TYPE_COMMAND;
235 0 : break;
236 : }
237 0 : m_pFormContentType->SetText(ModuleRes(nCommandTypeResourceId).toString());
238 : }
239 :
240 0 : OControlWizardPage_Base::initializePage();
241 0 : }
242 :
243 0 : OControlWizard::OControlWizard( vcl::Window* _pParent,
244 : const Reference< XPropertySet >& _rxObjectModel, const Reference< XComponentContext >& _rxContext )
245 0 : :OWizardMachine(_pParent, WizardButtonFlags::CANCEL | WizardButtonFlags::PREVIOUS | WizardButtonFlags::NEXT | WizardButtonFlags::FINISH)
246 0 : ,m_xContext(_rxContext)
247 : {
248 0 : m_aContext.xObjectModel = _rxObjectModel;
249 0 : initContext();
250 :
251 0 : SetPageSizePixel(LogicToPixel(::Size(WINDOW_SIZE_X, WINDOW_SIZE_Y), MAP_APPFONT));
252 0 : defaultButton(WizardButtonFlags::NEXT);
253 0 : enableButtons(WizardButtonFlags::FINISH, false);
254 0 : }
255 :
256 0 : OControlWizard::~OControlWizard()
257 : {
258 0 : }
259 :
260 0 : short OControlWizard::Execute()
261 : {
262 : // get the class id of the control we're dealing with
263 0 : sal_Int16 nClassId = FormComponentType::CONTROL;
264 : try
265 : {
266 0 : getContext().xObjectModel->getPropertyValue("ClassId") >>= nClassId;
267 : }
268 0 : catch(const Exception&)
269 : {
270 : OSL_FAIL("OControlWizard::activate: could not obtain the class id!");
271 : }
272 0 : if (!approveControl(nClassId))
273 : {
274 : // TODO: MessageBox or exception
275 0 : return RET_CANCEL;
276 : }
277 :
278 0 : ActivatePage();
279 :
280 0 : return OControlWizard_Base::Execute();
281 : }
282 :
283 :
284 0 : void OControlWizard::ActivatePage()
285 : {
286 0 : OControlWizard_Base::ActivatePage();
287 0 : }
288 :
289 :
290 0 : void OControlWizard::implDetermineShape()
291 : {
292 0 : Reference< XIndexAccess > xPageObjects(m_aContext.xDrawPage, UNO_QUERY);
293 : DBG_ASSERT(xPageObjects.is(), "OControlWizard::implDetermineShape: invalid page!");
294 :
295 : // for comparing the model
296 0 : Reference< XControlModel > xModelCompare(m_aContext.xObjectModel, UNO_QUERY);
297 :
298 0 : if (xPageObjects.is())
299 : {
300 : // loop through all objects of the page
301 0 : sal_Int32 nObjects = xPageObjects->getCount();
302 0 : Reference< XControlShape > xControlShape;
303 0 : Reference< XControlModel > xControlModel;
304 0 : for (sal_Int32 i=0; i<nObjects; ++i)
305 : {
306 0 : if (xPageObjects->getByIndex(i) >>= xControlShape)
307 : { // it _is_ a control shape
308 0 : xControlModel = xControlShape->getControl();
309 : DBG_ASSERT(xControlModel.is(), "OControlWizard::implDetermineShape: control shape without model!");
310 0 : if (xModelCompare.get() == xControlModel.get())
311 : {
312 0 : m_aContext.xObjectShape = xControlShape;
313 0 : break;
314 : }
315 : }
316 0 : }
317 0 : }
318 0 : }
319 :
320 :
321 0 : void OControlWizard::implDetermineForm()
322 : {
323 0 : Reference< XChild > xModelAsChild(m_aContext.xObjectModel, UNO_QUERY);
324 0 : Reference< XInterface > xControlParent;
325 0 : if (xModelAsChild.is())
326 0 : xControlParent = xModelAsChild->getParent();
327 :
328 0 : m_aContext.xForm = Reference< XPropertySet >(xControlParent, UNO_QUERY);
329 0 : m_aContext.xRowSet = Reference< XRowSet >(xControlParent, UNO_QUERY);
330 : DBG_ASSERT(m_aContext.xForm.is() && m_aContext.xRowSet.is(),
331 0 : "OControlWizard::implDetermineForm: missing some interfaces of the control parent!");
332 :
333 0 : }
334 :
335 :
336 0 : void OControlWizard::implDeterminePage()
337 : {
338 : try
339 : {
340 : // get the document model
341 0 : Reference< XChild > xControlAsChild(m_aContext.xObjectModel, UNO_QUERY);
342 0 : Reference< XChild > xModelSearch(xControlAsChild->getParent(), UNO_QUERY);
343 :
344 0 : Reference< XModel > xModel(xModelSearch, UNO_QUERY);
345 0 : while (xModelSearch.is() && !xModel.is())
346 : {
347 0 : xModelSearch = Reference< XChild >(xModelSearch->getParent(), UNO_QUERY);
348 0 : xModel = Reference< XModel >(xModelSearch, UNO_QUERY);
349 : }
350 :
351 0 : Reference< XDrawPage > xPage;
352 0 : if (xModel.is())
353 : {
354 0 : m_aContext.xDocumentModel = xModel;
355 :
356 0 : Reference< XDrawPageSupplier > xPageSupp(xModel, UNO_QUERY);
357 0 : if (xPageSupp.is())
358 : { // it's a document with only one page -> Writer
359 0 : xPage = xPageSupp->getDrawPage();
360 : }
361 : else
362 : {
363 : // get the controller currently working on this model
364 0 : Reference< XController > xController = xModel->getCurrentController();
365 : DBG_ASSERT(xController.is(), "OControlWizard::implDeterminePage: no current controller!");
366 :
367 : // maybe it's a spreadsheet
368 0 : Reference< XSpreadsheetView > xView(xController, UNO_QUERY);
369 0 : if (xView.is())
370 : { // okay, it is one
371 0 : Reference< XSpreadsheet > xSheet = xView->getActiveSheet();
372 0 : xPageSupp = Reference< XDrawPageSupplier >(xSheet, UNO_QUERY);
373 : DBG_ASSERT(xPageSupp.is(), "OControlWizard::implDeterminePage: a spreadsheet which is no page supplier!");
374 0 : if (xPageSupp.is())
375 0 : xPage = xPageSupp->getDrawPage();
376 : }
377 : else
378 : { // can be a draw/impress doc only
379 0 : Reference< XDrawView > xDrawView(xController, UNO_QUERY);
380 : DBG_ASSERT(xDrawView.is(), "OControlWizard::implDeterminePage: no alternatives left ... can't determine the page!");
381 0 : if (xDrawView.is())
382 0 : xPage = xDrawView->getCurrentPage();
383 0 : }
384 0 : }
385 : }
386 : else
387 : {
388 : DBG_ASSERT(xPage.is(), "OControlWizard::implDeterminePage: can't determine the page (no model)!");
389 : }
390 0 : m_aContext.xDrawPage = xPage;
391 : }
392 0 : catch(const Exception&)
393 : {
394 : OSL_FAIL("OControlWizard::implDeterminePage: caught an exception!");
395 : }
396 0 : }
397 :
398 :
399 0 : void OControlWizard::implGetDSContext()
400 : {
401 : try
402 : {
403 : DBG_ASSERT(m_xContext.is(), "OControlWizard::implGetDSContext: invalid service factory!");
404 :
405 0 : m_aContext.xDatasourceContext = DatabaseContext::create(m_xContext);
406 : }
407 0 : catch(const Exception&)
408 : {
409 : OSL_FAIL("OControlWizard::implGetDSContext: invalid database context!");
410 : }
411 0 : }
412 :
413 :
414 0 : Reference< XConnection > OControlWizard::getFormConnection(const OAccessRegulator&) const
415 : {
416 0 : return getFormConnection();
417 : }
418 :
419 0 : Reference< XConnection > OControlWizard::getFormConnection() const
420 : {
421 0 : Reference< XConnection > xConn;
422 : try
423 : {
424 0 : if ( !::dbtools::isEmbeddedInDatabase(m_aContext.xForm,xConn) )
425 0 : m_aContext.xForm->getPropertyValue("ActiveConnection") >>= xConn;
426 : }
427 0 : catch(const Exception&)
428 : {
429 : OSL_FAIL("OControlWizard::getFormConnection: caught an exception!");
430 : }
431 0 : return xConn;
432 : }
433 :
434 :
435 0 : void OControlWizard::setFormConnection( const OAccessRegulator& _rAccess, const Reference< XConnection >& _rxConn, bool _bAutoDispose )
436 : {
437 : try
438 : {
439 0 : Reference< XConnection > xOldConn = getFormConnection(_rAccess);
440 0 : if (xOldConn.get() == _rxConn.get())
441 0 : return;
442 :
443 0 : disposeComponent(xOldConn);
444 :
445 : // set the new connection
446 0 : if ( _bAutoDispose )
447 : {
448 : // for this, use a AutoDisposer (so the conn is cleaned up when the form dies or get's another connection)
449 0 : Reference< XRowSet > xFormRowSet( m_aContext.xForm, UNO_QUERY );
450 0 : OAutoConnectionDisposer* pAutoDispose = new OAutoConnectionDisposer( xFormRowSet, _rxConn );
451 0 : Reference< XPropertyChangeListener > xEnsureDelete( pAutoDispose );
452 : }
453 : else
454 : {
455 0 : m_aContext.xForm->setPropertyValue("ActiveConnection", makeAny( _rxConn ) );
456 0 : }
457 : }
458 0 : catch(const Exception&)
459 : {
460 : OSL_FAIL("OControlWizard::setFormConnection: caught an exception!");
461 : }
462 : }
463 :
464 :
465 0 : bool OControlWizard::updateContext(const OAccessRegulator&)
466 : {
467 0 : return initContext();
468 : }
469 :
470 0 : Reference< XInteractionHandler > OControlWizard::getInteractionHandler(vcl::Window* _pWindow) const
471 : {
472 0 : Reference< XInteractionHandler > xHandler;
473 : try
474 : {
475 0 : xHandler = Reference< XInteractionHandler >( InteractionHandler::createWithParent(m_xContext, 0), UNO_QUERY_THROW );
476 : }
477 0 : catch(const Exception&) { }
478 0 : if (!xHandler.is())
479 : {
480 0 : const OUString sInteractionHandlerServiceName("com.sun.star.task.InteractionHandler");
481 0 : ShowServiceNotAvailableError(_pWindow, sInteractionHandlerServiceName, true);
482 : }
483 0 : return xHandler;
484 : }
485 :
486 0 : bool OControlWizard::initContext()
487 : {
488 : DBG_ASSERT(m_aContext.xObjectModel.is(), "OGroupBoxWizard::initContext: have no control model to work with!");
489 0 : if (!m_aContext.xObjectModel.is())
490 0 : return false;
491 :
492 : // reset the context
493 0 : m_aContext.xForm.clear();
494 0 : m_aContext.xRowSet.clear();
495 0 : m_aContext.xDocumentModel.clear();
496 0 : m_aContext.xDrawPage.clear();
497 0 : m_aContext.xObjectShape.clear();
498 0 : m_aContext.aFieldNames.realloc(0);
499 :
500 0 : m_aContext.xObjectContainer.clear();
501 0 : m_aContext.aTypes.clear();
502 0 : m_aContext.bEmbedded = false;
503 :
504 0 : Any aSQLException;
505 0 : Reference< XPreparedStatement > xStatement;
506 : try
507 : {
508 : // get the datasource context
509 0 : implGetDSContext();
510 :
511 : // first, determine the form the control belongs to
512 0 : implDetermineForm();
513 :
514 : // need the page, too
515 0 : implDeterminePage();
516 :
517 : // the shape of the control
518 0 : implDetermineShape();
519 :
520 : // get the columns of the object the settins refer to
521 0 : Reference< XNameAccess > xColumns;
522 :
523 0 : if (m_aContext.xForm.is())
524 : {
525 : // collect some properties of the form
526 0 : OUString sObjectName = ::comphelper::getString(m_aContext.xForm->getPropertyValue("Command"));
527 0 : sal_Int32 nObjectType = ::comphelper::getINT32(m_aContext.xForm->getPropertyValue("CommandType"));
528 :
529 : // calculate the connection the rowset is working with
530 0 : Reference< XConnection > xConnection;
531 0 : m_aContext.bEmbedded = ::dbtools::isEmbeddedInDatabase( m_aContext.xForm, xConnection );
532 0 : if ( !m_aContext.bEmbedded )
533 0 : xConnection = ::dbtools::connectRowset( m_aContext.xRowSet, m_xContext, true );
534 :
535 : // get the fields
536 0 : if (xConnection.is())
537 : {
538 0 : switch (nObjectType)
539 : {
540 : case 0:
541 : {
542 0 : Reference< XTablesSupplier > xSupplyTables(xConnection, UNO_QUERY);
543 0 : if (xSupplyTables.is() && xSupplyTables->getTables().is() && xSupplyTables->getTables()->hasByName(sObjectName))
544 : {
545 0 : Reference< XColumnsSupplier > xSupplyColumns;
546 0 : m_aContext.xObjectContainer = xSupplyTables->getTables();
547 0 : m_aContext.xObjectContainer->getByName(sObjectName) >>= xSupplyColumns;
548 : DBG_ASSERT(xSupplyColumns.is(), "OControlWizard::initContext: invalid table columns!");
549 0 : xColumns = xSupplyColumns->getColumns();
550 0 : }
551 : }
552 0 : break;
553 : case 1:
554 : {
555 0 : Reference< XQueriesSupplier > xSupplyQueries(xConnection, UNO_QUERY);
556 0 : if (xSupplyQueries.is() && xSupplyQueries->getQueries().is() && xSupplyQueries->getQueries()->hasByName(sObjectName))
557 : {
558 0 : Reference< XColumnsSupplier > xSupplyColumns;
559 0 : m_aContext.xObjectContainer = xSupplyQueries->getQueries();
560 0 : m_aContext.xObjectContainer->getByName(sObjectName) >>= xSupplyColumns;
561 : DBG_ASSERT(xSupplyColumns.is(), "OControlWizard::initContext: invalid query columns!");
562 0 : xColumns = xSupplyColumns->getColumns();
563 0 : }
564 : }
565 0 : break;
566 : default:
567 : {
568 0 : xStatement = xConnection->prepareStatement(sObjectName);
569 :
570 : // not interested in any results, only in the fields
571 0 : Reference< XPropertySet > xStatementProps(xStatement, UNO_QUERY);
572 0 : xStatementProps->setPropertyValue("MaxRows", makeAny(sal_Int32(0)));
573 :
574 : // TODO: think about handling local SQLExceptions here ...
575 0 : Reference< XColumnsSupplier > xSupplyCols(xStatement->executeQuery(), UNO_QUERY);
576 0 : if (xSupplyCols.is())
577 0 : xColumns = xSupplyCols->getColumns();
578 : }
579 : }
580 0 : }
581 : }
582 :
583 0 : if (xColumns.is())
584 : {
585 0 : m_aContext.aFieldNames = xColumns->getElementNames();
586 : static const char s_sFieldTypeProperty[] = "Type";
587 0 : const OUString* pBegin = m_aContext.aFieldNames.getConstArray();
588 0 : const OUString* pEnd = pBegin + m_aContext.aFieldNames.getLength();
589 0 : for(;pBegin != pEnd;++pBegin)
590 : {
591 0 : sal_Int32 nFieldType = DataType::OTHER;
592 : try
593 : {
594 0 : Reference< XPropertySet > xColumn;
595 0 : xColumns->getByName(*pBegin) >>= xColumn;
596 0 : xColumn->getPropertyValue(s_sFieldTypeProperty) >>= nFieldType;
597 : }
598 0 : catch(const Exception&)
599 : {
600 : OSL_FAIL("OControlWizard::initContext: unexpected exception while gathering column information!");
601 : }
602 0 : m_aContext.aTypes.insert(OControlWizardContext::TNameTypeMap::value_type(*pBegin,nFieldType));
603 : }
604 0 : }
605 : }
606 0 : catch(const SQLContext& e) { aSQLException <<= e; }
607 0 : catch(const SQLWarning& e) { aSQLException <<= e; }
608 0 : catch(const SQLException& e) { aSQLException <<= e; }
609 0 : catch(const Exception&)
610 : {
611 : OSL_FAIL("OControlWizard::initContext: could not retrieve the control context (caught an exception)!");
612 : }
613 :
614 0 : ::comphelper::disposeComponent(xStatement);
615 :
616 0 : if (aSQLException.hasValue())
617 : { // an SQLException (or derivee) was thrown ...
618 :
619 : // prepend an extra SQLContext explaining what we were doing
620 0 : SQLContext aContext;
621 0 : aContext.Message = ModuleRes(RID_STR_COULDNOTOPENTABLE).toString();
622 0 : aContext.NextException = aSQLException;
623 :
624 : // create an interaction handler to display this exception
625 0 : Reference< XInteractionHandler > xHandler = getInteractionHandler(this);
626 0 : if ( !xHandler.is() )
627 0 : return false;
628 :
629 0 : Reference< XInteractionRequest > xRequest = new OInteractionRequest(makeAny(aContext));
630 : try
631 : {
632 0 : xHandler->handle(xRequest);
633 : }
634 0 : catch(const Exception&) { }
635 0 : return false;
636 : }
637 :
638 0 : return 0 != m_aContext.aFieldNames.getLength();
639 : }
640 :
641 :
642 0 : void OControlWizard::commitControlSettings(OControlWizardSettings* _pSettings)
643 : {
644 : DBG_ASSERT(m_aContext.xObjectModel.is(), "OControlWizard::commitControlSettings: have no control model to work with!");
645 0 : if (!m_aContext.xObjectModel.is())
646 0 : return;
647 :
648 : // the only thing we have at the moment is the label
649 : try
650 : {
651 0 : OUString sLabelPropertyName("Label");
652 0 : Reference< XPropertySetInfo > xInfo = m_aContext.xObjectModel->getPropertySetInfo();
653 0 : if (xInfo.is() && xInfo->hasPropertyByName(sLabelPropertyName))
654 : {
655 0 : OUString sControlLabel(_pSettings->sControlLabel);
656 0 : m_aContext.xObjectModel->setPropertyValue(
657 : OUString("Label"),
658 : makeAny(sControlLabel)
659 0 : );
660 0 : }
661 : }
662 0 : catch(const Exception&)
663 : {
664 : OSL_FAIL("OControlWizard::commitControlSettings: could not commit the basic control settings!");
665 : }
666 : }
667 :
668 :
669 0 : void OControlWizard::initControlSettings(OControlWizardSettings* _pSettings)
670 : {
671 : DBG_ASSERT(m_aContext.xObjectModel.is(), "OControlWizard::initControlSettings: have no control model to work with!");
672 0 : if (!m_aContext.xObjectModel.is())
673 0 : return;
674 :
675 : // initialize some settings from the control model give
676 : try
677 : {
678 0 : OUString sLabelPropertyName("Label");
679 0 : Reference< XPropertySetInfo > xInfo = m_aContext.xObjectModel->getPropertySetInfo();
680 0 : if (xInfo.is() && xInfo->hasPropertyByName(sLabelPropertyName))
681 : {
682 0 : OUString sControlLabel;
683 0 : m_aContext.xObjectModel->getPropertyValue(sLabelPropertyName) >>= sControlLabel;
684 0 : _pSettings->sControlLabel = sControlLabel;
685 0 : }
686 : }
687 0 : catch(const Exception&)
688 : {
689 : OSL_FAIL("OControlWizard::initControlSettings: could not retrieve the basic control settings!");
690 : }
691 : }
692 :
693 :
694 0 : bool OControlWizard::needDatasourceSelection()
695 : {
696 : // lemme see ...
697 0 : return (0 == getContext().aFieldNames.getLength());
698 : // if we got fields, the data source is valid ...
699 : }
700 :
701 :
702 0 : } // namespace dbp
703 :
704 :
705 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|