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
|
---|
9 | package org.greenstone.gsdl3.service;
|
---|
10 |
|
---|
11 | // Greenstone classes
|
---|
12 | import org.greenstone.gsdl3.util.*;
|
---|
13 |
|
---|
14 | // XML classes
|
---|
15 | import org.w3c.dom.Document;
|
---|
16 | import org.w3c.dom.Element;
|
---|
17 | import org.w3c.dom.NodeList;
|
---|
18 |
|
---|
19 | import org.apache.log4j.*;
|
---|
20 |
|
---|
21 | import java.util.Iterator;
|
---|
22 | import java.util.Vector;
|
---|
23 | import java.util.Set;
|
---|
24 |
|
---|
25 | // change the class name (and the filename) to something more appropriate
|
---|
26 | public 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.desc_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.desc_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.desc_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(Document doc, 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 | * 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(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 | * 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 = 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(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 = 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 | Document result_doc = XMLConverter.newDOM();
|
---|
233 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
234 | result.setAttribute(GSXML.FROM_ATT, COVERAGE_SERVICE);
|
---|
235 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
236 |
|
---|
237 | Element metadataSetList = result_doc.createElement("metadataSetList");
|
---|
238 | result.appendChild(metadataSetList);
|
---|
239 |
|
---|
240 | // Iterate over valid keys and build up response
|
---|
241 | Element metadataSet = null;
|
---|
242 | Element metadata = null;
|
---|
243 | String value = null;
|
---|
244 | String name = null;
|
---|
245 | iter = valid_keys.iterator();
|
---|
246 |
|
---|
247 | while (iter.hasNext())
|
---|
248 | {
|
---|
249 | current_key = iter.next();
|
---|
250 |
|
---|
251 | // Create metadataSet using the current key and add to metadataSetList
|
---|
252 | metadataSet = result_doc.createElement("metadataSet");
|
---|
253 | if (current_key.indexOf("-") != -1)
|
---|
254 | {
|
---|
255 | name = current_key.split("-")[1];
|
---|
256 | }
|
---|
257 | metadataSet.setAttribute(GSXML.NAME_ATT, name);
|
---|
258 | metadataSetList.appendChild(metadataSet);
|
---|
259 |
|
---|
260 | // Create a metadata element for each value and add to metadataSet
|
---|
261 | Vector<String> sub_info = collection_info.getMultiInfo(current_key);
|
---|
262 | Iterator<String> iter2 = sub_info.iterator();
|
---|
263 | while (iter2.hasNext())
|
---|
264 | {
|
---|
265 | value = iter2.next();
|
---|
266 | metadata = result_doc.createElement("metadata");
|
---|
267 | metadata.setAttribute(GSXML.NAME_ATT, value);
|
---|
268 | metadataSet.appendChild(metadata);
|
---|
269 | }
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|
273 | return result;
|
---|
274 |
|
---|
275 | }
|
---|
276 | }
|
---|