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(imageis.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 pq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
82 | pq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
83 | pq_service.setAttribute(GSXML.NAME_ATT, PHAROS_QUERY_SERVICE);
|
---|
84 | pq_service.setAttribute(GSXML.HIDDEN_ATT, "true");
|
---|
85 | this.short_service_info.appendChild(pq_service);
|
---|
86 |
|
---|
87 | // add some format info to service map if there is any - look in extra info
|
---|
88 | // first look in buildConfig
|
---|
89 | Element format = (Element)GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
|
---|
90 |
|
---|
91 | if (format==null) {
|
---|
92 | String path = GSPath.appendLink(GSXML.SEARCH_ELEM, GSXML.FORMAT_ELEM);
|
---|
93 |
|
---|
94 | //note by xiao: instead of retrieving the first 'format' element inside the 'search'
|
---|
95 | // element, we are trying to find the real format element which has at least one
|
---|
96 | // 'gsf:template' child element. (extra_info is collectionConfig.xml)
|
---|
97 | //format = (Element) GSXML.getNodeByPath(extra_info, path);
|
---|
98 | Element search_elem = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
|
---|
99 | NodeList format_elems = null;
|
---|
100 | if (search_elem != null) {
|
---|
101 | format_elems = search_elem.getElementsByTagName(GSXML.FORMAT_ELEM);
|
---|
102 | }
|
---|
103 | for(int i=0; i<format_elems.getLength(); i++) {
|
---|
104 | format = (Element)format_elems.item(i);
|
---|
105 | if (format.getElementsByTagName("gsf:template").getLength() != 0) {
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }//end of if(format==null)
|
---|
110 | //
|
---|
111 | if (format != null) {
|
---|
112 | this.format_info_map.put(PHAROS_QUERY_SERVICE, this.doc.importNode(format, true));
|
---|
113 | }
|
---|
114 |
|
---|
115 | // is the following relevant here. and what is it anyway??
|
---|
116 | // look for document display format - for documentType
|
---|
117 | /* String path = GSPath.appendLink(GSXML.DISPLAY_ELEM, GSXML.FORMAT_ELEM);
|
---|
118 | Element display_format = (Element)GSXML.getNodeByPath(extra_info, path);
|
---|
119 | if (display_format != null) {
|
---|
120 | // check for docType option.
|
---|
121 | Element doc_type_opt = GSXML.getNamedElement(display_format, "gsf:option", GSXML.NAME_ATT, "documentType");
|
---|
122 | if (doc_type_opt != null) {
|
---|
123 | String value = doc_type_opt.getAttribute(GSXML.VALUE_ATT);
|
---|
124 | if (!value.equals("")) {
|
---|
125 | this.default_document_type = value;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }*/
|
---|
129 |
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** returns the description of the TextQuery service. If a subclass
|
---|
134 | * provides other services they need to provides their own descriptions */
|
---|
135 | public Element getServiceDescription(String service, String lang, String subset)
|
---|
136 | {
|
---|
137 | if (!service.equals(PHAROS_QUERY_SERVICE)) {
|
---|
138 | return null;
|
---|
139 | }
|
---|
140 |
|
---|
141 | Element pq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
142 | pq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
143 | pq_service.setAttribute(GSXML.NAME_ATT, PHAROS_QUERY_SERVICE);
|
---|
144 | pq_service.setAttribute(GSXML.HIDDEN_ATT, "true");
|
---|
145 | if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
146 | pq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(PHAROS_QUERY_SERVICE, lang) ));
|
---|
147 | pq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getServiceSubmit(PHAROS_QUERY_SERVICE, lang) ));
|
---|
148 | pq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(PHAROS_QUERY_SERVICE, lang)));
|
---|
149 | }
|
---|
150 | if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
151 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
152 | Element param = GSXML.createParameterDescription(this.doc, IMAGE_ID_PARAM, getTextString("param."+IMAGE_ID_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
|
---|
153 | param_list.appendChild(param);
|
---|
154 |
|
---|
155 | pq_service.appendChild(param_list);
|
---|
156 | }
|
---|
157 | return pq_service;
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | // perhaps these should be changed to search down the class hierarchy for
|
---|
162 | // values - do can just put the info in the resource bundle to use it
|
---|
163 | /** returns the default name a service */
|
---|
164 | public String getServiceName(String service_id, String lang) {
|
---|
165 | return getTextString(service_id+".name", lang);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** returns the default description for a service */
|
---|
169 | public String getServiceDescription(String service_id, String lang) {
|
---|
170 | return getTextString(service_id+".description", lang);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** returns the default submit button text for a service */
|
---|
174 | public String getServiceSubmit(String service_id, String lang) {
|
---|
175 | return getTextString(service_id+".submit", lang);
|
---|
176 |
|
---|
177 | }
|
---|
178 | /** create an element to go into the search results list. A node element
|
---|
179 | * has the form
|
---|
180 | * <docNode nodeId='xxx' nodeType='root' docType='simple' rank='0.23'/>
|
---|
181 | */
|
---|
182 | public Element createDocNode(String node_id, String rank) {
|
---|
183 | Element node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
184 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
185 | node.setAttribute(GSXML.NODE_RANK_ATT, rank);
|
---|
186 | // default values for now
|
---|
187 | node.setAttribute(GSXML.DOC_TYPE_ATT, "simple");
|
---|
188 | node.setAttribute(GSXML.NODE_TYPE_ATT, "root");
|
---|
189 | return node;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /** do the actual query
|
---|
194 | * must be implemented by subclass */
|
---|
195 | public Element processPharosQuery(Element request) {
|
---|
196 |
|
---|
197 | // Create a new (empty) result message ('doc' is in ServiceRack.java)
|
---|
198 | Element result = this.doc.createElement (GSXML.RESPONSE_ELEM);
|
---|
199 | result.setAttribute (GSXML.FROM_ATT, PHAROS_QUERY_SERVICE);
|
---|
200 | result.setAttribute (GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
201 |
|
---|
202 | // Get the parameters of the request
|
---|
203 | Element param_list = (Element) GSXML.getChildByTagName (request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
204 | if (param_list == null) {
|
---|
205 | logger.error ("PharosQuery request had no paramList.");
|
---|
206 | return result; // Return the empty result
|
---|
207 | }
|
---|
208 |
|
---|
209 | // Process the request parameters
|
---|
210 | HashMap params = GSXML.extractParams (param_list, false);
|
---|
211 |
|
---|
212 | // Make sure an id has been specified
|
---|
213 | String query = (String) params.get (IMAGE_ID_PARAM);
|
---|
214 | if (query == null || query.equals ("")) {
|
---|
215 | return result; // Return the empty result
|
---|
216 | }
|
---|
217 |
|
---|
218 | String search_results = null;
|
---|
219 | try {
|
---|
220 | logger.error("querying on "+this.cluster_name+", "+query);
|
---|
221 | search_results = pharos_engine.query(this.cluster_name, query);
|
---|
222 | } catch (Exception e) {
|
---|
223 | logger.error(e.getMessage(), e);
|
---|
224 | }
|
---|
225 | if (search_results == null || search_results.equals("")) {
|
---|
226 | logger.error("Empty results string");
|
---|
227 | return result;
|
---|
228 | }
|
---|
229 |
|
---|
230 | Element document_list = this.doc.createElement (GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
231 |
|
---|
232 | try {
|
---|
233 | Document result_xml = this.converter.getDOM(search_results);
|
---|
234 | if (result_xml == null) {
|
---|
235 | logger.error("Couldn't parse XML");
|
---|
236 | return result;
|
---|
237 | }
|
---|
238 |
|
---|
239 | Element result_element = result_xml.getDocumentElement();
|
---|
240 | NodeList result_items = result_element.getElementsByTagName("ResultItem");
|
---|
241 | String coll_prefix = this.cluster_name+":";
|
---|
242 | int prefix_len = coll_prefix.length();
|
---|
243 | for (int i=0; i<result_items.getLength(); i++) {
|
---|
244 | Element item = (Element)result_items.item(i);
|
---|
245 | String rank = item.getAttribute("confidence");
|
---|
246 | Element resource = (Element)GSXML.getChildByTagName(item, "MediaResource");
|
---|
247 | String id = GSXML.getNodeText(resource);
|
---|
248 |
|
---|
249 | System.err.println("id = "+id+", rank = "+rank);
|
---|
250 | if (id.startsWith(coll_prefix)) {
|
---|
251 | id = id.substring(prefix_len);
|
---|
252 | id = id.replace(".jpg","");
|
---|
253 | Element result_node = createDocNode(id, rank);
|
---|
254 | document_list.appendChild(result_node);
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | } catch (Exception e) {
|
---|
259 | logger.error(e.getMessage(), e);
|
---|
260 | }
|
---|
261 |
|
---|
262 | result.appendChild (document_list);
|
---|
263 |
|
---|
264 | return result;
|
---|
265 |
|
---|
266 |
|
---|
267 |
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|