Changeset 4260


Ignore:
Timestamp:
2003-05-08T16:21:21+12:00 (21 years ago)
Author:
kjdon
Message:

added in some extra stuff for structure retrieve - instead of just the actual structure, you can now retrieve info about it eg numSiblings, numChildren, siblingPosition

File:
1 edited

Legend:

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

    r4017 r4260  
    3333import java.util.StringTokenizer;
    3434import java.util.Vector;
    35 
     35import java.util.Set;
     36import java.util.Iterator;
    3637
    3738/** Implements the generic retrieval and classifier services for GS2
     
    5758    private static final String CLASSIFIER_METADATA_SERVICE = "ClassifierBrowseMetadataRetrieve";
    5859   
     60    protected static final String STRUCT_PARAM = "structure";
     61    protected static final String INFO_PARAM = "info";
     62   
     63    protected static final String STRUCT_ANCESTORS = "ancestors";
     64    protected static final String STRUCT_PARENT = "parent";
     65    protected static final String STRUCT_SIBS = "siblings";
     66    protected static final String STRUCT_CHILDREN = "children";
     67    protected static final String STRUCT_DESCENDS = "descendants";
     68
     69    protected static final String INFO_NUM_SIBS = "numSiblings";
     70    protected static final String INFO_NUM_CHILDREN = "numChildren";
     71    protected static final String INFO_SIB_POS = "siblingPosition";
     72
    5973    protected static final int DOCUMENT=1;
    6074    protected static final int CLASSIFIER=2;
     
    297311    } else {
    298312        node = doc_.createElement(GSXML.DOC_NODE_ELEM);
    299         if (!child && OID.isTop(doc_id)) {
     313        if (/*!child &&*/ OID.isTop(doc_id)) {
    300314        node.setAttribute(GSXML.NODE_TYPE_ATT, GSXML.NODE_TYPE_ROOT);
    301315        } else if (!parent && isLeafNode(doc_id)) {
     
    417431    }
    418432
     433    // the type of info required
     434    boolean want_structure = false;
     435    boolean want_info = false;
     436
     437    Vector info_types=new Vector();
    419438    // The document structure information desired
    420     boolean wantAncestors = false;
    421     boolean wantParent = false;
    422     boolean wantSiblings = false;
    423     boolean wantChildren = false;
    424     boolean wantDescendants = false;
     439    boolean want_ancestors = false;
     440    boolean want_parent = false;
     441    boolean want_siblings = false;
     442    boolean want_children = false;
     443    boolean want_descendants = false;
    425444
    426445    // Process the request parameters
    427     Element param = (Element) param_list.getFirstChild();
    428     while (param != null) {
    429         if (!param.getNodeName().equals(GSXML.PARAM_ELEM)) {
    430         System.err.println("Warning: Non-param in paramList (ignored).");
    431         }
    432         else {
    433         // Identify the structure information desired
    434         if (param.getAttribute(GSXML.NAME_ATT).equals("structure")) {
    435             String structure = GSXML.getValue(param);
    436 
    437             // This is NOT locale sensitive
    438             if (structure.compareToIgnoreCase("ancestors") == 0)
    439             wantAncestors = true;
    440             else if (structure.compareToIgnoreCase("parent") == 0)
    441             wantParent = true;
    442             else if (structure.compareToIgnoreCase("siblings") == 0)
    443             wantSiblings = true;
    444             else if (structure.compareToIgnoreCase("children") == 0)
    445             wantChildren = true;
    446             else if (structure.compareToIgnoreCase("descendants") == 0)
    447             wantDescendants = true;
    448             else
    449             System.err.println("Warning: Unknown value \"" + structure + "\".");
    450         }
    451         }
    452 
    453         param = (Element) param.getNextSibling();
     446    NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
     447    for (int i=0; i<params.getLength();i++) {
     448       
     449        Element param = (Element)params.item(i);
     450        String p_name = param.getAttribute(GSXML.NAME_ATT);
     451        String p_value = GSXML.getValue(param);
     452        // Identify the structure information desired
     453        if (p_name.equals(STRUCT_PARAM)) {
     454        want_structure = true;
     455       
     456        // This is NOT locale sensitive
     457        if (p_value.equals(STRUCT_ANCESTORS))
     458            want_ancestors = true;
     459        else if (p_value.equals(STRUCT_PARENT))
     460            want_parent = true;
     461        else if (p_value.equals(STRUCT_SIBS))
     462            want_siblings = true;
     463        else if (p_value.equals(STRUCT_CHILDREN))
     464            want_children = true;
     465        else if (p_value.equals(STRUCT_DESCENDS))
     466            want_descendants = true;
     467        else
     468            System.err.println("Warning: Unknown value \"" + p_value + "\".");
     469        } else if (p_name.equals(INFO_PARAM)) {
     470        want_info = true;
     471        info_types.add(p_value);
     472        }
    454473    }
    455474
    456475    // Make sure there is no repeated information
    457     if (wantAncestors)
    458         wantParent = false;
    459     if (wantDescendants)
    460         wantChildren = false;
     476    if (want_ancestors)
     477        want_parent = false;
     478    if (want_descendants)
     479        want_children = false;
    461480   
    462481   
     
    476495        String doc_id = doc_ids[i];
    477496       
    478        
     497        System.out.println("doc_id = "+doc_id);
    479498        if (OID.needsTranslating(doc_id)) {
    480499        doc_id = gdbm_src_.translateOID(doc_id);
     500        System.out.println("translated doc_id = "+doc_id);
    481501        }
    482502
     
    485505        doc_list.appendChild(doc);
    486506        doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
    487         // all structure info goes into a docNodestructure elem
    488         // classifier elem should be something else???
    489         Element structure_elem = doc_.createElement(GSXML.NODE_STRUCTURE_ELEM);
    490         doc.appendChild(structure_elem);
    491 
    492         // Add the requested structure information
    493         Element current = createDocNode(doc_id, false, false);
    494 
    495         //Ancestors: continually add parent nodes until the root is reached
    496         Element top_node = current; // the top node so far
    497         if (wantAncestors) {
    498         String current_id = doc_id;
    499         while (true) {
    500             Element parent = getParent(current_id);
    501             if (parent == null)
    502             break;
    503 
    504             parent.appendChild(top_node);
    505             current_id = parent.getAttribute(GSXML.NODE_ID_ATT);
    506             top_node = parent;
    507         }
    508         }
    509         // Parent: get the parent of the selected node
    510         if (wantParent) {
    511         Element parent = getParent(doc_id);
    512         if (parent != null) {
    513             parent.appendChild(current);
    514             top_node = parent;
    515         }
    516         }
    517 
    518         // now the top node is the root of the structure
    519         structure_elem.appendChild(top_node);
    520        
    521         //Siblings: get the other descendants of the selected node's parent
    522         if (wantSiblings) {
    523         Element parent = (Element)current.getParentNode(); // this may be the structure element if there has been no request for parents or ancestors
    524         String parent_id = OID.getParent(doc_id);
    525 
    526         // add siblings, - returns a pointer to the new current node
    527         current = addSiblings(parent, parent_id, doc_id);
    528             }
    529 
     507
     508
     509        if (want_info) {
     510
     511        Element info_elem = doc_.createElement("nodeStructureInfo");
     512        doc.appendChild(info_elem);
     513       
     514        for (int j=0; j<info_types.size(); j++) {
     515            String info_type = (String)info_types.get(j);
     516            Element inf = getInfo(doc_id, info_type);
     517            if (inf != null) {
     518            info_elem.appendChild(inf);
     519            }
     520        }
     521        }
     522        if (want_structure) {
     523        // all structure info goes into a nodeStructure elem
     524        Element structure_elem = doc_.createElement(GSXML.NODE_STRUCTURE_ELEM);
     525        doc.appendChild(structure_elem);
     526       
     527        // Add the requested structure information
     528        Element current = createDocNode(doc_id, false, false);
     529       
     530        //Ancestors: continually add parent nodes until the root is reached
     531        Element top_node = current; // the top node so far
     532        if (want_ancestors) {
     533            String current_id = doc_id;
     534            while (true) {
     535            Element parent = getParent(current_id);
     536            if (parent == null)
     537                break;
     538           
     539            parent.appendChild(top_node);
     540            current_id = parent.getAttribute(GSXML.NODE_ID_ATT);
     541            top_node = parent;
     542            }
     543        }
     544        // Parent: get the parent of the selected node
     545        if (want_parent) {
     546            Element parent = getParent(doc_id);
     547            if (parent != null) {
     548            parent.appendChild(current);
     549            top_node = parent;
     550            }
     551        }
     552
     553       
     554        // now the top node is the root of the structure
     555        structure_elem.appendChild(top_node);
     556       
     557        //Siblings: get the other descendants of the selected node's parent
     558        if (want_siblings) {
     559            Element parent = (Element)current.getParentNode(); // this may be the structure element if there has been no request for parents or ancestors
     560            String parent_id = OID.getParent(doc_id);
     561           
     562            // add siblings, - returns a pointer to the new current node
     563            current = addSiblings(parent, parent_id, doc_id);
     564        }
     565       
    530566        // Children: get the descendants, but only one level deep
    531         if (wantChildren)
    532         addDescendants(current, doc_id, false);
    533         // Descendants: recursively get every descendant of the selected node
    534         if (wantDescendants)
    535         addDescendants(current, doc_id, true);
    536     }
     567        if (want_children)
     568            addDescendants(current, doc_id, false);
     569        // Descendants: recursively get every descendant of the selected node
     570        if (want_descendants)
     571            addDescendants(current, doc_id, true);
     572        } // if want structure
     573    } // for each doc
    537574    return result;
    538575    }
     
    576613    // The metadata information required
    577614    Vector metadata_list = new Vector();
    578    
     615    boolean all_metadata = false;
    579616    // Process the request parameters
    580617    Element param = (Element) param_list.getFirstChild();
     
    583620        if (param.getAttribute(GSXML.NAME_ATT).equals("metadata")) {
    584621        String metadata = GSXML.getValue(param);
     622        if (metadata.equals("all")) {
     623            all_metadata = true;
     624            break;
     625        }
    585626        metadata_list.add(metadata);
    586627        }
     
    590631    Element node_list = doc_.createElement(node_name+GSXML.LIST_MODIFIER);
    591632    result.appendChild(node_list);
    592 
     633   
    593634    // Get the documents
    594635    Element request_node_list = (Element) GSXML.getChildByTagName(request, node_name+GSXML.LIST_MODIFIER);
     
    615656        new_node.appendChild(node_meta_list);
    616657        DBInfo info = gdbm_src_.getInfo(node_id);
    617         for (int m = 0; m < metadata_list.size(); m++) {
    618         String metadata = (String) metadata_list.get(m);
    619         String value = info.getInfo(metadata);
    620         GSXML.addMetadata(doc_, node_meta_list, metadata, value);
     658        if (info == null) {// I have had a case where it is null!
     659        continue;
     660        }
     661        System.out.println("found the info");
     662        if (all_metadata) {
     663        System.out.println("trying to get all the metadata");
     664        // return everything out of the database
     665        Set keys = info.getKeys();
     666        Iterator it = keys.iterator();
     667        while(it.hasNext()) {
     668            String key = (String)it.next();
     669            System.out.println("getting metadata "+key);
     670            String value = info.getInfo(key);
     671            GSXML.addMetadata(doc_, node_meta_list, key, value);
     672        }
     673        } else { // just get the selected ones
     674       
     675        for (int m = 0; m < metadata_list.size(); m++) {
     676            String metadata = (String) metadata_list.get(m);
     677            String value = info.getInfo(metadata);
     678            GSXML.addMetadata(doc_, node_meta_list, metadata, value);
     679        }
    621680        }
    622681    }
     
    641700    return doc_content;
    642701    }
     702
     703    protected Element getInfo(String doc_id, String info_type) {
     704
     705    String value="";
     706    if (info_type.equals(INFO_NUM_SIBS)) {
     707        String parent_id = OID.getParent(doc_id);
     708        if (parent_id.equals(doc_id)) {
     709        value="0";
     710        } else {
     711        value = String.valueOf(getNumChildren(parent_id));
     712        }
     713    } else if (info_type.equals(INFO_NUM_CHILDREN)) {
     714        value = String.valueOf(getNumChildren(doc_id));
     715    } else if (info_type.equals(INFO_SIB_POS)) {
     716        String parent_id = OID.getParent(doc_id);
     717        if (parent_id.equals(doc_id)) {
     718        value="-1";
     719        } else {
     720        DBInfo info = gdbm_src_.getInfo(parent_id);
     721        String contains = info.getInfo("contains");
     722        contains = contains.replaceAll("\"", parent_id);
     723        String [] children = contains.split(";");
     724        for (int i=0;i<children.length;i++) {
     725            String child_id = children[i];
     726            System.out.println("child="+child_id+" doc="+doc_id);
     727            if (child_id.equals(doc_id)) {
     728            value = String.valueOf(i+1); // make it from 1 to length
     729            break;
     730            }
     731        }
     732        }
     733    } else {
     734        return null;
     735    }
     736    Element info_elem = doc_.createElement("info");
     737    info_elem.setAttribute(GSXML.NAME_ATT, info_type);
     738    info_elem.setAttribute(GSXML.VALUE_ATT, value);
     739    return info_elem;
     740    }
     741
     742    protected int getNumChildren(String doc_id) {
     743    DBInfo info = gdbm_src_.getInfo(doc_id);
     744    String contains = info.getInfo("contains");
     745    if (contains.equals("")) {
     746        return 0;
     747    }
     748    String [] children = contains.split(";");
     749    return children.length;
     750    }
     751   
    643752}   
Note: See TracChangeset for help on using the changeset viewer.