Changeset 3754


Ignore:
Timestamp:
2003-02-21T11:47:07+13:00 (21 years ago)
Author:
mdewsnip
Message:

Alpha version of service for document retrieval using MG.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2MGRetrieve.java

    r3678 r3754  
    1919package org.greenstone.gsdl3.service;
    2020
     21// greenstone classes
     22import org.greenstone.mg.*;
     23import org.greenstone.gdbm.*;
    2124import org.greenstone.gsdl3.util.*;
    2225
     26// xml classes
     27import org.w3c.dom.CDATASection;
    2328import org.w3c.dom.Document;
     29import org.w3c.dom.Element;
    2430import org.w3c.dom.Node;
     31import org.w3c.dom.NodeList;
    2532import org.w3c.dom.Text;
    26 import org.w3c.dom.Element;
    27 import org.w3c.dom.NodeList;
     33
     34// general Java classes
     35import java.io.File;
     36import java.util.HashMap;
     37
    2838/**
    2939 *
     
    3646
    3747    // the services on offer
     48    // these strings must match what is found in the properties file
    3849    private static final String DOCUMENT_RETRIEVE_SERVICE = "DocumentRetrieve";
    3950    private static final String METADATA_RETRIEVE_SERVICE = "MetadataRetrieve";
     51
     52    // params used
     53    private static final String INDEX_PARAM = "index";
     54
     55    // elements used in the config file that are specific to this class
     56    private static final String DEFAULT_INDEX_ELEM = "defaultIndex";
     57
     58    private MGWrapper mg_src_ = null;
     59    private GDBMWrapper gdbm_src_ = null;
    4060   
    41 
    42     /** creates a display element containing all the text strings needed to display the service page, in the language specified */
    43     protected Element createServiceDisplay(String service, String lang) {
    44     Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
    45     display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM,  getTextString(service+".name", lang)));
    46     display.appendChild(GSXML.createTextElement(doc_,  GSXML.DISPLAY_SUBMIT_ELEM, getTextString(service+".submit", lang)));
    47 
    48     Element param;
    49 
    50     return display;
    51    
    52     }
    53 
     61    private String default_index_ = null;
     62
     63
     64    /** constructor */
     65    public GS2MGRetrieve() {
     66    mg_src_ = new MGWrapper();
     67    gdbm_src_ = new GDBMWrapper();
     68    }
    5469
    5570    /** configure this service */
    56     public boolean configure(Element info) {
    57 
     71    public boolean configure(Element info)
     72    {
    5873    System.out.println("configuring GS2MGRetrieve");
     74
     75    // get the default index out of <defaultIndex> (buildConfig.xml)
     76    Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM);
     77    if (def != null) {
     78        default_index_ = def.getAttribute(GSXML.NAME_ATT);
     79    }
     80    if (default_index_ == null || default_index_.equals("")) {
     81        System.err.println("Error: default index not specified!");
     82        return false;
     83    }
     84    System.out.println("Default index: " + default_index_);
    5985
    6086    Element e = null;
     
    7197    short_service_info_.appendChild(e);
    7298
    73 
    7499    // set up service_info_map_ - for now, just has the same elements as above
    75100    // should have full details about each service incl params lists etc.
     
    78103    e.setAttribute(GSXML.NAME_ATT, DOCUMENT_RETRIEVE_SERVICE);
    79104    service_info_map_.put(DOCUMENT_RETRIEVE_SERVICE, e);
    80    
     105
    81106    e = doc_.createElement(GSXML.SERVICE_ELEM);
    82107    e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
     
    84109    service_info_map_.put(METADATA_RETRIEVE_SERVICE, e);
    85110
    86     return true;
    87     }
    88   /** retrieve a document */
    89     protected Element processDocumentRetrieve(Element request) {
     111    // Open GDBM database for querying
     112    String gdbm_db_file = GSFile.GDBMDatabaseFile(site_home_, cluster_name_);
     113    if (gdbm_src_.openDatabase(gdbm_db_file, GDBMWrapper.READER)) {
     114        return true;
     115    }
     116    else {
     117        System.err.println("Error: Could not open gdbm database!");
     118        return false;
     119    }
     120    }
     121
     122    /** creates a display element containing all the text strings needed to display the service page, in the language specified
     123     * these retrieval services dont get displayed to the users - they are only used internally by the actions. so this returns an empty display element*/
     124    protected Element createServiceDisplay(String service, String lang)
     125    {
     126    return doc_.createElement(GSXML.DISPLAY_ELEM);
     127    }
     128
     129
     130    /** retrieve a document */
     131    protected Element processDocumentRetrieve(Element request)
     132    {
     133    // an empty result
    90134    Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
    91     result.setAttribute(GSXML.NAME_ATT, DOCUMENT_RETRIEVE_SERVICE);
     135    String from = GSPath.appendLink(cluster_name_, DOCUMENT_RETRIEVE_SERVICE);
     136    result.setAttribute(GSXML.FROM_ATT, from);
     137    result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
     138    Element result_doc = doc_.createElement(GSXML.CONTENT_ELEM);
     139    result.appendChild(result_doc);
     140
     141    // get param list and content
     142    Element param_elem = null;
     143    Element content_elem = null;
     144    Node n = request.getFirstChild();
     145    while (n != null) {
     146        String node_name = n.getNodeName();
     147        if (node_name.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
     148        param_elem = (Element) n;
     149        } else if (node_name.equals(GSXML.CONTENT_ELEM)) {
     150        content_elem = (Element) n;
     151        }
     152        n = n.getNextSibling();
     153    }
     154
     155    if (param_elem==null || content_elem==null) {
     156        System.err.println("GS2MGRetrieve: malformed request sent to DocumentRetrieve service");
     157        return result; // empty result
     158    }
    92159   
    93     // dummy result
    94     Text t = null;
    95     t = doc_.createTextNode(DOCUMENT_RETRIEVE_SERVICE+" result... ");
    96     result.appendChild(t);
     160    HashMap params = GSXML.extractParams(param_elem);
     161
     162    // get docs from mg
     163    String index = (String) params.get(INDEX_PARAM);
     164    if (index == null) { // if it is not present, use the default index
     165        index = default_index_;
     166    }
     167    System.out.println("Index: " + index);
     168
     169    String basedir = GSFile.collectionBaseDir(site_home_, cluster_name_) + File.separatorChar;  // Needed by MG
     170    String textdir = GSFile.collectionTextPath(cluster_name_);
     171    String indexpath = GSFile.collectionIndexPath(cluster_name_, index);
     172    System.out.println("Base directory: " + basedir);
     173    System.out.println("Text directory: " + textdir);
     174    System.out.println("Index path: " + indexpath);
     175
     176    mg_src_.setIndex(indexpath);
     177
     178    // get the text for each doc in the list
     179    String[] ids = GSXML.getDocumentNameList(content_elem);
     180    for (int j=0; j<ids.length; j++) {
     181        long real_num = gdbm_src_.oid2Docnum(ids[j]);
     182        String document = mg_src_.getDocument(basedir, textdir, real_num);
     183        System.out.println("(MGRetrieve) Document: " + document);
     184        // for now, stick it in a text node - eventually should be parsed as xml??
     185        // something funny with the doc -
     186        Element new_doc = GSXML.createDocumentElement(doc_, ids[j]);
     187        GSXML.addDocText(doc_, new_doc, document);
     188
     189        result_doc.appendChild(new_doc);
     190    }
    97191    return result;
    98192    }
    99193
    100   /** retrieve metadata */
    101     protected Element processMetadataRetrieve(Element request) {
     194
     195    /** retrieve metadata */
     196    protected Element processMetadataRetrieve(Element request)
     197    {
    102198    Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
    103     result.setAttribute(GSXML.NAME_ATT, METADATA_RETRIEVE_SERVICE);
    104    
    105     // dummy result
    106     Text t = null;
    107     t = doc_.createTextNode(METADATA_RETRIEVE_SERVICE+" result... ");
    108     result.appendChild(t);
     199    String from = GSPath.appendLink(cluster_name_, METADATA_RETRIEVE_SERVICE);
     200    result.setAttribute(GSXML.FROM_ATT, from);
     201    result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
     202
     203    Element result_content = doc_.createElement(GSXML.CONTENT_ELEM);
     204    result.appendChild(result_content);
     205    Element document_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
     206    result_content.appendChild(document_list);
     207
     208    // get the metadata
     209        Element content = (Element) request.getElementsByTagName(GSXML.CONTENT_ELEM).item(0);
     210    if (content == null) {
     211        // error: query had no content!! - should make an error message
     212        return result;
     213    }
     214    String[] metas = GSXML.getMetaNameList(content);
     215    String[] ids = GSXML.getDocumentNameList(content);
     216    for (int j = 0; j < ids.length; j++) { // for each document
     217        Element doc = GSXML.createDocumentElement(doc_, ids[j]);
     218        Element list = GSXML.addMetaList(doc_, doc);
     219        DBInfo info = gdbm_src_.getInfo(ids[j]);
     220        for (int m = 0; m < metas.length; m++) {
     221        String value = info.getInfo(metas[m]);
     222        GSXML.addMetadata(doc_, list, metas[m], value);
     223        }
     224        document_list.appendChild(doc);
     225    }
    109226    return result;
    110227    }
    111 
    112228}   
Note: See TracChangeset for help on using the changeset viewer.