source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/OAICollection.java@ 31912

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

Now GS3 tries to obtain the _earliesttimestamp entry of the oai-inf.db for each collection to, work out the earliest among them to be the earliest timestamp of the repository. For each collection, if there is no such entry or the oai-inf db doesn't exist or can't be accessed, the collection falls back to using the value in the build config file as before. Also as before, if that doesn't exist either, it uses the lastmod date of the collection (I think also taken from build config) as the next fallback value.

File size: 6.9 KB
Line 
1/*
2 * OAICollection.java
3 * Copyright (C) 2002 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.collection;
20
21import java.io.BufferedReader;
22import java.io.BufferedWriter;
23import java.io.File;
24import java.io.FileReader;
25import java.io.FileWriter;
26import java.io.IOException;
27import java.io.PrintWriter;
28import java.io.StringWriter;
29import java.util.ArrayList;
30import java.util.HashMap;
31
32import org.apache.commons.lang3.StringUtils;
33import org.apache.log4j.Logger;
34import org.greenstone.gsdl3.core.ModuleInterface;
35import org.greenstone.gsdl3.service.ServiceRack;
36import org.greenstone.gsdl3.service.OAIPMH;
37import org.greenstone.gsdl3.util.GSFile;
38import org.greenstone.gsdl3.util.GSXML;
39import org.greenstone.gsdl3.util.GSXSLT;
40import org.greenstone.gsdl3.util.OAIXML;
41import org.greenstone.gsdl3.util.SimpleMacroResolver;
42import org.greenstone.gsdl3.util.UserContext;
43import org.greenstone.gsdl3.util.XMLConverter;
44import org.greenstone.gsdl3.util.XMLTransformer;
45import org.w3c.dom.Document;
46import org.w3c.dom.Element;
47import org.w3c.dom.Node;
48import org.w3c.dom.NodeList;
49
50/**
51 * Represents a collection for the OAI server. This is a cut down version of Collection, as we
52 * only want to load the OAIPMH service rack, not any of the others
53 *
54 */
55public class OAICollection extends Collection
56{
57
58 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.OAICollection.class.getName());
59
60 /** does this collection provide the OAI service */
61 protected boolean has_oai = false;
62
63 /** a reference to the OAIPMH service rack */
64 protected OAIPMH oai_service_rack = null;
65
66 /**
67 * Configures the collection.
68 *
69 * site_home and cluster_name must be set before configure is called.
70 *
71 * collection metadata is obtained, and services loaded.
72 *
73 * @return true/false on success/fail
74 */
75 public boolean configure()
76 {
77 if (this.site_home == null || this.cluster_name == null)
78 {
79 logger.error("Collection: site_home and collection_name must be set before configure called!");
80 return false;
81 }
82
83 macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
84
85 Element coll_config_xml = loadCollConfigFile();
86 if (coll_config_xml == null) {
87 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load collection config file");
88
89 return false;
90 }
91 Element build_config_xml = loadBuildConfigFile();
92 if (build_config_xml == null)
93 {
94 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load build config file");
95
96 return false;
97 }
98 GSXSLT.modifyCollectionConfigForDebug(coll_config_xml);
99
100 // process the metadata and display items and default library params
101 super.configureLocalData(coll_config_xml);
102 super.configureLocalData(build_config_xml);
103 // get extra collection specific stuff
104 findAndLoadInfo(coll_config_xml, build_config_xml);
105
106 // load up the OAIPMH serviceRack
107 configureServiceRacks(coll_config_xml, build_config_xml);
108
109 return true;
110
111 }
112
113
114 /**
115 * whether this collection has OAIPMH services
116 */
117 public boolean hasOAI()
118 {
119 return has_oai;
120 }
121
122 /** add any extra info for collection from OAIConfig.xml */
123 public boolean configureOAI(Element oai_config) {
124 // just pass the element to each service - should only be one
125 return this.oai_service_rack.configureOAI(oai_config);
126 }
127
128 /** override this to only load up OAIPMH serviceRack - don't need all the rest of them for oai*/
129 protected boolean configureServiceRackList(Element service_rack_list, Element extra_info)
130 {
131
132 // find the OAIPMH service
133 Element oai_service_xml = GSXML.getNamedElement(service_rack_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, "OAIPMH");
134 if (oai_service_xml == null) {
135 return false;
136 }
137
138 // the xml request to send to the serviceRack to query what services it provides
139 Document doc = XMLConverter.newDOM();
140 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
141 Element request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
142 message.appendChild(request);
143
144 this.oai_service_rack = new OAIPMH();
145 this.oai_service_rack.setSiteHome(this.site_home);
146 this.oai_service_rack.setSiteAddress(this.site_http_address);
147 this.oai_service_rack.setClusterName(this.cluster_name);
148 this.oai_service_rack.setServiceCluster(this);
149 this.oai_service_rack.setMessageRouter(this.router);
150 // pass the xml node to the service for configuration
151 if (this.oai_service_rack.configure(oai_service_xml, extra_info)) {
152
153 // once we've configured the OAIPMH service, we can use the OAIPMH service to
154 // retrieve the earliest timestamp of the collection from the oai-inf db and
155 // overwrite Collection.earliestDatestamp with it.
156 long earliestTimestamp = this.oai_service_rack.getEarliestTimestamp();
157 if(earliestTimestamp == -1) {
158 //this.earliestDatestamp = -1;
159 logger.warn("No OAI timestamp for collection " + this.cluster_name
160 + ". Falling back to using its earliestDatestamp from build config: " + this.earliestDatestamp);
161 } else {
162 this.earliestDatestamp = earliestTimestamp; // milliseconds
163 }
164
165
166 // find out the supported service types for this service module
167 Node types = this.oai_service_rack.process(message);
168 NodeList typenodes = ((Element) types).getElementsByTagName(GSXML.SERVICE_ELEM);
169
170 for (int j = 0; j < typenodes.getLength(); j++)
171 {
172 String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);
173
174 if (service_map.get(service) != null)
175 {
176 char extra = '0';
177 String new_service = service + extra;
178
179 while (service_map.get(new_service) != null)
180 {
181 extra++;
182 new_service = service + extra;
183 }
184 this.service_name_map.put(new_service, service);
185 service = new_service;
186 ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
187 }
188 this.service_map.put(service, this.oai_service_rack);
189 // also add info to the ServiceInfo XML element
190 this.service_list.appendChild(this.desc_doc.importNode(typenodes.item(j), true));
191 }
192 has_oai = true;
193 return true;
194 }
195
196
197 return false;
198 }
199
200}
Note: See TracBrowser for help on using the repository browser.