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

Last change on this file since 13830 was 13830, checked in by kjdon, 17 years ago

before adding display items into the list, we'll check for gs2 style macros - allowed _httpsite_ and _httpcollection_ at the moment

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 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: 13830 $
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 protected XMLTransformer transformer = null;
51 /** same as setClusterName */
52 public void setCollectionName(String name) {
53 setClusterName(name);
54 }
55
56 public Collection() {
57 super();
58 this.description = this.doc.createElement(GSXML.COLLECTION_ELEM);
59
60 }
61
62 /**
63 * Configures the collection.
64 *
65 * gsdlHome and collectionName must be set before configure is called.
66 *
67 * the file buildcfg.xml is located in gsdlHome/collect/collectionName
68 * collection metadata is obtained, and services loaded.
69 *
70 * @return true/false on success/fail
71 */
72 public boolean configure() {
73
74 if (this.site_home == null || this.cluster_name== null) {
75 logger.error("Collection: site_home and collection_name must be set before configure called!");
76 return false;
77 }
78
79 Element coll_config_xml = loadCollConfigFile();
80 Element build_config_xml = loadBuildConfigFile();
81
82 if (coll_config_xml==null||build_config_xml==null) {
83 return false;
84 }
85 // process the metadata and display items
86 findAndLoadInfo(coll_config_xml, build_config_xml);
87
88 // now do the services
89 Element service_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
90 configureServiceRack(service_list, coll_config_xml);
91
92 return true;
93
94 }
95 /**
96 * load in the collection config file into a DOM Element
97 */
98 protected Element loadCollConfigFile() {
99
100 File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
101
102 if (!coll_config_file.exists()) {
103 logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+coll_config_file+" does not exist");
104 return null;
105 }
106 // get the xml for both files
107 Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
108 Element coll_config_elem = null;
109 if (coll_config_doc != null) {
110 coll_config_elem = coll_config_doc.getDocumentElement();
111 }
112 return coll_config_elem;
113
114 }
115
116 /**
117 * load in the collection build config file into a DOM Element
118 */
119 protected Element loadBuildConfigFile() {
120
121 File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
122 if (!build_config_file.exists()) {
123 logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+build_config_file+" does not exist");
124 return null;
125 }
126 Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
127 Element build_config_elem = null;
128 if (build_config_doc != null) {
129 build_config_elem = build_config_doc.getDocumentElement();
130 }
131 return build_config_elem;
132 }
133
134 /**
135 * find the metadata and display elems from the two config files and add it to the appropriate lists
136 */
137 protected boolean findAndLoadInfo(Element coll_config_xml,
138 Element build_config_xml){
139
140 // metadata
141 Element meta_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
142 addMetadata(meta_list);
143 meta_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
144 addMetadata(meta_list);
145
146 meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
147 GSXML.addMetadata(this.doc, meta_list, "httpPath", this.site_http_address+"/collect/"+this.cluster_name);
148 addMetadata(meta_list);
149
150 // display stuff
151 Element display_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
152 if (display_list != null) {
153 resolveMacros(display_list);
154 addDisplayItems(display_list);
155 }
156 return true;
157
158 }
159
160 protected boolean resolveMacros(Element display_list) {
161 if (display_list==null) return false;
162 NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
163 if (displaynodes.getLength()>0) {
164 String http_site = this.site_http_address;
165 String http_collection = this.site_http_address +"/collect/"+this.cluster_name;
166 for(int k=0; k<displaynodes.getLength(); k++) {
167 Element d = (Element) displaynodes.item(k);
168 String text = GSXML.getNodeText(d);
169 text = text.replaceAll("_httpsite_", http_site);
170 text = text.replaceAll("_httpcollection_", http_collection);
171 GSXML.setNodeText(d, text);
172 }
173 }
174 return true;
175 }
176 /**
177 * do a configure on only part of the collection
178 */
179 protected boolean configureSubset(String subset) {
180
181 // need the coll config files
182 Element coll_config_elem = loadCollConfigFile();
183 Element build_config_elem = loadBuildConfigFile();
184 if (coll_config_elem == null||build_config_elem == null) {
185 // wont be able to do any of teh requests
186 return false;
187 }
188
189 if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
190 Element service_rack_list = (Element)GSXML.getChildByTagName(build_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
191
192 return configureServiceRack(service_rack_list, coll_config_elem);
193 }
194
195 if (subset.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
196 return findAndLoadInfo(coll_config_elem, build_config_elem);
197
198 }
199
200 logger.error("Collection: cant process system request, configure "+subset);
201 return false;
202 }
203
204}
205
206
207
208
Note: See TracBrowser for help on using the repository browser.