source: trunk/gsdl/src/oaiservr/oaiconfig.cpp@ 8311

Last change on this file since 8311 was 8311, checked in by kjdon, 20 years ago

oai config now resides in oai.cfg, not main.cfg

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