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