LibreOffice Module extensions (master) 1
bibconfig.cxx
Go to the documentation of this file.
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 <memory>
22#include "bibconfig.hxx"
23#include <com/sun/star/uno/Sequence.hxx>
24#include <com/sun/star/beans/PropertyValue.hpp>
25#include <com/sun/star/sdb/DatabaseContext.hpp>
28#include <o3tl/any.hxx>
29
30using namespace ::com::sun::star::uno;
31using namespace ::com::sun::star::beans;
32using namespace ::com::sun::star::container;
33using namespace ::com::sun::star::lang;
34using namespace ::com::sun::star::sdb;
35
36
37constexpr OUStringLiteral cDataSourceHistory = u"DataSourceHistory";
38
39Sequence<OUString> const & BibConfig::GetPropertyNames()
40{
41 static Sequence<OUString> aNames =
42 {
43 "CurrentDataSource/DataSourceName",
44 "CurrentDataSource/Command",
45 "CurrentDataSource/CommandType",
46 "BeamerHeight",
47 "ViewHeight",
48 "QueryText",
49 "QueryField",
50 "ShowColumnAssignmentWarning"
51 };
52 return aNames;
53}
54
56 : ConfigItem("Office.DataAccess/Bibliography", ConfigItemMode::NONE)
57 , nTblOrQuery(0)
58 , nBeamerSize(0)
59 , nViewSize(0)
60 , bShowColumnAssignmentWarning(false)
61{
62 //Names of the default columns
63 aColumnDefaults[0] = "Identifier";
64 aColumnDefaults[1] = "BibliographyType";
65 aColumnDefaults[2] = "Author";
66 aColumnDefaults[3] = "Title";
67 aColumnDefaults[4] = "Year";
68 aColumnDefaults[5] = "ISBN";
69 aColumnDefaults[6] = "Booktitle";
70 aColumnDefaults[7] = "Chapter";
71 aColumnDefaults[8] = "Edition";
72 aColumnDefaults[9] = "Editor";
73 aColumnDefaults[10] = "Howpublished";
74 aColumnDefaults[11] = "Institution";
75 aColumnDefaults[12] = "Journal";
76 aColumnDefaults[13] = "Month";
77 aColumnDefaults[14] = "Note";
78 aColumnDefaults[15] = "Annote";
79 aColumnDefaults[16] = "Number";
80 aColumnDefaults[17] = "Organizations";
81 aColumnDefaults[18] = "Pages";
82 aColumnDefaults[19] = "Publisher";
83 aColumnDefaults[20] = "Address";
84 aColumnDefaults[21] = "School";
85 aColumnDefaults[22] = "Series";
86 aColumnDefaults[23] = "ReportType";
87 aColumnDefaults[24] = "Volume";
88 aColumnDefaults[25] = "URL";
89 aColumnDefaults[26] = "Custom1";
90 aColumnDefaults[27] = "Custom2";
91 aColumnDefaults[28] = "Custom3";
92 aColumnDefaults[29] = "Custom4";
93 aColumnDefaults[30] = "Custom5";
94 aColumnDefaults[31] = "LocalURL";
95
96
97 const Sequence< OUString > aPropertyNames = GetPropertyNames();
98 const Sequence<Any> aPropertyValues = GetProperties( aPropertyNames );
99 const Any* pValues = aPropertyValues.getConstArray();
100 if(aPropertyValues.getLength() == aPropertyNames.getLength())
101 {
102 for(int nProp = 0; nProp < aPropertyNames.getLength(); nProp++)
103 {
104 if(pValues[nProp].hasValue())
105 {
106 switch(nProp)
107 {
108 case 0: pValues[nProp] >>= sDataSource; break;
109 case 1: pValues[nProp] >>= sTableOrQuery; break;
110 case 2: pValues[nProp] >>= nTblOrQuery; break;
111 case 3: pValues[nProp] >>= nBeamerSize; break;
112 case 4: pValues[nProp] >>= nViewSize ; break;
113 case 5: pValues[nProp] >>= sQueryText ; break;
114 case 6: pValues[nProp] >>= sQueryField; break;
115 case 7:
116 bShowColumnAssignmentWarning = *o3tl::doAccess<bool>(pValues[nProp]);
117 break;
118 }
119 }
120 }
121 }
122 const Sequence< OUString > aNodeNames = GetNodeNames(cDataSourceHistory);
123 for(OUString const & nodeName : aNodeNames)
124 {
125 Sequence<OUString> aHistoryNames(3);
126 OUString* pHistoryNames = aHistoryNames.getArray();
127
128 OUString sPrefix = OUString::Concat(cDataSourceHistory) + "/" + nodeName + "/";
129 pHistoryNames[0] = sPrefix + "DataSourceName";
130 pHistoryNames[1] = sPrefix + "Command";
131 pHistoryNames[2] = sPrefix + "CommandType";
132
133 Sequence<Any> aHistoryValues = GetProperties( aHistoryNames );
134 const Any* pHistoryValues = aHistoryValues.getConstArray();
135
136 if(aHistoryValues.getLength() == aHistoryNames.getLength())
137 {
138 Mapping* pMapping = new Mapping;
139 pHistoryValues[0] >>= pMapping->sURL;
140 pHistoryValues[1] >>= pMapping->sTableName;
141 pHistoryValues[2] >>= pMapping->nCommandType;
142 //field assignment is contained in another set
143 sPrefix += "Fields";
144 const Sequence< OUString > aAssignmentNodeNames = GetNodeNames(sPrefix);
145 Sequence<OUString> aAssignmentPropertyNames(aAssignmentNodeNames.getLength() * 2);
146 OUString* pAssignmentPropertyNames = aAssignmentPropertyNames.getArray();
147 sal_Int16 nFieldIdx = 0;
148 for(OUString const & assignName : aAssignmentNodeNames)
149 {
150 OUString sSubPrefix = sPrefix + "/" + assignName;
151 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
152 pAssignmentPropertyNames[nFieldIdx++] += "/ProgrammaticFieldName";
153 pAssignmentPropertyNames[nFieldIdx] = sSubPrefix;
154 pAssignmentPropertyNames[nFieldIdx++] += "/AssignedFieldName";
155 }
156 Sequence<Any> aAssignmentValues = GetProperties(aAssignmentPropertyNames);
157 const Any* pAssignmentValues = aAssignmentValues.getConstArray();
158 OUString sTempLogical;
159 OUString sTempReal;
160 sal_Int16 nSetMapping = 0;
161 nFieldIdx = 0;
162 for(sal_Int32 nFieldVal = 0; nFieldVal < aAssignmentValues.getLength() / 2; nFieldVal++)
163 {
164 pAssignmentValues[nFieldIdx++] >>= sTempLogical;
165 pAssignmentValues[nFieldIdx++] >>= sTempReal;
166 if(!(sTempLogical.isEmpty() || sTempReal.isEmpty()))
167 {
168 pMapping->aColumnPairs[nSetMapping].sLogicalColumnName = sTempLogical;
169 pMapping->aColumnPairs[nSetMapping++].sRealColumnName = sTempReal;
170 }
171 }
172 mvMappings.push_back(std::unique_ptr<Mapping>(pMapping));
173 }
174 }
175}
176
178{
179 assert(!IsModified()); // should have been committed
180}
181
183{
184 BibDBDescriptor aRet;
188 return aRet;
189};
190
192{
193 sDataSource = rDesc.sDataSource;
196 SetModified();
197};
198
199void BibConfig::Notify( const css::uno::Sequence<OUString>& )
200{
201}
202
204{
207 {css::uno::Any(sDataSource), css::uno::Any(sTableOrQuery),
208 css::uno::Any(nTblOrQuery), css::uno::Any(nBeamerSize),
209 css::uno::Any(nViewSize), css::uno::Any(sQueryText),
210 css::uno::Any(sQueryField),
211 css::uno::Any(bShowColumnAssignmentWarning)});
213 Sequence< PropertyValue > aNodeValues(mvMappings.size() * 3);
214 PropertyValue* pNodeValues = aNodeValues.getArray();
215
216 sal_Int32 nIndex = 0;
217 for(sal_Int32 i = 0; i < static_cast<sal_Int32>(mvMappings.size()); i++)
218 {
219 const Mapping* pMapping = mvMappings[i].get();
220 OUString sPrefix = OUString::Concat(cDataSourceHistory) + "/_" + OUString::number(i) + "/";
221 pNodeValues[nIndex].Name = sPrefix + "DataSourceName";
222 pNodeValues[nIndex++].Value <<= pMapping->sURL;
223 pNodeValues[nIndex].Name = sPrefix + "Command";
224 pNodeValues[nIndex++].Value <<= pMapping->sTableName;
225 pNodeValues[nIndex].Name = sPrefix + "CommandType";
226 pNodeValues[nIndex++].Value <<= pMapping->nCommandType;
228
229 sPrefix += "Fields";
230 sal_Int32 nFieldAssignment = 0;
231 OUString sFieldName = "/ProgrammaticFieldName";
232 OUString sDatabaseFieldName = "/AssignedFieldName";
234
235 while(nFieldAssignment < COLUMN_COUNT &&
236 !pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName.isEmpty())
237 {
238 OUString sSubPrefix = sPrefix + "/_" + OUString::number(nFieldAssignment);
239 Sequence< PropertyValue > aAssignmentValues
240 {
241 comphelper::makePropertyValue(sSubPrefix + sFieldName, pMapping->aColumnPairs[nFieldAssignment].sLogicalColumnName),
242 comphelper::makePropertyValue(sSubPrefix + sDatabaseFieldName, pMapping->aColumnPairs[nFieldAssignment].sRealColumnName)
243 };
244 SetSetProperties( sPrefix, aAssignmentValues );
245 nFieldAssignment++;
246 }
247 }
248}
249
251{
252 for(std::unique_ptr<Mapping> const & i : mvMappings)
253 {
254 Mapping& rMapping = *i;
255 bool bURLEqual = rDesc.sDataSource == rMapping.sURL;
256 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
257 return &rMapping;
258 }
259 return nullptr;
260}
261
262void BibConfig::SetMapping(const BibDBDescriptor& rDesc, const Mapping* pSetMapping)
263{
264 for(size_t i = 0; i < mvMappings.size(); i++)
265 {
266 Mapping& rMapping = *mvMappings[i];
267 bool bURLEqual = rDesc.sDataSource == rMapping.sURL;
268 if(rDesc.sTableOrQuery == rMapping.sTableName && bURLEqual)
269 {
270 mvMappings.erase(mvMappings.begin()+i);
271 break;
272 }
273 }
274 mvMappings.push_back(std::make_unique<Mapping>(*pSetMapping));
275 SetModified();
276}
277
279{
280}
281
283{
284}
285
287{
288 if(!aSourceNames.hasElements())
289 {
290 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
291 Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
292 aSourceNames = xDBContext->getElementNames();
293 }
294 return aSourceNames;
295}
296
297/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const PropertyValue * pValues
PropertyValueVector_t aPropertyValues
constexpr OUStringLiteral cDataSourceHistory
Definition: bibconfig.cxx:37
#define COLUMN_COUNT
Definition: bibconfig.hxx:31
const Mapping * GetMapping(const BibDBDescriptor &rDesc) const
Definition: bibconfig.cxx:250
OUString aColumnDefaults[COLUMN_COUNT]
Definition: bibconfig.hxx:103
OUString sDataSource
Definition: bibconfig.hxx:92
OUString sQueryField
Definition: bibconfig.hxx:96
static css::uno::Sequence< OUString > const & GetPropertyNames()
Definition: bibconfig.cxx:39
tools::Long nBeamerSize
Definition: bibconfig.hxx:99
OUString sQueryText
Definition: bibconfig.hxx:97
void SetMapping(const BibDBDescriptor &rDesc, const Mapping *pMapping)
Definition: bibconfig.cxx:262
virtual ~BibConfig() override
Definition: bibconfig.cxx:177
OUString sTableOrQuery
Definition: bibconfig.hxx:93
tools::Long nViewSize
Definition: bibconfig.hxx:100
MappingArray mvMappings
Definition: bibconfig.hxx:98
virtual void ImplCommit() override
Definition: bibconfig.cxx:203
void SetBibliographyURL(const BibDBDescriptor &rDesc)
Definition: bibconfig.cxx:191
bool bShowColumnAssignmentWarning
Definition: bibconfig.hxx:101
virtual void Notify(const css::uno::Sequence< OUString > &aPropertyNames) override
Definition: bibconfig.cxx:199
sal_Int32 nTblOrQuery
Definition: bibconfig.hxx:94
BibDBDescriptor GetBibliographyURL()
Definition: bibconfig.cxx:182
const css::uno::Sequence< OUString > & GetDataSourceNames()
Definition: bibconfig.cxx:286
css::uno::Sequence< OUString > aSourceNames
Definition: bibconfig.hxx:144
static bool PutProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const css::uno::Sequence< OUString > &rNames, const css::uno::Sequence< css::uno::Any > &rValues, bool bAllLocales)
static bool SetSetProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const OUString &rNode, const css::uno::Sequence< css::beans::PropertyValue > &rValues)
static bool ClearNodeSet(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const OUString &rNode)
bool IsModified() const
static css::uno::Sequence< css::uno::Any > GetProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const css::uno::Sequence< OUString > &rNames, bool bAllLocales)
static css::uno::Sequence< OUString > GetNodeNames(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const OUString &rNode, ConfigNameFormat eFormat)
ConfigItemMode
float u
OUString sPrefix
sal_Int32 nIndex
uno_Mapping * pMapping
NONE
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
int i
OUString sDataSource
Definition: bibconfig.hxx:84
OUString sTableOrQuery
Definition: bibconfig.hxx:85
sal_Int32 nCommandType
Definition: bibconfig.hxx:86
OUString sTableName
Definition: bibconfig.hxx:73
OUString sURL
Definition: bibconfig.hxx:74