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

Last change on this file since 14640 was 14640, checked in by anna, 17 years ago

Added the buildType of a collection to its description, to generate ct param.

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