1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
30 #include <connectivity/dbtools.hxx>
31 #include <comphelper/diagnose_ex.hxx>
33 #include <RelationDesignView.hxx>
34 #include <JoinController.hxx>
35 #include <TableWindow.hxx>
36 #include <TableWindowData.hxx>
37 #include "RTableConnection.hxx"
38 #include <RTableConnectionData.hxx>
39 #include <RelationDlg.hxx>
40 #include <sqlmessage.hxx>
41 #include <strings.hrc>
42 #include <connectivity/dbexception.hxx>
43 #include "RTableWindow.hxx"
44 #include <JAccess.hxx>
45 #include <vcl/stdtext.hxx>
46 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
48 using namespace dbaui
;
49 using namespace ::dbtools
;
50 using namespace ::com::sun::star
;
51 using namespace ::com::sun::star::uno
;
52 using namespace ::com::sun::star::sdbc
;
53 using namespace ::com::sun::star::sdbcx
;
54 using namespace ::com::sun::star::beans
;
55 using namespace ::com::sun::star::container
;
56 using namespace ::com::sun::star::accessibility
;
58 ORelationTableView::ORelationTableView( vcl::Window
* pParent
, ORelationDesignView
* pView
)
59 :OJoinTableView( pParent
, pView
)
60 , ::comphelper::OContainerListener(m_aMutex
)
61 ,m_pExistingConnection(nullptr)
65 SetHelpId(HID_CTL_RELATIONTAB
);
68 ORelationTableView::~ORelationTableView()
73 void ORelationTableView::dispose()
75 if ( m_pContainerListener
.is() )
76 m_pContainerListener
->dispose();
77 m_pExistingConnection
.clear();
78 OJoinTableView::dispose();
81 void ORelationTableView::ReSync()
83 if ( !m_pContainerListener
.is() )
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
);
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
;
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
)
102 TTableWindowData::value_type pData
= *aIter
;
103 VclPtr
<OTableWindow
> pTabWin
= createWindow(pData
);
105 if (!pTabWin
->Init())
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());
113 rTabWinDataList
.erase( std::remove(rTabWinDataList
.begin(), rTabWinDataList
.end(), *aIter
), rTabWinDataList
.end());
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())
120 SetDefaultTabWinPosSize(pTabWin
);
126 TTableConnectionData
& rTabConnDataList
= m_pView
->getController().getTableConnectionData();
127 TTableConnectionData::const_reverse_iterator aConIter
= rTabConnDataList
.rbegin();
129 for(;aConIter
!= rTabConnDataList
.rend();++aConIter
)
131 ORelationTableConnectionData
* pTabConnData
= static_cast<ORelationTableConnectionData
*>(aConIter
->get());
132 if ( !arrInvalidTables
.empty() )
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();
142 // no -> bad luck, the connection is gone
143 rTabConnDataList
.erase( std::remove(rTabConnDataList
.begin(), rTabConnDataList
.end(), *aConIter
), rTabConnDataList
.end() );
148 addConnection( VclPtr
<ORelationTableConnection
>::Create(this, *aConIter
), false );
151 if ( !GetTabWinMap().empty() )
152 GetTabWinMap().begin()->second
->GrabFocus();
155 bool ORelationTableView::IsAddAllowed()
158 return !m_pView
->getController().isReadOnly();
161 void ORelationTableView::AddConnection(const OJoinExchangeData
& jxdSource
, const OJoinExchangeData
& jxdDest
)
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();
168 for(VclPtr
<OTableConnection
> const & pFirst
: getTableConnections())
170 if((pFirst
->GetSourceWin() == pSourceWin
&& pFirst
->GetDestWin() == pDestWin
) ||
171 (pFirst
->GetSourceWin() == pDestWin
&& pFirst
->GetDestWin() == pSourceWin
))
173 m_pExistingConnection
= pFirst
;
177 // insert table connection into view
178 TTableConnectionData::value_type pTabConnData
= std::make_shared
<ORelationTableConnectionData
>(pSourceWin
->GetData(),
179 pDestWin
->GetData());
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
);
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;
191 pTabConnData
->SetConnLine( 0, sSourceFieldName
, sDestFieldName
);
193 if ( bAskUser
|| m_pExistingConnection
)
194 m_pCurrentlyTabConnData
= pTabConnData
; // this implies that we ask the user what to do
199 // hand over data to the database
200 if( pTabConnData
->Update() )
202 // enter UI-object into ConnList
203 addConnection( VclPtr
<ORelationTableConnection
>::Create( this, pTabConnData
) );
206 catch(const SQLException
&)
210 catch(const Exception
&)
212 TOOLS_WARN_EXCEPTION( "dbaccess", "ORelationTableView::AddConnection");
217 void ORelationTableView::ConnDoubleClicked(VclPtr
<OTableConnection
>& rConnection
)
219 ORelationDialog
aRelDlg(this, rConnection
->GetData());
220 switch (aRelDlg
.run())
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
230 // tried at least one update, but did not succeed -> the original connection is lost
231 RemoveConnection(rConnection
,true);
235 // no break, as nothing happened and we don't need the code below
240 Invalidate(InvalidateFlags::NoChildren
);
243 void ORelationTableView::AddNewRelation()
246 TTableConnectionData::value_type pNewConnData
= std::make_shared
<ORelationTableConnectionData
>();
247 ORelationDialog
aRelDlg(this, pNewConnData
, true);
249 bool bSuccess
= (aRelDlg
.run() == RET_OK
);
252 // already updated by the dialog
253 // announce it to the document
254 addConnection( VclPtr
<ORelationTableConnection
>::Create(this, pNewConnData
) );
258 bool ORelationTableView::RemoveConnection(VclPtr
<OTableConnection
>& rConn
, bool /*_bDelete*/)
260 ORelationTableConnectionData
* pTabConnData
= static_cast<ORelationTableConnectionData
*>(rConn
->GetData().get());
264 pTabConnData
->DropRelation();
265 return OJoinTableView::RemoveConnection(rConn
, true);
267 catch(SQLException
& e
)
269 getDesignView()->getController().showError(SQLExceptionInfo(e
));
273 TOOLS_WARN_EXCEPTION( "dbaccess", "ORelationTableView::RemoveConnection: Something other than SQLException occurred!");
278 void ORelationTableView::AddTabWin(const OUString
& _rComposedName
, const OUString
& rWinName
, bool /*bNewTable*/)
280 OSL_ENSURE(!_rComposedName
.isEmpty(),"There must be a table name supplied!");
281 OJoinTableView::OTableWindowMap::const_iterator aIter
= GetTabWinMap().find(_rComposedName
);
283 if(aIter
!= GetTabWinMap().end())
285 aIter
->second
->SetZOrder(nullptr, ZOrderFlags::First
);
286 aIter
->second
->GrabFocus();
287 EnsureVisible(aIter
->second
);
292 // enter the new data structure into DocShell
293 TTableWindowData::value_type
pNewTabWinData(createTableWindowData( _rComposedName
, rWinName
,rWinName
));
294 pNewTabWinData
->ShowAll(false);
296 // link new window into the window list
297 VclPtr
<OTableWindow
> pNewTabWin
= createWindow( pNewTabWinData
);
298 if(pNewTabWin
->Init())
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
;
304 SetDefaultTabWinPosSize( pNewTabWin
);
310 m_pAccessible
->notifyAccessibleEvent( AccessibleEventId::CHILD
,
312 Any(pNewTabWin
->GetAccessible()));
316 pNewTabWin
->clearListBox();
317 pNewTabWin
.disposeAndClear();
321 void ORelationTableView::RemoveTabWin( OTableWindow
* pTabWin
)
323 OSQLWarningBox
aDlg(GetFrameWeld(), DBA_RES(STR_QUERY_REL_DELETE_WINDOW
), MessBoxStyle::YesNo
| MessBoxStyle::DefaultYes
);
324 if (m_bInRemove
|| aDlg
.run() == RET_YES
)
326 m_pView
->getController().ClearUndoManager();
327 OJoinTableView::RemoveTabWin( pTabWin
);
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
);
335 void ORelationTableView::lookForUiActivities()
337 if(m_pExistingConnection
)
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
);
344 aDlg
.set_default_response(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
)
350 m_pCurrentlyTabConnData
.reset();
352 else if ( nRet
== RET_OK
) // EDIT
354 ConnDoubleClicked(m_pExistingConnection
);
355 m_pCurrentlyTabConnData
.reset();
357 m_pExistingConnection
= nullptr;
359 if(m_pCurrentlyTabConnData
)
361 ORelationDialog
aRelDlg(this, m_pCurrentlyTabConnData
);
362 if (aRelDlg
.run() == RET_OK
)
364 // already updated by the dialog
365 addConnection( VclPtr
<ORelationTableConnection
>::Create( this, m_pCurrentlyTabConnData
) );
367 m_pCurrentlyTabConnData
.reset();
371 VclPtr
<OTableWindow
> ORelationTableView::createWindow(const TTableWindowData::value_type
& _pData
)
373 return VclPtr
<ORelationTableWindow
>::Create(this,_pData
);
376 bool ORelationTableView::allowQueries() const
381 void ORelationTableView::_elementInserted( const container::ContainerEvent
& /*_rEvent*/ )
386 void ORelationTableView::_elementRemoved( const container::ContainerEvent
& _rEvent
)
390 if ( _rEvent
.Accessor
>>= sName
)
392 OTableWindow
* pTableWindow
= GetTabWindow(sName
);
395 m_pView
->getController().ClearUndoManager();
396 OJoinTableView::RemoveTabWin( pTableWindow
);
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
);
406 void ORelationTableView::_elementReplaced( const container::ContainerEvent
& /*_rEvent*/ )
410 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */