LibreOffice Module dbaccess (master) 1
RelationTableView.cxx
Go to the documentation of this file.
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 <core_resource.hxx>
22#include <browserids.hxx>
23#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24#include <com/sun/star/sdbc/XConnection.hpp>
25#include <com/sun/star/sdbc/SQLException.hpp>
26#include <com/sun/star/container/XContainer.hpp>
27#include <com/sun/star/container/XIndexAccess.hpp>
28#include <com/sun/star/container/XNameAccess.hpp>
29#include <com/sun/star/beans/XPropertySet.hpp>
32#include <helpids.h>
34#include <JoinController.hxx>
35#include <TableWindow.hxx>
36#include <TableWindowData.hxx>
37#include "RTableConnection.hxx"
39#include <RelationDlg.hxx>
40#include <sqlmessage.hxx>
41#include <strings.hrc>
43#include "RTableWindow.hxx"
44#include <JAccess.hxx>
45#include <vcl/stdtext.hxx>
46#include <com/sun/star/accessibility/AccessibleEventId.hpp>
47
48using namespace dbaui;
49using namespace ::dbtools;
50using namespace ::com::sun::star;
51using namespace ::com::sun::star::uno;
52using namespace ::com::sun::star::sdbc;
53using namespace ::com::sun::star::sdbcx;
54using namespace ::com::sun::star::beans;
55using namespace ::com::sun::star::container;
56using namespace ::com::sun::star::accessibility;
57
58ORelationTableView::ORelationTableView( vcl::Window* pParent, ORelationDesignView* pView )
59 :OJoinTableView( pParent, pView )
61 ,m_pExistingConnection(nullptr)
62 ,m_bInRemove(false)
63
64{
66}
67
69{
71}
72
74{
75 if ( m_pContainerListener.is() )
76 m_pContainerListener->dispose();
79}
80
82{
83 if ( !m_pContainerListener.is() )
84 {
85 Reference< XConnection> xConnection = m_pView->getController().getConnection();
86 Reference< XTablesSupplier > xTableSupp( xConnection, UNO_QUERY_THROW );
87 Reference< XNameAccess > xTables = xTableSupp->getTables();
88 Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
89 if ( xContainer.is() )
90 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
91 }
92 // Tables could have been hidden in the database, which are part of a relation. Or a table was in layout
93 // (quite often without a relation) and does not exist anymore. In both cases creation of TabWins will fail
94 // and all TabWinDatas and related ConnDates should be deleted.
95 std::vector< OUString> arrInvalidTables;
96
97 // create and insert windows
98 TTableWindowData& rTabWinDataList = m_pView->getController().getTableWindowData();
99 TTableWindowData::const_reverse_iterator aIter = rTabWinDataList.rbegin();
100 for(;aIter != rTabWinDataList.rend();++aIter)
101 {
102 TTableWindowData::value_type pData = *aIter;
104
105 if (!pTabWin->Init())
106 {
107 // initialisation failed, which means this TabWin is not available, therefore,
108 // it should be cleaned up, including its data in the document
109 pTabWin->clearListBox();
110 pTabWin.disposeAndClear();
111 arrInvalidTables.push_back(pData->GetTableName());
112
113 rTabWinDataList.erase( std::remove(rTabWinDataList.begin(), rTabWinDataList.end(), *aIter), rTabWinDataList.end());
114 continue;
115 }
116
117 GetTabWinMap()[pData->GetComposedName()] = pTabWin; // insert at the beginning, as the Datalist is walked through backward
118 // if there's no position or size contained in the data -> Default
119 if (!pData->HasPosition() && !pData->HasSize())
121
122 pTabWin->Show();
123 }
124
125 // insert connection
126 TTableConnectionData& rTabConnDataList = m_pView->getController().getTableConnectionData();
127 TTableConnectionData::const_reverse_iterator aConIter = rTabConnDataList.rbegin();
128
129 for(;aConIter != rTabConnDataList.rend();++aConIter)
130 {
131 ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(aConIter->get());
132 if ( !arrInvalidTables.empty() )
133 {
134 // do the tables to the connection exist?
135 OUString strTabExistenceTest = pTabConnData->getReferencingTable()->GetTableName();
136 bool bInvalid = std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
137 strTabExistenceTest = pTabConnData->getReferencedTable()->GetTableName();
138 bInvalid = bInvalid || std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
139
140 if (bInvalid)
141 {
142 // no -> bad luck, the connection is gone
143 rTabConnDataList.erase( std::remove(rTabConnDataList.begin(), rTabConnDataList.end(), *aConIter), rTabConnDataList.end() );
144 continue;
145 }
146 }
147
149 }
150
151 if ( !GetTabWinMap().empty() )
152 GetTabWinMap().begin()->second->GrabFocus();
153}
154
156{
157
158 return !m_pView->getController().isReadOnly();
159}
160
162{
163 // Set LineDataObject based on selected fieldname
164 // check if relation already exists
165 OTableWindow* pSourceWin = jxdSource.pListBox->GetTabWin();
166 OTableWindow* pDestWin = jxdDest.pListBox->GetTabWin();
167
168 for(VclPtr<OTableConnection> const & pFirst : getTableConnections())
169 {
170 if((pFirst->GetSourceWin() == pSourceWin && pFirst->GetDestWin() == pDestWin) ||
171 (pFirst->GetSourceWin() == pDestWin && pFirst->GetDestWin() == pSourceWin))
172 {
173 m_pExistingConnection = pFirst;
174 break;
175 }
176 }
177 // insert table connection into view
178 TTableConnectionData::value_type pTabConnData = std::make_shared<ORelationTableConnectionData>(pSourceWin->GetData(),
179 pDestWin->GetData());
180
181 // the names of the affected fields
182 weld::TreeView& rSourceTreeView = jxdSource.pListBox->get_widget();
183 OUString sSourceFieldName = rSourceTreeView.get_text(jxdSource.nEntry);
184 weld::TreeView& rDestTreeView = jxdDest.pListBox->get_widget();
185 OUString sDestFieldName = rDestTreeView.get_text(jxdDest.nEntry);
186
187 // the number of PKey-Fields in the source
188 const Reference< XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(pSourceWin->GetData()->getTable());
189 bool bAskUser = xPrimaryKeyColumns.is() && Reference< XIndexAccess>(xPrimaryKeyColumns,UNO_QUERY_THROW)->getCount() > 1;
190
191 pTabConnData->SetConnLine( 0, sSourceFieldName, sDestFieldName );
192
193 if ( bAskUser || m_pExistingConnection )
194 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 if( pTabConnData->Update() )
201 {
202 // enter UI-object into ConnList
204 }
205 }
206 catch(const SQLException&)
207 {
208 throw;
209 }
210 catch(const Exception&)
211 {
212 TOOLS_WARN_EXCEPTION( "dbaccess", "ORelationTableView::AddConnection");
213 }
214 }
215}
216
218{
219 ORelationDialog aRelDlg(this, rConnection->GetData());
220 switch (aRelDlg.run())
221 {
222 case RET_OK:
223 // successfully updated
224 rConnection->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 break;
228
229 case RET_NO:
230 // tried at least one update, but did not succeed -> the original connection is lost
231 RemoveConnection(rConnection ,true);
232 break;
233
234 case RET_CANCEL:
235 // no break, as nothing happened and we don't need the code below
236 return;
237
238 }
239
240 Invalidate(InvalidateFlags::NoChildren);
241}
242
244{
245
246 TTableConnectionData::value_type pNewConnData = std::make_shared<ORelationTableConnectionData>();
247 ORelationDialog aRelDlg(this, pNewConnData, true);
248
249 bool bSuccess = (aRelDlg.run() == RET_OK);
250 if (bSuccess)
251 {
252 // already updated by the dialog
253 // announce it to the document
255 }
256}
257
259{
260 ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(rConn->GetData().get());
261 try
262 {
263 if (!m_bInRemove)
264 pTabConnData->DropRelation();
265 return OJoinTableView::RemoveConnection(rConn, true);
266 }
267 catch(SQLException& e)
268 {
270 }
271 catch(Exception&)
272 {
273 TOOLS_WARN_EXCEPTION( "dbaccess", "ORelationTableView::RemoveConnection: Something other than SQLException occurred!");
274 }
275 return false;
276}
277
278void ORelationTableView::AddTabWin(const OUString& _rComposedName, const OUString& rWinName, bool /*bNewTable*/)
279{
280 OSL_ENSURE(!_rComposedName.isEmpty(),"There must be a table name supplied!");
281 OJoinTableView::OTableWindowMap::const_iterator aIter = GetTabWinMap().find(_rComposedName);
282
283 if(aIter != GetTabWinMap().end())
284 {
285 aIter->second->SetZOrder(nullptr, ZOrderFlags::First);
286 aIter->second->GrabFocus();
287 EnsureVisible(aIter->second);
288 // no new one
289 return;
290 }
291
292 // enter the new data structure into DocShell
293 TTableWindowData::value_type pNewTabWinData(createTableWindowData( _rComposedName, rWinName,rWinName ));
294 pNewTabWinData->ShowAll(false);
295
296 // link new window into the window list
297 VclPtr<OTableWindow> pNewTabWin = createWindow( pNewTabWinData );
298 if(pNewTabWin->Init())
299 {
300 m_pView->getController().getTableWindowData().push_back( pNewTabWinData);
301 // when we already have a table with this name insert the full qualified one instead
302 GetTabWinMap()[_rComposedName] = pNewTabWin;
303
304 SetDefaultTabWinPosSize( pNewTabWin );
305 pNewTabWin->Show();
306
307 modified();
308
309 if ( m_pAccessible )
310 m_pAccessible->notifyAccessibleEvent( AccessibleEventId::CHILD,
311 Any(),
312 Any(pNewTabWin->GetAccessible()));
313 }
314 else
315 {
316 pNewTabWin->clearListBox();
317 pNewTabWin.disposeAndClear();
318 }
319}
320
322{
323 OSQLWarningBox aDlg(GetFrameWeld(), DBA_RES(STR_QUERY_REL_DELETE_WINDOW), MessBoxStyle::YesNo | MessBoxStyle::DefaultYes);
324 if (m_bInRemove || aDlg.run() == RET_YES)
325 {
326 m_pView->getController().ClearUndoManager();
328
329 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
330 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
331 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
332 }
333}
334
336{
338 {
339 OUString sTitle(DBA_RES(STR_RELATIONDESIGN));
340 sTitle = sTitle.copy(3);
341 OSQLMessageBox aDlg(GetFrameWeld(), DBA_RES(STR_QUERY_REL_EDIT_RELATION), OUString(), MessBoxStyle::NONE);
342 aDlg.set_title(sTitle);
343 aDlg.add_button(DBA_RES(STR_QUERY_REL_EDIT), RET_OK);
345 aDlg.add_button(DBA_RES(STR_QUERY_REL_CREATE), RET_YES);
346 aDlg.add_button(GetStandardText(StandardButtonType::Cancel), RET_CANCEL);
347 sal_uInt16 nRet = aDlg.run();
348 if (nRet == RET_CANCEL)
349 {
351 }
352 else if ( nRet == RET_OK ) // EDIT
353 {
356 }
357 m_pExistingConnection = nullptr;
358 }
360 {
362 if (aRelDlg.run() == RET_OK)
363 {
364 // already updated by the dialog
366 }
368 }
369}
370
371VclPtr<OTableWindow> ORelationTableView::createWindow(const TTableWindowData::value_type& _pData)
372{
373 return VclPtr<ORelationTableWindow>::Create(this,_pData);
374}
375
377{
378 return false;
379}
380
381void ORelationTableView::_elementInserted( const container::ContainerEvent& /*_rEvent*/ )
382{
383
384}
385
386void ORelationTableView::_elementRemoved( const container::ContainerEvent& _rEvent )
387{
388 m_bInRemove = true;
389 OUString sName;
390 if ( _rEvent.Accessor >>= sName )
391 {
392 OTableWindow* pTableWindow = GetTabWindow(sName);
393 if ( pTableWindow )
394 {
395 m_pView->getController().ClearUndoManager();
396 OJoinTableView::RemoveTabWin( pTableWindow );
397
398 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
399 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
400 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
401 }
402 }
403 m_bInRemove = false;
404}
405
406void ORelationTableView::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
407{
408}
409
410/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sName
#define ID_BROWSER_UNDO
Definition: browserids.hxx:35
#define ID_BROWSER_REDO
Definition: browserids.hxx:31
void disposeAndClear()
reference_type * get() const
static VclPtr< reference_type > Create(Arg &&... arg)
OJoinController & getController() const
virtual void dispose() override
rtl::Reference< OJoinDesignViewAccess > m_pAccessible
const std::vector< VclPtr< OTableConnection > > & getTableConnections() const
gives a read only access to the connection vector
virtual bool RemoveConnection(VclPtr< OTableConnection > &rConnection, bool bDelete)
RemoveConnection allows to remove connections from join table view.
OTableWindow * GetTabWindow(const OUString &rName)
TTableWindowData::value_type createTableWindowData(const OUString &_rComposedName, const OUString &_sTableName, const OUString &_rWinName)
virtual void EnsureVisible(const OTableWindow *_pWin)
OJoinDesignView * getDesignView() const
VclPtr< OJoinDesignView > m_pView
virtual void RemoveTabWin(OTableWindow *pTabWin)
void SetDefaultTabWinPosSize(OTableWindow *pTabWin)
void addConnection(OTableConnection *_pConnection, bool _bAddData=true)
allows to add new connections to join table view
OTableWindowMap & GetTabWinMap()
virtual short run() override
virtual VclPtr< OTableWindow > createWindow(const TTableWindowData::value_type &_pData) override
factory method to create table windows
virtual void AddConnection(const OJoinExchangeData &jxdSource, const OJoinExchangeData &jxdDest) override
virtual void RemoveTabWin(OTableWindow *pTabWin) override
virtual void _elementInserted(const css::container::ContainerEvent &_rEvent) override
TTableConnectionData::value_type m_pCurrentlyTabConnData
set when we creating a connection with more than one keycolumn
virtual void ConnDoubleClicked(VclPtr< OTableConnection > &rConnection) override
virtual bool allowQueries() const override
determines whether the classes Init method should accept a query name, or only table names
virtual void _elementRemoved(const css::container::ContainerEvent &_rEvent) override
virtual void lookForUiActivities() override
virtual bool RemoveConnection(VclPtr< OTableConnection > &rConn, bool _bDelete) override
RemoveConnection allows to remove connections from join table view.
virtual void _elementReplaced(const css::container::ContainerEvent &_rEvent) override
VclPtr< OTableConnection > m_pExistingConnection
is set when a connection was dragged on an existing connection
virtual void ReSync() override
take all WinData and ConnData from the document and create the corresponding Wins and Conns
virtual ~ORelationTableView() override
virtual void AddTabWin(const OUString &_rComposedName, const OUString &rWinName, bool bNewTable=false) override
void AddNewRelation()
Creates a dialogue for a completely new relation.
::rtl::Reference< comphelper::OContainerListenerAdapter > m_pContainerListener
virtual bool IsAddAllowed() override
used by AddTabDlg to check if tables can be added
virtual void dispose() override
void set_default_response(int nResponse)
Definition: sqlmessage.hxx:110
void add_button(const OUString &rText, int nResponse, const OUString &rHelpId={})
Definition: sqlmessage.hxx:109
void set_title(const OUString &rTitle)
Definition: sqlmessage.hxx:108
const TTableWindowData::value_type & getReferencingTable() const
const TTableWindowData::value_type & getReferencedTable() const
const TTableWindowData::value_type & GetData() const
void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
weld::Window * GetFrameWeld() const
void SetHelpId(const OUString &)
virtual short run()
virtual OUString get_text(int row, int col=-1) const=0
#define DBA_RES(id)
#define TOOLS_WARN_EXCEPTION(area, stream)
constexpr OUStringLiteral HID_CTL_RELATIONTAB
Definition: helpids.h:63
std::mutex m_aMutex
std::unique_ptr< sal_Int32[]> pData
@ Exception
std::vector< std::shared_ptr< OTableConnectionData > > TTableConnectionData
std::vector< std::shared_ptr< OTableWindowData > > TTableWindowData
Reference< XNameAccess > getPrimaryKeyColumns_throw(const Any &i_aTable)
end
OUString VCL_DLLPUBLIC GetStandardText(StandardButtonType eButton)
VclPtr< OTableWindowListBox > pListBox
RET_OK
RET_CANCEL
RET_NO
RET_YES