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

Last change on this file since 21312 was 21312, checked in by kjdon, 14 years ago

a little bit of code rearranging and editing so that we can have a serviceRackList in collectionConfig.xml (as well as buildConfig.xml) - may want to manually specify one and not have to edit the file each time after a rebuild

  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 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: 21312 $
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
101 // get the collection type attribute
102 Element search = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SEARCH_ELEM);
103 if(search!=null) {
104 col_type = search.getAttribute(GSXML.TYPE_ATT);
105 }
106
107 // process the metadata and display items
108 findAndLoadInfo(coll_config_xml, build_config_xml);
109
110 // now do the services
111 configureServiceRacks(coll_config_xml, build_config_xml);
112
113 return true;
114
115 }
116
117 public boolean useBook() {
118 return useBook;
119 }
120
121 public boolean isPublic() {
122 return is_public;
123 }
124 //used by the OAIReceptionist to find out the earliest datestamp amongst all oai collections in the repository
125 public long getLastmodified() {
126 return lastmodified;
127 }
128 /** whether the service_map in ServiceCluster.java contains the service 'OAIPMH'
129 * 11/06/2007 xiao
130 */
131 public boolean hasOAI() {
132 return has_oai;
133 }
134 /**
135 * load in the collection config file into a DOM Element
136 */
137 protected Element loadCollConfigFile() {
138
139 File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
140
141 if (!coll_config_file.exists()) {
142 logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+coll_config_file+" does not exist");
143 return null;
144 }
145 // get the xml for both files
146 Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
147 Element coll_config_elem = null;
148 if (coll_config_doc != null) {
149 coll_config_elem = coll_config_doc.getDocumentElement();
150 }
151 return coll_config_elem;
152
153 }
154
155 /**
156 * load in the collection build config file into a DOM Element
157 */
158 protected Element loadBuildConfigFile() {
159
160 File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
161 if (!build_config_file.exists()) {
162 logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+build_config_file+" does not exist");
163 return null;
164 }
165 Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
166 Element build_config_elem = null;
167 if (build_config_doc != null) {
168 build_config_elem = build_config_doc.getDocumentElement();
169 }
170
171 lastmodified = build_config_file.lastModified();
172
173 return build_config_elem;
174 }
175
176 /**
177 * find the metadata and display elems from the two config files and add it to the appropriate lists
178 */
179 protected boolean findAndLoadInfo(Element coll_config_xml,
180 Element build_config_xml){
181
182 // metadata
183 Element meta_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
184 addMetadata(meta_list);
185 meta_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
186 addMetadata(meta_list);
187
188 meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
189 GSXML.addMetadata(this.doc, meta_list, "httpPath", this.site_http_address+"/collect/"+this.cluster_name);
190 addMetadata(meta_list);
191
192 // display stuff
193 Element display_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
194 if (display_list != null) {
195 resolveMacros(display_list);
196 addDisplayItems(display_list);
197 }
198
199 //check whether the html are tidy or not
200 Element import_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.IMPORT_ELEM);
201 if (import_list != null) {
202 Element plugin_list = (Element)GSXML.getChildByTagName(import_list, GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
203 addPlugins(plugin_list);
204 if (plugin_list != null){
205 Element plugin_elem = (Element)GSXML.getNamedElement(plugin_list, GSXML.PLUGIN_ELEM, GSXML.NAME_ATT, "HTMLPlug");
206 if (plugin_elem != null) {
207 //get the option
208 Element option_elem = (Element)GSXML.getNamedElement(plugin_elem, GSXML.PARAM_OPTION_ELEM, GSXML.NAME_ATT, "-tidy_html");
209 if (option_elem != null) {
210 useBook = true;
211 }
212 }
213 }
214 }
215 meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
216 if (useBook == true)
217 GSXML.addMetadata(this.doc, meta_list, "tidyoption", "tidy");
218 else
219 GSXML.addMetadata(this.doc, meta_list, "tidyoption", "untidy");
220 addMetadata(meta_list);
221
222 // check whether we are public or not
223 if (meta_list != null) {
224 Element meta_elem = (Element) GSXML.getNamedElement(metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "public");
225 if (meta_elem != null) {
226 String value = GSXML.getValue(meta_elem).toLowerCase().trim();
227 if (value.equals("false")) {
228 is_public = false;
229 }
230 }
231 }
232 return true;
233
234 }
235
236 protected boolean configureServiceRacks(Element coll_config_xml,
237 Element build_config_xml){
238 clearServices();
239 Element service_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
240 configureServiceRack(service_list, coll_config_xml);
241
242 // Check for oai
243 Element oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, OAIXML.NAME, OAIXML.OAIPMH);
244 if (oai_service_rack == null) {
245 has_oai = false;
246 logger.info("No oai for collection: " + this.cluster_name);
247
248 } else {
249 has_oai = true;
250 }
251
252 // collection Config may also contain manually added service racks
253 service_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
254 if (service_list != null) {
255 configureServiceRack(service_list, coll_config_xml);
256 }
257 return true;
258 }
259
260 protected boolean resolveMacros(Element display_list) {
261 if (display_list==null) return false;
262 NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
263 if (displaynodes.getLength()>0) {
264 String http_site = this.site_http_address;
265 String http_collection = this.site_http_address +"/collect/"+this.cluster_name;
266 for(int k=0; k<displaynodes.getLength(); k++) {
267 Element d = (Element) displaynodes.item(k);
268 String text = GSXML.getNodeText(d);
269 text = text.replaceAll("_httpsite_", http_site);
270 text = text.replaceAll("_httpcollection_", http_collection);
271 GSXML.setNodeText(d, text);
272 }
273 }
274 return true;
275 }
276 /**
277 * do a configure on only part of the collection
278 */
279 protected boolean configureSubset(String subset) {
280
281 // need the coll config files
282 Element coll_config_elem = loadCollConfigFile();
283 Element build_config_elem = loadBuildConfigFile();
284 if (coll_config_elem == null||build_config_elem == null) {
285 // wont be able to do any of the requests
286 return false;
287 }
288
289 if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
290 return configureServiceRacks(coll_config_elem, build_config_elem);
291 }
292
293 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)) {
294 return findAndLoadInfo(coll_config_elem, build_config_elem);
295
296 }
297
298 logger.error("Collection: cant process system request, configure "+subset);
299 return false;
300 }
301
302}
303
304
305
306
Note: See TracBrowser for help on using the repository browser.