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 "RelationTableView.hxx"
21 : #include "JoinExchange.hxx"
22 : #include <comphelper/extract.hxx>
23 : #include "browserids.hxx"
24 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
25 : #include <com/sun/star/sdbc/XConnection.hpp>
26 : #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
27 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
28 : #include <com/sun/star/sdbcx/KeyType.hpp>
29 : #include <com/sun/star/container/XIndexAccess.hpp>
30 : #include <com/sun/star/container/XNameAccess.hpp>
31 : #include <com/sun/star/beans/XPropertySet.hpp>
32 : #include "dbustrings.hrc"
33 : #include <connectivity/dbtools.hxx>
34 : #include <comphelper/sequence.hxx>
35 : #include <tools/debug.hxx>
36 : #include "dbaccess_helpid.hrc"
37 : #include "RelationDesignView.hxx"
38 : #include "JoinController.hxx"
39 : #include "TableWindow.hxx"
40 : #include "TableWindowData.hxx"
41 : #include "RTableConnection.hxx"
42 : #include "RTableConnectionData.hxx"
43 : #include "RelationDlg.hxx"
44 : #include "sqlmessage.hxx"
45 : #include "dbu_rel.hrc"
46 : #include "UITools.hxx"
47 : #include <connectivity/dbexception.hxx>
48 : #include "RTableWindow.hxx"
49 : #include "JAccess.hxx"
50 : #include <svl/undo.hxx>
51 : #include <com/sun/star/accessibility/AccessibleEventId.hpp>
52 :
53 : using namespace dbaui;
54 : using namespace ::dbtools;
55 : using namespace ::com::sun::star;
56 : using namespace ::com::sun::star::uno;
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::accessibility;
62 :
63 : //==================================================================
64 : // class ORelationTableView
65 : //==================================================================
66 : DBG_NAME(ORelationTableView)
67 : //------------------------------------------------------------------------
68 0 : ORelationTableView::ORelationTableView( Window* pParent, ORelationDesignView* pView )
69 : :OJoinTableView( pParent, pView )
70 : , ::comphelper::OContainerListener(m_aMutex)
71 : ,m_pExistingConnection(NULL)
72 0 : ,m_bInRemove(false)
73 :
74 : {
75 : DBG_CTOR(ORelationTableView,NULL);
76 0 : SetHelpId(HID_CTL_RELATIONTAB);
77 0 : }
78 :
79 : //------------------------------------------------------------------------
80 0 : ORelationTableView::~ORelationTableView()
81 : {
82 : DBG_DTOR(ORelationTableView,NULL);
83 0 : if ( m_pContainerListener.is() )
84 0 : m_pContainerListener->dispose();
85 0 : }
86 :
87 : //------------------------------------------------------------------------
88 0 : void ORelationTableView::ReSync()
89 : {
90 : DBG_CHKTHIS(ORelationTableView,NULL);
91 0 : if ( !m_pContainerListener.is() )
92 : {
93 0 : Reference< XConnection> xConnection = m_pView->getController().getConnection();
94 0 : Reference< XTablesSupplier > xTableSupp( xConnection, UNO_QUERY_THROW );
95 0 : Reference< XNameAccess > xTables = xTableSupp->getTables();
96 0 : Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
97 0 : if ( xContainer.is() )
98 0 : m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
99 : }
100 : // Es kann sein, dass in der DB Tabellen ausgeblendet wurden, die eigentlich Bestandteil einer Relation sind. Oder eine Tabelle
101 : // befand sich im Layout (durchaus ohne Relation), existiert aber nicht mehr. In beiden Faellen wird das Anlegen des TabWins schief
102 : // gehen, und alle solchen TabWinDatas oder darauf bezogenen ConnDatas muss ich dann loeschen.
103 0 : ::std::vector< ::rtl::OUString> arrInvalidTables;
104 :
105 : //////////////////////////////////////////////////////////////////////
106 : // create and insert windows
107 0 : TTableWindowData* pTabWinDataList = m_pView->getController().getTableWindowData();
108 0 : TTableWindowData::reverse_iterator aIter = pTabWinDataList->rbegin();
109 0 : for(;aIter != pTabWinDataList->rend();++aIter)
110 : {
111 0 : TTableWindowData::value_type pData = *aIter;
112 0 : OTableWindow* pTabWin = createWindow(pData);
113 :
114 0 : if (!pTabWin->Init())
115 : {
116 : // das Initialisieren ging schief, dass heisst, dieses TabWin steht nicht zur Verfuegung, also muss ich es inklusive
117 : // seiner Daten am Dokument aufraeumen
118 0 : pTabWin->clearListBox();
119 0 : delete pTabWin;
120 0 : arrInvalidTables.push_back(pData->GetTableName());
121 :
122 0 : pTabWinDataList->erase( ::std::remove(pTabWinDataList->begin(),pTabWinDataList->end(),*aIter) ,pTabWinDataList->end());
123 0 : continue;
124 : }
125 :
126 0 : (*GetTabWinMap())[pData->GetComposedName()] = pTabWin; // am Anfang einfuegen, da ich die DataList ja rueckwaerts durchlaufe
127 : // wenn in den Daten keine Position oder Groesse steht -> Default
128 0 : if (!pData->HasPosition() && !pData->HasSize())
129 0 : SetDefaultTabWinPosSize(pTabWin);
130 :
131 0 : pTabWin->Show();
132 0 : }
133 :
134 : // Verbindungen einfuegen
135 0 : TTableConnectionData* pTabConnDataList = m_pView->getController().getTableConnectionData();
136 0 : TTableConnectionData::reverse_iterator aConIter = pTabConnDataList->rbegin();
137 :
138 0 : for(;aConIter != pTabConnDataList->rend();++aConIter)
139 : {
140 0 : ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(aConIter->get());
141 0 : if ( !arrInvalidTables.empty() )
142 : {
143 : // gibt es die beiden Tabellen zur Connection ?
144 0 : ::rtl::OUString strTabExistenceTest = pTabConnData->getReferencingTable()->GetTableName();
145 0 : sal_Bool bInvalid = ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
146 0 : strTabExistenceTest = pTabConnData->getReferencedTable()->GetTableName();
147 0 : bInvalid = bInvalid || ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
148 :
149 0 : if (bInvalid)
150 : { // nein -> Pech gehabt, die Connection faellt weg
151 0 : pTabConnDataList->erase( ::std::remove(pTabConnDataList->begin(),pTabConnDataList->end(),*aConIter),pTabConnDataList->end() );
152 0 : continue;
153 0 : }
154 : }
155 :
156 0 : addConnection( new ORelationTableConnection(this, *aConIter), sal_False ); // don't add the data again
157 : }
158 :
159 0 : if ( !GetTabWinMap()->empty() )
160 0 : GetTabWinMap()->begin()->second->GrabFocus();
161 0 : }
162 : //------------------------------------------------------------------------------
163 0 : sal_Bool ORelationTableView::IsAddAllowed()
164 : {
165 : DBG_CHKTHIS(ORelationTableView,NULL);
166 :
167 0 : return !m_pView->getController().isReadOnly();
168 : }
169 : //------------------------------------------------------------------------
170 0 : void ORelationTableView::AddConnection(const OJoinExchangeData& jxdSource, const OJoinExchangeData& jxdDest)
171 : {
172 : DBG_CHKTHIS(ORelationTableView,NULL);
173 : // Aus selektierten Feldnamen LineDataObject setzen
174 : // check if relation already exists
175 0 : OTableWindow* pSourceWin = jxdSource.pListBox->GetTabWin();
176 0 : OTableWindow* pDestWin = jxdDest.pListBox->GetTabWin();
177 :
178 0 : ::std::vector<OTableConnection*>::const_iterator aIter = getTableConnections()->begin();
179 0 : ::std::vector<OTableConnection*>::const_iterator aEnd = getTableConnections()->end();
180 0 : for(;aIter != aEnd;++aIter)
181 : {
182 0 : OTableConnection* pFirst = *aIter;
183 0 : if((pFirst->GetSourceWin() == pSourceWin && pFirst->GetDestWin() == pDestWin) ||
184 0 : (pFirst->GetSourceWin() == pDestWin && pFirst->GetDestWin() == pSourceWin))
185 : {
186 0 : m_pExistingConnection = pFirst;
187 0 : break;
188 : }
189 : }
190 : // insert table connection into view
191 :
192 : TTableConnectionData::value_type pTabConnData(new ORelationTableConnectionData(pSourceWin->GetData(),
193 0 : pDestWin->GetData()));
194 :
195 : // die Namen der betroffenen Felder
196 0 : ::rtl::OUString sSourceFieldName = jxdSource.pListBox->GetEntryText(jxdSource.pEntry);
197 0 : ::rtl::OUString sDestFieldName = jxdDest.pListBox->GetEntryText(jxdDest.pEntry);
198 :
199 : // die Anzahl der PKey-Felder in der Quelle
200 0 : const Reference< XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(pSourceWin->GetData()->getTable());
201 0 : bool bAskUser = xPrimaryKeyColumns.is() && Reference< XIndexAccess>(xPrimaryKeyColumns,UNO_QUERY)->getCount() > 1;
202 :
203 0 : pTabConnData->SetConnLine( 0, sSourceFieldName, sDestFieldName );
204 :
205 0 : if ( bAskUser || m_pExistingConnection )
206 0 : m_pCurrentlyTabConnData = pTabConnData; // this implies that we ask the user what to do
207 : else
208 : {
209 : try
210 : {
211 : //////////////////////////////////////////////////////////////////////
212 : // Daten der Datenbank uebergeben
213 0 : if( pTabConnData->Update() )
214 : {
215 : //////////////////////////////////////////////////////////////////////
216 : // UI-Object in ConnListe eintragen
217 0 : addConnection( new ORelationTableConnection( this, pTabConnData ) );
218 : }
219 : }
220 0 : catch(const SQLException&)
221 : {
222 0 : throw;
223 : }
224 0 : catch(const Exception&)
225 : {
226 : OSL_FAIL("ORelationTableView::AddConnection: Exception oocured!");
227 : }
228 0 : }
229 0 : }
230 :
231 :
232 : //------------------------------------------------------------------------
233 0 : void ORelationTableView::ConnDoubleClicked( OTableConnection* pConnection )
234 : {
235 : DBG_CHKTHIS(ORelationTableView,NULL);
236 0 : ORelationDialog aRelDlg( this, pConnection->GetData() );
237 0 : switch (aRelDlg.Execute())
238 : {
239 : case RET_OK:
240 : // successfully updated
241 0 : pConnection->UpdateLineList();
242 : // The connection references 1 ConnData and n ConnLines, each ConnData references n LineDatas, each Line exactly 1 LineData
243 : // As the Dialog and the ConnData->Update may have changed the LineDatas we have to restore the consistent state
244 0 : break;
245 :
246 : case RET_NO:
247 : // tried at least one update, but did not succeed -> the original connection is lost
248 0 : RemoveConnection( pConnection ,sal_True);
249 0 : break;
250 :
251 : case RET_CANCEL:
252 : // no break, as nothing happened and we don't need the code below
253 0 : return;
254 :
255 : }
256 :
257 0 : Invalidate(INVALIDATE_NOCHILDREN);
258 : }
259 :
260 : //------------------------------------------------------------------------------
261 0 : void ORelationTableView::AddNewRelation()
262 : {
263 : DBG_CHKTHIS(ORelationTableView,NULL);
264 :
265 0 : TTableConnectionData::value_type pNewConnData( new ORelationTableConnectionData() );
266 0 : ORelationDialog aRelDlg(this, pNewConnData, sal_True);
267 :
268 0 : sal_Bool bSuccess = (aRelDlg.Execute() == RET_OK);
269 0 : if (bSuccess)
270 : {
271 : // already updated by the dialog
272 : // dem Dokument bekanntgeben
273 0 : addConnection( new ORelationTableConnection(this, pNewConnData) );
274 0 : }
275 0 : }
276 :
277 : //------------------------------------------------------------------------------
278 0 : bool ORelationTableView::RemoveConnection( OTableConnection* pConn ,sal_Bool /*_bDelete*/)
279 : {
280 : DBG_CHKTHIS(ORelationTableView,NULL);
281 0 : ORelationTableConnectionData* pTabConnData = (ORelationTableConnectionData*)pConn->GetData().get();
282 : try
283 : {
284 0 : if ( m_bInRemove || pTabConnData->DropRelation())
285 0 : return OJoinTableView::RemoveConnection( pConn ,sal_True);
286 : }
287 0 : catch(SQLException& e)
288 : {
289 0 : getDesignView()->getController().showError(SQLExceptionInfo(e));
290 : }
291 0 : catch(Exception&)
292 : {
293 : OSL_FAIL("ORelationTableView::RemoveConnection: Something other than SQLException occurred!");
294 : }
295 0 : return false;
296 : }
297 :
298 : //------------------------------------------------------------------------------
299 0 : void ORelationTableView::AddTabWin(const ::rtl::OUString& _rComposedName, const ::rtl::OUString& rWinName, sal_Bool /*bNewTable*/)
300 : {
301 : DBG_CHKTHIS(ORelationTableView,NULL);
302 : OSL_ENSURE(!_rComposedName.isEmpty(),"There must be a table name supplied!");
303 0 : OJoinTableView::OTableWindowMap::iterator aIter = GetTabWinMap()->find(_rComposedName);
304 :
305 0 : if(aIter != GetTabWinMap()->end())
306 : {
307 0 : aIter->second->SetZOrder(NULL, WINDOW_ZORDER_FIRST);
308 0 : aIter->second->GrabFocus();
309 0 : EnsureVisible(aIter->second);
310 : // no new one
311 0 : return;
312 : }
313 :
314 : //////////////////////////////////////////////////////////////////
315 : // Neue Datenstruktur in DocShell eintragen
316 0 : TTableWindowData::value_type pNewTabWinData(createTableWindowData( _rComposedName, rWinName,rWinName ));
317 0 : pNewTabWinData->ShowAll(sal_False);
318 :
319 : //////////////////////////////////////////////////////////////////
320 : // Neues Fenster in Fensterliste eintragen
321 0 : OTableWindow* pNewTabWin = createWindow( pNewTabWinData );
322 0 : if(pNewTabWin->Init())
323 : {
324 0 : m_pView->getController().getTableWindowData()->push_back( pNewTabWinData);
325 : // when we already have a table with this name insert the full qualified one instead
326 0 : (*GetTabWinMap())[_rComposedName] = pNewTabWin;
327 :
328 0 : SetDefaultTabWinPosSize( pNewTabWin );
329 0 : pNewTabWin->Show();
330 :
331 0 : modified();
332 :
333 0 : if ( m_pAccessible )
334 : m_pAccessible->notifyAccessibleEvent( AccessibleEventId::CHILD,
335 : Any(),
336 0 : makeAny(pNewTabWin->GetAccessible()));
337 : }
338 : else
339 : {
340 0 : pNewTabWin->clearListBox();
341 0 : delete pNewTabWin;
342 0 : }
343 : }
344 : // -----------------------------------------------------------------------------
345 0 : void ORelationTableView::RemoveTabWin( OTableWindow* pTabWin )
346 : {
347 0 : OSQLWarningBox aDlg( this, ModuleRes( STR_QUERY_REL_DELETE_WINDOW ), WB_YES_NO | WB_DEF_YES );
348 0 : if ( m_bInRemove || aDlg.Execute() == RET_YES )
349 : {
350 0 : m_pView->getController().ClearUndoManager();
351 0 : OJoinTableView::RemoveTabWin( pTabWin );
352 :
353 0 : m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
354 0 : m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
355 0 : m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
356 0 : }
357 0 : }
358 :
359 0 : void ORelationTableView::lookForUiActivities()
360 : {
361 0 : if(m_pExistingConnection)
362 : {
363 0 : String sTitle(ModuleRes(STR_RELATIONDESIGN));
364 0 : sTitle.Erase(0,3);
365 0 : OSQLMessageBox aDlg(this,ModuleRes(STR_QUERY_REL_EDIT_RELATION),String(),0);
366 0 : aDlg.SetText(sTitle);
367 0 : aDlg.RemoveButton(aDlg.GetButtonId(0));
368 0 : aDlg.AddButton( ModuleRes(STR_QUERY_REL_EDIT), BUTTONID_OK, BUTTONDIALOG_DEFBUTTON | BUTTONDIALOG_FOCUSBUTTON);
369 0 : aDlg.AddButton( ModuleRes(STR_QUERY_REL_CREATE), BUTTONID_YES, 0);
370 0 : aDlg.AddButton(BUTTON_CANCEL,BUTTONID_CANCEL,0);
371 0 : sal_uInt16 nRet = aDlg.Execute();
372 0 : if( nRet == RET_CANCEL)
373 : {
374 0 : m_pCurrentlyTabConnData.reset();
375 : }
376 0 : else if ( nRet == RET_OK ) // EDIT
377 : {
378 0 : ConnDoubleClicked(m_pExistingConnection);
379 0 : m_pCurrentlyTabConnData.reset();
380 : }
381 0 : m_pExistingConnection = NULL;
382 : }
383 0 : if(m_pCurrentlyTabConnData)
384 : {
385 0 : ORelationDialog aRelDlg( this, m_pCurrentlyTabConnData );
386 0 : if (aRelDlg.Execute() == RET_OK)
387 : {
388 : // already updated by the dialog
389 0 : addConnection( new ORelationTableConnection( this, m_pCurrentlyTabConnData ) );
390 : }
391 0 : m_pCurrentlyTabConnData.reset();
392 : }
393 0 : }
394 :
395 : // -----------------------------------------------------------------------------
396 0 : OTableWindow* ORelationTableView::createWindow(const TTableWindowData::value_type& _pData)
397 : {
398 0 : return new ORelationTableWindow(this,_pData);
399 : }
400 : // -----------------------------------------------------------------------------
401 0 : bool ORelationTableView::allowQueries() const
402 : {
403 0 : return false;
404 : }
405 : // -----------------------------------------------------------------------------
406 0 : void ORelationTableView::_elementInserted( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)
407 : {
408 :
409 0 : }
410 : // -----------------------------------------------------------------------------
411 0 : void ORelationTableView::_elementRemoved( const container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException)
412 : {
413 0 : m_bInRemove = true;
414 0 : ::rtl::OUString sName;
415 0 : if ( _rEvent.Accessor >>= sName )
416 : {
417 0 : OTableWindow* pTableWindow = GetTabWindow(sName);
418 0 : if ( pTableWindow )
419 : {
420 0 : m_pView->getController().ClearUndoManager();
421 0 : OJoinTableView::RemoveTabWin( pTableWindow );
422 :
423 0 : m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
424 0 : m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
425 0 : m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
426 : }
427 : }
428 0 : m_bInRemove = false;
429 0 : }
430 : // -----------------------------------------------------------------------------
431 0 : void ORelationTableView::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)
432 : {
433 0 : }
434 :
435 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|