Ignore:
Timestamp:
2012-07-31T15:06:27+12:00 (12 years ago)
Author:
kjdon
Message:

added lots more functions to the BasicDocument classes. Code has come from Browse and Retrieve services

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/BasicDocumentDatabase.java

    r26041 r26045  
    3636import java.util.Map;
    3737import java.util.Set;
     38import java.util.StringTokenizer;
    3839import java.util.Iterator;
    3940import java.io.File;
    4041
     42import org.apache.commons.lang3.StringUtils;
    4143import org.apache.log4j.*;
    4244
     
    136138    }
    137139   
     140    /**
     141     * returns the structural information asked for. info_type may be one of
     142     * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS, INFO_DOC_TYPE
     143     */
     144  public String getStructureInfo(String doc_id, String info_type){
     145        String value = "";
     146        if (info_type.equals(INFO_NUM_SIBS))
     147        {
     148            String parent_id = OID.getParent(doc_id);
     149            if (parent_id.equals(doc_id))
     150            {
     151                value = "0";
     152            }
     153            else
     154            {
     155                value = String.valueOf(getNumChildren(parent_id));
     156            }
     157            return value;
     158        }
     159
     160        if (info_type.equals(INFO_NUM_CHILDREN))
     161        {
     162            return String.valueOf(getNumChildren(doc_id));
     163        }
     164
     165        if (info_type.equals(INFO_SIB_POS))
     166        {
     167            String parent_id = OID.getParent(doc_id);
     168            if (parent_id.equals(doc_id))
     169            {
     170                return "-1";
     171            }
     172
     173            DBInfo info = this.coll_db.getInfo(parent_id);
     174            if (info == null)
     175            {
     176                return "-1";
     177            }
     178
     179            String contains = info.getInfo("contains");
     180            contains = StringUtils.replace(contains, "\"", parent_id);
     181            String[] children = contains.split(";");
     182            for (int i = 0; i < children.length; i++)
     183            {
     184                String child_id = children[i];
     185                if (child_id.equals(doc_id))
     186                {
     187                    return String.valueOf(i + 1); // make it from 1 to length
     188
     189                }
     190            }
     191
     192            return "-1";
     193        }
     194        if (info_type.equals(INFO_DOC_TYPE))
     195   
     196        {
     197          return getDocType(doc_id);
     198        }
     199        return null;
     200
     201  }
     202
    138203    /** returns true if the node has child nodes */
    139204    public boolean hasChildren(String node_id){
     
    149214    }
    150215   
     216  public int getNumChildren(String node_id) {
     217        DBInfo info = this.coll_db.getInfo(node_id);
     218        if (info == null)
     219        {
     220            return 0;
     221        }
     222        String contains = info.getInfo("contains");
     223        if (contains.equals(""))
     224        {
     225            return 0;
     226        }
     227        String[] children = contains.split(";");
     228        return children.length;
     229
     230
     231  }
     232  /** returns a list of the child ids in order, null if no children
     233   */
     234  public ArrayList<String> getChildrenIds(String node_id) {
     235        DBInfo info = this.coll_db.getInfo(node_id);
     236        if (info == null)
     237        {
     238            return null;
     239        }
     240
     241        String contains = info.getInfo("contains");
     242        if (contains.equals(""))
     243        {
     244            return null;
     245        }
     246        ArrayList<String> children = new ArrayList<String>();
     247        StringTokenizer st = new StringTokenizer(contains, ";");
     248        while (st.hasMoreTokens())
     249        {
     250            String child_id = StringUtils.replace(st.nextToken(), "\"", node_id);
     251            children.add(child_id);
     252        }
     253        return children;
     254
     255
     256  }
     257
    151258    /** returns true if the node has a parent */
    152259    public boolean hasParent(String node_id){
     
    157264    return true;
    158265    }
     266    /**
     267     * returns the node id of the parent node, null if no parent
     268     */
     269  public String getParentId(String node_id) {
     270        String parent = OID.getParent(node_id);
     271        if (parent.equals(node_id))
     272        {
     273            return null;
     274        }
     275        return parent;
     276
     277  }
    159278   
     279  /**
     280   * returns the node id of the root node of the document containing node_id
     281   */
     282  public String getRootId(String node_id) {
     283    return OID.getTop(node_id);
     284  }
     285
    160286   /** convert indexer internal id to Greenstone oid */
    161287    public String internalNum2OID(long docnum)
     
    169295    }
    170296
     297
     298    /**
     299     * adds all the children of doc_id to the doc element, and if
     300     * recursive=true, adds all their children as well
     301     */
     302    public void addDescendants(Element doc, String doc_id, boolean recursive)
     303    {
     304        ArrayList<String> child_ids = getChildrenIds(doc_id);
     305        if (child_ids == null)
     306            return;
     307        for (int i = 0; i < child_ids.size(); i++)
     308        {
     309            String child_id = child_ids.get(i);
     310            Element child_elem = createDocNode(child_id);
     311            doc.appendChild(child_elem);
     312            if (recursive && (!child_elem.getAttribute(GSXML.NODE_TYPE_ATT).equals(GSXML.NODE_TYPE_LEAF) || child_elem.getAttribute(GSXML.DOC_TYPE_ATT).equals(GSXML.DOC_TYPE_PAGED)))
     313            {
     314                addDescendants(child_elem, child_id, recursive);
     315            }
     316        }
     317    }
     318
     319    /**
     320     * adds all the siblings of current_id to the parent element. returns the
     321     * new current element
     322     */
     323    public Element addSiblings(Element parent_node, String parent_id, String current_id)
     324    {
     325        Element current_node = GSXML.getFirstElementChild(parent_node);//(Element)parent_node.getFirstChild();
     326        if (current_node == null)
     327        {
     328            // create a sensible error message
     329            logger.error(" there should be a first child.");
     330            return null;
     331        }
     332        // remove the current child,- will add it in later in its correct place
     333        parent_node.removeChild(current_node);
     334
     335        // add in all the siblings,
     336        addDescendants(parent_node, parent_id, false);
     337
     338        // find the node that is now the current node
     339        // this assumes that the new node that was created is the same as
     340        // the old one that was removed - we may want to replace the new one
     341        // with the old one.
     342        Element new_current = GSXML.getNamedElement(parent_node, current_node.getNodeName(), GSXML.NODE_ID_ATT, current_id);
     343        return new_current;
     344    }
     345
     346  /** returns the list of sibling ids, including the specified node_id */
     347  public ArrayList<String> getSiblingIds(String node_id){
     348        String parent_id = getParentId(node_id);
     349        if (parent_id == null)
     350        {
     351            return null;
     352        }
     353        return getChildrenIds(parent_id);
     354
     355  }
     356
    171357}
    172358
Note: See TracChangeset for help on using the changeset viewer.