1 | /*
|
---|
2 | * PharosImageISServiceRack.java
|
---|
3 | * Copyright (C) 2009 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 | */
|
---|
19 | package imageis;
|
---|
20 |
|
---|
21 |
|
---|
22 | // Greenstone classes
|
---|
23 | import org.greenstone.gsdl3.service.ServiceRack;
|
---|
24 | import org.greenstone.gsdl3.util.GSXML;
|
---|
25 | import org.greenstone.gsdl3.util.GSPath;
|
---|
26 |
|
---|
27 | // XML classes
|
---|
28 | import org.w3c.dom.Document;
|
---|
29 | import org.w3c.dom.Element;
|
---|
30 | import org.w3c.dom.NodeList;
|
---|
31 |
|
---|
32 | // java classes
|
---|
33 | import java.util.ArrayList;
|
---|
34 | import java.util.HashMap;
|
---|
35 |
|
---|
36 | // pharos
|
---|
37 | //import PharosImageIS;
|
---|
38 |
|
---|
39 | import org.apache.log4j.*;
|
---|
40 |
|
---|
41 | /** Pharos Image Similarity Search service
|
---|
42 | *
|
---|
43 | * @author Katherine Don
|
---|
44 | */
|
---|
45 |
|
---|
46 | public class PharosImageISServiceRack
|
---|
47 | extends ServiceRack
|
---|
48 | {
|
---|
49 |
|
---|
50 | static Logger logger = Logger.getLogger(pharosimageis.PharosImageISServiceRack.class.getName());
|
---|
51 |
|
---|
52 |
|
---|
53 | // the search service
|
---|
54 | protected static final String PHAROS_QUERY_SERVICE = "PharosQuery";
|
---|
55 |
|
---|
56 | // compulsory params
|
---|
57 | protected static final String IMAGE_ID_PARAM = "id";
|
---|
58 | //protected static final String COLLECTION_PARAM = "collection";
|
---|
59 | protected ImageIS pharos_engine = null;
|
---|
60 |
|
---|
61 | public PharosImageISServiceRack()
|
---|
62 | {
|
---|
63 | pharos_engine = new ImageIS();
|
---|
64 | }
|
---|
65 |
|
---|
66 | /** sets up the short service info for PharosQuery. If other services
|
---|
67 | * will be provided, should be added in the subclass configure
|
---|
68 | * also looks for search format info, and document format info
|
---|
69 | */
|
---|
70 | public boolean configure(Element info, Element extra_info)
|
---|
71 | {
|
---|
72 | if (!super.configure(info, extra_info)){
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | logger.info("Configuring PharosImageISServiceRack...");
|
---|
77 |
|
---|
78 | this.config_info = info;
|
---|
79 |
|
---|
80 | // set up short_service_info_ - for now just has id and type. the name (lang dependent) will be added in if the list is requested.
|
---|
81 | Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
82 | tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
83 | tq_service.setAttribute(GSXML.NAME_ATT, PHAROS_QUERY_SERVICE);
|
---|
84 | this.short_service_info.appendChild(tq_service);
|
---|
85 |
|
---|
86 | // add some format info to service map if there is any - look in extra info
|
---|
87 | // first look in buildConfig
|
---|
88 | Element format = (Element)GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
|
---|
89 |
|
---|
90 | if (format==null) {
|
---|
91 | String path = GSPath.appendLink(GSXML.SEARCH_ELEM, GSXML.FORMAT_ELEM);
|
---|
92 |
|
---|
93 | //note by xiao: instead of retrieving the first 'format' element inside the 'search'
|
---|
94 | // element, we are trying to find the real format element which has at least one
|
---|
95 | // 'gsf:template' child element. (extra_info is collectionConfig.xml)
|
---|
96 | //format = (Element) GSXML.getNodeByPath(extra_info, path);
|
---|
97 | Element search_elem = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
|
---|
98 | NodeList format_elems = null;
|
---|
99 | if (search_elem != null) {
|
---|
100 | format_elems = search_elem.getElementsByTagName(GSXML.FORMAT_ELEM);
|
---|
101 | }
|
---|
102 | for(int i=0; i<format_elems.getLength(); i++) {
|
---|
103 | format = (Element)format_elems.item(i);
|
---|
104 | if (format.getElementsByTagName("gsf:template").getLength() != 0) {
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }//end of if(format==null)
|
---|
109 | //
|
---|
110 | if (format != null) {
|
---|
111 | this.format_info_map.put(PHAROS_QUERY_SERVICE, this.doc.importNode(format, true));
|
---|
112 | }
|
---|
113 |
|
---|
114 | // is the following relevant here. and what is it anyway??
|
---|
115 | // look for document display format - for documentType
|
---|
116 | /* String path = GSPath.appendLink(GSXML.DISPLAY_ELEM, GSXML.FORMAT_ELEM);
|
---|
117 | Element display_format = (Element)GSXML.getNodeByPath(extra_info, path);
|
---|
118 | if (display_format != null) {
|
---|
119 | // check for docType option.
|
---|
120 | Element doc_type_opt = GSXML.getNamedElement(display_format, "gsf:option", GSXML.NAME_ATT, "documentType");
|
---|
121 | if (doc_type_opt != null) {
|
---|
122 | String value = doc_type_opt.getAttribute(GSXML.VALUE_ATT);
|
---|
123 | if (!value.equals("")) {
|
---|
124 | this.default_document_type = value;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }*/
|
---|
128 |
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /** returns the description of the TextQuery service. If a subclass
|
---|
133 | * provides other services they need to provides their own descriptions */
|
---|
134 | public Element getServiceDescription(String service, String lang, String subset)
|
---|
135 | {
|
---|
136 | if (!service.equals(PHAROS_QUERY_SERVICE)) {
|
---|
137 | return null;
|
---|
138 | }
|
---|
139 |
|
---|
140 | Element pq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
141 | pq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
142 | pq_service.setAttribute(GSXML.NAME_ATT, PHAROS_QUERY_SERVICE);
|
---|
143 | if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
144 | pq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(PHAROS_QUERY_SERVICE, lang) ));
|
---|
145 | pq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getServiceSubmit(PHAROS_QUERY_SERVICE, lang) ));
|
---|
146 | pq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(PHAROS_QUERY_SERVICE, lang)));
|
---|
147 | }
|
---|
148 | if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
149 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
150 | Element param = GSXML.createParameterDescription(this.doc, IMAGE_ID_PARAM, getTextString("param."+IMAGE_ID_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
|
---|
151 | param_list.appendChild(param);
|
---|
152 |
|
---|
153 | pq_service.appendChild(param_list);
|
---|
154 | }
|
---|
155 | return pq_service;
|
---|
156 |
|
---|
157 | }
|
---|
158 |
|
---|
159 | // perhaps these should be changed to search down the class hierarchy for
|
---|
160 | // values - do can just put the info in the resource bundle to use it
|
---|
161 | /** returns the default name a service */
|
---|
162 | public String getServiceName(String service_id, String lang) {
|
---|
163 | return getTextString(service_id+".name", lang);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** returns the default description for a service */
|
---|
167 | public String getServiceDescription(String service_id, String lang) {
|
---|
168 | return getTextString(service_id+".description", lang);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** returns the default submit button text for a service */
|
---|
172 | public String getServiceSubmit(String service_id, String lang) {
|
---|
173 | return getTextString(service_id+".submit", lang);
|
---|
174 |
|
---|
175 | }
|
---|
176 | /** create an element to go into the search results list. A node element
|
---|
177 | * has the form
|
---|
178 | * <docNode nodeId='xxx' nodeType='root' docType='simple' rank='0.23'/>
|
---|
179 | */
|
---|
180 | public Element createDocNode(String node_id, String rank) {
|
---|
181 | Element node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
182 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
183 | node.setAttribute(GSXML.NODE_RANK_ATT, rank);
|
---|
184 | // default values for now
|
---|
185 | node.setAttribute(GSXML.DOC_TYPE_ATT, "simple");
|
---|
186 | node.setAttribute(GSXML.NODE_TYPE_ATT, "root");
|
---|
187 | return node;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | /** do the actual query
|
---|
192 | * must be implemented by subclass */
|
---|
193 | public Element processPharosQuery(Element request) {
|
---|
194 |
|
---|
195 | // Create a new (empty) result message ('doc' is in ServiceRack.java)
|
---|
196 | Element result = this.doc.createElement (GSXML.RESPONSE_ELEM);
|
---|
197 | result.setAttribute (GSXML.FROM_ATT, PHAROS_QUERY_SERVICE);
|
---|
198 | result.setAttribute (GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
199 |
|
---|
200 | // Get the parameters of the request
|
---|
201 | Element param_list = (Element) GSXML.getChildByTagName (request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
202 | if (param_list == null) {
|
---|
203 | logger.error ("PharosQuery request had no paramList.");
|
---|
204 | return result; // Return the empty result
|
---|
205 | }
|
---|
206 |
|
---|
207 | // Process the request parameters
|
---|
208 | HashMap params = GSXML.extractParams (param_list, false);
|
---|
209 |
|
---|
210 | // Make sure an id has been specified
|
---|
211 | String query = (String) params.get (IMAGE_ID_PARAM);
|
---|
212 | if (query == null || query.equals ("")) {
|
---|
213 | return result; // Return the empty result
|
---|
214 | }
|
---|
215 |
|
---|
216 | String search_results = null;
|
---|
217 | try {
|
---|
218 | search_results = pharos_engine.query(this.cluster_name, query);
|
---|
219 | } catch (Exception e) {
|
---|
220 | logger.error(e.getMessage(), e);
|
---|
221 | }
|
---|
222 | if (search_results == null || search_results.equals("")) {
|
---|
223 | logger.error("Empty results string");
|
---|
224 | return result;
|
---|
225 | }
|
---|
226 |
|
---|
227 | Element document_list = this.doc.createElement (GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
228 |
|
---|
229 | try {
|
---|
230 | Document result_xml = this.converter.getDOM(search_results);
|
---|
231 | if (result_xml == null) {
|
---|
232 | logger.error("Couldn't parse XML");
|
---|
233 | return result;
|
---|
234 | }
|
---|
235 |
|
---|
236 | Element result_element = result_xml.getDocumentElement();
|
---|
237 | NodeList result_items = result_element.getElementsByTagName("ResultItem");
|
---|
238 | String coll_prefix = this.cluster_name+":";
|
---|
239 | int prefix_len = coll_prefix.length();
|
---|
240 | for (int i=0; i<result_items.getLength(); i++) {
|
---|
241 | Element item = (Element)result_items.item(i);
|
---|
242 | String rank = item.getAttribute("confidence");
|
---|
243 | Element resource = (Element)GSXML.getChildByTagName(item, "MediaResource");
|
---|
244 | String id = GSXML.getNodeText(resource);
|
---|
245 |
|
---|
246 | System.err.println("id = "+id+", rank = "+rank);
|
---|
247 | if (id.startsWith(coll_prefix)) {
|
---|
248 |
|
---|
249 | Element result_node = createDocNode(id.substring(prefix_len), rank);
|
---|
250 | document_list.appendChild(result_node);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | } catch (Exception e) {
|
---|
255 | logger.error(e.getMessage(), e);
|
---|
256 | }
|
---|
257 |
|
---|
258 | result.appendChild (document_list);
|
---|
259 |
|
---|
260 | return result;
|
---|
261 |
|
---|
262 |
|
---|
263 |
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | }
|
---|
268 |
|
---|