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