Ignore:
Timestamp:
2002-11-26T13:55:24+13:00 (21 years ago)
Author:
kjdon
Message:

tidied up a bit, uses new page structure and new display elements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/action/QueryAction.java

    r3512 r3568  
    55// XML classes
    66import org.w3c.dom.Node;
     7import org.w3c.dom.NodeList;
    78import org.w3c.dom.Text;
    89import org.w3c.dom.Document;
     
    1112import java.util.HashMap;
    1213import java.util.Map;
    13 //import java.util.Map;
    1414import java.util.Iterator;
    1515import java.io.File;
    1616
     17/** action class for queries */
    1718public class QueryAction extends Action {
    1819   
    19 
    20     public String process (Element message) {
    21 
     20    public Element process (Element message) {
     21
     22    // get the request - assume there is only one
     23    Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
    2224
    2325    // create the return page tree
    2426    Element page = doc_.createElement(GSXML.PAGE_ELEM);
    25     page.setAttribute(GSXML.LANG_ATT, message.getAttribute(GSXML.LANG_ATT));
     27    page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
    2628    // add the lang stuff from message
    27     page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.TRANSLATION_ELEM), true));
     29    page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
    2830    // add the system stuff from message
    29     page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
    30 
    31     Element request = (Element)message.getElementsByTagName(GSXML.REQUEST_ELEM).item(0);
     31       page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
     32
    3233    // query type is teh subaction
    3334    String query_type = request.getAttribute(GSXML.SUBACTION_ATT);
     
    4950   
    5051    // we may be able to generalize this for all query types?
    51     protected String textQuery(Element page, Element request) {
    52 
    53     // chack that the stylesheet is present - cant output a page without one. we may adapt this to use unknownquery stylesheet? - or ask for one from the MR
     52    protected Element textQuery(Element page, Element request) {
     53
     54    // check that the stylesheet is present - cant output a page without one. we may adapt this to use unknownquery stylesheet? - or ask for one from the MR
    5455    String stylesheet = GSFile.stylesheetFile(config_, "textquery.xsl");
    5556    if (stylesheet==null) {
    56         return GSHTML.errorPage("textquery stylesheet not found!");
     57        System.err.println("QueryAction Error: textquery stylesheet not found!");
     58        return null;
    5759    }
    5860
    5961    // extract the params from the cgi-request, and check that we have a coll specified
    60     // first convert short to long names
    61     Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    62     cgi_.toLong(cgi_paramList);
    63     HashMap params = GSXML.extractParams(cgi_paramList);
     62    Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     63    HashMap params = GSXML.extractParams(cgi_param_list);
    6464
    6565    String collection = (String)params.get("collection");
    6666    if (collection == null || collection.equals("")) {
    67         return GSHTML.errorPage("textquery, need to specify a colleciton!");
    68     }
    69 
    70     // the first part of the data for the page is the cgi-params
    71     Element cgi_request = (Element)doc_.importNode(request, true);
    72     page.appendChild(cgi_request);
    73 
    74     // get the service info from the MR - this will probably need to be cached somehow later on. and add as a description node to the cgi-request - this doesn't work. change to add the specified value as a current attribute to the param - need to do somthing different for query
    75 
     67        System.err.println("QueryAction Error: no colleciton was specified!");
     68        return null;
     69    }
     70
     71    // get the service info from the MR - this will probably need to be cached somehow later on. and add the service node to the page
    7672    Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
    7773    Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM);
    7874    mr_info_message.appendChild(mr_info_request);
    7975    mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
     76    mr_info_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
    8077
    8178    String to = collection;
     
    8481
    8582    Element mr_info_response = (Element) mr_.process(mr_info_message);
    86     System.err.println("describe response for service");
    87     System.err.println(converter_.getString(mr_info_response));
    88     Element description = doc_.createElement(GSXML.DESCRIPTION_ELEM);
    89     // just param list for now - may need content info too
     83
    9084    String path = GSXML.RESPONSE_ELEM;
    9185    path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
    92     path = GSPath.appendLink(path, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    93     Node pl = GSXML.getNodeByPath(mr_info_response, path);
    94     if (pl ==null) {
    95         System.out.println("pl is null");
    96     }
    97     else {
    98         System.out.println("pl name is "+pl.getNodeName());
    99    
    100         Element imported_paramList = (Element)doc_.importNode(pl, true);
    101         // convert the name to short
    102         cgi_.addShortNames(imported_paramList);
    103 
    104         description.appendChild(imported_paramList);
    105    
    106         // for each param in page_request, set the current value if present
    107         //actually, prob dont need to know the default value, just which one to display - overwrite the default att?
    108        
    109         Element param = (Element)imported_paramList.getFirstChild();
     86    Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
     87    Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     88   
     89    if (pl !=null) {
     90        // add short names to the params in the param list
     91        cgi_.addShortNames(pl);
     92
     93        // for each param in the description, overwrite teh default value with the currently set value if present
     94        Element param = (Element)pl.getFirstChild();
    11095        while (param !=null) {
    11196        if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
     
    119104        }
    120105    }
    121     cgi_request.appendChild(description);
     106   
     107    // part of the data for the description is the cgi-params
     108    // if we have this here, do we need to do the previous step?
     109    Element cgi_request = (Element)doc_.importNode(request, true);
     110    page.appendChild(cgi_request);
     111
     112    page.appendChild(description);
    122113   
    123114    // check the query string
    124115    String query = (String)params.get(GSXML.REQUEST_TYPE_QUERY);
    125116
    126     if (query==null|| query.equals("")) {
    127         // if there is no q set, just output the search form
    128 
    129         Document style_doc = converter_.getDOM(new File(stylesheet));
    130         GSXSLT.absoluteIncludePaths(style_doc, config_);
    131         return transformer_.transform(style_doc, page);       
    132     }
    133    
    134     // else do the request, output search box and results
    135     Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
    136     Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
    137     mr_query_message.appendChild(mr_query_request);
    138 
    139     mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
    140     mr_query_request.setAttribute(GSXML.TO_ATT, to);
    141 
    142     // paramList
    143     Element paramList = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    144     mr_query_request.appendChild(paramList);
    145 
    146     Iterator i = params.entrySet().iterator();
    147     while (i.hasNext()) {
    148         Map.Entry e = (Map.Entry)i.next();
    149         String name = cgi_.toLong((String)e.getKey());
    150         if (!name.equals("")) {
    151         String value = (String)e.getValue();
    152         // if (!name.equals("c")) {
    153         Element p = doc_.createElement(GSXML.PARAM_ELEM);
    154         p.setAttribute(GSXML.NAME_ATT, name);
    155         p.setAttribute(GSXML.VALUE_ATT, value);
    156         paramList.appendChild(p);
    157         // }
     117    if (query!=null&& !query.equals("")) {
     118        // do the query
     119       
     120        Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
     121        Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
     122        mr_query_message.appendChild(mr_query_request);
     123
     124        mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
     125        mr_query_request.setAttribute(GSXML.TO_ATT, to);
     126        mr_query_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
     127        // paramList
     128        // should we remove the query from the param list?
     129        Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
     130        mr_query_request.appendChild(query_param_list);
     131       
     132        // content - contains the query string
     133        Element query_content = GSXML.createTextElement(doc_, GSXML.CONTENT_ELEM, query);
     134        mr_query_request.appendChild(query_content);
     135       
     136        Element mr_query_response = (Element)mr_.process(mr_query_message);
     137       
     138        // this result is the list of docs.
     139        // now take that list, and get the Titles
     140        // for now, just create a whole new request
     141       
     142        // check that there are some resources - for now check the list, but later should use a numdocs metadata elem
     143        path = GSXML.RESPONSE_ELEM;
     144        path = GSPath.appendLink(path, GSXML.CONTENT_ELEM);
     145        path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
     146       
     147        Element resource_list = (Element)GSXML.getNodeByPath(mr_query_response,
     148                                 path); // resourceList not present if no docs found
     149        if (resource_list == null) {
     150       
     151        Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
     152       
     153        page.appendChild(doc_.importNode(result_response, true));
     154       
     155        Document style_doc = converter_.getDOM(new File(stylesheet));
     156        GSXSLT.absoluteIncludePaths(style_doc, config_);
     157        return (Element)transformer_.transform(style_doc, page);   
     158       
    158159        }
    159     }
    160 
    161     // content - contains the query string
    162     Element query_content = doc_.createElement(GSXML.CONTENT_ELEM);
    163     Text query_text = doc_.createTextNode(query);
    164     query_content.appendChild(query_text);
    165     mr_query_request.appendChild(query_content);
    166    
    167     Element mr_query_response = (Element)mr_.process(mr_query_message);
    168 
    169     // this result is the list of docs.
    170     // now take that list, and get the Titles
    171     // for now, just create a whole new request
    172 
    173     // check that there are some resources - for now check the list, but later should use a numdocs metadata elem
    174     path = GSXML.RESPONSE_ELEM;
    175     path = GSPath.appendLink(path, GSXML.CONTENT_ELEM);
    176     path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
    177 
    178     Element resource_list = (Element)GSXML.getNodeByPath(mr_query_response,
    179                                  path); // resourceList not present if no docs found
    180     if (resource_list == null) {
    181 
    182         Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
     160       
     161        // we have a doc list, so get the metadata - for now, get title.
     162        // can we dynamically decide what metadata to get?
     163        Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
     164        Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
     165        mr_metadata_message.appendChild(mr_metadata_request);
     166       
     167        mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
     168        mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
     169        to = GSPath.appendLink(collection, "MetadataRetrieve");
     170        mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
     171       
     172        Element meta_content = doc_.createElement(GSXML.CONTENT_ELEM);
     173        mr_metadata_request.appendChild(meta_content);
     174       
     175        // the first part of the content is the doc list
     176        meta_content.appendChild(doc_.importNode(resource_list, true));
     177       
     178        // the second part of the content is the metadata list
     179        Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     180        Element title = doc_.createElement(GSXML.METADATA_ELEM);
     181        title.setAttribute(GSXML.NAME_ATT, "Title");
     182        metadata_list.appendChild(title);
     183        meta_content.appendChild(metadata_list);
     184
     185        Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
     186       
     187        Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
    183188       
    184189        page.appendChild(doc_.importNode(result_response, true));
    185 
    186         Document style_doc = converter_.getDOM(new File(stylesheet));
    187         GSXSLT.absoluteIncludePaths(style_doc, config_);
    188         return transformer_.transform(style_doc, page);   
    189 
    190     }
    191 
    192     // we have a doc list, so get the metadata - for now, get title.
    193     // can we dynamically decide what metadata to get?
    194     // get the Title metadata
    195     Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
    196     Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
    197     mr_metadata_message.appendChild(mr_metadata_request);
    198 
    199     mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
    200     to = GSPath.appendLink(collection, "MetadataRetrieve");
    201     mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
    202    
    203     Element meta_content = doc_.createElement(GSXML.CONTENT_ELEM);
    204     mr_metadata_request.appendChild(meta_content);
    205 
    206     // the first part of the content is the doc list
    207     meta_content.appendChild(doc_.importNode(resource_list, true));
    208 
    209     // the second part of the content is the metadata list
    210     Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    211     Element title = doc_.createElement(GSXML.METADATA_ELEM);
    212     title.setAttribute(GSXML.NAME_ATT, "Title");
    213     metadata_list.appendChild(title);
    214     meta_content.appendChild(metadata_list);
    215 
    216     Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
    217    
    218     Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
    219    
    220     page.appendChild(doc_.importNode(result_response, true));
    221    
     190   
     191    } // do teh query
     192
     193   
     194    // output the page
    222195    // process using the stylesheet
    223196    Document style_doc = converter_.getDOM(new File(stylesheet));
    224197    GSXSLT.absoluteIncludePaths(style_doc, config_);
    225     return transformer_.transform(style_doc, page);   
    226    
    227     }
    228 
    229     protected String fieldQuery(Element page, Element request) {
    230     return GSHTML.errorPage("field query not implemented");
    231 
    232 
    233     }
    234     protected String unknownQuery(Element page, Element request, String query_type) {
    235 
    236     return GSHTML.errorPage("unknown query subtype - "+query_type+" query not implemented");
     198    return (Element)transformer_.transform(style_doc, page);   
     199   
     200    }
     201
     202    protected Element fieldQuery(Element page, Element request) {
     203    String p =  GSHTML.errorPage("field query not implemented");
     204
     205    return converter_.getDOM(p).getDocumentElement();
     206    }
     207    protected Element unknownQuery(Element page, Element request, String query_type) {
     208
     209    String p =  GSHTML.errorPage("unknown query subtype - "+query_type+" query not implemented");
     210    return null;//converter_.getDOM(p).getDocumentElement();
    237211    }
    238212}
Note: See TracChangeset for help on using the changeset viewer.