source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/OAIPMH.java@ 31230

Last change on this file since 31230 was 31230, checked in by ak19, 7 years ago

Commit for GS3 server side part of OAI deletion police implementation. Still to implement the GS2 server side part. The earlier commits implemented the PERL side, the oai-inf db implementation. I think I've now got the GS3 server side working, but have yet to try validating against the OAI validator. (I need to test that on a machine that is publicly accessible).

File size: 34.1 KB
Line 
1/*
2 * OAIPMH.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.service;
20
21// Greenstone classes
22import org.greenstone.gsdl3.core.GSException;
23import org.greenstone.gsdl3.util.GSXML;
24import org.greenstone.gsdl3.util.OAIXML;
25import org.greenstone.gsdl3.util.OID;
26import org.greenstone.gsdl3.util.GSFile;
27import org.greenstone.gsdl3.util.XMLConverter;
28
29import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
30import org.greenstone.gsdl3.util.DBInfo;
31// XML classes
32import org.w3c.dom.Document;
33import org.w3c.dom.Element;
34import org.w3c.dom.NodeList;
35
36// General Java classes
37import java.io.File;
38import java.util.StringTokenizer;
39import java.util.Vector;
40import java.util.Set;
41import java.util.Iterator;
42import java.util.ArrayList;
43import java.util.Date;
44import java.util.HashMap;
45import java.util.HashSet;
46import java.util.Map.Entry;
47
48import org.apache.log4j.Logger;
49
50/** Implements the oai metadata retrieval service for GS3 collections.
51 * Dig into each collection's database and retrieve the metadata
52 *
53 */
54
55public class OAIPMH extends ServiceRack {
56
57 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.OAIPMH.class.getName());
58
59 protected SimpleCollectionDatabase coll_db = null;
60 protected SimpleCollectionDatabase oaiinf_db = null;
61
62 protected String site_name = "";
63 protected String coll_name = "";
64
65 // set this up during configure
66 protected Element list_sets_response = null;
67
68 protected Element meta_formats_definition = null;
69 protected HashMap<String, HashSet<String>> format_elements_map = null;
70 protected HashMap<String, Element> format_response_map = null;
71 /** constructor */
72 public OAIPMH() {
73
74 }
75
76 public void cleanUp() {
77 super.cleanUp();//??
78 this.coll_db.closeDatabase();
79 this.oaiinf_db.closeDatabase();
80 }
81 /** configure this service
82 info is the OAIPMH service rack from collectionConfig.xml, and
83 extra_info is buildConfig.xml */
84 public boolean configure(Element info, Element extra_info) {
85 if (!super.configure(info, extra_info)){
86 logger.info("Configuring ServiceRack.java returns false.");
87 return false;
88 }
89
90 //get the names from ServiceRack.java
91 this.site_name = this.router.getSiteName();
92 this.coll_name = this.cluster_name;
93
94 logger.info("Configuring OAIPMH...");
95
96 this.config_info = info;
97
98 // the index stem is either specified in the buildConfig.xml file (extra_info) or uses the collection name
99 Element metadata_list = (Element) GSXML.getChildByTagName(extra_info, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
100 String index_stem = "";
101 String infodb_type = "";
102 if (metadata_list != null) {
103
104 Element index_stem_elem = (Element) GSXML.getNamedElement(metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "indexStem");
105
106 if (index_stem_elem != null) {
107 index_stem = GSXML.getNodeText(index_stem_elem);
108 }
109
110 Element infodb_type_elem = (Element) GSXML.getNamedElement(metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "infodbType");
111 if (infodb_type_elem != null) {
112 infodb_type = GSXML.getNodeText(infodb_type_elem);
113 }
114
115 }
116
117 if (index_stem == null || index_stem.equals("")) {
118 index_stem = this.cluster_name; // index_stem is the name of the db in indext/text, it is <colname>.<db>
119 }
120 if (infodb_type == null || infodb_type.equals("")) {
121 infodb_type = "gdbm"; // the default
122 }
123
124 coll_db = new SimpleCollectionDatabase(infodb_type);
125 if (!coll_db.databaseOK()) {
126 logger.error("Couldn't create the collection database of type "+infodb_type);
127 return false;
128 }
129
130 oaiinf_db = new SimpleCollectionDatabase(infodb_type);
131 if (!oaiinf_db.databaseOK()) {
132 logger.error("Couldn't create the oai-inf database of type "+infodb_type);
133 oaiinf_db = null;
134 return false;
135 }
136
137
138 // Open databases for querying
139 String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, index_stem, infodb_type);
140 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) {
141 logger.error("Could not open collection database!");
142 return false;
143 }
144 // the oaiinf_db is called oai-inf.<infodb_type_extension>
145 String oaiinf_db_file = GSFile.OAIInfoDatabaseFile(this.site_home, this.cluster_name, "oai-inf", infodb_type);
146 if (oaiinf_db != null && !this.oaiinf_db.openDatabase(oaiinf_db_file, SimpleCollectionDatabase.READ)) {
147 logger.warn("Could not open oai-inf database for collection + " + this.cluster_name + "!");
148 }
149
150 // work out what sets this collection has. Will usually contain the collection itself, optional super collection, and maybe subcolls if appropriate classifiers are present.
151 configureSetInfo();
152 // the short_service_info is used by the message router to find the method names,
153
154 Element list_records = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
155 list_records.setAttribute(GSXML.NAME_ATT, OAIXML.LIST_RECORDS);
156 list_records.setAttribute(GSXML.TYPE_ATT, "oai");
157 this.short_service_info.appendChild(list_records);
158
159 Element list_identifiers = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
160 list_identifiers.setAttribute(GSXML.NAME_ATT, OAIXML.LIST_IDENTIFIERS);
161 list_identifiers.setAttribute(GSXML.TYPE_ATT, "oai");
162 this.short_service_info.appendChild(list_identifiers);
163
164 Element list_sets = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
165 list_sets.setAttribute(GSXML.NAME_ATT, OAIXML.LIST_SETS);
166 list_sets.setAttribute(GSXML.TYPE_ATT, "oai");
167 this.short_service_info.appendChild(list_sets);
168
169 Element list_metadata_formats = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
170 list_metadata_formats.setAttribute(GSXML.NAME_ATT, OAIXML.LIST_METADATA_FORMATS);
171 list_metadata_formats.setAttribute(GSXML.TYPE_ATT, "oai");
172 this.short_service_info.appendChild(list_metadata_formats);
173
174 Element get_record = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
175 get_record.setAttribute(GSXML.NAME_ATT, OAIXML.GET_RECORD);
176 get_record.setAttribute(GSXML.TYPE_ATT, "oai");
177 this.short_service_info.appendChild(get_record);
178
179 return true;
180 }
181
182 public boolean configureOAI(Element oai_config_elem) {
183 this.meta_formats_definition = this.desc_doc.createElement(OAIXML.LIST_METADATA_FORMATS);
184 this.format_response_map = new HashMap<String, Element>();
185 this.format_elements_map = new HashMap<String, HashSet<String>>();
186
187 // for now, all we want is the metadata prefix description and the mapping list
188 Element main_lmf_elem = (Element) GSXML.getChildByTagName(oai_config_elem, OAIXML.LIST_METADATA_FORMATS);
189 if (main_lmf_elem == null) {
190 logger.error("No listMetadataFormats element found in OAIConfig.xml");
191 return false;
192 }
193 NodeList meta_formats_list = this.config_info.getElementsByTagName(OAIXML.METADATA_FORMAT);
194 if (meta_formats_list.getLength() == 0) {
195 logger.error("no metadataFormat elements found in OAIPMH serviceRack element");
196 return false;
197 }
198 boolean found_meta_format = false;
199 for(int i=0; i<meta_formats_list.getLength(); i++) {
200 Element mf = (Element) meta_formats_list.item(i);
201 String prefix = mf.getAttribute(OAIXML.METADATA_PREFIX);
202 if (prefix.equals("")) {
203 logger.error("metadataFormat element had no metadataPrefix attribute");
204 continue;
205 }
206 // get the right format from OAICOnfig
207 Element meta_format = findNamedMetadataFormat(main_lmf_elem, prefix);
208 if (meta_format == null) {
209 logger.error("Couldn't find metadataFormat named "+prefix+" in OAIConfig.xml");
210 continue;
211 }
212 // copy the format definition into our stored Element
213 Element collection_version_format = (Element) this.desc_doc.importNode(meta_format, true);
214 collection_version_format.setAttribute(GSXML.NAME_ATT, prefix); // for convenience
215 this.meta_formats_definition.appendChild(collection_version_format);
216 // set up the response element for this format
217 format_response_map.put(prefix, OAIXML.getMetadataFormatShort(this.desc_doc, collection_version_format));
218 // add in collection specific mappings
219 addCollectionMappings(collection_version_format, mf);
220 // now set up a list of all collection elements for reverse lookup of the mapping
221 format_elements_map.put(prefix, getAllCollectionElements(collection_version_format));
222
223 }
224 return true;
225 }
226
227 protected Element findNamedMetadataFormat(Element list_meta_formats, String prefix) {
228 NodeList formats = list_meta_formats.getElementsByTagName(OAIXML.METADATA_FORMAT);
229 for (int i=0; i<formats.getLength(); i++) {
230 Element format = (Element)formats.item(i);
231 String meta_name = GSXML.getNodeText((Element)GSXML.getChildByTagName(format, OAIXML.METADATA_PREFIX));
232 if (prefix.equals(meta_name)) {
233 return format;
234 }
235 }
236 return null;
237 }
238
239 /** goes through the mappings from the collection one, and replaces existing ones in the main one */
240 protected void addCollectionMappings(Element main_meta_format, Element coll_meta_format) {
241
242 Element element_list = (Element)GSXML.getChildByTagName(main_meta_format, OAIXML.ELEMENT+GSXML.LIST_MODIFIER);
243 Document doc = element_list.getOwnerDocument();
244 NodeList coll_elements = coll_meta_format.getElementsByTagName(OAIXML.ELEMENT);
245 if (coll_elements.getLength()==0) {
246 // no mappings to include
247 return;
248 }
249 for (int i=0; i<coll_elements.getLength(); i++) {
250 Element e = (Element)coll_elements.item(i);
251 String elem_name = e.getAttribute(GSXML.NAME_ATT);
252 Element main_elem = GSXML.getNamedElement(element_list, OAIXML.ELEMENT, GSXML.NAME_ATT, elem_name);
253 if (main_elem == null) {
254 logger.error(elem_name+" not found in meta format, not using it");
255 } else {
256 element_list.replaceChild(doc.importNode(e, true),main_elem );
257 }
258 }
259 }
260
261 /** goes through all the mappings and makes a set of all collection
262 metadata names that could become an oai meta element - acts as
263 a reverse lookup for the mappings */
264 protected HashSet<String> getAllCollectionElements(Element meta_format) {
265 HashSet<String> meta_name_set = new HashSet<String>();
266 NodeList elements = meta_format.getElementsByTagName(OAIXML.ELEMENT);
267 for (int i=0; i<elements.getLength(); i++) {
268 Element e = (Element)elements.item(i);
269 Element map = (Element)GSXML.getChildByTagName(e, OAIXML.MAPPING);
270 if (map == null) {
271 // there is no mapping, just use the element name
272 meta_name_set.add(e.getAttribute(GSXML.NAME_ATT));
273 } else {
274 String list_of_names = map.getAttribute(OAIXML.ELEMENTS);
275 String[] name_array = list_of_names.split(",");
276 for (int j=0; j<name_array.length; j++) {
277 meta_name_set.add(name_array[j]);
278 }
279 }
280 }
281 return meta_name_set;
282 }
283
284 /** returns a specific service description */
285 public Element getServiceDescription(Document doc, String service_id, String lang, String subset) {
286
287 if (service_id.equals(OAIXML.LIST_RECORDS)) {
288 Element list_records = doc.createElement(GSXML.SERVICE_ELEM);
289 list_records.setAttribute(GSXML.NAME_ATT, OAIXML.LIST_RECORDS);
290 list_records.setAttribute(GSXML.TYPE_ATT, "oai");
291 return list_records;
292 }
293
294 if (service_id.equals(OAIXML.LIST_IDENTIFIERS)) {
295 Element list_identifiers = doc.createElement(GSXML.SERVICE_ELEM);
296 list_identifiers.setAttribute(GSXML.NAME_ATT, OAIXML.LIST_IDENTIFIERS);
297 list_identifiers.setAttribute(GSXML.TYPE_ATT, "oai");
298 return list_identifiers;
299 }
300 if (service_id.equals(OAIXML.LIST_SETS)) {
301 Element list_sets = doc.createElement(GSXML.SERVICE_ELEM);
302 list_sets.setAttribute(GSXML.NAME_ATT, OAIXML.LIST_SETS);
303 list_sets.setAttribute(GSXML.TYPE_ATT, "oai");
304 return list_sets;
305 }
306 if (service_id.equals(OAIXML.LIST_METADATA_FORMATS)) {
307 Element list_metadata_formats = doc.createElement(GSXML.SERVICE_ELEM);
308 list_metadata_formats.setAttribute(GSXML.NAME_ATT, OAIXML.LIST_METADATA_FORMATS);
309 list_metadata_formats.setAttribute(GSXML.TYPE_ATT, "oai");
310 return list_metadata_formats;
311 }
312
313 if (service_id.equals(OAIXML.GET_RECORD)) {
314 Element get_record = doc.createElement(GSXML.SERVICE_ELEM);
315 get_record.setAttribute(GSXML.NAME_ATT, OAIXML.GET_RECORD);
316 get_record.setAttribute(GSXML.TYPE_ATT, "oai");
317 return get_record;
318 }
319
320 return null;
321 }
322
323 /** The list sets service returns all the sets that this collection is/is part of/contains. This is gathered by Receptionist from all collections to answer the OAI ListSets request. */
324 protected Element processListSets(Element req) {
325 return list_sets_response;
326 }
327 /** returns the actual record element used in the OAI GetRecord response */
328 protected Element processGetRecord(Element req) {
329 /** arguments:
330 identifier: required
331 metadataPrefix: required
332 * Exceptions: badArgument; cannotDisseminateFormat; idDoesNotExist
333 */
334 NodeList params = GSXML.getChildrenByTagName(req, GSXML.PARAM_ELEM);
335 HashMap<String, String> param_map = GSXML.getParamMap(params);
336
337 String prefix = param_map.get(OAIXML.METADATA_PREFIX);
338 if (prefix == null || prefix.equals("")) {
339 //Just a double-check
340 logger.error("the value of metadataPrefix att is not present in the request.");
341 return OAIXML.createErrorResponse(OAIXML.CANNOT_DISSEMINATE_FORMAT, "");
342 }
343
344 // check that we support this format
345 if (!format_response_map.containsKey(prefix)) {
346 logger.error("metadata prefix is not supported for collection "+this.coll_name);
347 return OAIXML.createErrorResponse(OAIXML.CANNOT_DISSEMINATE_FORMAT, "");
348 }
349
350 Document doc = XMLConverter.newDOM();
351
352 String oid = param_map.get(OAIXML.OID); // TODO should this be identifier???
353 boolean OID_is_deleted = false;
354 long millis = -1;
355
356 DBInfo oai_info = null;
357 if(oaiinf_db != null) {
358 oai_info = this.oaiinf_db.getInfo(oid);
359 if (oai_info == null) {
360 logger.warn("OID: " + oid + " is not present in the collection's oai-inf database.");
361 } else {
362 String oaiinf_status = oai_info.getInfo(OAIXML.OAI_INF_STATUS);
363 if(oaiinf_status != null && oaiinf_status.equals(OAIXML.OAI_INF_DELETED)) {
364 OID_is_deleted = true;
365
366 // get the right timestamp for deletion: from oaiinf db
367 String timestamp = oai_info.getInfo(OAIXML.OAI_INF_TIMESTAMP); // in seconds presumably, like oailastmodified in the collection index db
368
369 millis = Long.parseLong(timestamp)*1000; // in milliseconds
370 }
371 }
372 }
373
374 //get a DBInfo object of the identifier; if this identifier is not present in the database,
375 // null is returned.
376 DBInfo info = this.coll_db.getInfo(oid);
377 if (info == null) {
378 logger.error("OID: " + oid + " is not present in the collection database.");
379 //return OAIXML.createErrorResponse(OAIXML.ID_DOES_NOT_EXIST, ""); // may exist as deleted in oai-inf db
380 }
381 else if (millis == -1) { // so !OID_is_deleted, get oailastmodified from collection's index db
382 ArrayList<String> keys = new ArrayList<String>(info.getKeys());
383 millis = getDateStampMillis(info);
384 }
385 String oailastmodified = (millis == -1) ? "" : OAIXML.getTime(millis);
386
387
388 Element get_record_response = doc.createElement(GSXML.RESPONSE_ELEM);
389 Element get_record = doc.createElement(OAIXML.GET_RECORD);
390 get_record_response.appendChild(get_record);
391 Element record = doc.createElement(OAIXML.RECORD);
392 //compose the header element
393 record.appendChild(createHeaderElement(doc, oid, oailastmodified, OID_is_deleted));
394 if(!OID_is_deleted) {
395 //compose the metadata element
396 record.appendChild(createMetadataElement(doc, prefix, info));
397 }
398 get_record.appendChild(record);
399 return get_record_response;
400 }
401
402 /** return a list of records in specified set, containing metadata from specified prefix*/
403 protected Element processListRecords(Element req) {
404 return processListIdentifiersOrRecords(req, OAIXML.LIST_RECORDS, true);
405 }
406
407 /** return a list of identifiers in specified set that contain metadata belonging to specified prefix. */
408 protected Element processListIdentifiers(Element req) {
409 return processListIdentifiersOrRecords(req, OAIXML.LIST_IDENTIFIERS, false);
410 }
411
412 // Get a list of records/identifiers that match the parameters.
413 protected Element processListIdentifiersOrRecords(Element req, String response_name, boolean include_metadata) {
414 /** arguments:
415 metadataPrefix: required
416 * from: optional
417 * until: optional
418 * set: optional
419 * resumptionToken: exclusive and optional (ignored as it has been handled by OAIReceptionist)
420 * Exceptions: badArgument; cannotDisseminateFormat; idDoesNotExist
421 */
422 NodeList params = GSXML.getChildrenByTagName(req, GSXML.PARAM_ELEM);
423
424 if(params.getLength() == 0) {
425 logger.error("must at least have the metadataPrefix parameter, can't be none");
426 return OAIXML.createErrorResponse(OAIXML.BAD_ARGUMENT, "");
427 }
428
429 HashMap<String, String> param_map = GSXML.getParamMap(params);
430
431 String prefix = "";
432 Date from_date = null;
433 Date until_date = null;
434
435 if(param_map.containsKey(OAIXML.METADATA_PREFIX) == false) {
436 //Just a double-check
437 logger.error("A param element containing the metadataPrefix is not present.");
438 return OAIXML.createErrorResponse(OAIXML.CANNOT_DISSEMINATE_FORMAT, "");
439 }
440 prefix = param_map.get(OAIXML.METADATA_PREFIX);
441 if (prefix == null || prefix.equals("")) {
442 //Just a double-check
443 logger.error("the value of metadataPrefix att is not present in the request.");
444 return OAIXML.createErrorResponse(OAIXML.CANNOT_DISSEMINATE_FORMAT, "");
445 }
446
447 if(param_map.containsKey(OAIXML.FROM)) {
448 String from = param_map.get(OAIXML.FROM);
449 from_date = OAIXML.getDate(from);
450 }
451 if(param_map.containsKey(OAIXML.UNTIL)) {
452 String until = param_map.get(OAIXML.UNTIL);
453 until_date = OAIXML.getDate(until);
454 }
455
456 if (!format_response_map.containsKey(prefix)) {
457 logger.error(prefix + " metadata prefix is not supported for collection "+this.coll_name);
458 return OAIXML.createErrorResponse(OAIXML.CANNOT_DISSEMINATE_FORMAT, "");
459 }
460
461 // get list of oids
462 ArrayList<String> oid_list = null;
463 if(oaiinf_db != null) { // try getting the OIDs from the oaiinf_db
464 oid_list = new ArrayList<String>(oaiinf_db.getAllKeys());
465
466 if(oid_list == null) { // try getting the OIDs from the oai entries in the index db
467 logger.warn("@@@@@@@@@@@@@ NO OIDs in oai-inf db for " + this.cluster_name);
468 oid_list = getChildrenIds(OAIXML.BROWSELIST);
469 }
470 }
471
472 if (oid_list == null) {
473 logger.error("No matched records found in collection: oai-inf and index db's browselist are empty");
474 return OAIXML.createErrorResponse(OAIXML.NO_RECORDS_MATCH, "");
475 }
476 // all validation is done
477
478 // get the list of elements that are in this metadata prefix
479 HashSet<String> set_of_elems = format_elements_map.get(prefix);
480
481 Document doc = XMLConverter.newDOM();
482 Element list_items_response = doc.createElement(GSXML.RESPONSE_ELEM);
483 Element list_items = doc.createElement(response_name);
484 list_items_response.appendChild(list_items);
485
486 for(int i=0; i<oid_list.size(); i++) {
487 String oid = oid_list.get(i);
488 boolean OID_is_deleted = false;
489 long millis = -1;
490
491 DBInfo oai_info = null;
492 if(oaiinf_db != null) {
493 oai_info = this.oaiinf_db.getInfo(oid);
494 if (oai_info == null) {
495 logger.warn("OID: " + oid + " is not present in the collection's oai-inf database.");
496 } else {
497 String oaiinf_status = oai_info.getInfo(OAIXML.OAI_INF_STATUS);
498 if(oaiinf_status != null && oaiinf_status.equals(OAIXML.OAI_INF_DELETED)) {
499 OID_is_deleted = true;
500
501 // get the right timestamp for deletion: from oaiinf db
502 String timestamp = oai_info.getInfo(OAIXML.OAI_INF_TIMESTAMP); // in seconds presumably, like oailastmodified in the collection index db
503
504 millis = Long.parseLong(timestamp)*1000; // in milliseconds
505 }
506 }
507 }
508 DBInfo info = this.coll_db.getInfo(oid);
509 if (info == null) { // can happen if oid was deleted, in which case only oai_info keeps a record of it
510 logger.error("Collection database does not contain information about oid: " +oid);
511 }
512 else if (millis == -1) { // so !OID_is_deleted, get oailastmodified from collection's index db
513
514 millis = getDateStampMillis(info);
515 }
516
517 Date this_date = null;
518 if (millis == -1) {
519 if (from_date != null || until_date !=null) {
520 continue; // if this doc doesn't have a date for some reason, and
521 // we are doing a date range, then don't include it.
522 }
523 } else {
524 this_date = new Date(millis);
525 if (from_date != null) {
526 if(this_date.before(from_date)) {
527 continue;
528 }
529 }
530 if (until_date != null) {
531 if (this_date.after(until_date)) {
532 continue;
533 }
534 }
535 }
536
537
538 // compose a record for adding header and metadata
539 Element record = doc.createElement(OAIXML.RECORD);
540 list_items.appendChild(record);
541 //compose the header element
542 record.appendChild(createHeaderElement(doc, oid, OAIXML.getTime(millis), OID_is_deleted));
543
544
545 //Now check that this id has metadata for the required prefix.
546 if (info != null && documentContainsMetadata(info, set_of_elems)) {
547 // YES, it does have some metadata for this prefix
548
549 if (include_metadata) {
550 //compose the metadata element
551 record.appendChild(createMetadataElement(doc, prefix, info));
552 } /*else {
553 //compose the header element and append it
554 list_items.appendChild(createHeaderElement(doc, oid, OAIXML.getTime(millis)));
555 }*/
556 } // otherwise we won't include this oid. with meta
557
558
559
560 }//end of for(int i=0; i<oid_list.size(); i++) of doing thru each record
561
562 return list_items_response;
563
564 }
565
566
567 // have implemented setDescription as an element, instead of a container containing metadata
568 private boolean configureSetInfo() {
569
570 Document doc = XMLConverter.newDOM();
571 this.list_sets_response = doc.createElement(GSXML.RESPONSE_ELEM);
572 Element list_sets_elem = doc.createElement(OAIXML.LIST_SETS);
573 this.list_sets_response.appendChild(list_sets_elem);
574 String set_name = this.coll_name;
575 String set_description = null;
576 Element name_elem = (Element)GSXML.getChildByTagName(this.config_info, OAIXML.SET_NAME);
577 if (name_elem!=null) {
578 set_name = GSXML.getNodeText(name_elem);
579 if (set_name.equals("")) {
580 set_name = this.coll_name; // default to coll name if can't find one
581 }
582 }
583 Element description_elem = (Element)GSXML.getChildByTagName(this.config_info, OAIXML.SET_DESCRIPTION);
584 if (description_elem!=null) {
585 set_description = GSXML.getNodeText(description_elem);
586 if (set_description.equals("")) {
587 set_description = null;
588 }
589 }
590 Element coll_set = OAIXML.createSet(doc, this.coll_name, set_name, set_description);
591 list_sets_elem.appendChild(coll_set);
592
593 // are we part of any super sets?
594 NodeList super_set_list = GSXML.getChildrenByTagName(this.config_info, OAIXML.OAI_SUPER_SET);
595 for (int i=0; i<super_set_list.getLength(); i++) {
596 String super_name = ((Element)super_set_list.item(i)).getAttribute(GSXML.NAME_ATT);
597 if (super_name != null && !super_name.equals("")) {
598 list_sets_elem.appendChild(OAIXML.createSet(doc, super_name, super_name, null));
599 }
600 }
601 return true;
602 }
603
604 /** create the metadata element used when processing ListRecords/GetRecord requests
605 */
606 protected Element createMetadataElement(Document doc, String prefix, DBInfo info) {
607 // the <metadata> element
608 Element metadata = doc.createElement(OAIXML.METADATA);
609 // the <oai:dc namespace...> element
610 Element prfx_str_elem = OAIXML.getMetadataPrefixElement(doc, prefix, OAIXML.oai_version);
611 metadata.appendChild(prfx_str_elem);
612
613 Element meta_format_element = GSXML.getNamedElement(this.meta_formats_definition, OAIXML.METADATA_FORMAT, GSXML.NAME_ATT, prefix);
614 NodeList elements = meta_format_element.getElementsByTagName(OAIXML.ELEMENT);
615 // for each element in the definition
616 for (int i=0; i<elements.getLength(); i++) {
617 Element e = (Element)elements.item(i);
618 Element map = (Element)GSXML.getChildByTagName(e, OAIXML.MAPPING);
619 if (map == null) {
620 // look up the element name
621 addMetadata(prfx_str_elem, e.getAttribute(GSXML.NAME_ATT), info);
622 } else {
623 // we go though the list of names in the mapping
624 addMetadata(prfx_str_elem, e.getAttribute(GSXML.NAME_ATT), map.getAttribute(OAIXML.SELECT), map.getAttribute(OAIXML.ELEMENTS), info);
625 }
626 }
627 // output any metadata that is not just a simple mapping
628 addCustomMetadata(prfx_str_elem, prefix, info);
629 return metadata;
630 }
631
632 /** a simple addMetadata where we look for meta_name metadata, and add as that name*/
633 protected void addMetadata(Element meta_list_elem, String meta_name, DBInfo info) {
634 Vector<String> values = info.getMultiInfo(meta_name);
635 if (values != null && values.size()!=0) {
636 for (int i=0; i<values.size(); i++) {
637 addMetadataElement(meta_list_elem, meta_name, values.get(i));
638 }
639 }
640 }
641
642 /** more complicated addMetadata - can add multiple items. */
643 protected void addMetadata(Element meta_list_elem, String new_meta_name, String select_type, String name_list, DBInfo info) {
644 String[] names = name_list.split(",");
645 for (int i=0; i<names.length; i++) {
646 Vector<String> values = info.getMultiInfo(names[i]);
647 if (values == null || values.size()==0) {
648 continue;
649 }
650 for (int j=0; j<values.size(); j++) {
651 addMetadataElement(meta_list_elem, new_meta_name, values.get(j));
652 if (select_type.equals(OAIXML.SELECT_SINGLE_VALUE)) {
653 return; // only want to add one value
654 }
655 }
656 if (select_type.equals(OAIXML.SELECT_FIRST_VALID_META)) {
657 return; // we have added all values of this meta elem
658 }
659 // otherwise, we will keep going through the list and add them all.
660 }
661 }
662
663 // specific metadata formats might need to do some custom metadata that is not
664 //just a standard mapping. eg oai_dc outputting an identifier that is a link
665 protected void addCustomMetadata(Element meta_list_elem, String prefix, DBInfo info) {
666
667 if (prefix.equals(OAIXML.META_FORMAT_DC)) {
668 // we want to add in another dc:identifier element with a link to the resource if possible
669 // try gs.OAIResourceURL first, then srclinkFile, then GS version of documnet
670 String gsURL = info.getInfo(OAIXML.GS_OAI_RESOURCE_URL);
671 if (gsURL.equals("")) {
672 String base_url = OAIXML.getBaseURL(); // e.g. e.g. http://host:port/greenstone3/oaiserver
673 // try srclinkFile
674 gsURL = info.getInfo("srclinkFile");
675 if (!gsURL.equals("")) {
676 // make up the link to the file
677 gsURL = base_url.replace("oaiserver", "") + "sites/" + this.site_name
678 + "/collect/" + this.coll_name + "/index/assoc/"
679 + info.getInfo("assocfilepath") + "/" + gsURL;
680 } else {
681 // no srclink file, lets provide a link to the greenstone doc
682 gsURL = base_url.replace("oaiserver", "library") + "/collection/" + this.coll_name + "/document/" + info.getInfo("Identifier");
683 }
684 }
685 // now we have the url link, add as metadata
686 addMetadataElement(meta_list_elem, "dc:identifier", gsURL);
687 }
688 }
689
690 /** create the actual metadata element for the list */
691 protected void addMetadataElement(Element meta_list_elem, String name, String value) {
692
693 Element meta = GSXML.createTextElement(meta_list_elem.getOwnerDocument(), name, value);
694 meta_list_elem.appendChild(meta);
695 }
696
697
698 /** create a header element used when processing requests like ListRecords/GetRecord/ListIdentifiers
699 */
700 protected Element createHeaderElement(Document doc, String oid, String oailastmodified, boolean deleted) {
701
702 Element header = doc.createElement(OAIXML.HEADER);
703
704 // if deleted, get the date and change oailastmodified to timestamp in oaiinfo
705 if(deleted) {
706 header.setAttribute(OAIXML.OAI_INF_STATUS, OAIXML.HEADER_STATUS_ATTR_DELETED); // set the header status to deleted
707 // then the timestamp for deletion will be from oai-inf database
708 }
709
710 Element identifier = doc.createElement(OAIXML.IDENTIFIER);
711 GSXML.setNodeText(identifier, coll_name + ":" + oid);
712 header.appendChild(identifier);
713 Element set_spec = doc.createElement(OAIXML.SET_SPEC);
714 GSXML.setNodeText(set_spec, coll_name);
715 header.appendChild(set_spec);
716 Element datestamp = doc.createElement(OAIXML.DATESTAMP);
717 GSXML.setNodeText(datestamp, oailastmodified);
718 header.appendChild(datestamp);
719 return header;
720 }
721
722 /** return the metadata information */
723 protected Element processListMetadataFormats(Element req) {
724 // the request sent here must contain an OID. see doListMetadataFormats() in OAIReceptionist
725 Element param = GSXML.getNamedElement(req, GSXML.PARAM_ELEM, GSXML.NAME_ATT, OAIXML.OID);
726 if (param == null) {
727 logger.error("An element containing the OID attribute not is present.");
728 return OAIXML.createErrorResponse(OAIXML.ID_DOES_NOT_EXIST, "");
729 }
730 String oid = param.getAttribute(GSXML.VALUE_ATT);
731 if (oid == null || oid.equals("")) {
732 logger.error("No OID is present in the request.");
733 return OAIXML.createErrorResponse(OAIXML.ID_DOES_NOT_EXIST, "");
734 }
735
736 /*
737 ArrayList<String> oid_list = null;
738 if(oaiinf_db != null) { // try getting the OIDs from the oaiinf_db
739 oid_list = new ArrayList<String>(oaiinf_db.getAllKeys());
740
741 if(oid_list == null) { // try getting the OIDs from the oai entries in the index db
742 oid_list = getChildrenIds(OAIXML.BROWSELIST);
743 }
744 }
745 */
746 // assume meta formats are only for OIDs that have not been deleted
747 // so don't need to check oai-inf db, and can just check collection's index db for list of OIDs
748 ArrayList<String> oid_list = getChildrenIds(OAIXML.BROWSELIST);
749 if (oid_list == null || oid_list.contains(oid) == false) {
750 logger.error("OID: " + oid + " is not present in the database.");
751 Element e= OAIXML.createErrorResponse(OAIXML.ID_DOES_NOT_EXIST, "");
752// logger.error((new XMLConverter()).getPrettyString (e));
753 return e;
754 }
755
756 DBInfo info = null;
757 info = this.coll_db.getInfo(oid);
758 if (info == null) { //just double check
759 return OAIXML.createErrorResponse(OAIXML.OAI_SERVICE_UNAVAILABLE, "");
760 }
761
762 Document doc = XMLConverter.newDOM();
763 Element list_metadata_formats_response = doc.createElement(GSXML.RESPONSE_ELEM);
764
765 Element list_metadata_formats = doc.createElement(OAIXML.LIST_METADATA_FORMATS);
766 list_metadata_formats_response.appendChild(list_metadata_formats);
767 boolean has_meta_format = false;
768
769 // for each format in format_elements_map
770 Iterator<String> it = format_elements_map.keySet().iterator();
771 while (it.hasNext()) {
772 String format = it.next();
773 HashSet<String> set_of_elems = format_elements_map.get(format);
774 if (documentContainsMetadata(info, set_of_elems)) {
775 // add this format into the response
776 has_meta_format = true;
777 list_metadata_formats.appendChild(doc.importNode(format_response_map.get(format), true));
778 }
779 }
780
781 if (has_meta_format == false) {
782 logger.error("Specified metadata names are not contained in the database.");
783 return OAIXML.createErrorResponse(OAIXML.NO_METADATA_FORMATS, "");
784 } else {
785 return list_metadata_formats_response;
786 }
787 }
788
789 protected boolean documentContainsMetadata(DBInfo info, HashSet<String> set_of_elems) {
790 if (set_of_elems.size() == 0) {
791 return false;
792 }
793 Iterator<String> i = set_of_elems.iterator();
794 while (i.hasNext()) {
795 if (!info.getInfo(i.next()).equals("")) {
796 return true;
797 }
798 }
799 return false;
800 }
801
802 /** returns a list of the child ids in order, null if no children */
803 protected ArrayList<String> getChildrenIds(String node_id) {
804 DBInfo info = this.coll_db.getInfo(node_id);
805 if (info == null) {
806 return null;
807 }
808
809 String contains = info.getInfo("contains");
810 if (contains.equals("")) {
811 return null;
812 }
813 ArrayList<String> children = new ArrayList<String>();
814 StringTokenizer st = new StringTokenizer(contains, ";");
815 while (st.hasMoreTokens()) {
816 String child_id = st.nextToken().replaceAll("\"", node_id);
817 children.add(child_id);
818 }
819 return children;
820 }
821 /**method to check whether any of the 'metadata_names' is contained in the 'info'.
822 * The name may be in the form: <name>,<mapped name>, in which the mapped name is
823 * optional. The mapped name is looked up in the DBInfo; if not present, use the first
824 * name which is mandatory.
825 */
826 protected boolean containsMetadata(DBInfo info, String[] metadata_names) {
827 if (metadata_names == null) return false;
828 logger.info("checking metadata names in db.");
829 for(int i=0; i<metadata_names.length; i++) {
830 int index = metadata_names[i].indexOf(",");
831 String meta_name = (index == -1) ? metadata_names[i] :
832 metadata_names[i].substring(index + 1);
833
834 if(info.getInfo(meta_name).equals("") == false) {
835 return true;
836 }
837 }
838 return false;
839 }
840
841 protected long getDateStampMillis(DBInfo info) {
842 // gs.OAIDateStamp is in YYYY-MM-DD
843 String time_stamp = info.getInfo(OAIXML.GS_OAI_DATE_STAMP);
844 long millis = -1;
845 if (!time_stamp.equals("")) {
846 millis = OAIXML.getTime(time_stamp);
847 }
848 if (millis == -1) {
849 // oailastmodified is in seconds
850 time_stamp = info.getInfo(OAIXML.OAI_LASTMODIFIED);
851 if (!time_stamp.equals("")) {
852 millis = Long.parseLong(time_stamp)*1000;
853 }
854 }
855 return millis;
856
857
858 }
859}
860
861
Note: See TracBrowser for help on using the repository browser.