1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 | // Greenstone classes
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 |
|
---|
6 | //Fedora Web Services classes
|
---|
7 | import fedora.client.utility.AutoFinder;
|
---|
8 | import fedora.client.APIAStubFactory;
|
---|
9 | import fedora.client.Downloader;
|
---|
10 | import fedora.server.access.FedoraAPIA;
|
---|
11 | import fedora.server.types.gen.FieldSearchQuery;
|
---|
12 | import fedora.server.types.gen.FieldSearchResult;
|
---|
13 | import fedora.server.types.gen.ListSession;
|
---|
14 | import fedora.server.types.gen.ObjectFields;
|
---|
15 | import fedora.server.types.gen.Condition;
|
---|
16 | import fedora.server.types.gen.ComparisonOperator;
|
---|
17 |
|
---|
18 | import javax.xml.transform.stream.StreamResult;
|
---|
19 | import javax.xml.transform.stream.StreamSource;
|
---|
20 | import javax.xml.transform.Templates;
|
---|
21 | import javax.xml.transform.Transformer;
|
---|
22 | import javax.xml.transform.TransformerFactory;
|
---|
23 |
|
---|
24 | // XML classes
|
---|
25 | import org.w3c.dom.Element;
|
---|
26 | import org.w3c.dom.Document;
|
---|
27 | import org.w3c.dom.NodeList;
|
---|
28 |
|
---|
29 | //Java classes
|
---|
30 | import java.util.ArrayList;
|
---|
31 | import java.util.HashMap;
|
---|
32 | import java.util.List;
|
---|
33 | import java.util.Properties;
|
---|
34 | import java.io.File;
|
---|
35 | import java.io.InputStream;
|
---|
36 | import java.io.BufferedReader;
|
---|
37 | import java.io.InputStreamReader;
|
---|
38 | import java.io.IOException;
|
---|
39 | import java.net.HttpURLConnection;
|
---|
40 | import java.net.URLConnection;
|
---|
41 | import java.net.URL;
|
---|
42 | import java.net.Authenticator;
|
---|
43 | import java.net.MalformedURLException;
|
---|
44 | import java.lang.Object;
|
---|
45 |
|
---|
46 | import org.apache.log4j.*;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | *
|
---|
50 | *
|
---|
51 | */
|
---|
52 |
|
---|
53 | public class FedoraSearch
|
---|
54 | extends AbstractSearch {
|
---|
55 |
|
---|
56 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.FedoraSearch.class.getName());
|
---|
57 | // the services on offer
|
---|
58 | // these strings must match what is found in the properties file
|
---|
59 | protected static final String FIELD_PARAM = "fields";
|
---|
60 |
|
---|
61 | //Parameters connect to Proxy Server
|
---|
62 | private String proxyUser = null;
|
---|
63 | private String proxyPassword = null;
|
---|
64 |
|
---|
65 | private int maxResult, matchNum;
|
---|
66 |
|
---|
67 | protected String fedora_server_url = null;
|
---|
68 |
|
---|
69 | public FedoraSearch()
|
---|
70 | {
|
---|
71 | QUERY_SERVICE = "TextQuery";
|
---|
72 | }
|
---|
73 |
|
---|
74 | //Configure FedoraSearch Service
|
---|
75 | public boolean configure(Element info, Element extra_info)
|
---|
76 | {
|
---|
77 | Element server_elem = (Element)GSXML.getChildByTagName(info, "fedoraServer");
|
---|
78 | if (server_elem == null) {
|
---|
79 | logger.error("no fedoraServer element found");
|
---|
80 | return false;
|
---|
81 | }
|
---|
82 | fedora_server_url = server_elem.getAttribute("url");
|
---|
83 | logger.info("Fedora Server:" + fedora_server_url);
|
---|
84 |
|
---|
85 | if (fedora_server_url.equals("")) {
|
---|
86 | logger.error("no url for the fedoraServer element");
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 | Element tq_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
90 | tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
91 | tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
|
---|
92 | this.short_service_info.appendChild(tq_service);
|
---|
93 |
|
---|
94 | // add some format info to service map if there is any
|
---|
95 | String path = GSPath.appendLink(GSXML.SEARCH_ELEM, GSXML.FORMAT_ELEM);
|
---|
96 | Element format = (Element) GSXML.getNodeByPath(extra_info, path);
|
---|
97 | if (format != null) {
|
---|
98 | this.format_info_map.put(QUERY_SERVICE, this.desc_doc.importNode(format, true));
|
---|
99 | }
|
---|
100 | return true;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Process a text query - implemented by concrete subclasses */
|
---|
104 | protected Element processTextQuery(Element request) {
|
---|
105 | // Create a new (empty) result message
|
---|
106 | Document result_doc = XMLConverter.newDOM();
|
---|
107 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
108 | result.setAttribute(GSXML.FROM_ATT, QUERY_SERVICE);
|
---|
109 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
110 |
|
---|
111 | Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
112 | result.appendChild(doc_node_list);
|
---|
113 |
|
---|
114 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
115 | if (param_list == null) {
|
---|
116 | logger.error("TextQuery request had no paramList.");
|
---|
117 | return result; // Return the empty result
|
---|
118 | }
|
---|
119 |
|
---|
120 | // Process the request parameters
|
---|
121 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
122 |
|
---|
123 | // Make sure a query has been specified
|
---|
124 | String query = (String) params.get(QUERY_PARAM);
|
---|
125 | if (query == null || query.equals("")) {
|
---|
126 | return result; // Return the empty result
|
---|
127 | }
|
---|
128 | //Check Max number of Documents hit
|
---|
129 | String maxDocs = (String) params.get(MAXDOCS_PARAM);
|
---|
130 | String hits_per_page = (String) params.get(HITS_PER_PAGE_PARAM);
|
---|
131 |
|
---|
132 | //Check the start_page number
|
---|
133 | String start_page = (String) params.get(START_PAGE_PARAM);
|
---|
134 |
|
---|
135 | // tidy whitespace
|
---|
136 | //query = query.replaceAll("\\s+", "+");
|
---|
137 | matchNum = 0;
|
---|
138 | maxResult = 20;
|
---|
139 |
|
---|
140 | try {
|
---|
141 | //Search Fedora Server
|
---|
142 | String host = "toetoe";
|
---|
143 | int port = 8200;
|
---|
144 | String user = "fedoraAdmin";
|
---|
145 | String pass = "fedoraAdmin";
|
---|
146 |
|
---|
147 | AutoFinder finder=new AutoFinder(host, port, user, pass);
|
---|
148 |
|
---|
149 | String[] fieldsArray = new String[] {"pid", "label", "fType", "bDef",
|
---|
150 | "bMech", "cModel", "state", "ownerId", "cDate", "mDate",
|
---|
151 | "dcmDate", "title", "creator", "subject", "description",
|
---|
152 | "publisher", "contributor", "date", "type", "format",
|
---|
153 | "identifier", "source", "language", "relation", "coverage",
|
---|
154 | "rights"};
|
---|
155 |
|
---|
156 | String match_fields = join(fieldsArray, "|");
|
---|
157 |
|
---|
158 | FieldSearchQuery query_fedora=fedoraSearch(query, match_fields);
|
---|
159 |
|
---|
160 | FieldSearchResult result_fedora = finder.findObjects(fieldsArray, maxResult, query_fedora);
|
---|
161 |
|
---|
162 | while (result_fedora != null) {
|
---|
163 | logger.info("Search Result:" + result_fedora.getResultList().length);
|
---|
164 | for (int i=0; i<result_fedora.getResultList().length; i++) {
|
---|
165 | ObjectFields o = result_fedora.getResultList()[i];
|
---|
166 | matchNum++;
|
---|
167 | String pid = o.getPid();
|
---|
168 | String fedora_url = fedora_server_url +"/fedora/get/" + pid;
|
---|
169 | String label = o.getLabel();
|
---|
170 | String[] creator = o.getCreator();
|
---|
171 | String[] subject = o.getSubject();
|
---|
172 | String[] publisher = o.getPublisher();
|
---|
173 | String[] contributor = o.getContributor();
|
---|
174 | String[] title = o.getTitle();
|
---|
175 | String[] description = o.getDescription();
|
---|
176 | String[] format = o.getFormat();
|
---|
177 | String[] identifier = o.getIdentifier();
|
---|
178 | String[] source = o.getSource();
|
---|
179 | String[] language = o.getLanguage();
|
---|
180 |
|
---|
181 | Element doc_node = result_doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
182 | doc_node_list.appendChild(doc_node);
|
---|
183 |
|
---|
184 | Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
185 | doc_node.appendChild(metadata_list);
|
---|
186 |
|
---|
187 | if (fedora_url != null) {
|
---|
188 | GSXML.addMetadata( metadata_list, "URL", fedora_url);
|
---|
189 | }
|
---|
190 | if (o.getDescription() != null) {
|
---|
191 | GSXML.addMetadata(metadata_list, "Description", description[0]);
|
---|
192 | }
|
---|
193 | if (title[0] != null) {
|
---|
194 | GSXML.addMetadata(metadata_list, "Title", title[0]);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | ListSession sess=result_fedora.getListSession();
|
---|
198 | if (sess!=null) {
|
---|
199 | result_fedora = finder.resumeFindObjects(sess.getToken());
|
---|
200 | } else {
|
---|
201 | result_fedora = null;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | } catch (Exception e) {
|
---|
205 | logger.error("ERROR: " + e.getClass().getName()
|
---|
206 | + ((e.getMessage()==null) ? "" : ": " + e.getMessage()));
|
---|
207 | }
|
---|
208 |
|
---|
209 | Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
210 | result.appendChild(metadata_list);
|
---|
211 |
|
---|
212 | // Add a metadata element specifying the number of matching documents
|
---|
213 | //logger.info("FedoraSearch Number of Documents:" + matchNum);
|
---|
214 | GSXML.addMetadata(metadata_list, "numDocsMatched", ""+matchNum);
|
---|
215 |
|
---|
216 | return result;
|
---|
217 | }
|
---|
218 |
|
---|
219 | protected FieldSearchQuery fedoraSearch(String query_terms, String matchFields) {
|
---|
220 |
|
---|
221 | FieldSearchQuery fedora_query=new FieldSearchQuery();
|
---|
222 | try {
|
---|
223 | // check if query contains any fields name (pid, label, fType....)
|
---|
224 | if (query_terms.matches("^("+ matchFields+").+")){
|
---|
225 | logger.info("Implementing Specific Query....");
|
---|
226 |
|
---|
227 | List conditions=fedora.server.search.Condition.getConditions(query_terms);
|
---|
228 |
|
---|
229 | Condition[] cond=new Condition[conditions.size()];
|
---|
230 | for (int i=0; i<conditions.size(); i++) {
|
---|
231 | fedora.server.search.Condition c = (fedora.server.search.Condition) conditions.get(i);
|
---|
232 | try {
|
---|
233 | String operator = transOperator(c.getOperator().getAbbreviation());
|
---|
234 | cond[i] = new Condition();
|
---|
235 | cond[i].setProperty(c.getProperty());
|
---|
236 | cond[i].setOperator(ComparisonOperator.fromValue(operator));
|
---|
237 | cond[i].setValue(c.getValue());
|
---|
238 | } catch (Exception e){
|
---|
239 | e.printStackTrace();
|
---|
240 | }
|
---|
241 | }
|
---|
242 | fedora_query.setConditions(cond);
|
---|
243 | } else {
|
---|
244 | logger.info("Implementing Simple Query...");
|
---|
245 | fedora_query.setTerms(query_terms);
|
---|
246 | }
|
---|
247 | } catch (Exception e){
|
---|
248 | e.printStackTrace();
|
---|
249 | }
|
---|
250 | return fedora_query;
|
---|
251 | }
|
---|
252 |
|
---|
253 | protected void getIndexData(ArrayList index_ids, ArrayList index_names,String lang){
|
---|
254 | index_ids.add("kw");
|
---|
255 | index_ids.add("au");
|
---|
256 | index_ids.add("su");
|
---|
257 | index_ids.add("ti");
|
---|
258 | index_ids.add("de");
|
---|
259 | index_ids.add("fu");
|
---|
260 | index_names.add(getTextString("param."+FIELD_PARAM+".kw", lang));
|
---|
261 | index_names.add(getTextString("param."+FIELD_PARAM+".au", lang));
|
---|
262 | index_names.add(getTextString("param."+FIELD_PARAM+".su", lang));
|
---|
263 | index_names.add(getTextString("param."+FIELD_PARAM+".ti", lang));
|
---|
264 | index_names.add(getTextString("param."+FIELD_PARAM+".de", lang));
|
---|
265 | index_names.add(getTextString("param."+FIELD_PARAM+".fu", lang));
|
---|
266 | }
|
---|
267 |
|
---|
268 | protected String getDocType (String node_id){
|
---|
269 | return node_id;
|
---|
270 | }
|
---|
271 | protected boolean hasChildren(String node_id){
|
---|
272 | return false;
|
---|
273 | }
|
---|
274 |
|
---|
275 | protected boolean hasParent(String node_id){
|
---|
276 | return false;
|
---|
277 | }
|
---|
278 | private String transOperator(String operator){
|
---|
279 | String oper_char ="";
|
---|
280 | if (operator == "~") {
|
---|
281 | oper_char = "has";
|
---|
282 | } else if (operator == "=") {
|
---|
283 | oper_char = "eq";
|
---|
284 | } else if (operator == ">"){
|
---|
285 | oper_char = "gt";
|
---|
286 | } else if (operator == ">="){
|
---|
287 | oper_char = "ge";
|
---|
288 | } else if (operator == "<"){
|
---|
289 | oper_char = "lt";
|
---|
290 | } else if (operator == "<="){
|
---|
291 | oper_char = "le";
|
---|
292 | }
|
---|
293 | return oper_char;
|
---|
294 | }
|
---|
295 | protected String join( String[] array, String delim ) {
|
---|
296 | String j = "";
|
---|
297 | for ( int i=0; i<array.length; i++ ) {
|
---|
298 | if (i!=0) j += delim;
|
---|
299 | j += array[i];
|
---|
300 | }
|
---|
301 | return j;
|
---|
302 | }
|
---|
303 | }
|
---|