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