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

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

Lots of changes. Mainly to do with removing this.doc from everywhere. Document is not thread safe. Now we tend to create a new Document everytime we are starting a new page/message etc. in service this.desc_doc is available as teh document to create service info stuff. But it should only be used for this and not for other messages. newDOM is now static for XMLConverter. method param changes for some GSXML methods.

  • 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    }
72   
73    //Configure FedoraSearch Service
74    public boolean configure(Element info, Element extra_info)
75    {
76    Element server_elem = (Element)GSXML.getChildByTagName(info, "fedoraServer");
77    if (server_elem == null) {
78        logger.error("no fedoraServer element found");
79        return false;
80    }
81    fedora_server_url = server_elem.getAttribute("url");
82    logger.info("Fedora Server:" + fedora_server_url);
83
84    if (fedora_server_url.equals("")) {
85        logger.error("no url for the fedoraServer element");
86        return false;
87    }
88    Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
89    tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
90    tq_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
91    this.short_service_info.appendChild(tq_service);
92    //does_paging = true;
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(TEXT_QUERY_SERVICE, this.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    Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
107    result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
108    result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
109   
110    Element doc_node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
111    result.appendChild(doc_node_list);
112   
113    Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
114    if (param_list == null) {
115        logger.error("TextQuery request had no paramList.");
116        return result;  // Return the empty result
117    }
118   
119    // Process the request parameters
120    HashMap params = GSXML.extractParams(param_list, false);
121   
122    // Make sure a query has been specified
123    String query = (String) params.get(QUERY_PARAM);
124    if (query == null || query.equals("")) {
125        return result;  // Return the empty result
126    }
127    //Check Max number of Documents hit
128    String maxDocs = (String) params.get(MAXDOCS_PARAM);
129        String hits_per_page = (String) params.get(HITS_PER_PAGE_PARAM);
130   
131    //Check the start_page number
132    String start_page = (String) params.get(START_PAGE_PARAM); 
133
134    // tidy whitespace
135    //query = query.replaceAll("\\s+", "+");
136    matchNum = 0;
137    maxResult = 20;
138
139    try {
140        //Search Fedora Server
141        String host = "toetoe";
142        int port = 8200;
143        String user = "fedoraAdmin";
144        String pass = "fedoraAdmin";
145       
146        AutoFinder finder=new AutoFinder(host, port, user, pass);
147
148        String[] fieldsArray = new String[] {"pid", "label", "fType", "bDef",
149                         "bMech", "cModel", "state", "ownerId", "cDate", "mDate",
150                         "dcmDate", "title", "creator", "subject", "description",
151                         "publisher", "contributor", "date", "type", "format",
152                         "identifier", "source", "language", "relation", "coverage",
153                         "rights"};
154       
155        String match_fields = join(fieldsArray, "|");
156       
157        FieldSearchQuery query_fedora=fedoraSearch(query, match_fields);
158       
159            FieldSearchResult result_fedora = finder.findObjects(fieldsArray, maxResult, query_fedora);
160       
161        while (result_fedora != null) {
162        logger.info("Search Result:" + result_fedora.getResultList().length);
163        for (int i=0; i<result_fedora.getResultList().length; i++) {
164            ObjectFields o = result_fedora.getResultList()[i];
165            matchNum++;
166            String pid = o.getPid();
167            String fedora_url = fedora_server_url +"/fedora/get/" + pid;
168            String label = o.getLabel();
169            String[] creator = o.getCreator();
170            String[] subject = o.getSubject();
171            String[] publisher = o.getPublisher();
172            String[] contributor = o.getContributor();
173            String[] title = o.getTitle();
174            String[] description = o.getDescription();
175            String[] format = o.getFormat();
176            String[] identifier = o.getIdentifier();
177            String[] source = o.getSource();
178            String[] language = o.getLanguage();
179
180            Element doc_node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
181            doc_node_list.appendChild(doc_node);
182           
183            Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
184            doc_node.appendChild(metadata_list);
185           
186            if (fedora_url != null) {
187            GSXML.addMetadata( metadata_list, "URL", fedora_url);
188            }
189            if (o.getDescription() != null) {
190            GSXML.addMetadata(metadata_list, "Description", description[0]);
191            }
192            if (title[0] != null) {
193            GSXML.addMetadata(metadata_list, "Title", title[0]);
194            }
195        }
196        ListSession sess=result_fedora.getListSession();
197        if (sess!=null) {
198            result_fedora = finder.resumeFindObjects(sess.getToken());
199        } else {
200            result_fedora = null;
201        }
202        }
203    } catch (Exception e) {
204        logger.error("ERROR: " + e.getClass().getName()
205                   + ((e.getMessage()==null) ? "" : ": " + e.getMessage()));
206    }
207       
208    Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
209    result.appendChild(metadata_list);
210   
211    // Add a metadata element specifying the number of matching documents
212    //logger.info("FedoraSearch Number of Documents:" + matchNum);
213    GSXML.addMetadata(metadata_list, "numDocsMatched", ""+matchNum);
214   
215    return result;
216    }
217
218    protected FieldSearchQuery fedoraSearch(String query_terms, String matchFields) {
219
220    FieldSearchQuery fedora_query=new FieldSearchQuery();
221    try {
222        // check if query contains any fields name (pid, label, fType....)
223        if (query_terms.matches("^("+ matchFields+").+")){
224        logger.info("Implementing Specific Query....");
225
226        List conditions=fedora.server.search.Condition.getConditions(query_terms);
227       
228        Condition[] cond=new Condition[conditions.size()];
229        for (int i=0; i<conditions.size(); i++) {
230            fedora.server.search.Condition c = (fedora.server.search.Condition) conditions.get(i);
231            try {
232            String operator = transOperator(c.getOperator().getAbbreviation());
233            cond[i] = new Condition();
234            cond[i].setProperty(c.getProperty());
235            cond[i].setOperator(ComparisonOperator.fromValue(operator));
236            cond[i].setValue(c.getValue());
237            } catch (Exception e){
238            e.printStackTrace();
239            }
240        }
241        fedora_query.setConditions(cond);
242        } else {
243        logger.info("Implementing Simple Query...");
244        fedora_query.setTerms(query_terms);
245        }
246    } catch (Exception e){
247        e.printStackTrace();
248    }
249    return fedora_query;
250    }
251   
252    protected void getIndexData(ArrayList index_ids, ArrayList index_names,String lang){
253        index_ids.add("kw");
254    index_ids.add("au");   
255    index_ids.add("su");   
256    index_ids.add("ti");
257    index_ids.add("de");
258    index_ids.add("fu");
259    index_names.add(getTextString("param."+FIELD_PARAM+".kw", lang));
260    index_names.add(getTextString("param."+FIELD_PARAM+".au", lang));
261    index_names.add(getTextString("param."+FIELD_PARAM+".su", lang));
262    index_names.add(getTextString("param."+FIELD_PARAM+".ti", lang));
263    index_names.add(getTextString("param."+FIELD_PARAM+".de", lang));
264    index_names.add(getTextString("param."+FIELD_PARAM+".fu", lang));
265    }
266   
267    protected String getDocType (String node_id){
268    return node_id;
269    }
270    protected boolean hasChildren(String node_id){
271    return false;
272    }
273
274    protected boolean hasParent(String node_id){
275    return false;
276    }
277    private String transOperator(String operator){
278    String oper_char ="";
279    if (operator == "~") {
280        oper_char = "has";
281    } else if (operator == "=") {
282        oper_char = "eq";
283    } else if (operator == ">"){
284        oper_char = "gt";
285    } else if (operator == ">="){
286        oper_char = "ge";
287    } else if (operator == "<"){
288        oper_char = "lt";
289    } else if (operator == "<="){
290        oper_char = "le";
291    }
292    return oper_char;
293    }
294    protected String join( String[] array, String delim ) {
295        String j = "";
296        for ( int i=0; i<array.length; i++ ) {
297            if (i!=0) j += delim;
298            j += array[i];
299        }
300        return j;
301    }
302}
Note: See TracBrowser for help on using the browser.