source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/CoverageMetadataRetrieve.java@ 22974

Last change on this file since 22974 was 22974, checked in by davidb, 14 years ago

Code used to test 'coll_db == null' to determine if a database was opened correctly. Since this value is returned by a constructor, it is always non-null, even when it failed to open the database. The routine databaseOK() was added to core class, and is now used in these routines instead of testing for null

File size: 8.9 KB
Line 
1/*
2 * MyNewServicesTemplate.java - a dummy class showing how to create new
3 * services for Greenstone3
4 *
5 * This class has two dummy services: TextQuery and MyDifferentService
6 */
7
8// This file needs to be put in org/greenstone/gsdl3/service
9package org.greenstone.gsdl3.service;
10
11// Greenstone classes
12import org.greenstone.gsdl3.util.*;
13
14// XML classes
15import org.w3c.dom.Document;
16import org.w3c.dom.Element;
17import org.w3c.dom.NodeList;
18
19import org.apache.log4j.*;
20
21import java.util.Iterator;
22import java.util.Vector;
23import java.util.Set;
24
25// change the class name (and the filename) to something more appropriate
26public class CoverageMetadataRetrieve
27 extends ServiceRack {
28
29 // add in a logger for error messages
30 static Logger logger = Logger.getLogger("CoverageMetadataRetrieve");
31
32 protected SimpleCollectionDatabase coll_db = null;
33 protected String index_stem = null;
34
35
36 // the new service names
37 protected static final String COVERAGE_SERVICE = "CoverageMetadataRetrieve";
38
39 // initialize any custom variables
40 public CoverageMetadataRetrieve() {
41
42 }
43
44 // clean up anything that we need to
45 public void cleanUp() {
46 super.cleanUp();
47 }
48
49 // Configure the class based in info in buildConfig.xml and collectionConfig.xml
50 // info is the <serviceRack name="MyNewServicesTemplate"/> element from
51 // buildConfig.xml, and extra_info is the whole collectionConfig.xml file
52 // in case its needed
53 public boolean configure(Element info, Element extra_info) {
54
55 if (!super.configure(info, extra_info)) {
56 return false;
57 }
58
59 logger.info("Configuring CoverageMetadataRetrieve...");
60
61 // set up short_service_info - this currently is a list of services,
62 // with their names and service types
63 // we have two services, a new textquery, and a new one of a new type
64 //Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
65 //tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
66 //tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
67 //this.short_service_info.appendChild(tq_service);
68
69 Element diff_service = this.doc.createElement(GSXML.SERVICE_ELEM);
70 diff_service.setAttribute(GSXML.TYPE_ATT, "retrieve");
71 diff_service.setAttribute(GSXML.NAME_ATT, COVERAGE_SERVICE);
72 this.short_service_info.appendChild(diff_service);
73
74 // the index stem is either specified in the config file or is the collection name
75 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM);
76 if (index_stem_elem != null) {
77 this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
78 }
79 if (this.index_stem == null || this.index_stem.equals("")) {
80 logger.error("CoverageMetadataRetrieve.configure(): indexStem element not found, stem will default to collection name");
81 this.index_stem = this.cluster_name;
82 }
83
84 // find out what kind of database we have
85 Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
86 String database_type = null;
87 if (database_type_elem != null) {
88 database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
89 }
90 if (database_type == null || database_type.equals("")) {
91 database_type = "gdbm"; // the default
92 }
93 coll_db = new SimpleCollectionDatabase(database_type);
94 if (!coll_db.databaseOK()) {
95 logger.error("Couldn't create the collection database of type "+database_type);
96 return false;
97 }
98
99 // Open database for querying
100 String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, this.index_stem, database_type);
101 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) {
102 logger.error("Could not open collection database!");
103 return false;
104 }
105
106
107 // Extract any relevant information from info and extra_info
108 // This can be used to set up variables.
109
110 // If there is any formatting information, add it in to format_info_map
111
112 // Do this for all services as appropriate
113 Element format = null; // find it from info/extra_info
114 if (format != null) {
115 this.format_info_map.put(COVERAGE_SERVICE, this.doc.importNode(format, true));
116 }
117
118 return true;
119
120 }
121
122 // get the desription of a service. Could include parameter lists, displayText
123 protected Element getServiceDescription(String service, String lang, String subset) {
124
125 // check that we have been asked for the right service
126 if (!service.equals(COVERAGE_SERVICE)) {
127 return null;
128 }
129
130 /*
131 if (service.equals(QUERY_SERVICE)) {
132 Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
133 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
134 tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
135 if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
136 // add in any <displayText> elements
137 // name, for example - get from properties file
138 tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(QUERY_SERVICE+".name", lang) ));
139 }
140
141 if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
142 // add in a param list if this service has parameters
143 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
144 tq_service.appendChild(param_list);
145 // create any params and append to param_list
146 }
147 return tq_service;
148 }
149 */
150
151 if (service.equals(COVERAGE_SERVICE)) {
152 Element diff_service = this.doc.createElement(GSXML.SERVICE_ELEM);
153 diff_service.setAttribute(GSXML.TYPE_ATT, "retrieve");
154 diff_service.setAttribute(GSXML.NAME_ATT, COVERAGE_SERVICE);
155 if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
156 // add in any <displayText> elements
157 // name, for example - get from properties file
158 diff_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(COVERAGE_SERVICE+".name", lang) ));
159 }
160
161 if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
162 // add in a param list if this service has parameters
163 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
164 diff_service.appendChild(param_list);
165 // create any params and append to param_list
166 }
167
168 return diff_service;
169 }
170
171 // not a valid service for this class
172 return null;
173
174
175
176 }
177
178 /** This is the method that actually handles the TextQuery Service */
179 //protected Element processTextQuery(Element request) {
180
181 //Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
182 //result.setAttribute(GSXML.FROM_ATT, QUERY_SERVICE);
183 //result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
184
185 // fill in the rest
186 //return result;
187 //}
188
189 /** This is the method that actually handles the MyDifferentService service */
190 protected Element processCoverageMetadataRetrieve(Element request) {
191
192
193 if (!this.coll_db.databaseOK()) {
194 logger.error("No valid database found\n");
195 return null;
196 }
197
198 DBInfo collection_info = this.coll_db.getInfo("collection");
199
200 Set keys = collection_info.getKeys();
201
202 Vector valid_keys = new Vector();
203
204 // Iterate over keys and add valid ones to the valid_keys vector
205 String current_key = null;
206 Iterator iter = keys.iterator();
207
208 while (iter.hasNext()) {
209 current_key = (String) iter.next();
210 if(current_key.matches("^metadatalist-([a-zA-Z][^-])*$"))
211 {
212 logger.error("********** ADDING " + current_key + " TO VALID KEYS LIST **********\n");
213 valid_keys.add(current_key);
214 }
215 }
216
217 // Create response
218 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
219 result.setAttribute(GSXML.FROM_ATT, COVERAGE_SERVICE);
220 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
221
222 Element metadataSetList = this.doc.createElement("metadataSetList");
223 result.appendChild(metadataSetList);
224
225
226 // Iterate over valid keys and build up response
227 Element metadataSet = null;
228 Element metadata = null;
229 String value = null;
230 String name = null;
231 iter = valid_keys.iterator();
232
233 while (iter.hasNext()) {
234 current_key = (String) iter.next();
235
236 // Create metadataSet using the current key and add to metadataSetList
237 metadataSet = this.doc.createElement("metadataSet");
238 if(current_key.indexOf("-") != -1)
239 {
240 name = current_key.split("-")[1];
241 }
242 metadataSet.setAttribute(GSXML.NAME_ATT, name);
243 metadataSetList.appendChild(metadataSet);
244
245 // Create a metadata element for each value and add to metadataSet
246 Vector sub_info = collection_info.getMultiInfo(current_key);
247 Iterator iter2 = sub_info.iterator();
248 while (iter2.hasNext()) {
249 value = (String) iter2.next();
250 metadata = this.doc.createElement("metadata");
251 metadata.setAttribute(GSXML.NAME_ATT, value);
252 metadataSet.appendChild(metadata);
253 }
254
255 }
256
257 return result;
258
259 }
260}
261
262
263
Note: See TracBrowser for help on using the repository browser.