1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBAUI_TABLECONNECTIONDATA_HXX
31 #include "TableConnectionData.hxx"
32 #endif
33 #ifndef _TOOLS_DEBUG_HXX
34 #include <tools/debug.hxx>
35 #endif
36 #ifndef _COMPHELPER_STLTYPES_HXX_
37 #include <comphelper/stl_types.hxx>
38 #endif
39 
40 using namespace dbaui;
41 using namespace comphelper;
42 //==================================================================
43 // class OTableConnectionData
44 //==================================================================
45 DBG_NAME(OTableConnectionData)
46 //------------------------------------------------------------------------
47 OTableConnectionData::OTableConnectionData()
48 {
49 	DBG_CTOR(OTableConnectionData,NULL);
50 	Init();
51 }
52 // -----------------------------------------------------------------------------
53 OTableConnectionData::OTableConnectionData(const TTableWindowData::value_type& _pReferencingTable
54                                           ,const TTableWindowData::value_type& _pReferencedTable
55                                           ,const String& rConnName )
56  :m_pReferencingTable(_pReferencingTable)
57  ,m_pReferencedTable(_pReferencedTable)
58  ,m_aConnName( rConnName )
59 {
60     DBG_CTOR(OTableConnectionData,NULL);
61 	Init();
62 }
63 //------------------------------------------------------------------------
64 void OTableConnectionData::Init()
65 {
66 	//////////////////////////////////////////////////////////////////////
67 	// LineDataList mit Defaults initialisieren
68 	DBG_ASSERT(m_vConnLineData.size() == 0, "OTableConnectionData::Init() : nur mit leere Linienliste aufzurufen !");
69 	ResetConnLines(sal_True);
70 		// das legt Defaults an
71 }
72 //------------------------------------------------------------------------
73 OTableConnectionData::OTableConnectionData( const OTableConnectionData& rConnData )
74 {
75 	DBG_CTOR(OTableConnectionData,NULL);
76 	*this = rConnData;
77 }
78 //------------------------------------------------------------------------
79 void OTableConnectionData::CopyFrom(const OTableConnectionData& rSource)
80 {
81 	*this = rSource;
82 	// hier ziehe ich mich auf das (nicht-virtuelle) operator= zurueck, das nur meine Members kopiert
83 }
84 
85 //------------------------------------------------------------------------
86 OTableConnectionData::~OTableConnectionData()
87 {
88 	DBG_DTOR(OTableConnectionData,NULL);
89 	// LineDataList loeschen
90 	OConnectionLineDataVec().swap(m_vConnLineData);
91 	//ResetConnLines(sal_False);
92 }
93 
94 //------------------------------------------------------------------------
95 OTableConnectionData& OTableConnectionData::operator=( const OTableConnectionData& rConnData )
96 {
97 	if (&rConnData == this)
98 		return *this;
99 
100 	m_pReferencingTable = rConnData.m_pReferencingTable;
101 	m_pReferencedTable = rConnData.m_pReferencedTable;
102 	m_aConnName = rConnData.GetConnName();
103 
104 	// clear line list
105 	ResetConnLines(sal_False);
106 
107 	// und kopieren
108 	OConnectionLineDataVec* pLineData = const_cast<OTableConnectionData*>(&rConnData)->GetConnLineDataList();
109 
110 	OConnectionLineDataVec::const_iterator aIter = pLineData->begin();
111     OConnectionLineDataVec::const_iterator aEnd = pLineData->end();
112 	for(;aIter != aEnd;++aIter)
113 		m_vConnLineData.push_back(new OConnectionLineData(**aIter));
114 
115 	return *this;
116 }
117 
118 //------------------------------------------------------------------------
119 sal_Bool OTableConnectionData::SetConnLine( sal_uInt16 nIndex, const String& rSourceFieldName, const String& rDestFieldName )
120 {
121 	if (sal_uInt16(m_vConnLineData.size()) < nIndex)
122 		return sal_False;
123 		// == ist noch erlaubt, das entspricht einem Append
124 
125 	if (m_vConnLineData.size() == nIndex)
126 		return AppendConnLine(rSourceFieldName, rDestFieldName);
127 
128 	OConnectionLineDataRef pConnLineData = m_vConnLineData[nIndex];
129 	DBG_ASSERT(pConnLineData != NULL, "OTableConnectionData::SetConnLine : habe ungueltiges LineData-Objekt");
130 
131 	pConnLineData->SetSourceFieldName( rSourceFieldName );
132 	pConnLineData->SetDestFieldName( rDestFieldName );
133 
134 	return sal_True;
135 }
136 
137 //------------------------------------------------------------------------
138 sal_Bool OTableConnectionData::AppendConnLine( const ::rtl::OUString& rSourceFieldName, const ::rtl::OUString& rDestFieldName )
139 {
140 	OConnectionLineDataVec::iterator aIter = m_vConnLineData.begin();
141     OConnectionLineDataVec::iterator aEnd = m_vConnLineData.end();
142 	for(;aIter != aEnd;++aIter)
143 	{
144 		if((*aIter)->GetDestFieldName() == rDestFieldName && (*aIter)->GetSourceFieldName() == rSourceFieldName)
145 			break;
146 	}
147 	if(aIter == aEnd)
148 	{
149 		OConnectionLineDataRef pNew = new OConnectionLineData(rSourceFieldName, rDestFieldName);
150 		if (!pNew.isValid())
151 			return sal_False;
152 
153 		m_vConnLineData.push_back(pNew);
154 	}
155 	return sal_True;
156 }
157 
158 //------------------------------------------------------------------------
159 void OTableConnectionData::ResetConnLines( sal_Bool /*bUseDefaults*/ )
160 {
161 	OConnectionLineDataVec().swap(m_vConnLineData);
162 }
163 
164 //------------------------------------------------------------------------
165 OConnectionLineDataRef OTableConnectionData::CreateLineDataObj()
166 {
167 	return new OConnectionLineData();
168 }
169 
170 //------------------------------------------------------------------------
171 OConnectionLineDataRef OTableConnectionData::CreateLineDataObj( const OConnectionLineData& rConnLineData )
172 {
173 	return new OConnectionLineData( rConnLineData );
174 }
175 // -----------------------------------------------------------------------------
176 OTableConnectionData* OTableConnectionData::NewInstance() const
177 {
178 	return new OTableConnectionData();
179 }
180 // -----------------------------------------------------------------------------
181 void OTableConnectionData::normalizeLines()
182 {
183 	// noch ein wenig Normalisierung auf den LineDatas : leere Lines vom Anfang an das Ende verschieben
184 	sal_Int32 nCount = m_vConnLineData.size();
185 	for(sal_Int32 i=0;i<nCount;)
186 	{
187 		if(!m_vConnLineData[i]->GetSourceFieldName().getLength() || !m_vConnLineData[i]->GetDestFieldName().getLength())
188 		{
189 			OConnectionLineDataRef pData = m_vConnLineData[i];
190 			m_vConnLineData.erase(m_vConnLineData.begin()+i);
191 			m_vConnLineData.push_back(pData);
192 			--nCount;
193 		}
194 		else
195 			++i;
196 	}
197 }
198 // -----------------------------------------------------------------------------
199 
200 
201 
202 
203