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