source: gsdl/trunk/runtime-src/src/oaiservr/oaiconfig.cpp@ 16708

Last change on this file since 16708 was 14284, checked in by xiao, 17 years ago

modify getCollectionConfig() to read 'baseURL' parameter from oai.cfg.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1#include "oaiconfig.h"
2
3#include <algorithm>
4#include <iostream>
5
6#include "fileutil.h"
7
8/**
9 * The mapping works as follows in the collect.cfg file.
10 *
11 * A line is in the format oaimapping <collection field> <oai field>
12 *
13 * The map here is used to look up the "Greenstone" name which is mapped from
14 * a given OAI field name, the reverse direction to that given in the
15 * Greenstone collect.cfg file. The oairecordaction class instance which
16 * produces output for an OAI record information request thus uses the map
17 * to work from the field in the collection it has on hand which OAI
18 * record name it should use instead.
19 *
20 * An extension is to be used for this in which the OAI field name in the
21 * collect.cfg file can be made specific for a particular record format.
22 * This is done using the OAI field name in the format of
23 * <OAI format>:<OAI field name>
24 * Thus, an rfc1807 Title field would be referred to as rfc1807:Title
25 *
26 * A collection-level mapping is not needed - to configure the behaviour
27 * of a collection, ensure that you place the appropriate configuration in
28 * its etc/collect.cfg file - the oaimapping stuff IS NOT picked up from
29 * the central main.cfg file at the moment.
30 *
31 * In the absence of a particular format name, the mapping is taken to be
32 * universal.
33 */
34
35oaiconfig::oaiconfig() : configurable () {
36 this->resumptionSize = -1; // Default = do not use resumption tokens
37}
38
39oaiconfig::oaiconfig(text_t &gsdlhome, text_t &gsdlcollect)
40{
41 // read main configuration file to get oai collections
42 text_t mainconfig = filename_cat(gsdlhome, "etc", "oai.cfg");
43 this->collection = "";
44 this->resumptionSize = -1;
45 this->read_configfile(mainconfig);
46
47 // then if we've not got a specified collection in the gsdlcollect
48 // parameter, read in all the collection's individual configurations
49 if (gsdlcollect == "") {
50 text_tarray::iterator here = this->collectList.begin();
51 text_tarray::iterator end = this->collectList.end();
52 while (here != end) {
53 this->configureCollection(gsdlhome, *here);
54 ++here;
55 }
56 }
57 else {
58 this->configureCollection(gsdlhome, gsdlcollect);
59 }
60}
61
62oaiconfig::~oaiconfig()
63{
64 oaicollectmap::iterator here = this->collectMap.begin();
65 oaicollectmap::iterator end = this->collectMap.end();
66 while (here != end) {
67 delete here->second;
68 ++here;
69 }
70}
71
72int oaiconfig::resumeAfter()
73{ return this->resumptionSize;
74}
75
76int oaiconfig::getOAIVersion()
77{
78 if (this->oaiVersion == "1.1") {
79 return 110;
80 }
81 return 200;
82}
83
84void oaiconfig::configureCollection(const text_t &gsdlhome, const text_t &gsdlcollect)
85{
86 text_t cnfgfile = filename_cat(gsdlhome, "collect", gsdlcollect, "etc", "collect.cfg");
87 this->collection = gsdlcollect;
88 this->read_configfile(cnfgfile);
89}
90
91void oaiconfig::configure (const text_t &key, const text_tarray &cfgline)
92{
93 // we've got an oai mapping item, and at least two fields
94 if (key == "oaimapping" && cfgline.size() > 1) {
95 text_t::const_iterator colonAt;
96 text_t index, name, configCollection;
97
98 // Take a default collection as being whatever the collection being configured is...
99 configCollection = this->collection;
100
101 // get the name of the (collection) field to map; this may actually
102 // be in a colon separated format of the type
103 // <collection name>:<field name>
104 index = cfgline[0];
105 if ((colonAt = find(index.begin(), index.end(), ':')) != index.end()) {
106 configCollection = substr(index.begin(), colonAt);
107
108 if (this->collection != "" && configCollection != this->collection) {
109 cerr << "Attempt to configure OAI mappings for " << configCollection << " in " << this->collection << endl;
110 }
111
112 colonAt += 1;
113 index = substr(colonAt, index.end());
114 }
115
116 // the second parameter is the metadata field to map the collection
117 // field onto. It may be provided with a metadata protocol (which
118 // will be given first and separated by a period or full stop). In
119 // the case of format.field name, the splitting is done here.
120 if ((colonAt = find(cfgline[1].begin(), cfgline[1].end(), '.')) != cfgline[1].end()) {
121 text_t stub = substr(cfgline[1].begin(), colonAt);
122 colonAt += 1;
123 name = substr(colonAt, cfgline[1].end());
124 index.append(":");
125 index.append(stub);
126 }
127 else {
128 name = cfgline[1];
129 }
130
131 // now 'index' is in the form <collectionfield>:(formatname)
132 // 'name' is simply the fieldname within the format
133 // 'configCollection' is the collection to be configured
134
135 // now simply map the field name (index) onto the collection name (name)
136 if (this->collectMap[configCollection] == NULL) {
137 this->collectMap[configCollection] = new oaicollectconfig(configCollection);
138 }
139 this->collectMap[configCollection]->fieldMap[index] = name;
140
141 // cerr << "Mapping " << index << " to " << name << " in " << configCollection << endl;
142
143 // TODO: check that the mapped field is actually in use
144 }
145 else if (key == "oaicollection" && cfgline.size() >= 1) {
146 // Configure a collection to be used as part of the OAI archive.
147 // This line should read:
148 //
149 // oaicollection <collectionname>
150 //
151 // Where <collectionname> is the name of the directory inside the
152 // gsdl/collect folder which contains the collection.
153 //
154 // To configure several collections, merely repeat this line,
155 // or alternatively use additional collection names after the
156 // first one.
157 //
158 // This configuration should only appear in main.cfg
159 //
160 if (this->collection != "") {
161 cerr << "Attempt to configure an oai collection outside of main.cfg" << endl;
162 cerr << "Configuration attempted in " << this->collection << " collection." << endl;
163 exit(1);
164 }
165 for (int c = 0; c < cfgline.size(); ++c) {
166 this->collectList.push_back(cfgline[c]);
167 }
168 }
169 else if (key == "oaiinfo" && cfgline.size() >= 1) {
170 // Get a piece of information for the oai repository information
171 // request. The line should read:
172 //
173 // oaiinfo <information field name> <value>
174 //
175 // This configuration should only be attempted in main.cfg
176 //
177 if (this->collection != "") {
178 cerr << "Attempt to set oai information outside of main.cfg" << endl;
179 cerr << "Configuration attempted in " << this->collection << " collection." << endl;
180 exit(1);
181 }
182
183 // if no second parameter is given, then the first parameter
184 if (cfgline.size() == 1) {
185 this->infoMap[cfgline[0]] = cfgline[0];
186 }
187 else {
188 this->infoMap[cfgline[0]] = cfgline[1];
189 }
190 }
191 else if (key == "oaiversion" && cfgline.size() >= 1) {
192 this->oaiVersion = cfgline[0];
193 }
194 else if (key == "resumeafter" && cfgline.size() >= 1) {
195 this->resumptionSize = cfgline[0].getint();
196 }
197 // get and note a maintainer item to support the Identify Verb of OAI
198 else if (key == "maintainer" && cfgline.size() >= 1) {
199 int line = 0;
200
201 // TODO: exhaustive checks for empty or default values of maintainer
202 while (line < cfgline.size()) {
203 if (cfgline[line] != "NULL" &&
204 cfgline[line] != "") {
205 // do something
206 break;
207 }
208 else {
209 ++line;
210 }
211 }
212
213 // Only try to set the configuration if we have a legitimate value ...
214 if (line < cfgline.size()) {
215 // ensure we have a map to write to
216 if (this->collectMap[this->collection] == NULL) {
217 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
218 }
219 this->collectMap[this->collection]->maintainer = cfgline[line];
220 }
221 }
222 else if (key == "repositoryName" && cfgline.size() >= 1) {
223 int line = 0;
224
225 // TODO: exhaustive checks for empty or default values of repositoryName
226 while (line < cfgline.size()) {
227 if (cfgline[line] != "NULL" &&
228 cfgline[line] != "") {
229 // do something
230 break;
231 }
232 else {
233 ++line;
234 }
235 }
236
237 // Only try to set the configuration if we have a legitimate value ...
238 if (line < cfgline.size()) {
239 // ensure we have a map to write to
240 if (this->collectMap[this->collection] == NULL) {
241 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
242 }
243 this->collectMap[this->collection]->repositoryName = cfgline[line];
244 }
245 }
246 else if (key == "baseURL" && cfgline.size() >= 1) {
247 int line = 0;
248
249 while (line < cfgline.size()) {
250 if (cfgline[line] != "NULL" &&
251 cfgline[line] != "") {
252 // do something
253 break;
254 }
255 else {
256 ++line;
257 }
258 }
259
260 // Only try to set the configuration if we have a legitimate value ...
261 if (line < cfgline.size()) {
262 // ensure we have a map to write to
263 if (this->collectMap[this->collection] == NULL) {
264 this->collectMap[this->collection] = new oaicollectconfig(this->collection);
265 }
266 this->collectMap[this->collection]->baseURL = cfgline[line];
267 }
268 }
269}
270
271/**
272 * TODO: store all field values in a map per collection
273 */
274text_t oaiconfig::getCollectionConfig(const text_t &collection, const text_t &field)
275{
276 if (this->collectMap[collection] == NULL) {
277 return "";
278 }
279 if (field == "maintainer") {
280 return this->collectMap[collection]->maintainer;
281 }
282
283 if (field == "repositoryName") {
284 return this->collectMap[collection]->repositoryName;
285 }
286
287 if (field == "baseURL") {
288 return this->collectMap[collection]->baseURL;
289 }
290
291 return "";
292}
293
294text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield)
295{
296 if (this->collectMap[collection] == NULL) {
297 return "";
298 }
299 return this->collectMap[collection]->fieldMap[collectfield];
300}
301
302/**
303 * Get the mapping for a field in a given collection; if no mapping
304 * exists, the result will be a blank string.
305 */
306text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield, const text_t &formatname)
307{
308 text_t fullName = collectfield;
309 fullName.append(":");
310 fullName.append(formatname);
311
312 // try the collection-specific options first
313 if (this->collectMap[collection] != NULL) {
314 // first try the most specific item - this collection, and given that protocol
315 if (this->collectMap[collection]->fieldMap.count(fullName) >= 1) {
316 return this->collectMap[collection]->fieldMap[fullName];
317 }
318 // otherwise, fall back to this collection, and all protocols
319 else if (this->collectMap[collection]->fieldMap.count(collectfield) >= 1) {
320 return this->collectMap[collection]->fieldMap[collectfield];
321 }
322 }
323
324 // if no mappings exist, return an empty item
325 if (this->collectMap[""] == NULL) {
326 return "";
327 }
328
329 // then try generic rules
330 if (this->collectMap[""]->fieldMap.count(fullName) >= 1) {
331 return this->collectMap[""]->fieldMap[fullName];
332 }
333 else {
334 return this->collectMap[""]->fieldMap[collectfield];
335 }
336}
Note: See TracBrowser for help on using the repository browser.