root/main/trunk/greenstone2/runtime-src/src/oaiservr/oaiconfig.cpp @ 22739

Revision 22739, 12.3 KB (checked in by mdewsnip, 9 years ago)

Added copyright header to runtime-src/src/oaiserver/*.cpp and runtime-src/src/oaiserver/*.h.

  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 * oaiconfig.cpp --
4 *
5 * Copyright (C) 2004-2010  The New Zealand Digital Library Project
6 *
7 * A component of the Greenstone digital library software
8 * from the New Zealand Digital Library Project at the
9 * University of Waikato, New Zealand.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *********************************************************************/
26
27#include "oaiconfig.h"
28#include <iostream>
29#include <stdlib.h>
30#include "fileutil.h"
31
32
33/**
34 *  The mapping works as follows in the oai.cfg or collect.cfg file.
35 *
36 *  A line is in the format oaimapping <collection field> <oai field>
37 *
38 *  The map here is used to look up the "Greenstone" name which is mapped from
39 *  a given OAI field name, the reverse direction to that given in the
40 *  Greenstone collect.cfg file.  The oairecordaction class instance which
41 *  produces output for an OAI record information request thus uses the map
42 *  to work from the field in the collection it has on hand which OAI
43 *  record name it should use instead.
44 *
45 *  An extension is to be used for this in which the OAI field name in the
46 *  collect.cfg file can be made specific for a particular record format.
47 *  This is done using the OAI field name in the format of
48 *    <OAI format>.<OAI field name>
49 *  Thus, an rfc1807 Title field would be referred to as rfc1807.Title
50 *
51 *  A collection-level mapping can be provided in oai.cfg by prepending
52 *  collname to collection field:
53 *  <collname:field> <oai field>
54
55 *  In the absence of a particular format name, the mapping is taken to be
56 *  universal.
57 */
58
59oaiconfig::oaiconfig() : configurable () {
60  this->resumptionSize = -1; // Default = do not use resumption tokens
61}
62
63oaiconfig::oaiconfig(text_t &gsdlhome, text_t &gsdlcollect)
64{
65  // read main configuration file (oai.cfg) to get oai collections
66  text_t mainconfig = filename_cat(gsdlhome, "etc", "oai.cfg");
67  this->collection = "";
68  this->resumptionSize = -1;
69  this->read_configfile(mainconfig);
70
71  // then if we've not got a specified collection in the gsdlcollect
72  // parameter, read in all the collection's individual configurations
73  if (gsdlcollect == "") {
74    text_tarray::iterator start = this->collectList.begin();
75    text_tarray::iterator here  = this->collectList.end()-1;
76    while (here != start) {     
77      if (!this->configureCollection(gsdlhome, *here)) {
78    this->collectList.erase(here);
79      }
80      --here;
81    }
82    // and do the first one
83    if (!this->configureCollection(gsdlhome, *here)) {
84      this->collectList.erase(here);
85    }
86   
87  }
88  else {
89    // what do we do if this fails?
90    this->configureCollection(gsdlhome, gsdlcollect);
91  }
92}
93
94oaiconfig::~oaiconfig()
95{
96  oaicollectmap::iterator here = this->collectMap.begin();
97  oaicollectmap::iterator end  = this->collectMap.end();
98  while (here != end) {
99    delete here->second;
100    ++here;
101  }
102}
103
104int oaiconfig::resumeAfter()
105{ return this->resumptionSize;
106}
107
108int oaiconfig::getOAIVersion()
109{
110  if (this->oaiVersion == "1.1") {
111    return 110;
112  }
113  return 200;
114}
115
116bool oaiconfig::configureCollection(const text_t &gsdlhome, const text_t &gsdlcollect)
117{
118  text_t cnfgfile = filename_cat(gsdlhome, "collect", gsdlcollect, "etc", "collect.cfg");
119  if (!file_exists(cnfgfile)) {
120    return false;
121  }
122  this->collection = gsdlcollect;
123  this->read_configfile(cnfgfile);
124
125  return true;
126}
127void oaiconfig::configure (const text_t &key, const text_tarray &cfgline)
128{
129  // we've got an oai mapping item, and at least two fields
130  if (key == "oaimapping" && cfgline.size() > 1) {
131    text_t::const_iterator colonAt;
132    text_t index, name, configCollection;
133
134    // Take a default collection as being whatever the collection being configured is...
135    configCollection = this->collection;
136   
137    // get the name of the (collection) field to map; this may actually
138    // be in a colon separated format of the type
139    // <collection name>:<field name>
140    index = cfgline[0];
141    if ((colonAt = findchar(index.begin(), index.end(), ':')) != index.end()) {
142      configCollection = substr(index.begin(), colonAt);
143
144      if (this->collection != "" && configCollection != this->collection) {
145    cerr << "Attempt to configure OAI mappings for " << configCollection << " in " << this->collection << endl;
146      }
147
148      colonAt += 1;
149      index = substr(colonAt, index.end());
150    }
151   
152    // the second parameter is the metadata field to map the collection
153    // field onto.  It may be provided with a metadata protocol (which
154    // will be given first and separated by a period or full stop).  In
155    // the case of format.field name, the splitting is done here.
156    if ((colonAt = findchar(cfgline[1].begin(), cfgline[1].end(), '.')) != cfgline[1].end()) {
157      text_t stub = substr(cfgline[1].begin(), colonAt);
158      colonAt += 1;
159      name = substr(colonAt, cfgline[1].end());
160      index.append(":");
161      index.append(stub);
162    }
163    else {
164      name = cfgline[1];
165    }
166   
167    // now 'index' is in the form <collectionfield>:(formatname)
168    //     'name' is simply the fieldname within the format
169    //     'configCollection' is the collection to be configured
170   
171    // now simply map the field name (index) onto the collection name (name)
172    if (this->collectMap[configCollection] == NULL) {
173      this->collectMap[configCollection] = new oaicollectconfig(configCollection);
174    }
175    this->collectMap[configCollection]->fieldMap[index] = name;
176   
177    //    cerr << "Mapping " << index << " to " << name << " in " << configCollection << endl;
178   
179    // TODO: check that the mapped field is actually in use
180  }
181  else if (key == "oaicollection" && cfgline.size() >= 1) {
182    // Configure a collection to be used as part of the OAI archive.
183    // This line should read:
184    //
185    // oaicollection <collectionname>
186    //
187    // Where <collectionname> is the name of the directory inside the
188    // gsdl/collect folder which contains the collection.
189    //
190    // To configure several collections, merely repeat this line,
191    // or alternatively use additional collection names after the
192    // first one.
193    //
194    // This configuration should only appear in oai.cfg
195    //
196    if (this->collection != "") {
197      cerr << "Attempt to configure an oai collection outside of oai.cfg" << endl;
198      cerr << "Configuration attempted in " << this->collection << " collection." << endl;
199      exit(1);
200    }
201    for (int c = 0; c < cfgline.size(); ++c) {
202      this->collectList.push_back(cfgline[c]);
203    }
204  }
205  else if (key == "oaimetadata" && cfgline.size() >= 1) {
206    // List of metadata prefixes to suuport
207    // This line should read:
208    //
209    // oaicollection <metadataname> <metadataname>...
210    //
211    //
212    // This configuration should only appear in oai.cfg
213    //
214    if (this->collection != "") {
215      cerr << "Attempt to configure oai metadata outside of oai.cfg" << endl;
216      cerr << "Configuration attempted in " << this->collection << " collection." << endl;
217      exit(1);
218    }
219    for (int c = 0; c < cfgline.size(); ++c) {
220      // todo: check that the set name is valid
221      this->metadataSet.insert(cfgline[c]);
222    }
223  }
224  else if (key == "oaiinfo" && cfgline.size() >= 1) {
225    // Get a piece of information for the oai repository information
226    // request.  The line should read:
227    //
228    // oaiinfo <information field name> <value>
229    //
230    // This configuration should only be attempted in oai.cfg
231    //
232    if (this->collection != "") {
233      cerr << "Attempt to set oai information outside of oai.cfg" << endl;
234      cerr << "Configuration attempted in " << this->collection << " collection." << endl;
235      exit(1);
236    }
237   
238    // if no second parameter is given, then the first parameter
239    if (cfgline.size() == 1) {
240      this->infoMap[cfgline[0]] = cfgline[0];
241    }
242    else {
243      this->infoMap[cfgline[0]] = cfgline[1];
244    }
245  }
246  else if ( key == "oaisetname" || key == "oaisetdescription") {
247    text_t coll_name;
248    text_t value = "";
249    if (this->collection != "") {
250      // we are in collect.cfg
251      coll_name = this->collection;
252      if (cfgline.size() == 1) {
253    // just the collection value
254    value = cfgline[0];
255      }
256      else if (cfgline.size() == 2) {
257    // we have a subset name (eg for classifier)
258    coll_name.append(":");
259    coll_name.append(cfgline[0]);
260    value = cfgline[1];
261      }
262    } else if (cfgline.size() == 2) {
263      // oai.cfg, line should be collname, setName
264      coll_name = cfgline[0];
265      value = cfgline[1];
266    }
267    if (value != "") {
268      if (this->collectMap[coll_name] == NULL) {
269    this->collectMap[coll_name] = new oaicollectconfig(coll_name);
270      }
271      if (key == "oaisetname") {
272    this->collectMap[coll_name]->setName = value;
273      } else if (key == "oaisetdescription") {
274    this->collectMap[coll_name]->setDescription = value;
275      }
276    }
277  }
278
279  else if (key == "resumeafter" && cfgline.size() >= 1) {
280    this->resumptionSize = cfgline[0].getint();
281  }
282
283  else if (key == "maintainer") {
284    this->maintainer = cfgline[0];
285  }
286  else if (key == "repositoryName") {
287    this->repositoryName = cfgline[0];
288  }
289  else if (key == "repositoryId") {
290    this->repositoryId = cfgline[0];
291  }
292  else if (key == "repositoryIdVersion") {
293    this->repositoryIdVersion = cfgline[0];
294  }
295  else if (key == "baseURL") {
296    this->baseURL = cfgline[0];
297  }
298  else if (key == "baseLibraryURL") {
299    this->baseLibraryURL = cfgline[0];
300  }
301  else if (key == "baseDocRoot") {
302    this->baseDocRoot = cfgline[0];
303  }
304  else if (key == "oaiversion") {
305    this->oaiVersion = cfgline[0];
306  }
307 
308}
309
310text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield)
311{
312  if (this->collectMap[collection] == NULL) {
313    return "";
314  }
315  return this->collectMap[collection]->fieldMap[collectfield];
316}
317
318/**
319 *  Get the mapping for a field in a given collection; if no mapping
320 *  exists, the result will be a blank string.
321 */
322text_t oaiconfig::getMapping(const text_t &collection, const text_t &collectfield, const text_t &formatname)
323{
324  text_t fullName = collectfield;
325  fullName.append(":");
326  fullName.append(formatname);
327
328  // try the collection-specific options first
329  if (this->collectMap[collection] != NULL) {
330    // first try the most specific item - this collection, and given that protocol
331    if (this->collectMap[collection]->fieldMap.count(fullName) >= 1) {
332      return this->collectMap[collection]->fieldMap[fullName];
333    }
334    // otherwise, fall back to this collection, and all protocols
335    else if (this->collectMap[collection]->fieldMap.count(collectfield) >= 1) {
336      return this->collectMap[collection]->fieldMap[collectfield];
337    }
338  }
339
340  // if no mappings exist, return an empty item
341  if (this->collectMap[""] == NULL) {
342    return "";
343  }
344
345  // then try generic rules
346  if (this->collectMap[""]->fieldMap.count(fullName) >= 1) {
347    return this->collectMap[""]->fieldMap[fullName];
348  }
349  else {
350    return this->collectMap[""]->fieldMap[collectfield];
351  }
352}
353
354text_t oaiconfig::getBaseURL()
355{
356  return this->baseURL;
357}
358text_t oaiconfig::getBaseLibraryURL()
359{
360  return this->baseLibraryURL;
361}
362text_t oaiconfig::getBaseDocRoot()
363{
364  return this->baseDocRoot;
365}
366text_t oaiconfig::getRepositoryName()
367{
368  return this->repositoryName;
369}
370text_t oaiconfig::getRepositoryId()
371{
372  return this->repositoryId;
373}
374text_t oaiconfig::getRepositoryIdVersion()
375{
376  return this->repositoryIdVersion;
377}
378text_t oaiconfig::getMaintainer()
379{
380  return this->maintainer;
381}
382text_t oaiconfig::getSetName(const text_t &setSpec)
383{
384  if (this->collectMap[setSpec] == NULL) {
385    return "" ;
386  }
387 
388  return this->collectMap[setSpec]->setName;
389 
390}
391
392text_t oaiconfig::getSetDescription(const text_t &setSpec)
393{
394  if (this->collectMap[setSpec] == NULL) {
395    return "" ;
396  }
397 
398  return this->collectMap[setSpec]->setDescription;
399}
400
Note: See TracBrowser for help on using the browser.