Changeset 4023


Ignore:
Timestamp:
2003-03-28T09:55:57+12:00 (21 years ago)
Author:
kjdon
Message:

added in stuff for using enrich services to markup the documents

File:
1 edited

Legend:

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

    r3997 r4023  
    4141    public static final String SIBLING_ARG = "sib";
    4242
     43    /** if this is set to true, when a document is displayed, any annotation
     44     * type services (enrich) will be offered to the user as well */
     45    protected static final boolean provide_annotations = true;
     46
    4347    /* add the action specific args to the cgi param list
    4448     */
     
    5256    // for now, no subaction eventually we may want to have subactions such as text assoc or something ?
    5357   
     58    // check that  the stylesheet is present
     59    String stylesheet = GSFile.stylesheetFile(config_, "document.xsl");
     60    if (stylesheet == null) {
     61        System.err.println("DocumentAction Error: document stylesheet not found!");
     62        return null;
     63    }
     64
    5465    // get the request - assume only one
    5566    Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
     
    5869
    5970    String collection = (String) params.get(GSCGI.COLLECTION_ARG);
    60     String to;
     71    String lang = request.getAttribute(GSXML.LANG_ATT);
    6172    String document_name = (String) params.get(GSCGI.DOCUMENT_ARG);
    6273    if (document_name == null || document_name.equals("")) {
     
    6475        return null;
    6576    }
     77
    6678    //whether to retrieve siblings or not
    6779    boolean get_siblings = false;
     
    7183    }
    7284   
     85    // Create the return page tree
     86    Element page = doc_.createElement(GSXML.PAGE_ELEM);
     87    page.setAttribute(GSXML.LANG_ATT, lang);
     88
     89    // Add the display element, config stuff, and original request from the message
     90    page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
     91    page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
     92    Element page_request = GSXML.duplicateWithNewName(doc_, request, GSXML.PAGE_REQUEST_ELEM, true);
     93    page.appendChild(page_request);
     94    // the element for all the response data
     95    Element page_response = doc_.createElement(GSXML.PAGE_RESPONSE_ELEM);
     96    page.appendChild(page_response);
     97
     98    // get the additional data needed for the page
     99    getBackgroundData(page_response, collection, lang);
     100
     101    // the_document is where all the doc info - structure and metadata etc
     102    //-  is added into, to be returned in the page
     103    Element the_document = doc_.createElement(GSXML.DOCUMENT_ELEM);
     104    page_response.appendChild(the_document);
     105
     106   
    73107    // Build a request to obtain the document structure
    74108    Element ds_message = doc_.createElement(GSXML.MESSAGE_ELEM);
    75     Element ds_request = doc_.createElement(GSXML.REQUEST_ELEM);
     109    String to = GSPath.appendLink(collection, "DocumentStructureRetrieve");  // Hard-wired?
     110    Element ds_request = GSXML.createBasicRequest(doc_,GSXML.REQUEST_TYPE_PROCESS, to, lang);
    76111    ds_message.appendChild(ds_request);
    77     to = GSPath.appendLink(collection, "DocumentStructureRetrieve");  // Hard-wired?
    78     ds_request.setAttribute(GSXML.TO_ATT, to);
    79     ds_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    80     ds_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
    81112
    82113    // Create a parameter list to specify the required structure information
     
    115146    Element ds_response_structure = (Element) GSXML.getNodeByPath(ds_response_message, path);
    116147
    117     // the_document is where all the extra info is added into, to be returned in the page
    118     Element the_document = doc_.createElement(GSXML.DOCUMENT_ELEM);
    119148    // add the contents of the structure bit into the_document
    120149    NodeList structs = ds_response_structure.getChildNodes();
     
    125154    // Build a request to obtain some document metadata
    126155    Element dm_message = doc_.createElement(GSXML.MESSAGE_ELEM);
    127     Element dm_request = doc_.createElement(GSXML.REQUEST_ELEM);
     156    to = GSPath.appendLink(collection, "DocumentMetadataRetrieve");  // Hard-wired?
     157    Element dm_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_PROCESS, to, lang);
    128158    dm_message.appendChild(dm_request);
    129     to = GSPath.appendLink(collection, "DocumentMetadataRetrieve");  // Hard-wired?
    130     dm_request.setAttribute(GSXML.TO_ATT, to);
    131     dm_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    132     dm_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
    133 
    134159    // Create a parameter list to specify the required metadata information
    135160    // for now get Title
     
    175200    // Build a request to obtain some document content
    176201    Element dc_message = doc_.createElement(GSXML.MESSAGE_ELEM);
    177     Element dc_request = doc_.createElement(GSXML.REQUEST_ELEM);
     202    to = GSPath.appendLink(collection, "DocumentContentRetrieve");  // Hard-wired?
     203    Element dc_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_PROCESS, to, lang);
    178204    dc_message.appendChild(dc_request);
    179     to = GSPath.appendLink(collection, "DocumentContentRetrieve");  // Hard-wired?
    180     dc_request.setAttribute(GSXML.TO_ATT, to);
    181     dc_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    182     dc_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
     205   
    183206
    184207    // Create a parameter list to specify the request parameters - empty for now
     
    193216    path = GSPath.appendLink(path, GSXML.DOC_NODE_ELEM);
    194217    Element dc_response_doc = (Element) GSXML.getNodeByPath(dc_response_message, path);
    195     //path = GSPath.appendLink(path, GSXML.DOC_NODE_CONTENT_ELEM);
    196218    Element dc_response_doc_content = (Element) GSXML.getChildByTagName(dc_response_doc, GSXML.NODE_CONTENT_ELEM);
     219    if (provide_annotations) {
     220        // now we can modifiy the response doc if needed
     221        String enrich_service = (String)params.get(GSCGI.SERVICE_ARG);
     222        if (enrich_service != null && !enrich_service.equals("")) {
     223        // send a message to the service
     224        Element enrich_message = doc_.createElement(GSXML.MESSAGE_ELEM);
     225        Element enrich_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_PROCESS, enrich_service, lang);
     226        enrich_message.appendChild(enrich_request);
     227        Element enrich_pl = getServiceParamList(cgi_paramList);
     228        enrich_request.appendChild(enrich_pl);
     229        Element doc_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
     230        enrich_request.appendChild(doc_list);
     231        doc_list.appendChild(doc_.importNode(dc_response_doc, true));
     232       
     233        Element enrich_response = mr_.process(enrich_message);
     234       
     235        String [] links = {GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER, GSXML.DOC_NODE_ELEM, GSXML.NODE_CONTENT_ELEM};
     236        path = GSPath.createPath(links);
     237        System.out.println("enrich path = "+path);
     238
     239        //appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
     240        //path = GSPath.appendLink(GSXML.DOC_NODE_ELEM
     241        dc_response_doc_content = (Element)GSXML.getNodeByPath(enrich_response, path);
     242        }
     243    }
    197244    // use the returned id rather than the sent one cos there may have
    198245    // been modifiers such as .pr that are removed.
    199246    String modified_doc_id = dc_response_doc.getAttribute(GSXML.NODE_ID_ATT);
     247    the_document.setAttribute("selectedNode", modified_doc_id);
    200248    // Merge the document content with the metadata and structure information
    201249    for (int i = 0; i < doc_nodes.getLength(); i++) {
     
    209257    }
    210258
    211     // Create the return page tree
    212     Element page = doc_.createElement(GSXML.PAGE_ELEM);
    213     page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
    214 
    215     // Add the display element, config stuff, and original request from the message
    216     page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
    217     page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
    218     Element page_request = GSXML.duplicateWithNewName(doc_, request, "pageRequest", true);
    219     page.appendChild(page_request);
    220 
    221     // get the collection or cluster description - for the httpAddress type metadata
    222     Element coll_about_message = doc_.createElement(GSXML.MESSAGE_ELEM);
    223     Element coll_about_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE,  collection, page.getAttribute(GSXML.LANG_ATT));
    224     coll_about_message.appendChild(coll_about_request);
    225     Element coll_about_response = (Element)mr_.process(coll_about_message);
    226 
    227     // add the response to the page
    228     Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
    229     Element page_response = GSXML.duplicateWithNewName(doc_, response, "pageResponse", false);
    230     page_response.appendChild(the_document);
    231 
    232     // Add the results of the action to the page
    233     page.appendChild(page_response);
    234     System.out.println("Page:\n" + converter_.getPrettyString(page));
    235 
    236     // Finally, process using the stylesheet, and return
    237     String stylesheet = GSFile.stylesheetFile(config_, "document.xsl");
    238     if (stylesheet == null) {
    239         System.err.println("DocumentAction Error: document stylesheet not found!");
    240         return null;
    241     }
    242259
    243260    Document style_doc = converter_.getDOM(new File(stylesheet));
     
    245262    return (Element) transformer_.transform(style_doc, page);   
    246263    }
     264
     265    protected Element getServiceParamList(Element cgi_param_list) {
     266
     267    Element new_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     268    Element param;
     269    NodeList cgi_params = cgi_param_list.getChildNodes();
     270    for (int i=0; i<cgi_params.getLength(); i++) {
     271        Element p = (Element) cgi_params.item(i);
     272        String name = p.getAttribute(GSXML.NAME_ATT);
     273        if (name.equals(GSCGI.SERVICE_ARG) || name.equals(GSCGI.REQUEST_TYPE_ARG) || name.equals(GSCGI.CLUSTER_ARG) || name.equals(GSCGI.DOCUMENT_ARG) || name.equals(SIBLING_ARG) ) {
     274        continue;
     275        }
     276        // esle add it in to the list
     277        new_param_list.appendChild(doc_.importNode(p, true));
     278    }
     279    return new_param_list;
     280    }
     281
     282    /** this method gets the collection description, the format info, the
     283     * list of enrich services, etc - stuff that is needed for the page,
     284     * but is the same whatever the query is - should be cached */
     285    protected  boolean getBackgroundData(Element page_response,
     286                     String collection, String lang) {
     287
     288    // create a message to process - contains requests for the collection
     289    // description, the format element, the enrich services on offer
     290    // these could all be cached
     291    Element info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
     292
     293    // the collection request
     294    Element coll_about_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE,  collection, lang);
     295    info_message.appendChild(coll_about_request);
     296
     297    // the format request - ignore for now, where does this request go to??
     298    //Element format_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_FORMAT, ???, lang);
     299    //info_message.appendChild(format_request);
     300
     301    // the enrich_services request - only do this if provide_annotations is true
     302
     303    if (provide_annotations) {
     304        Element enrich_services_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, "", lang);
     305        enrich_services_request.setAttribute(GSXML.INFO_ATT, "serviceList");
     306        info_message.appendChild(enrich_services_request);
     307    }
     308   
     309    Element info_response = (Element)mr_.process(info_message);
     310
     311    // the collection is the first response
     312    NodeList responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
     313    Element coll_resp = (Element)responses.item(0);
     314    page_response.appendChild(doc_.importNode(GSXML.getChildByTagName(coll_resp, GSXML.COLLECTION_ELEM), true));
     315   
     316    //Element format_resp = (Element) responses.item(1);
     317    //page_reponse.appendChild(doc_.importNode(GSXML.getChildByTagName(format_resp, GSXML.FORMAT_ELEM), true));
     318   
     319
     320    if (provide_annotations) {
     321        Element services_resp = (Element)responses.item(1); // change when format request added in
     322
     323        // a new message for the mr
     324        Element enrich_message = doc_.createElement(GSXML.MESSAGE_ELEM);
     325       
     326        NodeList e_services = services_resp.getElementsByTagName(GSXML.SERVICE_ELEM);
     327        boolean service_found = false;
     328        for (int j=0; j<e_services.getLength(); j++) {
     329        if (((Element)e_services.item(j)).getAttribute(GSXML.TYPE_ATT).equals("enrich")) {
     330            Element s = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, ((Element)e_services.item(j)).getAttribute(GSXML.NAME_ATT), lang);
     331            enrich_message.appendChild(s);
     332            service_found = true;
     333        }
     334        }
     335        if (service_found) {
     336        Element enrich_response = mr_.process(enrich_message);
     337
     338        e_services = enrich_response.getElementsByTagName(GSXML.SERVICE_ELEM);
     339        Element service_list = doc_.createElement(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
     340        for (int i=0; i<e_services.getLength(); i++) {
     341            Element pl = (Element)GSXML.getChildByTagName(e_services.item(i), GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     342            if (pl != null) {
     343            // add short names to the params in the param list
     344            cgi_.paramListAddShortNames(pl);
     345            }
     346            service_list.appendChild(doc_.importNode(e_services.item(i), true));
     347        }
     348        page_response.appendChild(service_list);
     349        }
     350    } // if provide_annotations
     351    return true;
     352       
     353    }
    247354}
Note: See TracChangeset for help on using the changeset viewer.