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