xref: /AOO41X/main/dbaccess/source/ui/relationdesign/RelationController.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
30*cdf0e10cSrcweir #include "dbu_reghelper.hxx"
31*cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
32*cdf0e10cSrcweir #include "dbu_rel.hrc"
33*cdf0e10cSrcweir #include <vcl/svapp.hxx>
34*cdf0e10cSrcweir #include "browserids.hxx"
35*cdf0e10cSrcweir #include <comphelper/types.hxx>
36*cdf0e10cSrcweir #include "dbustrings.hrc"
37*cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
38*cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
39*cdf0e10cSrcweir #include <comphelper/extract.hxx>
40*cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/sdbcx/KeyType.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XDrop.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XAlterTable.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XAppend.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/sdb/SQLContext.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLWarning.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
54*cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
55*cdf0e10cSrcweir #include <connectivity/dbmetadata.hxx>
56*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
57*cdf0e10cSrcweir #include <comphelper/streamsection.hxx>
58*cdf0e10cSrcweir #include <comphelper/basicio.hxx>
59*cdf0e10cSrcweir #include <comphelper/seqstream.hxx>
60*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSink.hpp>
62*cdf0e10cSrcweir #include "sqlmessage.hxx"
63*cdf0e10cSrcweir #include "RelationController.hxx"
64*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
65*cdf0e10cSrcweir #include "TableWindowData.hxx"
66*cdf0e10cSrcweir #include "dbustrings.hrc"
67*cdf0e10cSrcweir #include "UITools.hxx"
68*cdf0e10cSrcweir #include "RTableConnectionData.hxx"
69*cdf0e10cSrcweir #include "RelationTableView.hxx"
70*cdf0e10cSrcweir #include "RelationDesignView.hxx"
71*cdf0e10cSrcweir #include <tools/debug.hxx>
72*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
73*cdf0e10cSrcweir #include <vcl/waitobj.hxx>
74*cdf0e10cSrcweir #include <osl/thread.hxx>
75*cdf0e10cSrcweir #include <vos/mutex.hxx>
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir #define MAX_THREADS 10
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir extern "C" void SAL_CALL createRegistryInfo_ORelationControl()
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir 	static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::ORelationController > aAutoRegistration;
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
87*cdf0e10cSrcweir using namespace ::com::sun::star::io;
88*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
89*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
90*cdf0e10cSrcweir using namespace ::com::sun::star::util;
91*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
92*cdf0e10cSrcweir using namespace ::com::sun::star::container;
93*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
94*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
95*cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
96*cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs;
97*cdf0e10cSrcweir using namespace ::com::sun::star::util;
98*cdf0e10cSrcweir //	using namespace ::com::sun::star::sdbcx;
99*cdf0e10cSrcweir //	using namespace ::connectivity;
100*cdf0e10cSrcweir using namespace ::dbtools;
101*cdf0e10cSrcweir using namespace ::dbaui;
102*cdf0e10cSrcweir using namespace ::comphelper;
103*cdf0e10cSrcweir using namespace ::osl;
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir //------------------------------------------------------------------------------
106*cdf0e10cSrcweir ::rtl::OUString SAL_CALL ORelationController::getImplementationName() throw( RuntimeException )
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir 	return getImplementationName_Static();
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir //------------------------------------------------------------------------------
112*cdf0e10cSrcweir ::rtl::OUString ORelationController::getImplementationName_Static() throw( RuntimeException )
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir 	return ::rtl::OUString::createFromAscii("org.openoffice.comp.dbu.ORelationDesign");
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir //------------------------------------------------------------------------------
117*cdf0e10cSrcweir Sequence< ::rtl::OUString> ORelationController::getSupportedServiceNames_Static(void) throw( RuntimeException )
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir 	Sequence< ::rtl::OUString> aSupported(1);
120*cdf0e10cSrcweir 	aSupported.getArray()[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdb.RelationDesign");
121*cdf0e10cSrcweir 	return aSupported;
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir //-------------------------------------------------------------------------
124*cdf0e10cSrcweir Sequence< ::rtl::OUString> SAL_CALL ORelationController::getSupportedServiceNames() throw(RuntimeException)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir // -------------------------------------------------------------------------
129*cdf0e10cSrcweir Reference< XInterface > SAL_CALL ORelationController::Create(const Reference<XMultiServiceFactory >& _rxFactory)
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir 	return *(new ORelationController(_rxFactory));
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir DBG_NAME(ORelationController);
134*cdf0e10cSrcweir // -----------------------------------------------------------------------------
135*cdf0e10cSrcweir ORelationController::ORelationController(const Reference< XMultiServiceFactory >& _rM)
136*cdf0e10cSrcweir 	: OJoinController(_rM)
137*cdf0e10cSrcweir     ,m_nThreadEvent(0)
138*cdf0e10cSrcweir 	,m_bRelationsPossible(sal_True)
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir 	DBG_CTOR(ORelationController,NULL);
141*cdf0e10cSrcweir 	InvalidateAll();
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir // -----------------------------------------------------------------------------
144*cdf0e10cSrcweir ORelationController::~ORelationController()
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir 	DBG_DTOR(ORelationController,NULL);
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir // -----------------------------------------------------------------------------
149*cdf0e10cSrcweir FeatureState ORelationController::GetState(sal_uInt16 _nId) const
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	FeatureState aReturn;
152*cdf0e10cSrcweir 	aReturn.bEnabled = m_bRelationsPossible;
153*cdf0e10cSrcweir 	switch (_nId)
154*cdf0e10cSrcweir 	{
155*cdf0e10cSrcweir 		case SID_RELATION_ADD_RELATION:
156*cdf0e10cSrcweir 			aReturn.bEnabled = !m_vTableData.empty() && isConnected() && isEditable();
157*cdf0e10cSrcweir 			aReturn.bChecked = false;
158*cdf0e10cSrcweir 			break;
159*cdf0e10cSrcweir 		case ID_BROWSER_SAVEDOC:
160*cdf0e10cSrcweir 			aReturn.bEnabled = haveDataSource() && impl_isModified();
161*cdf0e10cSrcweir 			break;
162*cdf0e10cSrcweir 		default:
163*cdf0e10cSrcweir 			aReturn = OJoinController::GetState(_nId);
164*cdf0e10cSrcweir 			break;
165*cdf0e10cSrcweir 	}
166*cdf0e10cSrcweir 	return aReturn;
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir // -----------------------------------------------------------------------------
169*cdf0e10cSrcweir void ORelationController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& aArgs)
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir 	switch(_nId)
172*cdf0e10cSrcweir 	{
173*cdf0e10cSrcweir 		case ID_BROWSER_SAVEDOC:
174*cdf0e10cSrcweir 			{
175*cdf0e10cSrcweir 				OSL_ENSURE(isEditable(),"Slot ID_BROWSER_SAVEDOC should not be enabled!");
176*cdf0e10cSrcweir 				if(!::dbaui::checkDataSourceAvailable(::comphelper::getString(getDataSource()->getPropertyValue(PROPERTY_NAME)),getORB()))
177*cdf0e10cSrcweir 				{
178*cdf0e10cSrcweir 					String aMessage(ModuleRes(STR_DATASOURCE_DELETED));
179*cdf0e10cSrcweir 					OSQLWarningBox( getView(), aMessage ).Execute();
180*cdf0e10cSrcweir 				}
181*cdf0e10cSrcweir 				else
182*cdf0e10cSrcweir 				{
183*cdf0e10cSrcweir 					// now we save the layout information
184*cdf0e10cSrcweir 					//  create the output stream
185*cdf0e10cSrcweir 					try
186*cdf0e10cSrcweir 					{
187*cdf0e10cSrcweir 						if ( haveDataSource() && getDataSource()->getPropertySetInfo()->hasPropertyByName(PROPERTY_LAYOUTINFORMATION) )
188*cdf0e10cSrcweir 						{
189*cdf0e10cSrcweir                             ::comphelper::NamedValueCollection aWindowsData;
190*cdf0e10cSrcweir 							saveTableWindows( aWindowsData );
191*cdf0e10cSrcweir 							getDataSource()->setPropertyValue( PROPERTY_LAYOUTINFORMATION, makeAny( aWindowsData.getPropertyValues() ) );
192*cdf0e10cSrcweir 							setModified(sal_False);
193*cdf0e10cSrcweir 						}
194*cdf0e10cSrcweir 					}
195*cdf0e10cSrcweir 					catch ( const Exception& )
196*cdf0e10cSrcweir 					{
197*cdf0e10cSrcweir                         DBG_UNHANDLED_EXCEPTION();
198*cdf0e10cSrcweir 					}
199*cdf0e10cSrcweir 				}
200*cdf0e10cSrcweir 			}
201*cdf0e10cSrcweir 			break;
202*cdf0e10cSrcweir 		case SID_RELATION_ADD_RELATION:
203*cdf0e10cSrcweir 			static_cast<ORelationTableView*>(static_cast<ORelationDesignView*>( getView() )->getTableView())->AddNewRelation();
204*cdf0e10cSrcweir 			break;
205*cdf0e10cSrcweir 		default:
206*cdf0e10cSrcweir 			OJoinController::Execute(_nId,aArgs);
207*cdf0e10cSrcweir 			return;
208*cdf0e10cSrcweir 	}
209*cdf0e10cSrcweir 	InvalidateFeature(_nId);
210*cdf0e10cSrcweir }
211*cdf0e10cSrcweir // -----------------------------------------------------------------------------
212*cdf0e10cSrcweir void ORelationController::impl_initialize()
213*cdf0e10cSrcweir {
214*cdf0e10cSrcweir 	OJoinController::impl_initialize();
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     if( !getSdbMetaData().supportsRelations() )
217*cdf0e10cSrcweir 	{// check if this database supports relations
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 		setEditable(sal_False);
220*cdf0e10cSrcweir 		m_bRelationsPossible	= sal_False;
221*cdf0e10cSrcweir 		{
222*cdf0e10cSrcweir 			String sTitle(ModuleRes(STR_RELATIONDESIGN));
223*cdf0e10cSrcweir 			sTitle.Erase(0,3);
224*cdf0e10cSrcweir 			OSQLMessageBox aDlg(NULL,sTitle,ModuleRes(STR_RELATIONDESIGN_NOT_AVAILABLE));
225*cdf0e10cSrcweir 			aDlg.Execute();
226*cdf0e10cSrcweir 		}
227*cdf0e10cSrcweir 		disconnect();
228*cdf0e10cSrcweir 		throw SQLException();
229*cdf0e10cSrcweir 	}
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 	if(!m_bRelationsPossible)
232*cdf0e10cSrcweir 		InvalidateAll();
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	// we need a datasource
235*cdf0e10cSrcweir 	OSL_ENSURE(haveDataSource(),"ORelationController::initialize: need a datasource!");
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 	Reference<XTablesSupplier> xSup(getConnection(),UNO_QUERY);
238*cdf0e10cSrcweir 	OSL_ENSURE(xSup.is(),"Connection isn't a XTablesSupplier!");
239*cdf0e10cSrcweir 	if(xSup.is())
240*cdf0e10cSrcweir 		m_xTables = xSup->getTables();
241*cdf0e10cSrcweir 	// load the layoutInformation
242*cdf0e10cSrcweir 	loadLayoutInformation();
243*cdf0e10cSrcweir 	try
244*cdf0e10cSrcweir 	{
245*cdf0e10cSrcweir 		loadData();
246*cdf0e10cSrcweir         if ( !m_nThreadEvent )
247*cdf0e10cSrcweir             Application::PostUserEvent(LINK(this, ORelationController, OnThreadFinished));
248*cdf0e10cSrcweir 	}
249*cdf0e10cSrcweir     catch( const Exception& )
250*cdf0e10cSrcweir     {
251*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
252*cdf0e10cSrcweir     }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir }
255*cdf0e10cSrcweir // -----------------------------------------------------------------------------
256*cdf0e10cSrcweir ::rtl::OUString ORelationController::getPrivateTitle( ) const
257*cdf0e10cSrcweir {
258*cdf0e10cSrcweir     ::rtl::OUString sName = getDataSourceName();
259*cdf0e10cSrcweir 	return ::dbaui::getStrippedDatabaseName(getDataSource(),sName);
260*cdf0e10cSrcweir }
261*cdf0e10cSrcweir // -----------------------------------------------------------------------------
262*cdf0e10cSrcweir sal_Bool ORelationController::Construct(Window* pParent)
263*cdf0e10cSrcweir {
264*cdf0e10cSrcweir 	setView( * new ORelationDesignView( pParent, *this, getORB() ) );
265*cdf0e10cSrcweir 	OJoinController::Construct(pParent);
266*cdf0e10cSrcweir 	return sal_True;
267*cdf0e10cSrcweir }
268*cdf0e10cSrcweir // -----------------------------------------------------------------------------
269*cdf0e10cSrcweir short ORelationController::saveModified()
270*cdf0e10cSrcweir {
271*cdf0e10cSrcweir 	short nSaved = RET_YES;
272*cdf0e10cSrcweir 	if(haveDataSource() && isModified())
273*cdf0e10cSrcweir 	{
274*cdf0e10cSrcweir 		QueryBox aQry(getView(), ModuleRes(RELATION_DESIGN_SAVEMODIFIED));
275*cdf0e10cSrcweir 		nSaved = aQry.Execute();
276*cdf0e10cSrcweir 		if(nSaved == RET_YES)
277*cdf0e10cSrcweir 			Execute(ID_BROWSER_SAVEDOC,Sequence<PropertyValue>());
278*cdf0e10cSrcweir 	}
279*cdf0e10cSrcweir 	return nSaved;
280*cdf0e10cSrcweir }
281*cdf0e10cSrcweir // -----------------------------------------------------------------------------
282*cdf0e10cSrcweir void ORelationController::describeSupportedFeatures()
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir 	OJoinController::describeSupportedFeatures();
285*cdf0e10cSrcweir     implDescribeSupportedFeature( ".uno:DBAddRelation", SID_RELATION_ADD_RELATION, CommandGroup::EDIT );
286*cdf0e10cSrcweir }
287*cdf0e10cSrcweir namespace
288*cdf0e10cSrcweir {
289*cdf0e10cSrcweir     class RelationLoader : public ::osl::Thread
290*cdf0e10cSrcweir     {
291*cdf0e10cSrcweir         DECLARE_STL_MAP(::rtl::OUString,::boost::shared_ptr<OTableWindowData>,::comphelper::UStringMixLess,TTableDataHelper);
292*cdf0e10cSrcweir         TTableDataHelper                    m_aTableData;
293*cdf0e10cSrcweir         TTableConnectionData                m_vTableConnectionData;
294*cdf0e10cSrcweir         const Sequence< ::rtl::OUString>    m_aTableList;
295*cdf0e10cSrcweir         ORelationController*                m_pParent;
296*cdf0e10cSrcweir         const Reference< XDatabaseMetaData> m_xMetaData;
297*cdf0e10cSrcweir         const Reference< XNameAccess >	    m_xTables;
298*cdf0e10cSrcweir         const sal_Int32                     m_nStartIndex;
299*cdf0e10cSrcweir         const sal_Int32                     m_nEndIndex;
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir     public:
302*cdf0e10cSrcweir 	    RelationLoader(ORelationController* _pParent
303*cdf0e10cSrcweir                         ,const Reference< XDatabaseMetaData>& _xMetaData
304*cdf0e10cSrcweir                         ,const Reference< XNameAccess >& _xTables
305*cdf0e10cSrcweir                         ,const Sequence< ::rtl::OUString>& _aTableList
306*cdf0e10cSrcweir                         ,const sal_Int32 _nStartIndex
307*cdf0e10cSrcweir                         ,const sal_Int32 _nEndIndex)
308*cdf0e10cSrcweir             :m_aTableData(_xMetaData.is() && _xMetaData->supportsMixedCaseQuotedIdentifiers())
309*cdf0e10cSrcweir             ,m_aTableList(_aTableList)
310*cdf0e10cSrcweir             ,m_pParent(_pParent)
311*cdf0e10cSrcweir             ,m_xMetaData(_xMetaData)
312*cdf0e10cSrcweir             ,m_xTables(_xTables)
313*cdf0e10cSrcweir             ,m_nStartIndex(_nStartIndex)
314*cdf0e10cSrcweir             ,m_nEndIndex(_nEndIndex)
315*cdf0e10cSrcweir 	    {
316*cdf0e10cSrcweir         }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir         /// Working method which should be overridden.
319*cdf0e10cSrcweir 	    virtual void SAL_CALL run();
320*cdf0e10cSrcweir         virtual void SAL_CALL onTerminated();
321*cdf0e10cSrcweir     protected:
322*cdf0e10cSrcweir         virtual ~RelationLoader(){}
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir         void loadTableData(const Any& _aTable);
325*cdf0e10cSrcweir     };
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir     void SAL_CALL RelationLoader::run()
328*cdf0e10cSrcweir     {
329*cdf0e10cSrcweir         const ::rtl::OUString* pIter = m_aTableList.getConstArray() + m_nStartIndex;
330*cdf0e10cSrcweir         for(sal_Int32 i = m_nStartIndex; i < m_nEndIndex;++i,++pIter)
331*cdf0e10cSrcweir         {
332*cdf0e10cSrcweir             ::rtl::OUString sCatalog,sSchema,sTable;
333*cdf0e10cSrcweir 		    ::dbtools::qualifiedNameComponents(m_xMetaData,
334*cdf0e10cSrcweir 											    *pIter,
335*cdf0e10cSrcweir 											    sCatalog,
336*cdf0e10cSrcweir 											    sSchema,
337*cdf0e10cSrcweir 											    sTable,
338*cdf0e10cSrcweir 											    ::dbtools::eInDataManipulation);
339*cdf0e10cSrcweir 		    Any aCatalog;
340*cdf0e10cSrcweir 		    if ( sCatalog.getLength() )
341*cdf0e10cSrcweir 			    aCatalog <<= sCatalog;
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir             try
344*cdf0e10cSrcweir             {
345*cdf0e10cSrcweir 		        Reference< XResultSet > xResult = m_xMetaData->getImportedKeys(aCatalog, sSchema,sTable);
346*cdf0e10cSrcweir 		        if ( xResult.is() && xResult->next() )
347*cdf0e10cSrcweir                 {
348*cdf0e10cSrcweir                     ::comphelper::disposeComponent(xResult);
349*cdf0e10cSrcweir 			        loadTableData(m_xTables->getByName(*pIter));
350*cdf0e10cSrcweir                 } // if ( xResult.is() && xResult->next() )
351*cdf0e10cSrcweir             }
352*cdf0e10cSrcweir             catch( const Exception& )
353*cdf0e10cSrcweir             {
354*cdf0e10cSrcweir                 DBG_UNHANDLED_EXCEPTION();
355*cdf0e10cSrcweir             }
356*cdf0e10cSrcweir         }
357*cdf0e10cSrcweir     }
358*cdf0e10cSrcweir     void SAL_CALL RelationLoader::onTerminated()
359*cdf0e10cSrcweir     {
360*cdf0e10cSrcweir         m_pParent->mergeData(m_vTableConnectionData);
361*cdf0e10cSrcweir         delete this;
362*cdf0e10cSrcweir     }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir     void RelationLoader::loadTableData(const Any& _aTable)
365*cdf0e10cSrcweir     {
366*cdf0e10cSrcweir         Reference<XPropertySet> xTableProp(_aTable,UNO_QUERY);
367*cdf0e10cSrcweir         const ::rtl::OUString sSourceName = ::dbtools::composeTableName( m_xMetaData, xTableProp, ::dbtools::eInTableDefinitions, false, false, false );
368*cdf0e10cSrcweir         TTableDataHelper::iterator aFind = m_aTableData.find(sSourceName);
369*cdf0e10cSrcweir         bool bNotFound = true, bAdded = false;
370*cdf0e10cSrcweir         if ( aFind == m_aTableData.end() )
371*cdf0e10cSrcweir         {
372*cdf0e10cSrcweir             aFind = m_aTableData.insert(TTableDataHelper::value_type(sSourceName,::boost::shared_ptr<OTableWindowData>(new OTableWindowData(xTableProp,sSourceName, sSourceName)))).first;
373*cdf0e10cSrcweir 		    aFind->second->ShowAll(sal_False);
374*cdf0e10cSrcweir             bAdded = true;
375*cdf0e10cSrcweir         }
376*cdf0e10cSrcweir         TTableWindowData::value_type pReferencingTable = aFind->second;
377*cdf0e10cSrcweir 	    Reference<XIndexAccess> xKeys = pReferencingTable->getKeys();
378*cdf0e10cSrcweir         const Reference<XKeysSupplier> xKeySup(xTableProp,UNO_QUERY);
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir 	    if ( !xKeys.is() && xKeySup.is() )
381*cdf0e10cSrcweir 	    {
382*cdf0e10cSrcweir 		    xKeys = xKeySup->getKeys();
383*cdf0e10cSrcweir         }
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir 	    if ( xKeys.is() )
386*cdf0e10cSrcweir 	    {
387*cdf0e10cSrcweir 		    Reference<XPropertySet> xKey;
388*cdf0e10cSrcweir             const sal_Int32 nCount = xKeys->getCount();
389*cdf0e10cSrcweir 		    for(sal_Int32 i = 0 ; i < nCount ; ++i)
390*cdf0e10cSrcweir 		    {
391*cdf0e10cSrcweir 			    xKeys->getByIndex(i) >>= xKey;
392*cdf0e10cSrcweir 			    sal_Int32 nKeyType = 0;
393*cdf0e10cSrcweir 			    xKey->getPropertyValue(PROPERTY_TYPE) >>= nKeyType;
394*cdf0e10cSrcweir 			    if ( KeyType::FOREIGN == nKeyType )
395*cdf0e10cSrcweir 			    {
396*cdf0e10cSrcweir                     bNotFound = false;
397*cdf0e10cSrcweir 				    ::rtl::OUString sReferencedTable;
398*cdf0e10cSrcweir 				    xKey->getPropertyValue(PROPERTY_REFERENCEDTABLE) >>= sReferencedTable;
399*cdf0e10cSrcweir 				    //////////////////////////////////////////////////////////////////////
400*cdf0e10cSrcweir 				    // insert windows
401*cdf0e10cSrcweir                     TTableDataHelper::iterator aRefFind = m_aTableData.find(sReferencedTable);
402*cdf0e10cSrcweir                     if ( aRefFind == m_aTableData.end() )
403*cdf0e10cSrcweir                     {
404*cdf0e10cSrcweir                         if ( m_xTables->hasByName(sReferencedTable) )
405*cdf0e10cSrcweir                         {
406*cdf0e10cSrcweir                             Reference<XPropertySet>  xReferencedTable(m_xTables->getByName(sReferencedTable),UNO_QUERY);
407*cdf0e10cSrcweir 					        aRefFind = m_aTableData.insert(TTableDataHelper::value_type(sReferencedTable,::boost::shared_ptr<OTableWindowData>(new OTableWindowData(xReferencedTable,sReferencedTable, sReferencedTable)))).first;
408*cdf0e10cSrcweir 		                    aRefFind->second->ShowAll(sal_False);
409*cdf0e10cSrcweir                         }
410*cdf0e10cSrcweir                         else
411*cdf0e10cSrcweir                             continue; // table name could not be found so we do not show this table releation
412*cdf0e10cSrcweir                     } // if ( aFind == m_aTableData.end() )
413*cdf0e10cSrcweir                     TTableWindowData::value_type pReferencedTable = aRefFind->second;
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir 				    ::rtl::OUString sKeyName;
416*cdf0e10cSrcweir 				    xKey->getPropertyValue(PROPERTY_NAME) >>= sKeyName;
417*cdf0e10cSrcweir 				    //////////////////////////////////////////////////////////////////////
418*cdf0e10cSrcweir 				    // insert connection
419*cdf0e10cSrcweir                     ORelationTableConnectionData* pTabConnData = new ORelationTableConnectionData( pReferencingTable, pReferencedTable, sKeyName );
420*cdf0e10cSrcweir                     m_vTableConnectionData.push_back(TTableConnectionData::value_type(pTabConnData));
421*cdf0e10cSrcweir 				    //////////////////////////////////////////////////////////////////////
422*cdf0e10cSrcweir 				    // insert columns
423*cdf0e10cSrcweir 				    const Reference<XColumnsSupplier> xColsSup(xKey,UNO_QUERY);
424*cdf0e10cSrcweir 				    OSL_ENSURE(xColsSup.is(),"Key is no XColumnsSupplier!");
425*cdf0e10cSrcweir 				    const Reference<XNameAccess> xColumns		= xColsSup->getColumns();
426*cdf0e10cSrcweir 				    const Sequence< ::rtl::OUString> aNames	= xColumns->getElementNames();
427*cdf0e10cSrcweir 				    const ::rtl::OUString* pIter	= aNames.getConstArray();
428*cdf0e10cSrcweir 				    const ::rtl::OUString* pEnd		= pIter + aNames.getLength();
429*cdf0e10cSrcweir 				    ::rtl::OUString sColumnName,sRelatedName;
430*cdf0e10cSrcweir 				    for(sal_uInt16 j=0;pIter != pEnd;++pIter,++j)
431*cdf0e10cSrcweir 				    {
432*cdf0e10cSrcweir 					    const Reference<XPropertySet> xPropSet(xColumns->getByName(*pIter),UNO_QUERY);
433*cdf0e10cSrcweir 					    OSL_ENSURE(xPropSet.is(),"Invalid column found in KeyColumns!");
434*cdf0e10cSrcweir                         if ( xPropSet.is() )
435*cdf0e10cSrcweir                         {
436*cdf0e10cSrcweir 					        xPropSet->getPropertyValue(PROPERTY_NAME)			>>= sColumnName;
437*cdf0e10cSrcweir 					        xPropSet->getPropertyValue(PROPERTY_RELATEDCOLUMN)	>>= sRelatedName;
438*cdf0e10cSrcweir                         }
439*cdf0e10cSrcweir 					    pTabConnData->SetConnLine( j, sColumnName, sRelatedName );
440*cdf0e10cSrcweir 				    }
441*cdf0e10cSrcweir 				    //////////////////////////////////////////////////////////////////////
442*cdf0e10cSrcweir 				    // Update/Del-Flags setzen
443*cdf0e10cSrcweir 				    sal_Int32	nUpdateRule = 0;
444*cdf0e10cSrcweir 				    sal_Int32	nDeleteRule = 0;
445*cdf0e10cSrcweir 				    xKey->getPropertyValue(PROPERTY_UPDATERULE) >>= nUpdateRule;
446*cdf0e10cSrcweir 				    xKey->getPropertyValue(PROPERTY_DELETERULE) >>= nDeleteRule;
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir 				    pTabConnData->SetUpdateRules( nUpdateRule );
449*cdf0e10cSrcweir 				    pTabConnData->SetDeleteRules( nDeleteRule );
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir 				    //////////////////////////////////////////////////////////////////////
452*cdf0e10cSrcweir 				    // Kardinalitaet setzen
453*cdf0e10cSrcweir 				    pTabConnData->SetCardinality();
454*cdf0e10cSrcweir 			    }
455*cdf0e10cSrcweir 		    }
456*cdf0e10cSrcweir 	    } // if ( xKeys.is() )
457*cdf0e10cSrcweir     }
458*cdf0e10cSrcweir }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir void ORelationController::mergeData(const TTableConnectionData& _aConnectionData)
461*cdf0e10cSrcweir {
462*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( getMutex() );
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir     ::std::copy( _aConnectionData.begin(), _aConnectionData.end(), ::std::back_inserter( m_vTableConnectionData ));
465*cdf0e10cSrcweir     //const Reference< XDatabaseMetaData> xMetaData = getConnection()->getMetaData();
466*cdf0e10cSrcweir     const sal_Bool bCase = sal_True;//xMetaData.is() && xMetaData->supportsMixedCaseQuotedIdentifiers();
467*cdf0e10cSrcweir     // here we are finished, so we can collect the table from connection data
468*cdf0e10cSrcweir     TTableConnectionData::iterator aConnDataIter = m_vTableConnectionData.begin();
469*cdf0e10cSrcweir     TTableConnectionData::iterator aConnDataEnd = m_vTableConnectionData.end();
470*cdf0e10cSrcweir     for(;aConnDataIter != aConnDataEnd;++aConnDataIter)
471*cdf0e10cSrcweir     {
472*cdf0e10cSrcweir         if ( !existsTable((*aConnDataIter)->getReferencingTable()->GetComposedName(),bCase) )
473*cdf0e10cSrcweir         {
474*cdf0e10cSrcweir             m_vTableData.push_back((*aConnDataIter)->getReferencingTable());
475*cdf0e10cSrcweir         }
476*cdf0e10cSrcweir         if ( !existsTable((*aConnDataIter)->getReferencedTable()->GetComposedName(),bCase) )
477*cdf0e10cSrcweir         {
478*cdf0e10cSrcweir             m_vTableData.push_back((*aConnDataIter)->getReferencedTable());
479*cdf0e10cSrcweir         }
480*cdf0e10cSrcweir     } // for(;aConnDataIter != aConnDataEnd;++aConnDataIter)
481*cdf0e10cSrcweir     if ( m_nThreadEvent )
482*cdf0e10cSrcweir     {
483*cdf0e10cSrcweir         --m_nThreadEvent;
484*cdf0e10cSrcweir         if ( !m_nThreadEvent )
485*cdf0e10cSrcweir             Application::PostUserEvent(LINK(this, ORelationController, OnThreadFinished));
486*cdf0e10cSrcweir     }
487*cdf0e10cSrcweir }
488*cdf0e10cSrcweir // -----------------------------------------------------------------------------
489*cdf0e10cSrcweir IMPL_LINK( ORelationController, OnThreadFinished, void*, /*NOTINTERESTEDIN*/ )
490*cdf0e10cSrcweir {
491*cdf0e10cSrcweir     vos::OGuard aSolarGuard( Application::GetSolarMutex() );
492*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( getMutex() );
493*cdf0e10cSrcweir     try
494*cdf0e10cSrcweir 	{
495*cdf0e10cSrcweir         getView()->initialize();	// show the windows and fill with our informations
496*cdf0e10cSrcweir 	    getView()->Invalidate(INVALIDATE_NOERASE);
497*cdf0e10cSrcweir         ClearUndoManager();
498*cdf0e10cSrcweir 	    setModified(sal_False);		// and we are not modified yet
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir 	    if(m_vTableData.empty())
501*cdf0e10cSrcweir 		    Execute(ID_BROWSER_ADDTABLE,Sequence<PropertyValue>());
502*cdf0e10cSrcweir     }
503*cdf0e10cSrcweir     catch( const Exception& )
504*cdf0e10cSrcweir     {
505*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
506*cdf0e10cSrcweir     }
507*cdf0e10cSrcweir     m_pWaitObject.reset();
508*cdf0e10cSrcweir     return 0L;
509*cdf0e10cSrcweir }
510*cdf0e10cSrcweir // -----------------------------------------------------------------------------
511*cdf0e10cSrcweir void ORelationController::loadData()
512*cdf0e10cSrcweir {
513*cdf0e10cSrcweir 	m_pWaitObject.reset( new WaitObject(getView()) );
514*cdf0e10cSrcweir 	try
515*cdf0e10cSrcweir 	{
516*cdf0e10cSrcweir 		if ( !m_xTables.is() )
517*cdf0e10cSrcweir 			return;
518*cdf0e10cSrcweir         DatabaseMetaData aMeta(getConnection());
519*cdf0e10cSrcweir 	    // this may take some time
520*cdf0e10cSrcweir 	    const Reference< XDatabaseMetaData> xMetaData = getConnection()->getMetaData();
521*cdf0e10cSrcweir 	    const Sequence< ::rtl::OUString> aNames = m_xTables->getElementNames();
522*cdf0e10cSrcweir         const sal_Int32 nCount = aNames.getLength();
523*cdf0e10cSrcweir         if ( aMeta.supportsThreads() )
524*cdf0e10cSrcweir         {
525*cdf0e10cSrcweir 		    const sal_Int32 nMaxElements = (nCount / MAX_THREADS) +1;
526*cdf0e10cSrcweir             sal_Int32 nStart = 0,nEnd = ::std::min(nMaxElements,nCount);
527*cdf0e10cSrcweir 		    while(nStart != nEnd)
528*cdf0e10cSrcweir 		    {
529*cdf0e10cSrcweir                 ++m_nThreadEvent;
530*cdf0e10cSrcweir                 RelationLoader* pThread = new RelationLoader(this,xMetaData,m_xTables,aNames,nStart,nEnd);
531*cdf0e10cSrcweir                 pThread->createSuspended();
532*cdf0e10cSrcweir                 pThread->setPriority(osl_Thread_PriorityBelowNormal);
533*cdf0e10cSrcweir                 pThread->resume();
534*cdf0e10cSrcweir                 nStart = nEnd;
535*cdf0e10cSrcweir                 nEnd += nMaxElements;
536*cdf0e10cSrcweir                 nEnd = ::std::min(nEnd,nCount);
537*cdf0e10cSrcweir             } // for(;pIter != pEnd;++pIter)
538*cdf0e10cSrcweir         } // if ( aMeta.supportsThreads() )
539*cdf0e10cSrcweir         else
540*cdf0e10cSrcweir         {
541*cdf0e10cSrcweir             RelationLoader* pThread = new RelationLoader(this,xMetaData,m_xTables,aNames,0,nCount);
542*cdf0e10cSrcweir             pThread->run();
543*cdf0e10cSrcweir             pThread->onTerminated();
544*cdf0e10cSrcweir         }
545*cdf0e10cSrcweir 	}
546*cdf0e10cSrcweir 	catch(SQLException& e)
547*cdf0e10cSrcweir 	{
548*cdf0e10cSrcweir 		showError(SQLExceptionInfo(e));
549*cdf0e10cSrcweir 	}
550*cdf0e10cSrcweir 	catch(const Exception&)
551*cdf0e10cSrcweir 	{
552*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
553*cdf0e10cSrcweir 	}
554*cdf0e10cSrcweir }
555*cdf0e10cSrcweir // -----------------------------------------------------------------------------
556*cdf0e10cSrcweir TTableWindowData::value_type ORelationController::existsTable(const ::rtl::OUString& _rComposedTableName,sal_Bool _bCase)  const
557*cdf0e10cSrcweir {
558*cdf0e10cSrcweir 	::comphelper::UStringMixEqual bCase(_bCase);
559*cdf0e10cSrcweir 	TTableWindowData::const_iterator aIter = m_vTableData.begin();
560*cdf0e10cSrcweir     TTableWindowData::const_iterator aEnd = m_vTableData.end();
561*cdf0e10cSrcweir 	for(;aIter != aEnd;++aIter)
562*cdf0e10cSrcweir 	{
563*cdf0e10cSrcweir 		if(bCase((*aIter)->GetComposedName(),_rComposedTableName))
564*cdf0e10cSrcweir 			break;
565*cdf0e10cSrcweir 	}
566*cdf0e10cSrcweir     return ( aIter != aEnd) ? *aIter : TTableWindowData::value_type();
567*cdf0e10cSrcweir }
568*cdf0e10cSrcweir // -----------------------------------------------------------------------------
569*cdf0e10cSrcweir void ORelationController::loadLayoutInformation()
570*cdf0e10cSrcweir {
571*cdf0e10cSrcweir 	try
572*cdf0e10cSrcweir 	{
573*cdf0e10cSrcweir 		OSL_ENSURE(haveDataSource(),"We need a datasource from our connection!");
574*cdf0e10cSrcweir 		if ( haveDataSource() )
575*cdf0e10cSrcweir 		{
576*cdf0e10cSrcweir 			if ( getDataSource()->getPropertySetInfo()->hasPropertyByName(PROPERTY_LAYOUTINFORMATION) )
577*cdf0e10cSrcweir 			{
578*cdf0e10cSrcweir 				Sequence<PropertyValue> aWindows;
579*cdf0e10cSrcweir 				getDataSource()->getPropertyValue(PROPERTY_LAYOUTINFORMATION) >>= aWindows;
580*cdf0e10cSrcweir 				loadTableWindows(aWindows);
581*cdf0e10cSrcweir 			}
582*cdf0e10cSrcweir 		}
583*cdf0e10cSrcweir 	}
584*cdf0e10cSrcweir 	catch(Exception&)
585*cdf0e10cSrcweir 	{
586*cdf0e10cSrcweir 	}
587*cdf0e10cSrcweir }
588*cdf0e10cSrcweir // -----------------------------------------------------------------------------
589*cdf0e10cSrcweir void ORelationController::reset()
590*cdf0e10cSrcweir {
591*cdf0e10cSrcweir 	loadLayoutInformation();
592*cdf0e10cSrcweir 	ODataView* pView = getView();
593*cdf0e10cSrcweir 	OSL_ENSURE(pView,"No current view!");
594*cdf0e10cSrcweir 	if(pView)
595*cdf0e10cSrcweir 	{
596*cdf0e10cSrcweir 		pView->initialize();
597*cdf0e10cSrcweir 		pView->Invalidate(INVALIDATE_NOERASE);
598*cdf0e10cSrcweir 	}
599*cdf0e10cSrcweir }
600*cdf0e10cSrcweir 
601*cdf0e10cSrcweir // -----------------------------------------------------------------------------
602*cdf0e10cSrcweir bool ORelationController::allowViews() const
603*cdf0e10cSrcweir {
604*cdf0e10cSrcweir     return false;
605*cdf0e10cSrcweir }
606*cdf0e10cSrcweir 
607*cdf0e10cSrcweir // -----------------------------------------------------------------------------
608*cdf0e10cSrcweir bool ORelationController::allowQueries() const
609*cdf0e10cSrcweir {
610*cdf0e10cSrcweir     return false;
611*cdf0e10cSrcweir }
612*cdf0e10cSrcweir 
613*cdf0e10cSrcweir // -----------------------------------------------------------------------------
614*cdf0e10cSrcweir 
615*cdf0e10cSrcweir 
616