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

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

pass build_config_xml as the extra info for a servicerack coming from coll config

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