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 : // class ORelationTableView
64 0 : ORelationTableView::ORelationTableView( vcl::Window* pParent, ORelationDesignView* pView )
65 : :OJoinTableView( pParent, pView )
66 : , ::comphelper::OContainerListener(m_aMutex)
67 : ,m_pExistingConnection(NULL)
68 0 : ,m_bInRemove(false)
69 :
70 : {
71 0 : SetHelpId(HID_CTL_RELATIONTAB);
72 0 : }
73 :
74 0 : ORelationTableView::~ORelationTableView()
75 : {
76 0 : if ( m_pContainerListener.is() )
77 0 : m_pContainerListener->dispose();
78 0 : }
79 :
80 0 : void ORelationTableView::ReSync()
81 : {
82 0 : if ( !m_pContainerListener.is() )
83 : {
84 0 : Reference< XConnection> xConnection = m_pView->getController().getConnection();
85 0 : Reference< XTablesSupplier > xTableSupp( xConnection, UNO_QUERY_THROW );
86 0 : Reference< XNameAccess > xTables = xTableSupp->getTables();
87 0 : Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
88 0 : if ( xContainer.is() )
89 0 : m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
90 : }
91 : // Tables could have been hidden in the database, which are part of a relation. Or a table was in layout
92 : // (quite often without a relation) and does not exist anymore. In both cases creation of TabWins will fail
93 : // and all TabWinDatas and related ConnDates should be deleted.
94 0 : ::std::vector< OUString> arrInvalidTables;
95 :
96 : // create and insert windows
97 0 : TTableWindowData& rTabWinDataList = m_pView->getController().getTableWindowData();
98 0 : TTableWindowData::reverse_iterator aIter = rTabWinDataList.rbegin();
99 0 : for(;aIter != rTabWinDataList.rend();++aIter)
100 : {
101 0 : TTableWindowData::value_type pData = *aIter;
102 0 : OTableWindow* pTabWin = createWindow(pData);
103 :
104 0 : if (!pTabWin->Init())
105 : {
106 : // initialisation failed, which means this TabWin is not available, therefore,
107 : // it should be cleaned up, including its data in the document
108 0 : pTabWin->clearListBox();
109 0 : delete pTabWin;
110 0 : arrInvalidTables.push_back(pData->GetTableName());
111 :
112 0 : rTabWinDataList.erase( ::std::remove(rTabWinDataList.begin(), rTabWinDataList.end(), *aIter), rTabWinDataList.end());
113 0 : continue;
114 : }
115 :
116 0 : GetTabWinMap()[pData->GetComposedName()] = pTabWin; // insert at the beginning, as the Datalist is walked through backward
117 : // if there's no position or size contained in the data -> Default
118 0 : if (!pData->HasPosition() && !pData->HasSize())
119 0 : SetDefaultTabWinPosSize(pTabWin);
120 :
121 0 : pTabWin->Show();
122 0 : }
123 :
124 : // insert connection
125 0 : TTableConnectionData& rTabConnDataList = m_pView->getController().getTableConnectionData();
126 0 : TTableConnectionData::reverse_iterator aConIter = rTabConnDataList.rbegin();
127 :
128 0 : for(;aConIter != rTabConnDataList.rend();++aConIter)
129 : {
130 0 : ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(aConIter->get());
131 0 : if ( !arrInvalidTables.empty() )
132 : {
133 : // do the tables to the connection exist?
134 0 : OUString strTabExistenceTest = pTabConnData->getReferencingTable()->GetTableName();
135 0 : bool bInvalid = ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
136 0 : strTabExistenceTest = pTabConnData->getReferencedTable()->GetTableName();
137 0 : bInvalid = bInvalid || ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
138 :
139 0 : if (bInvalid)
140 : {
141 : // no -> bad luck, the connection is gone
142 0 : rTabConnDataList.erase( ::std::remove(rTabConnDataList.begin(), rTabConnDataList.end(), *aConIter), rTabConnDataList.end() );
143 0 : continue;
144 0 : }
145 : }
146 :
147 0 : addConnection( new ORelationTableConnection(this, *aConIter), false ); // don't add the data again
148 : }
149 :
150 0 : if ( !GetTabWinMap().empty() )
151 0 : GetTabWinMap().begin()->second->GrabFocus();
152 0 : }
153 :
154 0 : bool ORelationTableView::IsAddAllowed()
155 : {
156 :
157 0 : return !m_pView->getController().isReadOnly();
158 : }
159 :
160 0 : void ORelationTableView::AddConnection(const OJoinExchangeData& jxdSource, const OJoinExchangeData& jxdDest)
161 : {
162 : // Set LineDataObject based on selected fieldname
163 : // check if relation already exists
164 0 : OTableWindow* pSourceWin = jxdSource.pListBox->GetTabWin();
165 0 : OTableWindow* pDestWin = jxdDest.pListBox->GetTabWin();
166 :
167 0 : ::std::vector<OTableConnection*>::const_iterator aIter = getTableConnections().begin();
168 0 : ::std::vector<OTableConnection*>::const_iterator aEnd = getTableConnections().end();
169 0 : for(;aIter != aEnd;++aIter)
170 : {
171 0 : OTableConnection* pFirst = *aIter;
172 0 : if((pFirst->GetSourceWin() == pSourceWin && pFirst->GetDestWin() == pDestWin) ||
173 0 : (pFirst->GetSourceWin() == pDestWin && pFirst->GetDestWin() == pSourceWin))
174 : {
175 0 : m_pExistingConnection = pFirst;
176 0 : break;
177 : }
178 : }
179 : // insert table connection into view
180 : TTableConnectionData::value_type pTabConnData(new ORelationTableConnectionData(pSourceWin->GetData(),
181 0 : pDestWin->GetData()));
182 :
183 : // the names of the affected fields
184 0 : OUString sSourceFieldName = jxdSource.pListBox->GetEntryText(jxdSource.pEntry);
185 0 : OUString sDestFieldName = jxdDest.pListBox->GetEntryText(jxdDest.pEntry);
186 :
187 : // the number of PKey-Fields in the source
188 0 : const Reference< XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(pSourceWin->GetData()->getTable());
189 0 : bool bAskUser = xPrimaryKeyColumns.is() && Reference< XIndexAccess>(xPrimaryKeyColumns,UNO_QUERY)->getCount() > 1;
190 :
191 0 : pTabConnData->SetConnLine( 0, sSourceFieldName, sDestFieldName );
192 :
193 0 : if ( bAskUser || m_pExistingConnection )
194 0 : m_pCurrentlyTabConnData = pTabConnData; // this implies that we ask the user what to do
195 : else
196 : {
197 : try
198 : {
199 : // hand over data to the database
200 0 : if( pTabConnData->Update() )
201 : {
202 : // enter UI-object into ConnList
203 0 : addConnection( new ORelationTableConnection( this, pTabConnData ) );
204 : }
205 : }
206 0 : catch(const SQLException&)
207 : {
208 0 : throw;
209 : }
210 0 : catch(const Exception&)
211 : {
212 : OSL_FAIL("ORelationTableView::AddConnection: Exception oocured!");
213 : }
214 0 : }
215 0 : }
216 :
217 0 : void ORelationTableView::ConnDoubleClicked( OTableConnection* pConnection )
218 : {
219 0 : ORelationDialog aRelDlg( this, pConnection->GetData() );
220 0 : switch (aRelDlg.Execute())
221 : {
222 : case RET_OK:
223 : // successfully updated
224 0 : pConnection->UpdateLineList();
225 : // The connection references 1 ConnData and n ConnLines, each ConnData references n LineDatas, each Line exactly 1 LineData
226 : // As the Dialog and the ConnData->Update may have changed the LineDatas we have to restore the consistent state
227 0 : break;
228 :
229 : case RET_NO:
230 : // tried at least one update, but did not succeed -> the original connection is lost
231 0 : RemoveConnection( pConnection ,true);
232 0 : break;
233 :
234 : case RET_CANCEL:
235 : // no break, as nothing happened and we don't need the code below
236 0 : return;
237 :
238 : }
239 :
240 0 : Invalidate(INVALIDATE_NOCHILDREN);
241 : }
242 :
243 0 : void ORelationTableView::AddNewRelation()
244 : {
245 :
246 0 : TTableConnectionData::value_type pNewConnData( new ORelationTableConnectionData() );
247 0 : ORelationDialog aRelDlg(this, pNewConnData, true);
248 :
249 0 : bool bSuccess = (aRelDlg.Execute() == RET_OK);
250 0 : if (bSuccess)
251 : {
252 : // already updated by the dialog
253 : // announce it to the document
254 0 : addConnection( new ORelationTableConnection(this, pNewConnData) );
255 0 : }
256 0 : }
257 :
258 0 : bool ORelationTableView::RemoveConnection( OTableConnection* pConn ,bool /*_bDelete*/)
259 : {
260 0 : ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(pConn->GetData().get());
261 : try
262 : {
263 0 : if ( m_bInRemove || pTabConnData->DropRelation())
264 0 : return OJoinTableView::RemoveConnection( pConn ,true);
265 : }
266 0 : catch(SQLException& e)
267 : {
268 0 : getDesignView()->getController().showError(SQLExceptionInfo(e));
269 : }
270 0 : catch(Exception&)
271 : {
272 : OSL_FAIL("ORelationTableView::RemoveConnection: Something other than SQLException occurred!");
273 : }
274 0 : return false;
275 : }
276 :
277 0 : void ORelationTableView::AddTabWin(const OUString& _rComposedName, const OUString& rWinName, bool /*bNewTable*/)
278 : {
279 : OSL_ENSURE(!_rComposedName.isEmpty(),"There must be a table name supplied!");
280 0 : OJoinTableView::OTableWindowMap::iterator aIter = GetTabWinMap().find(_rComposedName);
281 :
282 0 : if(aIter != GetTabWinMap().end())
283 : {
284 0 : aIter->second->SetZOrder(NULL, WINDOW_ZORDER_FIRST);
285 0 : aIter->second->GrabFocus();
286 0 : EnsureVisible(aIter->second);
287 : // no new one
288 0 : return;
289 : }
290 :
291 : // enter the new data structure into DocShell
292 0 : TTableWindowData::value_type pNewTabWinData(createTableWindowData( _rComposedName, rWinName,rWinName ));
293 0 : pNewTabWinData->ShowAll(false);
294 :
295 : // link new window into the window list
296 0 : OTableWindow* pNewTabWin = createWindow( pNewTabWinData );
297 0 : if(pNewTabWin->Init())
298 : {
299 0 : m_pView->getController().getTableWindowData().push_back( pNewTabWinData);
300 : // when we already have a table with this name insert the full qualified one instead
301 0 : GetTabWinMap()[_rComposedName] = pNewTabWin;
302 :
303 0 : SetDefaultTabWinPosSize( pNewTabWin );
304 0 : pNewTabWin->Show();
305 :
306 0 : modified();
307 :
308 0 : if ( m_pAccessible )
309 : m_pAccessible->notifyAccessibleEvent( AccessibleEventId::CHILD,
310 : Any(),
311 0 : makeAny(pNewTabWin->GetAccessible()));
312 : }
313 : else
314 : {
315 0 : pNewTabWin->clearListBox();
316 0 : delete pNewTabWin;
317 0 : }
318 : }
319 :
320 0 : void ORelationTableView::RemoveTabWin( OTableWindow* pTabWin )
321 : {
322 0 : OSQLWarningBox aDlg( this, ModuleRes( STR_QUERY_REL_DELETE_WINDOW ), WB_YES_NO | WB_DEF_YES );
323 0 : if ( m_bInRemove || aDlg.Execute() == RET_YES )
324 : {
325 0 : m_pView->getController().ClearUndoManager();
326 0 : OJoinTableView::RemoveTabWin( pTabWin );
327 :
328 0 : m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
329 0 : m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
330 0 : m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
331 0 : }
332 0 : }
333 :
334 0 : void ORelationTableView::lookForUiActivities()
335 : {
336 0 : if(m_pExistingConnection)
337 : {
338 0 : OUString sTitle(ModuleRes(STR_RELATIONDESIGN));
339 0 : sTitle = sTitle.copy(3);
340 0 : OSQLMessageBox aDlg(this,ModuleRes(STR_QUERY_REL_EDIT_RELATION),OUString(),0);
341 0 : aDlg.SetText(sTitle);
342 0 : aDlg.RemoveButton(aDlg.GetButtonId(0));
343 0 : aDlg.AddButton( ModuleRes(STR_QUERY_REL_EDIT), RET_OK, BUTTONDIALOG_DEFBUTTON | BUTTONDIALOG_FOCUSBUTTON);
344 0 : aDlg.AddButton( ModuleRes(STR_QUERY_REL_CREATE), RET_YES, 0);
345 0 : aDlg.AddButton(BUTTON_CANCEL,RET_CANCEL,0);
346 0 : sal_uInt16 nRet = aDlg.Execute();
347 0 : if( nRet == RET_CANCEL)
348 : {
349 0 : m_pCurrentlyTabConnData.reset();
350 : }
351 0 : else if ( nRet == RET_OK ) // EDIT
352 : {
353 0 : ConnDoubleClicked(m_pExistingConnection);
354 0 : m_pCurrentlyTabConnData.reset();
355 : }
356 0 : m_pExistingConnection = NULL;
357 : }
358 0 : if(m_pCurrentlyTabConnData)
359 : {
360 0 : ORelationDialog aRelDlg( this, m_pCurrentlyTabConnData );
361 0 : if (aRelDlg.Execute() == RET_OK)
362 : {
363 : // already updated by the dialog
364 0 : addConnection( new ORelationTableConnection( this, m_pCurrentlyTabConnData ) );
365 : }
366 0 : m_pCurrentlyTabConnData.reset();
367 : }
368 0 : }
369 :
370 0 : OTableWindow* ORelationTableView::createWindow(const TTableWindowData::value_type& _pData)
371 : {
372 0 : return new ORelationTableWindow(this,_pData);
373 : }
374 :
375 0 : bool ORelationTableView::allowQueries() const
376 : {
377 0 : return false;
378 : }
379 :
380 0 : void ORelationTableView::_elementInserted( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException, std::exception)
381 : {
382 :
383 0 : }
384 :
385 0 : void ORelationTableView::_elementRemoved( const container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException, std::exception)
386 : {
387 0 : m_bInRemove = true;
388 0 : OUString sName;
389 0 : if ( _rEvent.Accessor >>= sName )
390 : {
391 0 : OTableWindow* pTableWindow = GetTabWindow(sName);
392 0 : if ( pTableWindow )
393 : {
394 0 : m_pView->getController().ClearUndoManager();
395 0 : OJoinTableView::RemoveTabWin( pTableWindow );
396 :
397 0 : m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
398 0 : m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
399 0 : m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
400 : }
401 : }
402 0 : m_bInRemove = false;
403 0 : }
404 :
405 0 : void ORelationTableView::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException, std::exception)
406 : {
407 72 : }
408 :
409 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|