[22457] | 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;
|
---|
[25976] | 16 | import org.w3c.dom.Element;
|
---|
[22457] | 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
|
---|
[25976] | 26 | public class CoverageMetadataRetrieve extends ServiceRack
|
---|
| 27 | {
|
---|
[22457] | 28 |
|
---|
[25976] | 29 | // add in a logger for error messages
|
---|
| 30 | static Logger logger = Logger.getLogger("CoverageMetadataRetrieve");
|
---|
[22457] | 31 |
|
---|
[25976] | 32 | protected SimpleCollectionDatabase coll_db = null;
|
---|
| 33 | protected String index_stem = null;
|
---|
[22457] | 34 |
|
---|
[25976] | 35 | // the new service names
|
---|
| 36 | protected static final String COVERAGE_SERVICE = "CoverageMetadataRetrieve";
|
---|
[22457] | 37 |
|
---|
[25976] | 38 | // initialize any custom variables
|
---|
| 39 | public CoverageMetadataRetrieve()
|
---|
| 40 | {
|
---|
[22457] | 41 |
|
---|
[25976] | 42 | }
|
---|
[22457] | 43 |
|
---|
[25976] | 44 | // clean up anything that we need to
|
---|
| 45 | public void cleanUp()
|
---|
| 46 | {
|
---|
| 47 | super.cleanUp();
|
---|
| 48 | }
|
---|
[22457] | 49 |
|
---|
[25976] | 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 | {
|
---|
[22457] | 56 |
|
---|
[25976] | 57 | if (!super.configure(info, extra_info))
|
---|
| 58 | {
|
---|
| 59 | return false;
|
---|
| 60 | }
|
---|
[22457] | 61 |
|
---|
[25976] | 62 | logger.info("Configuring CoverageMetadataRetrieve...");
|
---|
[22457] | 63 |
|
---|
[25976] | 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);
|
---|
[22457] | 71 |
|
---|
[25976] | 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);
|
---|
[22457] | 76 |
|
---|
[25976] | 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 | }
|
---|
[22457] | 88 |
|
---|
[25976] | 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 | }
|
---|
[22457] | 106 |
|
---|
[25976] | 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 | }
|
---|
[22457] | 114 |
|
---|
[25976] | 115 | // Extract any relevant information from info and extra_info
|
---|
| 116 | // This can be used to set up variables.
|
---|
[22457] | 117 |
|
---|
[25976] | 118 | // If there is any formatting information, add it in to format_info_map
|
---|
[22457] | 119 |
|
---|
[25976] | 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 |
|
---|
[22457] | 129 | }
|
---|
| 130 |
|
---|
[25976] | 131 | // get the desription of a service. Could include parameter lists, displayText
|
---|
| 132 | protected Element getServiceDescription(String service, String lang, String subset)
|
---|
| 133 | {
|
---|
[22457] | 134 |
|
---|
[25976] | 135 | // check that we have been asked for the right service
|
---|
| 136 | if (!service.equals(COVERAGE_SERVICE))
|
---|
| 137 | {
|
---|
| 138 | return null;
|
---|
| 139 | }
|
---|
[22457] | 140 |
|
---|
[25976] | 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 | */
|
---|
[22457] | 161 |
|
---|
[25976] | 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 | }
|
---|
[22457] | 173 |
|
---|
[25976] | 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 | }
|
---|
[22457] | 181 |
|
---|
[25976] | 182 | return diff_service;
|
---|
| 183 | }
|
---|
[22457] | 184 |
|
---|
[25976] | 185 | // not a valid service for this class
|
---|
| 186 | return null;
|
---|
[22457] | 187 |
|
---|
[25976] | 188 | }
|
---|
[22457] | 189 |
|
---|
[25976] | 190 | /** This is the method that actually handles the TextQuery Service */
|
---|
| 191 | //protected Element processTextQuery(Element request) {
|
---|
[22457] | 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;
|
---|
[25976] | 199 | //}
|
---|
[22457] | 200 |
|
---|
[25976] | 201 | /** This is the method that actually handles the MyDifferentService service */
|
---|
| 202 | protected Element processCoverageMetadataRetrieve(Element request)
|
---|
| 203 | {
|
---|
[22457] | 204 |
|
---|
[25976] | 205 | if (!this.coll_db.databaseOK())
|
---|
| 206 | {
|
---|
| 207 | logger.error("No valid database found\n");
|
---|
| 208 | return null;
|
---|
| 209 | }
|
---|
[22974] | 210 |
|
---|
[25976] | 211 | DBInfo collection_info = this.coll_db.getInfo("collection");
|
---|
[22457] | 212 |
|
---|
[25976] | 213 | Set<String> keys = collection_info.getKeys();
|
---|
[22457] | 214 |
|
---|
[25976] | 215 | Vector<String> valid_keys = new Vector<String>();
|
---|
[22457] | 216 |
|
---|
[25976] | 217 | // Iterate over keys and add valid ones to the valid_keys vector
|
---|
| 218 | String current_key = null;
|
---|
| 219 | Iterator<String> iter = keys.iterator();
|
---|
[22457] | 220 |
|
---|
[25976] | 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 | }
|
---|
[22457] | 230 |
|
---|
[25976] | 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);
|
---|
[22457] | 235 |
|
---|
[25976] | 236 | Element metadataSetList = this.doc.createElement("metadataSetList");
|
---|
| 237 | result.appendChild(metadataSetList);
|
---|
[22457] | 238 |
|
---|
[25976] | 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();
|
---|
[22457] | 245 |
|
---|
[25976] | 246 | while (iter.hasNext())
|
---|
| 247 | {
|
---|
| 248 | current_key = iter.next();
|
---|
[22457] | 249 |
|
---|
[25976] | 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);
|
---|
[22457] | 258 |
|
---|
[25976] | 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 | }
|
---|
[22457] | 269 |
|
---|
[25976] | 270 | }
|
---|
[22457] | 271 |
|
---|
[25976] | 272 | return result;
|
---|
| 273 |
|
---|
| 274 | }
|
---|
[22457] | 275 | }
|
---|