root/main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/FedoraSearch.java.tmp @ 28969

Revision 28969, 9.8 KB (checked in by kjdon, 6 years ago)

removing this.doc. Haven't tested as can't compile properly without Fedora/Google classes. But there are no errors regarding this.doc. Also, not sure if these have been updated properly after restructuring around AbstractSearch?. Will need testing properly if they are to be used again.

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