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