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 .
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>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/propertyvalue.hxx>
28 #include <o3tl/any.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 constexpr OUString cDataSourceHistory
= u
"DataSourceHistory"_ustr
;
39 Sequence
<OUString
> const & BibConfig::GetPropertyNames()
41 static Sequence
<OUString
> aNames
=
43 "CurrentDataSource/DataSourceName",
44 "CurrentDataSource/Command",
45 "CurrentDataSource/CommandType",
50 "ShowColumnAssignmentWarning"
55 BibConfig::BibConfig()
56 : ConfigItem("Office.DataAccess/Bibliography", ConfigItemMode::NONE
)
60 , bShowColumnAssignmentWarning(false)
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";
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())
102 for(int nProp
= 0; nProp
< aPropertyNames
.getLength(); nProp
++)
104 if(pValues
[nProp
].hasValue())
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;
116 bShowColumnAssignmentWarning
= *o3tl::doAccess
<bool>(pValues
[nProp
]);
122 const Sequence
< OUString
> aNodeNames
= GetNodeNames(cDataSourceHistory
);
123 for(OUString
const & nodeName
: aNodeNames
)
125 Sequence
<OUString
> aHistoryNames(3);
126 OUString
* pHistoryNames
= aHistoryNames
.getArray();
128 OUString sPrefix
= OUString::Concat(cDataSourceHistory
) + "/" + nodeName
+ "/";
129 pHistoryNames
[0] = sPrefix
+ "DataSourceName";
130 pHistoryNames
[1] = sPrefix
+ "Command";
131 pHistoryNames
[2] = sPrefix
+ "CommandType";
133 Sequence
<Any
> aHistoryValues
= GetProperties( aHistoryNames
);
134 const Any
* pHistoryValues
= aHistoryValues
.getConstArray();
136 if(aHistoryValues
.getLength() == aHistoryNames
.getLength())
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
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
)
150 OUString sSubPrefix
= sPrefix
+ "/" + assignName
;
151 pAssignmentPropertyNames
[nFieldIdx
] = sSubPrefix
;
152 pAssignmentPropertyNames
[nFieldIdx
++] += "/ProgrammaticFieldName";
153 pAssignmentPropertyNames
[nFieldIdx
] = sSubPrefix
;
154 pAssignmentPropertyNames
[nFieldIdx
++] += "/AssignedFieldName";
156 Sequence
<Any
> aAssignmentValues
= GetProperties(aAssignmentPropertyNames
);
157 const Any
* pAssignmentValues
= aAssignmentValues
.getConstArray();
158 OUString sTempLogical
;
160 sal_Int16 nSetMapping
= 0;
162 for(sal_Int32 nFieldVal
= 0; nFieldVal
< aAssignmentValues
.getLength() / 2; nFieldVal
++)
164 pAssignmentValues
[nFieldIdx
++] >>= sTempLogical
;
165 pAssignmentValues
[nFieldIdx
++] >>= sTempReal
;
166 if(!(sTempLogical
.isEmpty() || sTempReal
.isEmpty()))
168 pMapping
->aColumnPairs
[nSetMapping
].sLogicalColumnName
= sTempLogical
;
169 pMapping
->aColumnPairs
[nSetMapping
++].sRealColumnName
= sTempReal
;
172 mvMappings
.push_back(std::unique_ptr
<Mapping
>(pMapping
));
177 BibConfig::~BibConfig()
179 assert(!IsModified()); // should have been committed
182 BibDBDescriptor
BibConfig::GetBibliographyURL()
184 BibDBDescriptor aRet
;
185 aRet
.sDataSource
= sDataSource
;
186 aRet
.sTableOrQuery
= sTableOrQuery
;
187 aRet
.nCommandType
= nTblOrQuery
;
191 void BibConfig::SetBibliographyURL(const BibDBDescriptor
& rDesc
)
193 sDataSource
= rDesc
.sDataSource
;
194 sTableOrQuery
= rDesc
.sTableOrQuery
;
195 nTblOrQuery
= rDesc
.nCommandType
;
199 void BibConfig::Notify( const css::uno::Sequence
<OUString
>& )
203 void BibConfig::ImplCommit()
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
)});
212 ClearNodeSet(cDataSourceHistory
);
213 Sequence
< PropertyValue
> aNodeValues(mvMappings
.size() * 3);
214 PropertyValue
* pNodeValues
= aNodeValues
.getArray();
216 sal_Int32 nIndex
= 0;
217 for(sal_Int32 i
= 0; i
< static_cast<sal_Int32
>(mvMappings
.size()); i
++)
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
;
227 SetSetProperties(cDataSourceHistory
, aNodeValues
);
230 sal_Int32 nFieldAssignment
= 0;
231 OUString sFieldName
= "/ProgrammaticFieldName";
232 OUString sDatabaseFieldName
= "/AssignedFieldName";
233 ClearNodeSet( sPrefix
);
235 while(nFieldAssignment
< COLUMN_COUNT
&&
236 !pMapping
->aColumnPairs
[nFieldAssignment
].sLogicalColumnName
.isEmpty())
238 OUString sSubPrefix
= sPrefix
+ "/_" + OUString::number(nFieldAssignment
);
239 Sequence
< PropertyValue
> aAssignmentValues
241 comphelper::makePropertyValue(sSubPrefix
+ sFieldName
, pMapping
->aColumnPairs
[nFieldAssignment
].sLogicalColumnName
),
242 comphelper::makePropertyValue(sSubPrefix
+ sDatabaseFieldName
, pMapping
->aColumnPairs
[nFieldAssignment
].sRealColumnName
)
244 SetSetProperties( sPrefix
, aAssignmentValues
);
250 const Mapping
* BibConfig::GetMapping(const BibDBDescriptor
& rDesc
) const
252 for(std::unique_ptr
<Mapping
> const & i
: mvMappings
)
254 Mapping
& rMapping
= *i
;
255 bool bURLEqual
= rDesc
.sDataSource
== rMapping
.sURL
;
256 if(rDesc
.sTableOrQuery
== rMapping
.sTableName
&& bURLEqual
)
262 void BibConfig::SetMapping(const BibDBDescriptor
& rDesc
, const Mapping
* pSetMapping
)
264 for(size_t i
= 0; i
< mvMappings
.size(); i
++)
266 Mapping
& rMapping
= *mvMappings
[i
];
267 bool bURLEqual
= rDesc
.sDataSource
== rMapping
.sURL
;
268 if(rDesc
.sTableOrQuery
== rMapping
.sTableName
&& bURLEqual
)
270 mvMappings
.erase(mvMappings
.begin()+i
);
274 mvMappings
.push_back(std::make_unique
<Mapping
>(*pSetMapping
));
278 DBChangeDialogConfig_Impl::DBChangeDialogConfig_Impl()
282 DBChangeDialogConfig_Impl::~DBChangeDialogConfig_Impl()
286 const Sequence
<OUString
>& DBChangeDialogConfig_Impl::GetDataSourceNames()
288 if(!aSourceNames
.hasElements())
290 Reference
< XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
291 Reference
<XDatabaseContext
> xDBContext
= DatabaseContext::create(xContext
);
292 aSourceNames
= xDBContext
->getElementNames();
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */