source: greenstone3/trunk/src/java/org/greenstone/gsdl3/collection/Collection.java@ 14208

Last change on this file since 14208 was 14208, checked in by xiao, 17 years ago

add a method hasOAI() identifying itself whether it's serviceRackList contains the OAIPMH service rack; and a method returning the build time of the collection.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/*
2 * Collection.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 org.greenstone.gsdl3.util.*;
22import org.greenstone.gsdl3.core.*;
23import org.greenstone.gsdl3.service.*;
24
25
26// java XML classes we're using
27import org.w3c.dom.Document;
28import org.w3c.dom.Node;
29import org.w3c.dom.Element;
30import org.w3c.dom.NodeList;
31
32import java.io.File;
33import java.util.HashMap;
34
35import org.apache.log4j.*;
36
37/**
38 * Represents a collection in Greenstone. A collection is an extension of
39 * a ServiceCluster - it has local data that the services use.
40 *
41 * @author <a href="mailto:[email protected]">Katherine Don</a>
42 * @version $Revision: 14208 $
43 * @see ModuleInterface
44 */
45public class Collection
46 extends ServiceCluster {
47
48 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
49
50 /** is this collection being tidied */
51 protected boolean useBook = false;
52 /** is this collection public or private */
53 protected boolean is_public = true;
54
55 /** does this collection provide the OAI service */
56 protected boolean has_oai = true;
57 /** time when this collection was built */
58 protected long lastmodified = 0;
59
60 /** An element containing the serviceRackList element of buildConfig.xml, used to determine whether it contains
61 * the OAIPMH serviceRack
62 */
63 protected Element service_rack_list = null;
64
65 protected XMLTransformer transformer = null;
66 /** same as setClusterName */
67 public void setCollectionName(String name) {
68 setClusterName(name);
69 }
70
71 public Collection() {
72 super();
73 this.description = this.doc.createElement(GSXML.COLLECTION_ELEM);
74
75 }
76
77 /**
78 * Configures the collection.
79 *
80 * gsdlHome and collectionName must be set before configure is called.
81 *
82 * the file buildcfg.xml is located in gsdlHome/collect/collectionName
83 * collection metadata is obtained, and services loaded.
84 *
85 * @return true/false on success/fail
86 */
87 public boolean configure() {
88
89 if (this.site_home == null || this.cluster_name== null) {
90 logger.error("Collection: site_home and collection_name must be set before configure called!");
91 return false;
92 }
93
94 Element coll_config_xml = loadCollConfigFile();
95 Element build_config_xml = loadBuildConfigFile();
96
97 if (coll_config_xml==null||build_config_xml==null) {
98 return false;
99 }
100 // process the metadata and display items
101 findAndLoadInfo(coll_config_xml, build_config_xml);
102
103 // now do the services
104 Element service_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
105 configureServiceRack(service_list, coll_config_xml);
106
107 this.service_rack_list = service_list;
108
109 return true;
110
111 }
112
113 public boolean useBook() {
114 return useBook;
115 }
116
117 public boolean isPublic() {
118 return is_public;
119 }
120 //used by the OAIReceptionist to find out the earliest datestamp amongst all oai collections in the repository
121 public long getLastmodified() {
122 return lastmodified;
123 }
124 /** whether the service_map in ServiceCluster.java contains the service 'OAIPMH'
125 * 11/06/2007 xiao
126 */
127 public boolean hasOAI() {
128 if (service_rack_list == null) return false;
129 Element oai_service_rack = GSXML.getNamedElement(service_rack_list, GSXML.SERVICE_CLASS_ELEM,
130 OAIXML.NAME, OAIXML.OAIPMH);
131 if (oai_service_rack == null) {
132 logger.info("No oai for collection: " + this.cluster_name);
133 return false;
134 }
135 return true;//oai_service_rack == null;
136 }
137 /**
138 * load in the collection config file into a DOM Element
139 */
140 protected Element loadCollConfigFile() {
141
142 File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
143
144 if (!coll_config_file.exists()) {
145 logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+coll_config_file+" does not exist");
146 return null;
147 }
148 // get the xml for both files
149 Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
150 Element coll_config_elem = null;
151 if (coll_config_doc != null) {
152 coll_config_elem = coll_config_doc.getDocumentElement();
153 }
154 return coll_config_elem;
155
156 }
157
158 /**
159 * load in the collection build config file into a DOM Element
160 */
161 protected Element loadBuildConfigFile() {
162
163 File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
164 if (!build_config_file.exists()) {
165 logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+build_config_file+" does not exist");
166 return null;
167 }
168 Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
169 Element build_config_elem = null;
170 if (build_config_doc != null) {
171 build_config_elem = build_config_doc.getDocumentElement();
172 }
173
174 lastmodified = build_config_file.lastModified();
175
176 return build_config_elem;
177 }
178
179 /**
180 * find the metadata and display elems from the two config files and add it to the appropriate lists
181 */
182 protected boolean findAndLoadInfo(Element coll_config_xml,
183 Element build_config_xml){
184
185 // metadata
186 Element meta_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
187 addMetadata(meta_list);
188 meta_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
189 addMetadata(meta_list);
190
191 meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
192 GSXML.addMetadata(this.doc, meta_list, "httpPath", this.site_http_address+"/collect/"+this.cluster_name);
193 addMetadata(meta_list);
194
195 // display stuff
196 Element display_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
197 if (display_list != null) {
198 resolveMacros(display_list);
199 addDisplayItems(display_list);
200 }
201
202 //check whether the html are tidy or not
203 Element import_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.IMPORT_ELEM);
204 if (import_list != null) {
205 Element plugin_list = (Element)GSXML.getChildByTagName(import_list, GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
206 addPlugins(plugin_list);
207 if (plugin_list != null){
208 Element plugin_elem = (Element)GSXML.getNamedElement(plugin_list, GSXML.PLUGIN_ELEM, GSXML.NAME_ATT, "HTMLPlug");
209 if (plugin_elem != null) {
210 //get the option
211 Element option_elem = (Element)GSXML.getNamedElement(plugin_elem, GSXML.PARAM_OPTION_ELEM, GSXML.NAME_ATT, "-tidy_html");
212 if (option_elem != null) {
213 useBook = true;
214 }
215 }
216 }
217 }
218 meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
219 if (useBook == true)
220 GSXML.addMetadata(this.doc, meta_list, "tidyoption", "tidy");
221 else
222 GSXML.addMetadata(this.doc, meta_list, "tidyoption", "untidy");
223 addMetadata(meta_list);
224
225 // check whether we are public or not
226 if (meta_list != null) {
227 Element meta_elem = (Element) GSXML.getNamedElement(metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "public");
228 if (meta_elem != null) {
229 String value = GSXML.getValue(meta_elem).toLowerCase().trim();
230 if (value.equals("false")) {
231 is_public = false;
232 }
233 }
234 }
235 return true;
236
237 }
238
239 protected boolean resolveMacros(Element display_list) {
240 if (display_list==null) return false;
241 NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
242 if (displaynodes.getLength()>0) {
243 String http_site = this.site_http_address;
244 String http_collection = this.site_http_address +"/collect/"+this.cluster_name;
245 for(int k=0; k<displaynodes.getLength(); k++) {
246 Element d = (Element) displaynodes.item(k);
247 String text = GSXML.getNodeText(d);
248 text = text.replaceAll("_httpsite_", http_site);
249 text = text.replaceAll("_httpcollection_", http_collection);
250 GSXML.setNodeText(d, text);
251 }
252 }
253 return true;
254 }
255 /**
256 * do a configure on only part of the collection
257 */
258 protected boolean configureSubset(String subset) {
259
260 // need the coll config files
261 Element coll_config_elem = loadCollConfigFile();
262 Element build_config_elem = loadBuildConfigFile();
263 if (coll_config_elem == null||build_config_elem == null) {
264 // wont be able to do any of teh requests
265 return false;
266 }
267
268 if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
269 Element service_rack_list = (Element)GSXML.getChildByTagName(build_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
270
271 return configureServiceRack(service_rack_list, coll_config_elem);
272 }
273
274 if (subset.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER) || subset.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
275 return findAndLoadInfo(coll_config_elem, build_config_elem);
276
277 }
278
279 logger.error("Collection: cant process system request, configure "+subset);
280 return false;
281 }
282
283}
284
285
286
287
Note: See TracBrowser for help on using the repository browser.