root/gsdl/trunk/runtime-src/src/oaiservr/oaiconfig.cpp @ 18885

Revision 18885, 11.2 KB (checked in by kjdon, 11 years ago)

changed some comments

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