1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 
27 #include <bibconfig.hxx>
28 #include <svl/svarray.hxx>
29 #include <tools/debug.hxx>
30 #include <com/sun/star/uno/Sequence.hxx>
31 #include <com/sun/star/uno/Any.hxx>
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <comphelper/processfactory.hxx>
36 
37 using namespace rtl;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::lang;
42 /* -----------------11.11.99 14:34-------------------
43 
44  --------------------------------------------------*/
45 typedef Mapping* MappingPtr;
46 SV_DECL_PTRARR_DEL(MappingArray, MappingPtr, 2, 2)
47 SV_IMPL_PTRARR(MappingArray, MappingPtr);
48 
49 #define C2U(cChar) OUString::createFromAscii(cChar)
50 
51 const char* cDataSourceHistory = "DataSourceHistory";
52 /* -----------------------------13.11.00 12:21--------------------------------
53 
54  ---------------------------------------------------------------------------*/
GetPropertyNames()55 Sequence<OUString> BibConfig::GetPropertyNames()
56 {
57 	static Sequence<OUString> aNames;
58 	if(!aNames.getLength())
59 	{
60         aNames.realloc(8);
61 		OUString* pNames = aNames.getArray();
62 		pNames[0] = C2U("CurrentDataSource/DataSourceName");
63 		pNames[1] = C2U("CurrentDataSource/Command");
64 		pNames[2] = C2U("CurrentDataSource/CommandType");
65 		pNames[3] = C2U("BeamerHeight");
66 		pNames[4] = C2U("ViewHeight");
67 		pNames[5] = C2U("QueryText");
68 		pNames[6] = C2U("QueryField");
69         pNames[7] = C2U("ShowColumnAssignmentWarning");
70     }
71 	return aNames;
72 }
73 /* -----------------------------13.11.00 11:00--------------------------------
74 
75  ---------------------------------------------------------------------------*/
BibConfig()76 BibConfig::BibConfig() :
77     ConfigItem(C2U("Office.DataAccess/Bibliography"), CONFIG_MODE_DELAYED_UPDATE),
78 	pMappingsArr(new MappingArray),
79 	nBeamerSize(0),
80 	nViewSize(0),
81 	bShowColumnAssignmentWarning(sal_False)
82 {
83 	//Names of the default columns
84 	aColumnDefaults[0] = C2U("Identifier");
85 	aColumnDefaults[1] = C2U("BibliographyType");
86 	aColumnDefaults[2] = C2U("Author");
87 	aColumnDefaults[3] = C2U("Title");
88 	aColumnDefaults[4] = C2U("Year");
89 	aColumnDefaults[5] = C2U("ISBN");
90 	aColumnDefaults[6] = C2U("Booktitle");
91 	aColumnDefaults[7] = C2U("Chapter");
92 	aColumnDefaults[8] = C2U("Edition");
93 	aColumnDefaults[9] = C2U("Editor");
94 	aColumnDefaults[10] = C2U("Howpublished");
95 	aColumnDefaults[11] = C2U("Institution");
96 	aColumnDefaults[12] = C2U("Journal");
97 	aColumnDefaults[13] = C2U("Month");
98 	aColumnDefaults[14] = C2U("Note");
99 	aColumnDefaults[15] = C2U("Annote");
100 	aColumnDefaults[16] = C2U("Number");
101 	aColumnDefaults[17] = C2U("Organizations");
102 	aColumnDefaults[18] = C2U("Pages");
103 	aColumnDefaults[19] = C2U("Publisher");
104 	aColumnDefaults[20] = C2U("Address");
105 	aColumnDefaults[21] = C2U("School");
106 	aColumnDefaults[22] = C2U("Series");
107 	aColumnDefaults[23] = C2U("ReportType");
108 	aColumnDefaults[24] = C2U("Volume");
109 	aColumnDefaults[25] = C2U("URL");
110 	aColumnDefaults[26] = C2U("Custom1");
111 	aColumnDefaults[27] = C2U("Custom2");
112 	aColumnDefaults[28] = C2U("Custom3");
113 	aColumnDefaults[29] = C2U("Custom4");
114 	aColumnDefaults[30] = C2U("Custom5");
115 
116 
117 	const Sequence< OUString > aPropertyNames = GetPropertyNames();
118 	const Sequence<Any> aPropertyValues = GetProperties( aPropertyNames );
119 	const Any* pValues = aPropertyValues.getConstArray();
120 	if(aPropertyValues.getLength() == aPropertyNames.getLength())
121 	{
122 		for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
123 		{
124 			if(pValues[nProp].hasValue())
125 			{
126 				switch(nProp)
127 				{
128 					case  0: pValues[nProp] >>= sDataSource; break;
129 					case  1: pValues[nProp] >>= sTableOrQuery; break;
130 					case  2: pValues[nProp] >>= nTblOrQuery;  break;
131 					case  3: pValues[nProp] >>= nBeamerSize;  break;
132 					case  4: pValues[nProp] >>= nViewSize  ;  break;
133 					case  5: pValues[nProp] >>= sQueryText ;  break;
134 					case  6: pValues[nProp] >>= sQueryField;  break;
135                     case  7:
136                         bShowColumnAssignmentWarning = *(sal_Bool*)pValues[nProp].getValue();
137                     break;
138                 }
139 			}
140 		}
141 	}
142 	OUString sName(C2U("DataSourceName"));
143 	OUString sTable(C2U("Command"));
144 	OUString sCommandType(C2U("CommandType"));
145 	Sequence< OUString > aNodeNames = GetNodeNames(C2U(cDataSourceHistory));
146 	const OUString* pNodeNames = aNodeNames.getConstArray();
147 	for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength(); nNode++)
148 	{
149         Sequence<OUString> aHistoryNames(3);
150 		OUString* pHistoryNames = aHistoryNames.getArray();
151 
152 		OUString sPrefix(C2U(cDataSourceHistory));
153 		sPrefix += C2U("/");
154 		sPrefix += pNodeNames[nNode];
155 		sPrefix += C2U("/");
156 		pHistoryNames[0] = sPrefix;
157 		pHistoryNames[0] += sName;
158 		pHistoryNames[1] = sPrefix;
159 		pHistoryNames[1] += sTable;
160 		pHistoryNames[2] = sPrefix;
161 		pHistoryNames[2] += sCommandType;
162 
163         Sequence<Any> aHistoryValues = GetProperties( aHistoryNames );
164         const Any* pHistoryValues = aHistoryValues.getConstArray();
165 
166         if(aHistoryValues.getLength() == aHistoryNames.getLength())
167 		{
168 			Mapping* pMapping = new Mapping;
169 			pHistoryValues[0] >>= pMapping->sURL;
170 			pHistoryValues[1] >>= pMapping->sTableName;
171 			pHistoryValues[2] >>= pMapping->nCommandType;
172 			//field assignment is contained in another set
173 			sPrefix += C2U("Fields");
174 			Sequence< OUString > aAssignmentNodeNames = GetNodeNames(sPrefix);
175 			const OUString* pAssignmentNodeNames = aAssignmentNodeNames.getConstArray();
176 			Sequence<OUString> aAssignmentPropertyNames(aAssignmentNodeNames.getLength() * 2);
177 			OUString* pAssignmentPropertyNames = aAssignmentPropertyNames.getArray();
178 			sal_Int16 nFieldIdx = 0;
179 			for(sal_Int16 nField = 0; nField < aAssignmentNodeNames.getLength(); nField++)
180 			{
181 				OUString sSubPrefix(sPrefix);
182 				sSubPrefix += C2U("/");
183 				sSubPrefix += pAssignmentNodeNames[nField];
184 				pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
185 				pAssignmentPropertyNames[nFieldIdx++] += C2U("/ProgrammaticFieldName");
186 				pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
187 				pAssignmentPropertyNames[nFieldIdx++]	+= C2U("/AssignedFieldName");
188 			}
189 			Sequence<Any> aAssignmentValues = GetProperties(aAssignmentPropertyNames);
190 			const Any* pAssignmentValues = aAssignmentValues.getConstArray();
191 			OUString sTempLogical;
192 			OUString sTempReal;
193 			sal_Int16 nSetMapping = 0;
194 			nFieldIdx = 0;
195 			for(sal_Int16 nFieldVal = 0; nFieldVal < aAssignmentValues.getLength() / 2; nFieldVal++)
196 			{
197 				pAssignmentValues[nFieldIdx++] >>= sTempLogical;
198 				pAssignmentValues[nFieldIdx++] >>= sTempReal;
199 				if(sTempLogical.getLength() && sTempReal.getLength())
200 				{
201 					pMapping->aColumnPairs[nSetMapping].sLogicalColumnName = sTempLogical;
202 					pMapping->aColumnPairs[nSetMapping++].sRealColumnName = sTempReal;
203 				}
204 			}
205 			pMappingsArr->Insert(pMapping, pMappingsArr->Count());
206 		}
207 	}
208 }
209 /* -----------------------------13.11.00 11:00--------------------------------
210 
211  ---------------------------------------------------------------------------*/
~BibConfig()212 BibConfig::~BibConfig()
213 {
214 	if(IsModified())
215 		Commit();
216 	delete pMappingsArr;
217 }
218 /* -----------------------------13.11.00 12:08--------------------------------
219 
220  ---------------------------------------------------------------------------*/
GetBibliographyURL()221 BibDBDescriptor BibConfig::GetBibliographyURL()
222 {
223 	BibDBDescriptor	aRet;
224 	aRet.sDataSource = sDataSource;
225 	aRet.sTableOrQuery = sTableOrQuery;
226 	aRet.nCommandType = nTblOrQuery;
227 	return aRet;
228 };
229 /* -----------------------------13.11.00 12:20--------------------------------
230 
231  ---------------------------------------------------------------------------*/
SetBibliographyURL(const BibDBDescriptor & rDesc)232 void BibConfig::SetBibliographyURL(const BibDBDescriptor& rDesc)
233 {
234 	sDataSource = rDesc.sDataSource;
235 	sTableOrQuery = rDesc.sTableOrQuery;
236 	nTblOrQuery = rDesc.nCommandType;
237 	SetModified();
238 };
239 //---------------------------------------------------------------------------
Notify(const com::sun::star::uno::Sequence<rtl::OUString> &)240 void BibConfig::Notify( const com::sun::star::uno::Sequence<rtl::OUString>& )
241 {
242 }
243 
Commit()244 void	BibConfig::Commit()
245 {
246 	const Sequence<OUString> aPropertyNames = GetPropertyNames();
247 	Sequence<Any> aValues(aPropertyNames.getLength());
248 	Any* pValues = aValues.getArray();
249 
250 	for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
251 	{
252 		switch(nProp)
253 		{
254 			case  0: pValues[nProp] <<= sDataSource; break;
255 			case  1: pValues[nProp] <<= sTableOrQuery; break;
256 			case  2: pValues[nProp] <<= nTblOrQuery;  break;
257 			case  3: pValues[nProp] <<= nBeamerSize;  break;
258 			case  4: pValues[nProp] <<= nViewSize;  break;
259 			case  5: pValues[nProp] <<= sQueryText;  break;
260 			case  6: pValues[nProp] <<= sQueryField;  break;
261             case  7:
262                 pValues[nProp].setValue(&bShowColumnAssignmentWarning, ::getBooleanCppuType());
263             break;
264 		}
265 	}
266 	PutProperties(aPropertyNames, aValues);
267 	ClearNodeSet( C2U(cDataSourceHistory));
268 	OUString sEmpty;
269 	Sequence< PropertyValue > aNodeValues(pMappingsArr->Count() * 3);
270 	PropertyValue* pNodeValues = aNodeValues.getArray();
271 
272 	sal_Int32 nIndex = 0;
273 	OUString sName(C2U("DataSourceName"));
274 	OUString sTable(C2U("Command"));
275 	OUString sCommandType(C2U("CommandType"));
276 	for(sal_Int32 i = 0; i < pMappingsArr->Count(); i++)
277 	{
278         const Mapping* pMapping = pMappingsArr->GetObject((sal_uInt16)i);
279 		OUString sPrefix(C2U(cDataSourceHistory));
280 		sPrefix += C2U("/_");
281 		sPrefix += OUString::valueOf(i);
282 		sPrefix += C2U("/");
283 		pNodeValues[nIndex].Name	= sPrefix;
284 		pNodeValues[nIndex].Name	+= sName;
285 		pNodeValues[nIndex++].Value <<= pMapping->sURL;
286 		pNodeValues[nIndex].Name	= sPrefix;
287 		pNodeValues[nIndex].Name    += sTable;
288 		pNodeValues[nIndex++].Value <<= pMapping->sTableName;
289 		pNodeValues[nIndex].Name	= sPrefix;
290 		pNodeValues[nIndex].Name    += sCommandType;
291 		pNodeValues[nIndex++].Value <<= pMapping->nCommandType;
292 		SetSetProperties( C2U(cDataSourceHistory), aNodeValues);
293 
294 		sPrefix += C2U("Fields");
295 		sal_Int32 nFieldAssignment = 0;
296 		OUString sFieldName = C2U("/ProgrammaticFieldName");
297 		OUString sDatabaseFieldName = C2U("/AssignedFieldName");
298 		ClearNodeSet( sPrefix );
299 
300 		while(nFieldAssignment < COLUMN_COUNT &&
301 			pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName.getLength())
302 		{
303 			OUString sSubPrefix(sPrefix);
304 			sSubPrefix += C2U("/_");
305 			sSubPrefix += OUString::valueOf(nFieldAssignment);
306 			Sequence< PropertyValue > aAssignmentValues(2);
307 			PropertyValue* pAssignmentValues = aAssignmentValues.getArray();
308 			pAssignmentValues[0].Name	= sSubPrefix;
309 			pAssignmentValues[0].Name	+= sFieldName;
310 			pAssignmentValues[0].Value <<= pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName;
311 			pAssignmentValues[1].Name	= sSubPrefix;
312 			pAssignmentValues[1].Name	+= sDatabaseFieldName;
313 			pAssignmentValues[1].Value <<= pMapping->aColumnPairs[nFieldAssignment].sRealColumnName;
314 			SetSetProperties( sPrefix, aAssignmentValues );
315 			nFieldAssignment++;
316 		}
317 	}
318 }
319 /* -----------------------------13.11.00 12:23--------------------------------
320 
321  ---------------------------------------------------------------------------*/
GetMapping(const BibDBDescriptor & rDesc) const322 const Mapping* 	BibConfig::GetMapping(const BibDBDescriptor& rDesc) const
323 {
324 	for(sal_uInt16 i = 0; i < pMappingsArr->Count(); i++)
325 	{
326 		const Mapping* pMapping = pMappingsArr->GetObject(i);
327 		sal_Bool bURLEqual = rDesc.sDataSource.equals(pMapping->sURL);
328 		if(rDesc.sTableOrQuery == pMapping->sTableName && bURLEqual)
329 			return pMapping;
330 	}
331 	return 0;
332 }
333 /* -----------------------------13.11.00 12:23--------------------------------
334 
335  ---------------------------------------------------------------------------*/
SetMapping(const BibDBDescriptor & rDesc,const Mapping * pSetMapping)336 void BibConfig::SetMapping(const BibDBDescriptor& rDesc, const Mapping* pSetMapping)
337 {
338 	for(sal_uInt16 i = 0; i < pMappingsArr->Count(); i++)
339 	{
340 		const Mapping* pMapping = pMappingsArr->GetObject(i);
341 		sal_Bool bURLEqual = rDesc.sDataSource.equals(pMapping->sURL);
342 		if(rDesc.sTableOrQuery == pMapping->sTableName && bURLEqual)
343 		{
344 			pMappingsArr->DeleteAndDestroy(i, 1);
345 			break;
346 		}
347 	}
348 	Mapping* pNew = new Mapping(*pSetMapping);
349 	pMappingsArr->Insert(pNew, pMappingsArr->Count());
350 	SetModified();
351 }
352 /* -----------------------------20.11.00 11:56--------------------------------
353 
354  ---------------------------------------------------------------------------*/
DBChangeDialogConfig_Impl()355 DBChangeDialogConfig_Impl::DBChangeDialogConfig_Impl()
356 {
357 }
358 /* -----------------------------20.11.00 11:57--------------------------------
359 
360  ---------------------------------------------------------------------------*/
~DBChangeDialogConfig_Impl()361 DBChangeDialogConfig_Impl::~DBChangeDialogConfig_Impl()
362 {
363 }
364 /* -----------------------------14.03.01 12:53--------------------------------
365 
366  ---------------------------------------------------------------------------*/
GetDataSourceNames()367 const Sequence<OUString>& DBChangeDialogConfig_Impl::GetDataSourceNames()
368 {
369     if(!aSourceNames.getLength())
370     {
371         Reference<XNameAccess> xDBContext;
372         Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
373         if( xMgr.is() )
374         {
375             Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
376             xDBContext = Reference<XNameAccess>(xInstance, UNO_QUERY) ;
377         }
378         if(xDBContext.is())
379         {
380             aSourceNames = xDBContext->getElementNames();
381         }
382     }
383     return aSourceNames;
384 }
385 
386