Changeset 9002


Ignore:
Timestamp:
2005-02-10T16:44:15+13:00 (19 years ago)
Author:
kjdon
Message:

reworked the search classes, now have a base AbstractSearch class, hopefully all searches can inherit from this. base classes are mg/mgpp split rather than gs2/gs3

Location:
trunk/gsdl3/src/java/org/greenstone/gsdl3/service
Files:
4 added
2 deleted
3 edited

Legend:

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

    r8954 r9002  
    1 
    21/*
    32 *    GS2MGPPSearch.java
     
    54 *
    65 *    This program is free software; you can redistribute it and/or modify
    7  *    it under the terms of the GNU General Public License as published by
    8  *    the Free Software Foundation; either version 2 of the License, or
     6 *   the Free Software Foundation; either version 2 of the License, or
    97 *    (at your option) any later version.
    108 *
     
    2220
    2321// Greenstone classes
    24 import org.greenstone.mgpp.*;
    2522import org.greenstone.gsdl3.util.*;
     23import org.greenstone.gdbm.*;
    2624
    2725// XML classes
     26import org.w3c.dom.Document;
    2827import org.w3c.dom.Element;
    29 import org.w3c.dom.Node;
    30 import org.w3c.dom.NodeList;
    31 
    32 // General Java classes
    33 import java.io.File;
    34 import java.util.HashMap;
    35 import java.util.Vector;
    36 import java.util.Set;
    37 import java.util.Map;
    38 import java.util.Iterator;
    39 
     28import org.w3c.dom.NodeList;
    4029
    4130/**
    42  * A ServiceRack class for searching in greenstone 2 MGPP collections
    4331 *
    4432 * @author <a href="mailto:[email protected]">Katherine Don</a>
    45  * @version $Revision$
    46  * @see ServiceRack
     33 * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
    4734 */
     35
    4836public class GS2MGPPSearch
    49     extends GS2Search {
     37    extends AbstractMGPPSearch
     38{
     39    protected GDBMWrapper gdbm_src = null;
    5040
    51     // extra services offered by mgpp collections
    52     private static final String FIELD_QUERY_SERVICE = "FieldQuery";
    53     private static final String ADVANCED_FIELD_QUERY_SERVICE = "AdvancedFieldQuery";
    54 
    55     // extra parameters used
    56     private static final String INDEX_FIELD_PARAM = "index";
    57     private static final String LEVEL_PARAM = "level";
    58     private static final String RANK_PARAM = "sortBy";
    59     private static final String RANK_PARAM_RANK = "1";
    60     private static final String RANK_PARAM_NONE = "0";
    61     private static final String SIMPLE_FIELD_PARAM = "simpleField";
    62     private static final String ADVANCED_FIELD_PARAM = "complexField";
    63     // more params for field query
    64     private static final String FIELD_QUERY_PARAM = "fqv";
    65     private static final String FIELD_STEM_PARAM = "fqs";
    66     private static final String FIELD_CASE_PARAM = "fqc";
    67     private static final String FIELD_FIELD_PARAM = "fqf";
    68     private static final String FIELD_COMBINE_PARAM = "fqk";
    69     private static final String FIELD_COMBINE_PARAM_AND = "0";
    70     private static final String FIELD_COMBINE_PARAM_OR = "1";
    71     private static final String FIELD_COMBINE_PARAM_NOT = "2";
    72 
    73     // some stuff for config files
    74     private static final String SEARCH_TYPE_ELEM = "searchType";
    75     private static final String SEARCH_TYPE_PLAIN = "plain";
    76     private static final String SEARCH_TYPE_FORM = "form";
    77     private static final String SEARCH_TYPE_FORM_SIMPLE = "simple";
    78     private static final String SEARCH_TYPE_FORM_ADVANCED = "advanced";
    79 
    80 
    81     private static final int TEXT_QUERY = 0;
    82     private static final int SIMPLE_QUERY = 1;
    83     private static final int ADVANCED_QUERY = 2;
    84    
    85     protected static final String FIELD_ATT = "field";
    86     private MGPPWrapper mgpp_src=null;
    87 
    88     // the default level for retrieval - and we'll use it for searching too
    89     private String default_level=null;
    90     // the default field for searching
    91     private String default_field = null;
    92     // which search services will we offer??
    93     private boolean plain_search = false;
    94     private boolean simple_form_search = false;
    95     private boolean advanced_form_search = false;
    96    
    9741    /** constructor */
    9842    public GS2MGPPSearch()
    9943    {
    100     this.mgpp_src = new MGPPWrapper();
    101     this.dictionary_name = "MGPPSearch";
     44    this.gdbm_src = new GDBMWrapper();
    10245    }
    10346
    104    
    10547    /** configure this service */
    10648    public boolean configure(Element info, Element extra_info)
    10749    {
    108     // Do generic configuration
    109     if (super.configure(info, extra_info) == false)
     50
     51        // Open GDBM database for querying
     52    String gdbm_db_file = GSFile.GDBMDatabaseFile(this.site_home, this.cluster_name);
     53    if (!this.gdbm_src.openDatabase(gdbm_db_file, GDBMWrapper.READER)) {
     54        System.err.println("Error: Could not open GDBM database!");
    11055        return false;
    111    
    112     // the format info is the same for all services
    113     Element format_info = (Element)format_info_map.get(TEXT_QUERY_SERVICE);
    114 
    115     // the generic config has set up the text query service, but we may not want it
    116     Element search_type_list = (Element) GSXML.getChildByTagName(info, SEARCH_TYPE_ELEM + GSXML.LIST_MODIFIER);
    117     if (search_type_list == null) {
    118         // assume form and plain
    119         this.plain_search = true;
    120         this.simple_form_search = true;
    121         this.advanced_form_search = true;
    122     } else {
    123         NodeList types = search_type_list.getElementsByTagName(SEARCH_TYPE_ELEM);
    124         for (int i=0; i<types.getLength(); i++) {
    125         Element t = (Element)types.item(i);
    126         String type_name = t.getAttribute(GSXML.NAME_ATT);
    127         if (type_name.equals(SEARCH_TYPE_PLAIN)) {
    128             this.plain_search = true;
    129         } else if (type_name.equals(SEARCH_TYPE_FORM)) {
    130             String type_type = t.getAttribute(GSXML.TYPE_ATT);
    131             if (type_type.equals("")) {
    132             this.simple_form_search = true;
    133             this.advanced_form_search = true;
    134             } else if (type_type.equals(SEARCH_TYPE_FORM_SIMPLE)) {
    135             this.simple_form_search = true;
    136             } else if (type_type.equals(SEARCH_TYPE_FORM_ADVANCED)) {
    137             this.advanced_form_search = true;
    138            
    139             }
    140         }
    141         }
     56    }
     57    return super.configure(info, extra_info);
     58    }
     59    /** returns the document type of the doc that the specified node
     60    belongs to. should be one of
     61    GSXML.DOC_TYPE_SIMPLE,
     62    GSXML.DOC_TYPE_PAGED,
     63    GSXML.DOC_TYPE_HIERARCHY
     64    */
     65    protected String getDocType(String node_id){
     66    DBInfo info = this.gdbm_src.getInfo(node_id);
     67    if (info == null) {
     68        return GSXML.DOC_TYPE_SIMPLE;
     69    }
     70    String doc_type = info.getInfo("doctype");
     71    if (!doc_type.equals("")&&!doc_type.equals("doc")) {
     72        return doc_type;
    14273    }
    14374
    144     if (!this.plain_search) {
    145         // need to remove the TextQuery service
    146         Element tq_service = GSXML.getNamedElement(short_service_info, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
    147         short_service_info.removeChild(tq_service);
    148        
     75    String top_id = OID.getTop(node_id);
     76    boolean is_top = (top_id.equals(node_id) ? true : false);
     77   
     78    String children = info.getInfo("contains");
     79    boolean is_leaf = (children.equals("") ? true : false);
     80
     81    if (is_top && is_leaf) { // a single section document
     82        return GSXML.DOC_TYPE_SIMPLE;
    14983    }
    15084
    151    
    152     // Do specific configuration
    153     System.out.println("Configuring GS2MGPPSearch...");
     85    // now we just check the top node
     86    if (!is_top) { // we need to look at the top info
     87        info = this.gdbm_src.getInfo(top_id);
     88    }
     89    if (info == null) {
     90        return GSXML.DOC_TYPE_HIERARCHY;
     91    }
     92 
     93    String childtype = info.getInfo("childtype");
     94    if (childtype.equals("Paged")) {
     95        return GSXML.DOC_TYPE_PAGED;
     96    }
     97    return GSXML.DOC_TYPE_HIERARCHY;
    15498
    155     // Get the default level out of <defaultLevel> (buildConfig.xml)
    156     Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
    157     if (def != null) {
    158         this.default_level = def.getAttribute(GSXML.NAME_ATT);
    159     }
    160     if (this.default_level == null || this.default_level.equals("")) {
    161         System.err.println("Error: default level not specified!");
     99    }
     100   
     101    /** returns true if the node has child nodes */
     102    protected boolean hasChildren(String node_id){
     103    DBInfo info = this.gdbm_src.getInfo(node_id);
     104    if (info == null) {
    162105        return false;
    163106    }
    164 
    165     // the default level is also the level which gdbm is expecting
    166     // this must not be overwritten
    167     this.mgpp_src.setReturnLevel(this.default_level);
    168     // return term info
    169     this.mgpp_src.setReturnTerms(true);
    170     // set the default - this may be overwritten by query params
    171     this.mgpp_src.setQueryLevel(this.default_level);
    172 
    173     // set up the extra services which are available for this collection
    174     // check the config info - if there is no field list, then there is no fielded searching
    175    
    176     Element field_list = (Element) GSXML.getChildByTagName(info, GSXML.FIELD_ELEM+GSXML.LIST_MODIFIER);
    177     if (field_list==null) {
    178         // nothing more to do
    179         return true;
    180     }
    181     // find the default field - use the first one
    182     Element first_field = (Element)GSXML.getChildByTagName(field_list, GSXML.FIELD_ELEM);
    183     default_field = first_field.getAttribute(GSXML.NAME_ATT);
    184     // else set up the fielded query services
    185    
    186     if (this.simple_form_search) {
    187         // set up short_service_info_ - for now just has id and type - name will be added in on teh fly
    188         Element fq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
    189         fq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
    190         fq_service.setAttribute(GSXML.NAME_ATT, FIELD_QUERY_SERVICE);
    191         this.short_service_info.appendChild(fq_service);
    192 
    193         if (format_info != null) {
    194         this.format_info_map.put(FIELD_QUERY_SERVICE, format_info);
    195         }
    196     }
    197    
    198     if (this.advanced_form_search) {
    199         Element afq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
    200         afq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
    201         afq_service.setAttribute(GSXML.NAME_ATT, ADVANCED_FIELD_QUERY_SERVICE);
    202         this.short_service_info.appendChild(afq_service);
    203        
    204         if (format_info != null) {
    205         this.format_info_map.put(ADVANCED_FIELD_QUERY_SERVICE, format_info);
    206         }
     107    String contains = info.getInfo("contains");
     108    if (contains.equals("")) {
     109        return false;
    207110    }
    208111    return true;
    209112    }
    210 
    211     protected Element getServiceDescription(String service_id, String lang, String subset) {
    212     // should we check that the service is actually on offer? presumably we wont get asked for services that we haven't advertised previously.
    213    
    214     if (!service_id.equals(FIELD_QUERY_SERVICE) && !service_id.equals(ADVANCED_FIELD_QUERY_SERVICE)) {
    215         return super.getServiceDescription(service_id, lang, subset);
     113   
     114    /** returns true if the node has a parent */
     115    protected boolean hasParent(String node_id){
     116    String parent = OID.getParent(node_id);
     117    if (parent.equals(node_id)) {
     118        return false;
    216119    }
    217    
    218    
    219     Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
    220     service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
    221     service.setAttribute(GSXML.NAME_ATT, service_id);
    222     if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
    223         service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(service_id+".name", lang)));
    224         service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(service_id+".submit", lang)));
    225         service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(service_id+".description", lang)));
    226    
    227     }
    228     if (subset == null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
    229         Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 
    230         service.appendChild(param_list);
    231         if (service_id.equals(FIELD_QUERY_SERVICE)) {
    232        
    233         createFieldQueryParamList(param_list, lang);
    234         } else {
    235         createAdvancedFieldQueryParamList(param_list, lang);
    236         }
    237     }
    238     return service;
     120    return true;
     121    }
     122   
     123   /** convert MGPP internal id to Greenstone oid */
     124    protected String MGPPNum2OID(long docnum)
     125    {
     126    return this.gdbm_src.docnum2OID(docnum);
    239127   
    240128    }
    241129
    242      /** creates a new param element and adds it to the param list */
    243     protected void createParameter(String id, Element param_list, String lang) {
    244     Element param=null;
    245    
    246     if (id.equals(LEVEL_PARAM)) {
    247         // the level info - read from config file
    248         Element level_list = (Element)GSXML.getChildByTagName(this.config_info, LEVEL_ELEM+GSXML.LIST_MODIFIER);
    249         NodeList levels = level_list.getElementsByTagName(LEVEL_ELEM);
    250         int len = levels.getLength();
    251         if (len > 1) { // add level param to list only if more than one index specified
    252         String [] levs  = new String[len];
    253         String [] lev_names = new String[len];
    254         for (int i=0; i<len; i++) {
    255             levs[i] = ((Element)levels.item(i)).getAttribute(GSXML.NAME_ATT);
    256             lev_names[i] = getTextString("level."+levs[i], lang);
    257            
    258         }
    259         // the first one is the default
    260             param = GSXML.createParameterDescription(this.doc, LEVEL_PARAM, getTextString("param."+LEVEL_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, levs[0], levs, lev_names);
    261            
    262         }
    263     } else if (id.equals(RANK_PARAM)) {
    264         String [] vals1 = {RANK_PARAM_RANK, RANK_PARAM_NONE };
    265         String [] vals1_texts = { getTextString("param."+RANK_PARAM+"."+RANK_PARAM_RANK, lang), getTextString("param."+RANK_PARAM+"."+RANK_PARAM_NONE, lang)};
    266        
    267         param = GSXML.createParameterDescription(this.doc, RANK_PARAM, getTextString("param."+RANK_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, RANK_PARAM_RANK, vals1, vals1_texts );
    268        
    269     } else if (id.equals(FIELD_QUERY_PARAM)) {
    270         param = GSXML.createParameterDescription(this.doc, FIELD_QUERY_PARAM, getTextString("param."+FIELD_QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
    271        
    272        
    273     } else if (id.equals(FIELD_CASE_PARAM) || id.equals(FIELD_STEM_PARAM)) {
    274         String[] bool_ops = {"0", "1"};
    275         String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
    276         param = GSXML.createParameterDescription(this.doc, id, getTextString("param."+id, lang), GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, bool_ops, bool_texts);
    277        
    278     } else if (id.equals(FIELD_FIELD_PARAM) || id.equals(INDEX_FIELD_PARAM)) {
    279        
    280         // the field list -  read from config file
    281         Element field_list = (Element)GSXML.getChildByTagName(this.config_info, GSXML.FIELD_ELEM+GSXML.LIST_MODIFIER);
    282         NodeList fields = field_list.getElementsByTagName(GSXML.FIELD_ELEM);
    283         int len = fields.getLength();
    284         String [] f_names = new String [len];
    285         String [] f_texts = new String [len];
    286         for (int i=0; i< len;i++) {
    287         f_names[i] = ((Element)fields.item(i)).getAttribute(GSXML.SHORTNAME_ATT);
    288         // should these be changed to a text element based on lang?
    289         // or is the name of a metadata element eg dc:Title its
    290         // name in all langs
    291         f_texts[i] = ((Element)fields.item(i)).getAttribute(GSXML.NAME_ATT);
    292        
    293         }
    294         param = GSXML.createParameterDescription(this.doc, id, getTextString("param."+id, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, f_names[0], f_names, f_texts );
    295        
    296     } else if (id.equals(FIELD_COMBINE_PARAM)) {
    297        
    298         String []vals = {FIELD_COMBINE_PARAM_AND, FIELD_COMBINE_PARAM_OR, FIELD_COMBINE_PARAM_NOT};
    299         String []val_texts = {getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_AND, lang), getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_OR, lang), getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_NOT, lang)};
    300        
    301        
    302         param = GSXML.createParameterDescription(this.doc, FIELD_COMBINE_PARAM, "", GSXML.PARAM_TYPE_ENUM_SINGLE, FIELD_COMBINE_PARAM_AND, vals, val_texts);
    303         param.setAttribute(GSXML.PARAM_IGNORE_POS_ATT, "0");
    304        
    305        
    306     }
    307    
    308     // add the param to the list
    309     if (param !=null) {
    310         param_list.appendChild(param);
    311     }
    312     else {
    313         super.createParameter(id, param_list, lang);
    314     }
    315     }
    316    
    317 
    318     /** this creates all teh params and appends them to param_list.
    319      * if display=true it creates the text strings version
    320      * otherwise it creates the description version
    321      */
    322     protected boolean createTextQueryParamList(Element param_list,
    323                            
    324                            String lang)
    325     {
    326     // the order they are specified here is the order they appear on
    327     // the query form
    328     //createParameter(INDEX_PARAM, param_list, display, lang); - there is only ever one index now
    329     createParameter(INDEX_FIELD_PARAM, param_list,  lang);
    330     createParameter(LEVEL_PARAM, param_list, lang);
    331     createParameter(CASE_PARAM, param_list, lang);
    332     createParameter(STEM_PARAM, param_list,  lang);
    333     createParameter(MATCH_PARAM, param_list, lang);
    334     createParameter(RANK_PARAM, param_list,  lang);
    335     createParameter(MAXDOCS_PARAM, param_list,  lang);
    336     createParameter(QUERY_PARAM, param_list,  lang);
    337 
    338     return true;
    339     }
     130}
    340131
    341132
    342 
    343     /** this creates all teh params and appends them to param_list.
    344      * if display=true it creates the text strings version
    345      * otherwise it creates the description version
    346      */
    347     protected boolean createFieldQueryParamList(Element param_list,
    348                         String lang)
    349     {
    350     // add in the index param
    351     //createParameter(INDEX_PARAM, param_list, display, lang);
    352     createParameter(LEVEL_PARAM, param_list, lang);
    353     createParameter(MATCH_PARAM, param_list,  lang);
    354     createParameter(CASE_PARAM, param_list,  lang);
    355     createParameter(STEM_PARAM, param_list,  lang);
    356     createParameter(RANK_PARAM, param_list,  lang);
    357     createParameter(MAXDOCS_PARAM, param_list,  lang);
    358 
    359     // create a multi param for the fields etc
    360     // text box, field
    361     Element multiparam = null;
    362     Element param=null;
    363     multiparam = GSXML.createParameterDescription(this.doc, SIMPLE_FIELD_PARAM, "", GSXML.PARAM_TYPE_MULTI, null, null, null);
    364     multiparam.setAttribute("occurs", "4");
    365     param_list.appendChild(multiparam);
    366    
    367     // the components
    368     createParameter(FIELD_QUERY_PARAM, multiparam,  lang);
    369     createParameter(FIELD_FIELD_PARAM, multiparam,  lang);
    370    
    371     return true;
    372     }
    373 
    374     protected boolean createAdvancedFieldQueryParamList(Element param_list,
    375                             String lang) {
    376 
    377     // first do index and level params
    378     //createParameter(INDEX_PARAM, param_list, display, lang);
    379     createParameter(LEVEL_PARAM, param_list,  lang);
    380     createParameter(RANK_PARAM, param_list, lang);
    381     createParameter(MAXDOCS_PARAM, param_list,  lang);
    382 
    383 
    384     // create a multi param for the fields etc
    385     // text box, stem, case, field
    386 
    387     Element multiparam = null;
    388     Element param=null;
    389 
    390     multiparam = GSXML.createParameterDescription(this.doc, ADVANCED_FIELD_PARAM, "", GSXML.PARAM_TYPE_MULTI, null, null, null);
    391     multiparam.setAttribute("occurs", "4");
    392     param_list.appendChild(multiparam);
    393    
    394     createParameter(FIELD_COMBINE_PARAM, multiparam,  lang);
    395     createParameter(FIELD_QUERY_PARAM, multiparam,  lang);
    396     createParameter(FIELD_CASE_PARAM, multiparam,  lang);
    397     createParameter(FIELD_STEM_PARAM, multiparam,  lang);
    398     createParameter(FIELD_FIELD_PARAM, multiparam,  lang);   
    399    
    400 
    401     return true;
    402     }
    403 
    404     // the following three functions are needed so the base class can
    405     // call the process+SERVICE_NAME methods
    406     /** process a text query */
    407     protected Element processTextQuery(Element request) {
    408     return processAnyQuery(request, TEXT_QUERY);
    409     }
    410 
    411     /** process a field query */   
    412     protected Element processFieldQuery(Element request) {
    413     return processAnyQuery(request, SIMPLE_QUERY);
    414     }
    415 
    416     /** process an advanced field query */
    417     protected Element processAdvancedFieldQuery(Element request) {
    418     return processAnyQuery(request, ADVANCED_QUERY);
    419     }
    420    
    421     /** process a  query */
    422     protected Element processAnyQuery(Element request, int query_type)
    423     {
    424 
    425     String service_name=null;
    426     String empty_query_test_param=null;
    427     // set up the type specific bits
    428     switch (query_type) {
    429     case TEXT_QUERY:
    430         service_name = TEXT_QUERY_SERVICE;
    431         empty_query_test_param = QUERY_PARAM;
    432         break;
    433     case SIMPLE_QUERY:
    434         service_name = FIELD_QUERY_SERVICE;
    435         empty_query_test_param = FIELD_QUERY_PARAM;
    436         break;
    437     case ADVANCED_QUERY:
    438         service_name = ADVANCED_FIELD_QUERY_SERVICE;
    439         empty_query_test_param = FIELD_QUERY_PARAM;
    440         break;
    441     default:
    442         // should never get here
    443         System.err.println("GS2MGPPSearch: wrong query type!!");
    444         return null;
    445     }
    446    
    447     // Create a new (empty) result message
    448     Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
    449     result.setAttribute(GSXML.FROM_ATT, service_name);
    450     result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    451    
    452     // Get the parameters of the request
    453     Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    454     if (param_list == null) {
    455         System.err.println("Error: TextQuery request had no paramList.");
    456         return result;  // Return the empty result
    457     }
    458 
    459     // Process the request parameters
    460     HashMap params = GSXML.extractParams(param_list, false);
    461 
    462     // Make sure a query has been specified
    463     String query = (String) params.get(empty_query_test_param);
    464     if (query == null || query.equals("")) {
    465         return result;  // Return the empty result
    466     }
    467 
    468     // If a field hasn't been specified, use the default
    469     String field = (String) params.get(INDEX_FIELD_PARAM);
    470     if (field == null) {
    471         field = default_field;
    472     }
    473    
    474     // set up mgpp_src
    475     String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) +
    476         File.separatorChar + GSFile.collectionIndexPath(this.cluster_name, this.default_index);
    477     this.mgpp_src.loadIndexData(indexdir);
    478     setStandardQueryParams(params);
    479    
    480     // if field search, create the query string
    481     switch (query_type) {
    482     case TEXT_QUERY:
    483         query = addFieldInfo(query, field);
    484         break;
    485     case SIMPLE_QUERY:
    486         query = parseFieldQueryParams(params);
    487         break;
    488     case ADVANCED_QUERY:
    489         query = parseAdvancedFieldQueryParams(params);
    490         break;
    491     }
    492     // run the query
    493         this.mgpp_src.runQuery(query);
    494     MGPPQueryResult mqr= this.mgpp_src.getQueryResult();
    495 
    496     // build up the response
    497    
    498     // Create a metadata list to store information about the query results
    499     // should we be using metadataList? or something else?
    500     Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    501     result.appendChild(metadata_list);
    502    
    503     // Add a metadata element specifying the number of matching documents
    504     long totalDocs = mqr.getTotalDocs();
    505     GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+totalDocs);
    506 
    507     // Create a document list to store the matching documents, and add them
    508     Vector docs = mqr.getDocs();
    509 
    510     // add a metadata item to specify docs returned
    511     GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+docs.size());
    512     // add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query
    513     GSXML.addMetadata(this.doc, metadata_list, "query", query);
    514     Element document_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    515     result.appendChild(document_list);
    516     for (int d = 0; d < docs.size(); d++) {
    517         long docnum = ((MGPPDocInfo) docs.elementAt(d)).num_;
    518         String doc_id = this.gdbm_src.docnum2OID(docnum);
    519         Element doc_node = createDocumentNodeElement(doc_id);
    520         document_list.appendChild(doc_node);
    521     }
    522 
    523     // Create a term list to store the term information, and add it
    524     String query_level = (String)params.get(LEVEL_PARAM); // the current query level
    525     Element term_list = this.doc.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
    526     result.appendChild(term_list);
    527     Vector terms = mqr.getTerms();
    528     for (int t = 0; t < terms.size(); t++) {
    529         MGPPTermInfo term_info = (MGPPTermInfo) terms.get(t);
    530        
    531         Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
    532         term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
    533         term_elem.setAttribute(STEM_ATT, "" + term_info.stem_method_);
    534         term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
    535         term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
    536         field = term_info.tag_;
    537         if (field.equals(query_level)) {
    538         // ignore
    539         field = "";
    540         }
    541         term_elem.setAttribute(FIELD_ATT, field);
    542        
    543         Vector equiv_terms = term_info.equiv_terms_;
    544         Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
    545         term_elem.appendChild(equiv_term_list);
    546        
    547         for (int et = 0; et < equiv_terms.size(); et++) {
    548         String equiv_term = (String) equiv_terms.get(et);
    549 
    550         Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
    551         equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term);
    552         equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "");
    553         equiv_term_elem.setAttribute(FREQ_ATT, "");
    554         equiv_term_list.appendChild(equiv_term_elem);
    555         }
    556 
    557         term_list.appendChild(term_elem);
    558     }
    559 
    560     return result;
    561     }
    562 
    563     // should probably use a list rather than map
    564     protected boolean setStandardQueryParams(HashMap params) {
    565 
    566     Set entries = params.entrySet();
    567     Iterator i = entries.iterator();
    568     while (i.hasNext()) {
    569         Map.Entry m = (Map.Entry)i.next();
    570         String name = (String)m.getKey();
    571         String value = (String)m.getValue();
    572        
    573         if (name.equals(CASE_PARAM)) {
    574         boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
    575         this.mgpp_src.setCase(val);     
    576         } else if (name.equals(STEM_PARAM)) {
    577         boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
    578         this.mgpp_src.setStem(val);
    579         } else if (name.equals(MAXDOCS_PARAM)&& !value.equals("")) {
    580         int docs = Integer.parseInt(value);
    581         this.mgpp_src.setMaxDocs(docs);
    582         } else if (name.equals(LEVEL_PARAM)) {
    583         this.mgpp_src.setQueryLevel(value);
    584         } else if (name.equals(MATCH_PARAM)) {
    585         int mode;
    586         if (value.equals(MATCH_PARAM_ALL)) mode=1;
    587         else mode=0;
    588         this.mgpp_src.setMatchMode(mode);
    589         } else if (name.equals(RANK_PARAM)) {
    590         if (value.equals(RANK_PARAM_RANK)) {
    591             this.mgpp_src.setSortByRank(true);
    592         } else if (value.equals(RANK_PARAM_NONE)) {
    593             this.mgpp_src.setSortByRank(false);
    594         }
    595         } // ignore any others
    596     }
    597     return true;
    598     }
    599 
    600     protected String addFieldInfo(String query, String field) {
    601     if (field.equals("") || field.equals("ZZ")) {
    602         return query;
    603     }
    604     return "["+query+"]:"+field;
    605     }
    606     /** combines all the field params into a single query
    607      * - for simple field query */
    608     protected String parseFieldQueryParams(HashMap params) {
    609 
    610     StringBuffer final_query = new StringBuffer(256);
    611     String text_line = (String)params.get(FIELD_QUERY_PARAM);
    612     String[] texts = text_line.split(",", -1);
    613     String field_line = (String)params.get(FIELD_FIELD_PARAM);
    614     String[] fields = field_line.split(",", -1);
    615     String combine="&";
    616     String match = (String)params.get(MATCH_PARAM);
    617     if (match.equals(MATCH_PARAM_SOME)) {
    618         combine = "|";
    619     }
    620    
    621     for (int i=0; i<texts.length; i++) {
    622 
    623         String q = texts[i].trim();
    624         if (!q.equals("")) {
    625         addQueryElem(final_query, q, fields[i], combine);
    626         }
    627     }
    628 
    629     return final_query.toString();
    630     }
    631 
    632     /** combines all the field params into a single query
    633      * - for advanced field query */   
    634     protected String parseAdvancedFieldQueryParams(HashMap params) {
    635 
    636     StringBuffer final_query = new StringBuffer(256);
    637     String text_line = (String)params.get(FIELD_QUERY_PARAM);
    638     String[] texts = text_line.split(",", -1);
    639     String field_line = (String)params.get(FIELD_FIELD_PARAM);
    640     String[] fields = field_line.split(",", -1);
    641     String case_line = (String)params.get(FIELD_CASE_PARAM);
    642     String[] cases = case_line.split(",", -1);
    643     String stem_line = (String)params.get(FIELD_STEM_PARAM);
    644     String[] stems = stem_line.split(",", -1);
    645     String  combine_line = (String)params.get(FIELD_COMBINE_PARAM);
    646     String [] combines = combine_line.split(",", -1);
    647     String combine = "&";
    648     for (int i=0; i<texts.length; i++) {
    649         if (i==0) {// assume first one is blank
    650         combine = "";
    651         } else {
    652         String x = combines[i];
    653         if (x.equals(FIELD_COMBINE_PARAM_AND)) {
    654             combine = "&";
    655         } else if (x.equals(FIELD_COMBINE_PARAM_OR)) {
    656             combine = "|";
    657         } else if (x.equals(FIELD_COMBINE_PARAM_NOT)) {
    658             combine = "!";
    659         }
    660        
    661         }
    662 
    663         String q = texts[i].trim();
    664         if (!q.equals("")) {
    665         q = addStemAndCase(q, stems[i], cases[i]);
    666         addQueryElem(final_query, q, fields[i], combine);
    667         }
    668     }
    669 
    670     return final_query.toString();
    671     }
    672 
    673     protected void addQueryElem(StringBuffer s, String q, String f, String c) {
    674 
    675     String combine="";
    676     if (s.length()>0) {
    677         combine = " "+c+" ";
    678     }
    679     if (f.equals("")||f.equals("ZZ")) {
    680         s.append(combine+q);
    681     } else {
    682         s.append(combine+"["+q+"]:"+f);
    683     }
    684     }
    685    
    686     protected String addStemAndCase(String q, String s, String c) {
    687     String mods = "#";
    688     if (c.equals("1")) {
    689         mods += "i";
    690     } else {
    691         mods += "c";
    692     }
    693     if (s.equals("1")) {
    694         mods += "s";
    695     } else {
    696         mods+= "u";
    697     }
    698     StringBuffer temp = new StringBuffer();
    699     String [] terms = q.split(" ");
    700     for (int i=0; i<terms.length; i++) {
    701         String t = terms[i].trim();
    702        
    703         if (!t.equals("") && !t.equals("TX")) {
    704         temp.append(" "+t+mods);
    705         }
    706     }
    707     return temp.toString();
    708     }
    709 
    710 
    711 }
    712  
    713 
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2MGSearch.java

    r8955 r9002  
    2020
    2121// Greenstone classes
    22 import org.greenstone.mg.*;
    2322import org.greenstone.gsdl3.util.*;
     23import org.greenstone.gdbm.*;
    2424
    2525// XML classes
     26import org.w3c.dom.Document;
    2627import org.w3c.dom.Element;
    27 import org.w3c.dom.Node;
    28 import org.w3c.dom.NodeList;
    29 
    30 // General Java classes
    31 import java.io.File;
    32 import java.util.HashMap;
    33 import java.util.Iterator;
    34 import java.util.Map;
    35 import java.util.Set;
    36 import java.util.Vector;
    37 
     28import org.w3c.dom.NodeList;
    3829
    3930/**
     
    4132 * @author <a href="mailto:[email protected]">Katherine Don</a>
    4233 * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
    43  * @version $Revision$
    4434 */
    4535
    4636public class GS2MGSearch
    47     extends GS2Search {
    48 
    49 
    50     private MGWrapper mg_src = null;
    51 
     37    extends AbstractMGSearch
     38{
     39    protected GDBMWrapper gdbm_src = null;
    5240
    5341    /** constructor */
    5442    public GS2MGSearch()
    55     {   
    56     this.mg_src = new MGWrapper();
    57     this.dictionary_name = "MGSearch";
     43    {
     44    this.gdbm_src = new GDBMWrapper();
    5845    }
    5946
     47    /** configure this service */
     48    public boolean configure(Element info, Element extra_info)
     49    {
    6050
    61     /** this creates all teh params and appends them to param_list.
    62      * if display=true it creates the text strings version
    63      * otherwise it creates the description version
    64      */
    65     protected boolean createTextQueryParamList(Element param_list,
    66                            String lang)
    67     {
    68     // the order they are specified here is the order they appear on
    69     // the query form
    70     createParameter(INDEX_PARAM, param_list, lang);
    71     createParameter(CASE_PARAM, param_list, lang);
    72     createParameter(STEM_PARAM, param_list, lang);
    73     createParameter(MATCH_PARAM, param_list,  lang);
    74     createParameter(MAXDOCS_PARAM, param_list,  lang);
    75     createParameter(QUERY_PARAM, param_list,  lang);
    76 
    77     return true;
     51        // Open GDBM database for querying
     52    String gdbm_db_file = GSFile.GDBMDatabaseFile(this.site_home, this.cluster_name);
     53    if (!this.gdbm_src.openDatabase(gdbm_db_file, GDBMWrapper.READER)) {
     54        System.err.println("Error: Could not open GDBM database!");
     55        return false;
     56    }
     57    return super.configure(info, extra_info);
    7858    }
    79 
    80 
    81     /** Process a text query */
    82     protected Element processTextQuery(Element request)
    83     {
    84     // Create a new (empty) result message
    85     Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
    86     result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
    87     result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    88 
    89     // Get the parameters of the request
    90     Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    91     if (param_list == null) {
    92         System.err.println("Error: TextQuery request had no paramList.");
    93         return result;  // Return the empty result
     59    /** returns the document type of the doc that the specified node
     60    belongs to. should be one of
     61    GSXML.DOC_TYPE_SIMPLE,
     62    GSXML.DOC_TYPE_PAGED,
     63    GSXML.DOC_TYPE_HIERARCHY
     64    */
     65    protected String getDocType(String node_id){
     66    DBInfo info = this.gdbm_src.getInfo(node_id);
     67    if (info == null) {
     68        return GSXML.DOC_TYPE_SIMPLE;
     69    }
     70    String doc_type = info.getInfo("doctype");
     71    if (!doc_type.equals("")&&!doc_type.equals("doc")) {
     72        return doc_type;
    9473    }
    9574
    96     // Process the request parameters
    97     HashMap params = GSXML.extractParams(param_list, false);
     75    String top_id = OID.getTop(node_id);
     76    boolean is_top = (top_id.equals(node_id) ? true : false);
     77   
     78    String children = info.getInfo("contains");
     79    boolean is_leaf = (children.equals("") ? true : false);
    9880
    99     // Make sure a query has been specified
    100     String query = (String) params.get(QUERY_PARAM);
    101     if (query == null || query.equals("")) {
    102         return result;  // Return the empty result
     81    if (is_top && is_leaf) { // a single section document
     82        return GSXML.DOC_TYPE_SIMPLE;
    10383    }
    10484
    105     // If an index hasn't been specified, use the default
    106     String index = (String) params.get(INDEX_PARAM);
    107     if (index == null) {
    108         index = this.default_index;
     85    // now we just check the top node
     86    if (!is_top) { // we need to look at the top info
     87        info = this.gdbm_src.getInfo(top_id);
    10988    }
     89    if (info == null) {
     90        return GSXML.DOC_TYPE_HIERARCHY;
     91    }
     92 
     93    String childtype = info.getInfo("childtype");
     94    if (childtype.equals("Paged")) {
     95        return GSXML.DOC_TYPE_PAGED;
     96    }
     97    return GSXML.DOC_TYPE_HIERARCHY;
    11098
    111     // The location of the MG index and text files
    112     String basedir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) +
    113                        File.separatorChar;  // Needed for MG
    114     String textdir = GSFile.collectionTextPath(this.cluster_name);
    115     String indexpath = GSFile.collectionIndexPath(this.cluster_name, index);
    116     this.mg_src.setIndex(indexpath);
    117 
    118     // set the mg query parameters to the values the user has specified
    119     setStandardQueryParams(params);
    120     this.mg_src.runQuery(basedir, textdir, query);
    121     MGQueryResult mqr = this.mg_src.getQueryResult();
    122     long totalDocs = mqr.getTotalDocs();
    123 
    124     // Get the docnums out, and convert to HASH ids
    125     Vector docs = mqr.getDocs();
    126     if (docs.size() == 0) {
    127         System.err.println("GS2MGSearch: Warning: No results found...\n");
     99    }
     100   
     101    /** returns true if the node has child nodes */
     102    protected boolean hasChildren(String node_id){
     103    DBInfo info = this.gdbm_src.getInfo(node_id);
     104    if (info == null) {
     105        return false;
    128106    }
    129 
    130     // Create a metadata list to store information about the query results
    131     Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    132     result.appendChild(metadata_list);
    133 
    134     // Add a metadata element specifying the number of matching documents
    135     // because teh total number is just the number returned, use numDocsReturned, not numDocsMatched
    136     GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+totalDocs);
    137     // add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query
    138     GSXML.addMetadata(this.doc, metadata_list, "query", query);
    139 
    140     // Create a document list to store the matching documents, and add them
    141     Element document_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    142     result.appendChild(document_list);
    143     for (int d = 0; d < docs.size(); d++) {
    144         long docnum = ((MGDocInfo) docs.elementAt(d)).num_;
    145         String doc_id = this.gdbm_src.docnum2OID(docnum);
    146         Element doc_node = createDocumentNodeElement(doc_id);
    147         document_list.appendChild(doc_node);
    148     }
    149 
    150     // Create a term list to store the term information, and add it
    151     Element term_list = this.doc.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
    152     result.appendChild(term_list);
    153     Vector terms = mqr.getTerms();
    154     for (int t = 0; t < terms.size(); t++) {
    155         MGTermInfo term_info = (MGTermInfo) terms.get(t);
    156 
    157         String term = term_info.term_;
    158         int stem_method = term_info.stem_method_;
    159         Vector equiv_terms = term_info.equiv_terms_;
    160 
    161         Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
    162         term_elem.setAttribute(GSXML.NAME_ATT, term);
    163         term_elem.setAttribute(STEM_ATT, "" + stem_method);
    164 
    165         Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
    166         term_elem.appendChild(equiv_term_list);
    167 
    168         long total_term_freq = 0;
    169         for (int et = 0; et < equiv_terms.size(); et++) {
    170         MGEquivTermInfo equiv_term_info = (MGEquivTermInfo) equiv_terms.get(et);
    171 
    172         Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
    173         equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term_info.term_);
    174         equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + equiv_term_info.match_docs_);
    175         equiv_term_elem.setAttribute(FREQ_ATT, "" + equiv_term_info.term_freq_);
    176         equiv_term_list.appendChild(equiv_term_elem);
    177 
    178         total_term_freq += equiv_term_info.term_freq_;
    179         }
    180 
    181         term_elem.setAttribute(FREQ_ATT, "" + total_term_freq);
    182         term_list.appendChild(term_elem);
    183     }
    184 
    185     return result;
    186     }
    187 
    188 
    189     // should probably use a list rather than map
    190     protected boolean setStandardQueryParams(HashMap params)
    191     {
    192     // set the default ones
    193     this.mg_src.setReturnTerms(true);
    194     this.mg_src.setCase(true); // turn casefolding on by default
    195     Set entries = params.entrySet();
    196     Iterator i = entries.iterator();
    197     while (i.hasNext()) {
    198         Map.Entry m = (Map.Entry)i.next();
    199         String name = (String)m.getKey();
    200         String value = (String)m.getValue();
    201 
    202         if (name.equals(CASE_PARAM)) {
    203         boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
    204         this.mg_src.setCase(val);
    205         }
    206         else if (name.equals(STEM_PARAM)) {
    207         boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
    208         this.mg_src.setStem(val);
    209         }
    210         else if (name.equals(MATCH_PARAM)) {
    211         int mode = (value.equals(MATCH_PARAM_ALL) ? 1 : 0);
    212         this.mg_src.setMatchMode(mode);
    213         }
    214         else if (name.equals(MAXDOCS_PARAM)) {
    215         int docs = Integer.parseInt(value);
    216         this.mg_src.setMaxDocs(docs);
    217         } // ignore any others
     107    String contains = info.getInfo("contains");
     108    if (contains.equals("")) {
     109        return false;
    218110    }
    219111    return true;
    220112    }
     113   
     114    /** returns true if the node has a parent */
     115    protected boolean hasParent(String node_id){
     116    String parent = OID.getParent(node_id);
     117    if (parent.equals(node_id)) {
     118        return false;
     119    }
     120    return true;
     121    }
     122   
     123   /** convert MG internal id to Greenstone oid */
     124    protected String MGNum2OID(long docnum)
     125    {
     126    return this.gdbm_src.docnum2OID(docnum);
     127   
     128    }
     129
    221130}
     131
     132
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS3MGSearch.java

    r8955 r9002  
    11/*
    22 *    GS3MGSearch.java
    3  *    Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
     3 *    Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
    44 *
    55 *    This program is free software; you can redistribute it and/or modify
     
    1818package org.greenstone.gsdl3.service;
    1919
    20 
    2120// Greenstone classes
    2221import org.greenstone.mg.*;
    23 import org.greenstone.gsdl3.util.*;
     22import org.greenstone.gsdl3.util.GSXML;
     23import org.greenstone.gsdl3.util.GS3OID;
     24import org.greenstone.gsdl3.util.SQLQuery;
    2425
    2526// XML classes
     27import org.w3c.dom.Document;
    2628import org.w3c.dom.Element;
    27 import org.w3c.dom.Node;
    28 import org.w3c.dom.NodeList;
     29import org.w3c.dom.NodeList;
    2930
    30 // General Java classes
     31// java classes
    3132import java.io.File;
    32 import java.util.HashMap;
    33 import java.util.Iterator;
    34 import java.util.Map;
    35 import java.util.Set;
    36 import java.util.Vector;
    3733
    3834
     
    4036 *
    4137 * @author <a href="mailto:[email protected]">Katherine Don</a>
    42  * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
    43  * @version $Revision$
    4438 */
    4539
    4640public class GS3MGSearch
    47     extends GS3Search {
     41    extends AbstractMGSearch
     42{
     43    protected SQLQuery database = null;
     44    /** the base index prefix */
     45    protected String base_index_prefix = null;
    4846
    49 
    50     private MGWrapper mg_src = null;
    5147
    5248    /** constructor */
    5349    public GS3MGSearch()
    5450    {
    55     this.mg_src = new MGWrapper();
    56     this.dictionary_name = "MGSearch";
     51    this.database = new SQLQuery();
    5752    }
    5853
    59 
    60     /** this creates all teh params and appends them to param_list.
    61      * if display=true it creates the text strings version
    62      * otherwise it creates the description version
    63      */
    64     protected boolean createTextQueryParamList(Element param_list,
    65                            String lang)
     54    /** configure this service */
     55    public boolean configure(Element info, Element extra_info)
    6656    {
    67     // the order they are specified here is the order they appear on
    68     // the query form
    69     createParameter(INDEX_PARAM, param_list, lang);
    70     createParameter(CASE_PARAM, param_list, lang);
    71     createParameter(STEM_PARAM, param_list, lang);
    72     createParameter(MATCH_PARAM, param_list,  lang);
    73     createParameter(MAXDOCS_PARAM, param_list,  lang);
    74     createParameter(QUERY_PARAM, param_list,  lang);
    75 
    76     return true;
    77     }
    78 
    79 
    80     /** Process a text query */
    81     protected Element processTextQuery(Element request)
    82     {
    83     // Create a new (empty) result message
    84     Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
    85     result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
    86     result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    87 
    88     // Get the parameters of the request
    89     Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    90     if (param_list == null) {
    91         System.err.println("Error: TextQuery request had no paramList.");
    92         return result;  // Return the empty result
     57   
     58    // get the base prefix
     59    Element def = (Element) GSXML.getChildByTagName(info, "baseIndexPrefix");
     60    if (def != null) {
     61        this.base_index_prefix = def.getAttribute(GSXML.NAME_ATT);
     62    }
     63    if (this.base_index_prefix == null || this.base_index_prefix.equals("")) {
     64        System.err.println("Error: base index prefix not specified!");
     65        return false;
     66    }
     67    String site_name = this.site_home.substring(this.site_home.lastIndexOf(File.separator)+1);
     68    if (site_name.equals("")) {
     69        System.err.println("GS3Search Error: Cannot extract the site name from site home: "+this.site_home);
     70        return false;
     71    }
     72    if (!database.setDatabase(site_name+"_"+this.cluster_name)) {
     73        System.err.println("GS3Search Error: Could not open SQL database!");
     74        return false;
    9375    }
    9476
    95     // Process the request parameters
    96     HashMap params = GSXML.extractParams(param_list, false);
     77    return super.configure(info, extra_info);
     78    }
     79    /** returns the document type of the doc that the specified node
     80    belongs to. should be one of
     81    GSXML.DOC_TYPE_SIMPLE,
     82    GSXML.DOC_TYPE_PAGED,
     83    GSXML.DOC_TYPE_HIERARCHY
     84    */
     85    protected String getDocType(String node_id){
     86    boolean hierarchical = false;
     87    if (!GS3OID.isDocTop(node_id) || database.isHierarchicalDocument(node_id) ) {
     88        hierarchical = true;
     89    }
     90    // what about paged???
     91    if (!hierarchical) {
     92        // a simple document
     93        return GSXML.DOC_TYPE_SIMPLE;
     94    } else {
     95        // a hierarchical doc
     96        return GSXML.DOC_TYPE_HIERARCHY;
     97    }
     98    // don't have paged yet.
    9799
    98     // Make sure a query has been specified
    99     String query = (String) params.get(QUERY_PARAM);
    100     if (query == null || query.equals("")) {
    101         return result;  // Return the empty result
    102     }
    103 
    104     // If an index hasn't been specified, use the default
    105     String index = (String) params.get(INDEX_PARAM);
    106     if (index == null) {
    107         index = this.default_index;
    108     }
    109 
    110     // The location of the MG index and text files
    111     String basedir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar;  // Needed for MG
    112     String textdir = GSFile.collectionTextPath("index");
    113     String indexpath = GSFile.collectionIndexPath("index", index);
    114     //String textdir = GSFile.collectionTextPath(this.cluster_name);
    115     //String indexpath = GSFile.collectionIndexPath(this.cluster_name, index);
    116     this.mg_src.setIndex(indexpath);
    117 
    118     // set the mg query parameters to the values the user has specified
    119     setStandardQueryParams(params);
    120     this.mg_src.runQuery(basedir, textdir, query);
    121     MGQueryResult mqr = this.mg_src.getQueryResult();
    122     long totalDocs = mqr.getTotalDocs();
    123 
    124     // Get the docnums out, and convert to HASH ids
    125     Vector docs = mqr.getDocs();
    126     if (docs.size() == 0) {
    127         System.err.println("GS3MGSearch: Warning: No results found...\n");
    128     }
    129 
    130     // Create a metadata list to store information about the query results
    131     Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    132     result.appendChild(metadata_list);
    133 
    134     // Add a metadata element specifying the number of matching documents
    135     // because teh total number is just the number returned, use numDocsReturned, not numDocsMatched
    136     GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+totalDocs);
    137 
    138     // add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query
    139     GSXML.addMetadata(this.doc, metadata_list, "query", query);
    140    
    141     // Create a document list to store the matching documents, and add them
    142     Element document_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    143     result.appendChild(document_list);
    144     for (int d = 0; d < docs.size(); d++) {
    145         long docnum = ((MGDocInfo) docs.elementAt(d)).num_;
    146         ////String mg_id = index+"."+docnum;
    147         String mg_id = this.base_index_prefix+"."+docnum;
    148         String doc_id = database.MGNum2OID(mg_id);
    149         // hack !!!
    150         if (doc_id.endsWith("-All")) {
    151         // get rid of the All bit
    152         doc_id = doc_id.substring(0,doc_id.length()-4);
    153         //doc_id=doc_id.replaceAll("All","1");
    154         }
    155        
    156         Element doc_node = createDocumentNodeElement(doc_id);
    157         document_list.appendChild(doc_node);
    158     }
    159 
    160     // Create a term list to store the term information, and add it
    161     Element term_list = this.doc.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
    162     result.appendChild(term_list);
    163     Vector terms = mqr.getTerms();
    164     for (int t = 0; t < terms.size(); t++) {
    165         MGTermInfo term_info = (MGTermInfo) terms.get(t);
    166 
    167         String term = term_info.term_;
    168         int stem_method = term_info.stem_method_;
    169         Vector equiv_terms = term_info.equiv_terms_;
    170 
    171         Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
    172         term_elem.setAttribute(GSXML.NAME_ATT, term);
    173         term_elem.setAttribute(STEM_ATT, "" + stem_method);
    174 
    175         Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
    176         term_elem.appendChild(equiv_term_list);
    177 
    178         long total_term_freq = 0;
    179         for (int et = 0; et < equiv_terms.size(); et++) {
    180         MGEquivTermInfo equiv_term_info = (MGEquivTermInfo) equiv_terms.get(et);
    181 
    182         Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
    183         equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term_info.term_);
    184         equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + equiv_term_info.match_docs_);
    185         equiv_term_elem.setAttribute(FREQ_ATT, "" + equiv_term_info.term_freq_);
    186         equiv_term_list.appendChild(equiv_term_elem);
    187 
    188         total_term_freq += equiv_term_info.term_freq_;
    189         }
    190 
    191         term_elem.setAttribute(FREQ_ATT, "" + total_term_freq);
    192         term_list.appendChild(term_elem);
    193     }
    194 
    195     return result;
    196100    }
    197 
    198 
    199     // should probably use a list rather than map
    200     protected boolean setStandardQueryParams(HashMap params)
    201     {
    202     // set the default ones
    203     this.mg_src.setReturnTerms(true);
    204     Set entries = params.entrySet();
    205     Iterator i = entries.iterator();
    206     while (i.hasNext()) {
    207         Map.Entry m = (Map.Entry)i.next();
    208         String name = (String)m.getKey();
    209         String value = (String)m.getValue();
    210 
    211         if (name.equals(CASE_PARAM)) {
    212         boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
    213         this.mg_src.setCase(val);
    214         }
    215         else if (name.equals(STEM_PARAM)) {
    216         boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
    217         this.mg_src.setStem(val);
    218         }
    219         else if (name.equals(MATCH_PARAM)) {
    220         int mode = (value.equals(MATCH_PARAM_ALL) ? 1 : 0);
    221         this.mg_src.setMatchMode(mode);
    222         }
    223         else if (name.equals(MAXDOCS_PARAM)) {
    224         int docs = Integer.parseInt(value);
    225         this.mg_src.setMaxDocs(docs);
    226         } // ignore any others
     101   
     102    /** returns true if the node has child nodes */
     103    protected boolean hasChildren(String node_id){
     104    return database.documentHasChildren(node_id);
     105    }
     106   
     107    /** returns true if the node has a parent */
     108    protected boolean hasParent(String node_id){
     109    String parent = GS3OID.getParent(node_id);
     110    if (parent.equals(node_id)) {
     111        return false;
    227112    }
    228113    return true;
    229114    }
     115   
     116   /** convert MG internal id to Greenstone oid */
     117    protected String MGNum2OID(long docnum)
     118    {
     119    String mg_id = this.base_index_prefix+"."+docnum;
     120    String doc_id = database.MGNum2OID(mg_id);
     121    // hack !!!
     122    if (doc_id.endsWith("-All")) {
     123        // get rid of the All bit
     124        doc_id = doc_id.substring(0,doc_id.length()-4);
     125        //doc_id=doc_id.replaceAll("All","1");
     126    }
     127    return doc_id;
     128   
     129    }
     130
    230131}
     132
     133
Note: See TracChangeset for help on using the changeset viewer.