1 |
|
---|
2 | /**********************************************************************
|
---|
3 | *
|
---|
4 | * collectserver.cpp --
|
---|
5 | * Copyright (C) 1999 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 "collectserver.h"
|
---|
28 | #include "infodbclass.h"
|
---|
29 | #include "OIDtools.h"
|
---|
30 | #include <assert.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | collectserver::collectserver () {
|
---|
34 | configinfo.collection = "null";
|
---|
35 | }
|
---|
36 |
|
---|
37 | collectserver::~collectserver () {
|
---|
38 |
|
---|
39 | // clean up the sources
|
---|
40 | sourcelistclass::iterator source_here = sources.begin();
|
---|
41 | sourcelistclass::iterator source_end = sources.end();
|
---|
42 | while (source_here != source_end) {
|
---|
43 | if ((*source_here).s != NULL)
|
---|
44 | delete (*source_here).s;
|
---|
45 | source_here++;
|
---|
46 | }
|
---|
47 | sources.clear();
|
---|
48 |
|
---|
49 | // clean up the filters
|
---|
50 | filtermapclass::iterator filter_here = filters.begin();
|
---|
51 | filtermapclass::iterator filter_end = filters.end();
|
---|
52 | while (filter_here != filter_end) {
|
---|
53 | if ((*filter_here).second.f != NULL)
|
---|
54 | delete (*filter_here).second.f;
|
---|
55 | filter_here++;
|
---|
56 | }
|
---|
57 | filters.clear();
|
---|
58 | }
|
---|
59 |
|
---|
60 | // configure should be called for each line in the
|
---|
61 | // configuration files to configure the collection server and everything
|
---|
62 | // it contains. The configuration should take place just before initialisation.
|
---|
63 | void collectserver::configure (const text_t &key, const text_tarray &cfgline) {
|
---|
64 | if (cfgline.size() >= 1) {
|
---|
65 | const text_t &value = cfgline[0];
|
---|
66 | if (key == "gsdlhome") configinfo.gsdlhome = value;
|
---|
67 | else if (key == "gdbmhome") configinfo.gdbmhome = value;
|
---|
68 | else if (key == "collection") {
|
---|
69 | configinfo.collection = value;
|
---|
70 | collectinfo.shortInfo.name = value;
|
---|
71 | } else if (key == "collectdir") configinfo.collectdir = value;
|
---|
72 | else if (key == "host") collectinfo.shortInfo.host = value;
|
---|
73 | else if (key == "port") collectinfo.shortInfo.port = value.getint();
|
---|
74 | else if (key == "public") {
|
---|
75 | if (value == "true") collectinfo.isPublic = true;
|
---|
76 | else collectinfo.isPublic = false;
|
---|
77 | } else if (key == "beta") {
|
---|
78 | if (value == "true") collectinfo.isBeta = true;
|
---|
79 | else collectinfo.isBeta = false;
|
---|
80 | } else if (key == "ccscols") collectinfo.ccsCols = cfgline;
|
---|
81 | else if (key == "builddate") collectinfo.buildDate = value.getint();
|
---|
82 | else if (key == "languages") collectinfo.languages = cfgline;
|
---|
83 | else if (key == "numdocs") collectinfo.numDocs = value.getint();
|
---|
84 | else if (key == "numsections") collectinfo.numSections = value.getint();
|
---|
85 | else if (key == "numwords") collectinfo.numWords = value.getint();
|
---|
86 | else if (key == "numbytes") collectinfo.numBytes = value.getint();
|
---|
87 | else if (key == "collectionmeta" && cfgline.size() == 2)
|
---|
88 | collectinfo.collectionmeta[cfgline[0]] = cfgline[1];
|
---|
89 | else if (key == "format" && cfgline.size() == 2)
|
---|
90 | collectinfo.format[cfgline[0]] = cfgline[1];
|
---|
91 | else if (key == "building" && cfgline.size() == 2)
|
---|
92 | collectinfo.building[cfgline[0]] = cfgline[1];
|
---|
93 | else if (key == "receptionist") collectinfo.receptionist = value;
|
---|
94 | else if (key == "buildtype") collectinfo.buildType = value;
|
---|
95 | }
|
---|
96 |
|
---|
97 | // configure the filters
|
---|
98 | filtermapclass::iterator filter_here = filters.begin();
|
---|
99 | filtermapclass::iterator filter_end = filters.end();
|
---|
100 | while (filter_here != filter_end) {
|
---|
101 | assert ((*filter_here).second.f != NULL);
|
---|
102 | if ((*filter_here).second.f != NULL)
|
---|
103 | (*filter_here).second.f->configure(key, cfgline);
|
---|
104 |
|
---|
105 | filter_here++;
|
---|
106 | }
|
---|
107 |
|
---|
108 | // configure the sources
|
---|
109 | sourcelistclass::iterator source_here = sources.begin();
|
---|
110 | sourcelistclass::iterator source_end = sources.end();
|
---|
111 | while (source_here != source_end) {
|
---|
112 | assert ((*source_here).s != NULL);
|
---|
113 | if ((*source_here).s != NULL)
|
---|
114 | (*source_here).s->configure(key, cfgline);
|
---|
115 |
|
---|
116 | source_here++;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | void collectserver::configure (const text_t &key, const text_t &value) {
|
---|
121 | text_tarray cfgline;
|
---|
122 | cfgline.push_back (value);
|
---|
123 | configure(key, cfgline);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | bool collectserver::init (ostream &logout) {
|
---|
128 | // init the filters
|
---|
129 | filtermapclass::iterator filter_here = filters.begin();
|
---|
130 | filtermapclass::iterator filter_end = filters.end();
|
---|
131 | while (filter_here != filter_end) {
|
---|
132 | assert ((*filter_here).second.f != NULL);
|
---|
133 | if (((*filter_here).second.f != NULL) &&
|
---|
134 | !(*filter_here).second.f->init(logout)) return false;
|
---|
135 |
|
---|
136 | filter_here++;
|
---|
137 | }
|
---|
138 |
|
---|
139 | // init the sources
|
---|
140 | sourcelistclass::iterator source_here = sources.begin();
|
---|
141 | sourcelistclass::iterator source_end = sources.end();
|
---|
142 | while (source_here != source_end) {
|
---|
143 | assert ((*source_here).s != NULL);
|
---|
144 | if (((*source_here).s != NULL) &&
|
---|
145 | !(*source_here).s->init(logout)) return false;
|
---|
146 |
|
---|
147 | source_here++;
|
---|
148 | }
|
---|
149 |
|
---|
150 | return true;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | void collectserver::get_collectinfo (ColInfoResponse_t &reponse,
|
---|
155 | comerror_t &err, ostream &/*logout*/) {
|
---|
156 | reponse = collectinfo;
|
---|
157 | err = noError;
|
---|
158 | }
|
---|
159 |
|
---|
160 | void collectserver::get_filterinfo (InfoFiltersResponse_t &response,
|
---|
161 | comerror_t &err, ostream &/*logout*/) {
|
---|
162 | response.clear ();
|
---|
163 |
|
---|
164 | // get a list of filter names
|
---|
165 | filtermapclass::iterator filter_here = filters.begin();
|
---|
166 | filtermapclass::iterator filter_end = filters.end();
|
---|
167 | while (filter_here != filter_end) {
|
---|
168 | response.filterNames.insert ((*filter_here).first);
|
---|
169 | filter_here++;
|
---|
170 | }
|
---|
171 |
|
---|
172 | err = noError;
|
---|
173 | }
|
---|
174 |
|
---|
175 | void collectserver::get_filteroptions (const InfoFilterOptionsRequest_t &request,
|
---|
176 | InfoFilterOptionsResponse_t &response,
|
---|
177 | comerror_t &err, ostream &logout) {
|
---|
178 | outconvertclass text_t2ascii;
|
---|
179 |
|
---|
180 | filterclass *thisfilter = filters.getfilter(request.filterName);
|
---|
181 | if (thisfilter != NULL) {
|
---|
182 | thisfilter->get_filteroptions (response, err, logout);
|
---|
183 | } else {
|
---|
184 | response.clear ();
|
---|
185 | err = protocolError;
|
---|
186 | logout << text_t2ascii << "Protocol Error: filter options requested for non-existent\n"
|
---|
187 | << "filter \"" << request.filterName << "\".\n\n";
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | void collectserver::filter (FilterRequest_t &request,
|
---|
192 | FilterResponse_t &response,
|
---|
193 | comerror_t &err, ostream &logout) {
|
---|
194 | outconvertclass text_t2ascii;
|
---|
195 |
|
---|
196 | // translate any ".fc", ".pr" etc. stuff in the docSet
|
---|
197 | text_t translatedOID;
|
---|
198 | text_tarray translatedOIDs;
|
---|
199 | text_tarray::iterator doc_here = request.docSet.begin();
|
---|
200 | text_tarray::iterator doc_end = request.docSet.end();
|
---|
201 | while (doc_here != doc_end) {
|
---|
202 | if (needs_translating (*doc_here)) {
|
---|
203 | sourcelistclass::iterator source_here = sources.begin();
|
---|
204 | sourcelistclass::iterator source_end = sources.end();
|
---|
205 | while (source_here != source_end) {
|
---|
206 | assert ((*source_here).s != NULL);
|
---|
207 | if (((*source_here).s != NULL) &&
|
---|
208 | ((*source_here).s->translate_OID (*doc_here, translatedOID, err, logout))) {
|
---|
209 | if (err != noError) return;
|
---|
210 | break;
|
---|
211 | }
|
---|
212 | source_here++;
|
---|
213 | }
|
---|
214 | translatedOIDs.push_back (translatedOID);
|
---|
215 | } else {
|
---|
216 | translatedOIDs.push_back (*doc_here);
|
---|
217 | }
|
---|
218 | doc_here ++;
|
---|
219 | }
|
---|
220 | request.docSet = translatedOIDs;
|
---|
221 |
|
---|
222 | response.clear();
|
---|
223 |
|
---|
224 | filterclass *thisfilter = filters.getfilter(request.filterName);
|
---|
225 | if (thisfilter != NULL) {
|
---|
226 | // filter the data
|
---|
227 | thisfilter->filter (request, response, err, logout);
|
---|
228 |
|
---|
229 | // fill in the metadata for each of the OIDs (if it is requested)
|
---|
230 | if (request.filterResultOptions & FRmetadata) {
|
---|
231 | bool processed = false;
|
---|
232 | ResultDocInfo_tarray::iterator resultdoc_here = response.docInfo.begin();
|
---|
233 | ResultDocInfo_tarray::iterator resultdoc_end = response.docInfo.end();
|
---|
234 | while (resultdoc_here != resultdoc_end) {
|
---|
235 | // try each of the sources in turn
|
---|
236 | sourcelistclass::iterator source_here = sources.begin();
|
---|
237 | sourcelistclass::iterator source_end = sources.end();
|
---|
238 | while (source_here != source_end) {
|
---|
239 | assert ((*source_here).s != NULL);
|
---|
240 | if (((*source_here).s != NULL) &&
|
---|
241 | ((*source_here).s->get_metadata(request.requestParams, request.refParams,
|
---|
242 | request.getParents, request.fields,
|
---|
243 | (*resultdoc_here).OID, (*resultdoc_here).metadata,
|
---|
244 | err, logout))) {
|
---|
245 | if (err != noError) return;
|
---|
246 | processed = true;
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | source_here++;
|
---|
250 | }
|
---|
251 | if (!processed) {
|
---|
252 | err = protocolError;
|
---|
253 | return;
|
---|
254 | }
|
---|
255 | resultdoc_here++;
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | } else {
|
---|
260 | response.clear ();
|
---|
261 | err = protocolError;
|
---|
262 | logout << text_t2ascii << "Protocol Error: filter options requested for non-existent\n"
|
---|
263 | << "filter \"" << request.filterName << "\".\n\n";
|
---|
264 | }
|
---|
265 |
|
---|
266 | err = noError;
|
---|
267 | }
|
---|
268 |
|
---|
269 | void collectserver::get_document (const DocumentRequest_t &request,
|
---|
270 | DocumentResponse_t &response,
|
---|
271 | comerror_t &err, ostream &logout) {
|
---|
272 |
|
---|
273 | sourcelistclass::iterator source_here = sources.begin();
|
---|
274 | sourcelistclass::iterator source_end = sources.end();
|
---|
275 | while (source_here != source_end) {
|
---|
276 | assert ((*source_here).s != NULL);
|
---|
277 | if (((*source_here).s != NULL) &&
|
---|
278 | ((*source_here).s->get_document (request.OID, response.doc, err, logout))) {
|
---|
279 | if (err != noError) return;
|
---|
280 | break;
|
---|
281 | }
|
---|
282 | source_here++;
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | bool operator==(const collectserverptr &x, const collectserverptr &y) {
|
---|
288 | return (x.c == y.c);
|
---|
289 | }
|
---|
290 |
|
---|
291 | bool operator<(const collectserverptr &x, const collectserverptr &y) {
|
---|
292 | return (x.c < y.c);
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | // thecollectserver remains the property of the calling code but
|
---|
297 | // should not be deleted until it is removed from this list.
|
---|
298 | void collectservermapclass::addcollectserver (collectserver *thecollectserver) {
|
---|
299 | // can't add a null collection server
|
---|
300 | assert (thecollectserver != NULL);
|
---|
301 | if (thecollectserver == NULL) return;
|
---|
302 |
|
---|
303 | // can't add an collection server with no collection name
|
---|
304 | assert (!(thecollectserver->get_collection_name()).empty());
|
---|
305 | if ((thecollectserver->get_collection_name()).empty()) return;
|
---|
306 |
|
---|
307 | collectserverptr cptr;
|
---|
308 | cptr.c = thecollectserver;
|
---|
309 | collectserverptrs[thecollectserver->get_collection_name()] = cptr;
|
---|
310 | }
|
---|
311 |
|
---|
312 | // getcollectserver will return NULL if the collectserver could not be found
|
---|
313 | collectserver *collectservermapclass::getcollectserver (const text_t &collection) {
|
---|
314 | // can't find a collection with no name
|
---|
315 | if (collection.empty()) return NULL;
|
---|
316 |
|
---|
317 | iterator here = collectserverptrs.find (collection);
|
---|
318 | if (here == collectserverptrs.end()) return NULL;
|
---|
319 |
|
---|
320 | return (*here).second.c;
|
---|
321 | }
|
---|