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 .
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>
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
;
37 const char cDataSourceHistory
[] = "DataSourceHistory";
39 Sequence
<OUString
> BibConfig::GetPropertyNames()
41 static Sequence
<OUString
> aNames
;
42 if(!aNames
.getLength())
45 OUString
* pNames
= aNames
.getArray();
46 pNames
[0] = "CurrentDataSource/DataSourceName";
47 pNames
[1] = "CurrentDataSource/Command";
48 pNames
[2] = "CurrentDataSource/CommandType";
49 pNames
[3] = "BeamerHeight";
50 pNames
[4] = "ViewHeight";
51 pNames
[5] = "QueryText";
52 pNames
[6] = "QueryField";
53 pNames
[7] = "ShowColumnAssignmentWarning";
58 BibConfig::BibConfig()
59 : ConfigItem("Office.DataAccess/Bibliography", ConfigItemMode::DelayedUpdate
)
61 , pMappingsArr(new MappingArray
)
64 , bShowColumnAssignmentWarning(false)
66 //Names of the default columns
67 aColumnDefaults
[0] = "Identifier";
68 aColumnDefaults
[1] = "BibliographyType";
69 aColumnDefaults
[2] = "Author";
70 aColumnDefaults
[3] = "Title";
71 aColumnDefaults
[4] = "Year";
72 aColumnDefaults
[5] = "ISBN";
73 aColumnDefaults
[6] = "Booktitle";
74 aColumnDefaults
[7] = "Chapter";
75 aColumnDefaults
[8] = "Edition";
76 aColumnDefaults
[9] = "Editor";
77 aColumnDefaults
[10] = "Howpublished";
78 aColumnDefaults
[11] = "Institution";
79 aColumnDefaults
[12] = "Journal";
80 aColumnDefaults
[13] = "Month";
81 aColumnDefaults
[14] = "Note";
82 aColumnDefaults
[15] = "Annote";
83 aColumnDefaults
[16] = "Number";
84 aColumnDefaults
[17] = "Organizations";
85 aColumnDefaults
[18] = "Pages";
86 aColumnDefaults
[19] = "Publisher";
87 aColumnDefaults
[20] = "Address";
88 aColumnDefaults
[21] = "School";
89 aColumnDefaults
[22] = "Series";
90 aColumnDefaults
[23] = "ReportType";
91 aColumnDefaults
[24] = "Volume";
92 aColumnDefaults
[25] = "URL";
93 aColumnDefaults
[26] = "Custom1";
94 aColumnDefaults
[27] = "Custom2";
95 aColumnDefaults
[28] = "Custom3";
96 aColumnDefaults
[29] = "Custom4";
97 aColumnDefaults
[30] = "Custom5";
100 const Sequence
< OUString
> aPropertyNames
= GetPropertyNames();
101 const Sequence
<Any
> aPropertyValues
= GetProperties( aPropertyNames
);
102 const Any
* pValues
= aPropertyValues
.getConstArray();
103 if(aPropertyValues
.getLength() == aPropertyNames
.getLength())
105 for(int nProp
= 0; nProp
< aPropertyNames
.getLength(); nProp
++)
107 if(pValues
[nProp
].hasValue())
111 case 0: pValues
[nProp
] >>= sDataSource
; break;
112 case 1: pValues
[nProp
] >>= sTableOrQuery
; break;
113 case 2: pValues
[nProp
] >>= nTblOrQuery
; break;
114 case 3: pValues
[nProp
] >>= nBeamerSize
; break;
115 case 4: pValues
[nProp
] >>= nViewSize
; break;
116 case 5: pValues
[nProp
] >>= sQueryText
; break;
117 case 6: pValues
[nProp
] >>= sQueryField
; break;
119 bShowColumnAssignmentWarning
= *static_cast<sal_Bool
const *>(pValues
[nProp
].getValue());
125 OUString
sName("DataSourceName");
126 OUString
sTable("Command");
127 OUString
sCommandType("CommandType");
128 Sequence
< OUString
> aNodeNames
= GetNodeNames(cDataSourceHistory
);
129 const OUString
* pNodeNames
= aNodeNames
.getConstArray();
130 for(sal_Int32 nNode
= 0; nNode
< aNodeNames
.getLength(); nNode
++)
132 Sequence
<OUString
> aHistoryNames(3);
133 OUString
* pHistoryNames
= aHistoryNames
.getArray();
135 OUString
sPrefix(cDataSourceHistory
);
137 sPrefix
+= pNodeNames
[nNode
];
139 pHistoryNames
[0] = sPrefix
;
140 pHistoryNames
[0] += sName
;
141 pHistoryNames
[1] = sPrefix
;
142 pHistoryNames
[1] += sTable
;
143 pHistoryNames
[2] = sPrefix
;
144 pHistoryNames
[2] += sCommandType
;
146 Sequence
<Any
> aHistoryValues
= GetProperties( aHistoryNames
);
147 const Any
* pHistoryValues
= aHistoryValues
.getConstArray();
149 if(aHistoryValues
.getLength() == aHistoryNames
.getLength())
151 Mapping
* pMapping
= new Mapping
;
152 pHistoryValues
[0] >>= pMapping
->sURL
;
153 pHistoryValues
[1] >>= pMapping
->sTableName
;
154 pHistoryValues
[2] >>= pMapping
->nCommandType
;
155 //field assignment is contained in another set
157 Sequence
< OUString
> aAssignmentNodeNames
= GetNodeNames(sPrefix
);
158 const OUString
* pAssignmentNodeNames
= aAssignmentNodeNames
.getConstArray();
159 Sequence
<OUString
> aAssignmentPropertyNames(aAssignmentNodeNames
.getLength() * 2);
160 OUString
* pAssignmentPropertyNames
= aAssignmentPropertyNames
.getArray();
161 sal_Int16 nFieldIdx
= 0;
162 for(sal_Int16 nField
= 0; nField
< aAssignmentNodeNames
.getLength(); nField
++)
164 OUString
sSubPrefix(sPrefix
);
166 sSubPrefix
+= pAssignmentNodeNames
[nField
];
167 pAssignmentPropertyNames
[nFieldIdx
] = sSubPrefix
;
168 pAssignmentPropertyNames
[nFieldIdx
++] += "/ProgrammaticFieldName";
169 pAssignmentPropertyNames
[nFieldIdx
] = sSubPrefix
;
170 pAssignmentPropertyNames
[nFieldIdx
++] += "/AssignedFieldName";
172 Sequence
<Any
> aAssignmentValues
= GetProperties(aAssignmentPropertyNames
);
173 const Any
* pAssignmentValues
= aAssignmentValues
.getConstArray();
174 OUString sTempLogical
;
176 sal_Int16 nSetMapping
= 0;
178 for(sal_Int16 nFieldVal
= 0; nFieldVal
< aAssignmentValues
.getLength() / 2; nFieldVal
++)
180 pAssignmentValues
[nFieldIdx
++] >>= sTempLogical
;
181 pAssignmentValues
[nFieldIdx
++] >>= sTempReal
;
182 if(!(sTempLogical
.isEmpty() || sTempReal
.isEmpty()))
184 pMapping
->aColumnPairs
[nSetMapping
].sLogicalColumnName
= sTempLogical
;
185 pMapping
->aColumnPairs
[nSetMapping
++].sRealColumnName
= sTempReal
;
188 pMappingsArr
->push_back(pMapping
);
193 BibConfig::~BibConfig()
195 assert(!IsModified()); // should have been committed
199 BibDBDescriptor
BibConfig::GetBibliographyURL()
201 BibDBDescriptor aRet
;
202 aRet
.sDataSource
= sDataSource
;
203 aRet
.sTableOrQuery
= sTableOrQuery
;
204 aRet
.nCommandType
= nTblOrQuery
;
208 void BibConfig::SetBibliographyURL(const BibDBDescriptor
& rDesc
)
210 sDataSource
= rDesc
.sDataSource
;
211 sTableOrQuery
= rDesc
.sTableOrQuery
;
212 nTblOrQuery
= rDesc
.nCommandType
;
216 void BibConfig::Notify( const com::sun::star::uno::Sequence
<OUString
>& )
220 void BibConfig::ImplCommit()
222 const Sequence
<OUString
> aPropertyNames
= GetPropertyNames();
223 Sequence
<Any
> aValues(aPropertyNames
.getLength());
224 Any
* pValues
= aValues
.getArray();
226 for(int nProp
= 0; nProp
< aPropertyNames
.getLength(); nProp
++)
230 case 0: pValues
[nProp
] <<= sDataSource
; break;
231 case 1: pValues
[nProp
] <<= sTableOrQuery
; break;
232 case 2: pValues
[nProp
] <<= nTblOrQuery
; break;
233 case 3: pValues
[nProp
] <<= nBeamerSize
; break;
234 case 4: pValues
[nProp
] <<= nViewSize
; break;
235 case 5: pValues
[nProp
] <<= sQueryText
; break;
236 case 6: pValues
[nProp
] <<= sQueryField
; break;
238 pValues
[nProp
].setValue(&bShowColumnAssignmentWarning
, cppu::UnoType
<bool>::get());
242 PutProperties(aPropertyNames
, aValues
);
243 ClearNodeSet(cDataSourceHistory
);
244 Sequence
< PropertyValue
> aNodeValues(pMappingsArr
->size() * 3);
245 PropertyValue
* pNodeValues
= aNodeValues
.getArray();
247 sal_Int32 nIndex
= 0;
248 OUString
sName("DataSourceName");
249 OUString
sTable("Command");
250 OUString
sCommandType("CommandType");
251 for(sal_Int32 i
= 0; i
< (sal_Int32
)pMappingsArr
->size(); i
++)
253 const Mapping
* pMapping
= &(*pMappingsArr
)[i
];
254 OUString
sPrefix(cDataSourceHistory
);
256 sPrefix
+= OUString::number(i
);
258 pNodeValues
[nIndex
].Name
= sPrefix
;
259 pNodeValues
[nIndex
].Name
+= sName
;
260 pNodeValues
[nIndex
++].Value
<<= pMapping
->sURL
;
261 pNodeValues
[nIndex
].Name
= sPrefix
;
262 pNodeValues
[nIndex
].Name
+= sTable
;
263 pNodeValues
[nIndex
++].Value
<<= pMapping
->sTableName
;
264 pNodeValues
[nIndex
].Name
= sPrefix
;
265 pNodeValues
[nIndex
].Name
+= sCommandType
;
266 pNodeValues
[nIndex
++].Value
<<= pMapping
->nCommandType
;
267 SetSetProperties(cDataSourceHistory
, aNodeValues
);
270 sal_Int32 nFieldAssignment
= 0;
271 OUString sFieldName
= "/ProgrammaticFieldName";
272 OUString sDatabaseFieldName
= "/AssignedFieldName";
273 ClearNodeSet( sPrefix
);
275 while(nFieldAssignment
< COLUMN_COUNT
&&
276 !pMapping
->aColumnPairs
[nFieldAssignment
].sLogicalColumnName
.isEmpty())
278 OUString
sSubPrefix(sPrefix
);
280 sSubPrefix
+= OUString::number(nFieldAssignment
);
281 Sequence
< PropertyValue
> aAssignmentValues(2);
282 PropertyValue
* pAssignmentValues
= aAssignmentValues
.getArray();
283 pAssignmentValues
[0].Name
= sSubPrefix
;
284 pAssignmentValues
[0].Name
+= sFieldName
;
285 pAssignmentValues
[0].Value
<<= pMapping
->aColumnPairs
[nFieldAssignment
].sLogicalColumnName
;
286 pAssignmentValues
[1].Name
= sSubPrefix
;
287 pAssignmentValues
[1].Name
+= sDatabaseFieldName
;
288 pAssignmentValues
[1].Value
<<= pMapping
->aColumnPairs
[nFieldAssignment
].sRealColumnName
;
289 SetSetProperties( sPrefix
, aAssignmentValues
);
295 const Mapping
* BibConfig::GetMapping(const BibDBDescriptor
& rDesc
) const
297 for(sal_uInt16 i
= 0; i
< pMappingsArr
->size(); i
++)
299 Mapping
& rMapping
= (*pMappingsArr
)[i
];
300 bool bURLEqual
= rDesc
.sDataSource
.equals(rMapping
.sURL
);
301 if(rDesc
.sTableOrQuery
== rMapping
.sTableName
&& bURLEqual
)
307 void BibConfig::SetMapping(const BibDBDescriptor
& rDesc
, const Mapping
* pSetMapping
)
309 for(sal_uInt16 i
= 0; i
< pMappingsArr
->size(); i
++)
311 Mapping
& rMapping
= (*pMappingsArr
)[i
];
312 bool bURLEqual
= rDesc
.sDataSource
.equals(rMapping
.sURL
);
313 if(rDesc
.sTableOrQuery
== rMapping
.sTableName
&& bURLEqual
)
315 pMappingsArr
->erase(pMappingsArr
->begin()+i
);
319 Mapping
* pNew
= new Mapping(*pSetMapping
);
320 pMappingsArr
->push_back(pNew
);
324 DBChangeDialogConfig_Impl::DBChangeDialogConfig_Impl()
328 DBChangeDialogConfig_Impl::~DBChangeDialogConfig_Impl()
332 const Sequence
<OUString
>& DBChangeDialogConfig_Impl::GetDataSourceNames()
334 if(!aSourceNames
.getLength())
336 Reference
< XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
337 Reference
<XDatabaseContext
> xDBContext
= DatabaseContext::create(xContext
);
338 aSourceNames
= xDBContext
->getElementNames();
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */