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