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

Last change on this file since 25976 was 25976, checked in by sjm84, 12 years ago

Reformatting this file ahead of some changes

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