Changeset 31873 for main


Ignore:
Timestamp:
2017-08-10T14:45:04+12:00 (7 years ago)
Author:
kjdon
Message:

modified so that we always get the siblings in any HLIsts, not jsut the top level ones. So if you have two hlists, eg years, then months, both will stay expanded as you browse through

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/GS2BrowseAction.java

    r30730 r31873  
    1212import org.greenstone.gsdl3.util.OID;
    1313import org.greenstone.gsdl3.util.UserContext;
     14import org.greenstone.gsdl3.util.XMLConverter;
    1415import org.w3c.dom.Document;
    1516import org.w3c.dom.Element;
     
    215216            return page_response;
    216217        }
    217 
    218         //If the user is viewing a horizontal classifier then we need to get extra information
     218        Document cl_struct_doc = cl_structure.getOwnerDocument();
     219
     220        //If the user is viewing a horizontal classifier then we need to get extra information - siblings along the horizontal
     221
     222        // cl_structure is the classifier structure containing ancestors and children of current node.
     223        // this test means that we won't do the right thing if we happened to have an HList inside a VList. Should really be testing to see if any nodes are HLists...
    219224        if (cl_structure.getAttribute(GSXML.CHILD_TYPE_ATT).equals(GSXML.HLIST))
    220225        {
     226     
    221227            //If we have a horizontal classifier and we have had the top-level node requested (e.g. CL1, CL2 etc.)
    222228            //then we want to get the children of the first classifier node (e.g. the children of CL2.1)
    223             if (OID.isTop(classifier_node))
    224             {
     229          if (OID.isTop(classifier_node)) {
     230           
    225231                boolean firstChildIsClassifierNode = false;
    226232                NodeList classifierChildrenNodes = GSXML.getChildrenByTagName(cl_structure, GSXML.CLASS_NODE_ELEM);
     
    236242                if (firstChildIsClassifierNode)
    237243                {
    238                     Element childStructure = getClassifierStructureFromID(doc, classifier_node + ".1", request, collection, service_name);
    239 
    240                     Element replacementElem = null;
    241                     NodeList childClassifierNodes = childStructure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
    242                     for (int i = 0; i < childClassifierNodes.getLength(); i++)
    243                     {
    244                         Element currentElem = (Element) childClassifierNodes.item(i);
    245                         if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(classifier_node + ".1"))
    246                         {
    247                             replacementElem = currentElem;
    248                             break;
    249                         }
    250                     }
    251 
     244                  // get the children of the first child
     245                    Element childStructure = getClassifierChildrenFromID(doc, classifier_node + ".1", request, collection, service_name);
     246
     247                    // and now insert it into the structure we already have
    252248                    NodeList nodesToSearch = cl_structure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
    253249                    for (int i = 0; i < nodesToSearch.getLength(); i++)
     
    257253                        {
    258254                            Element parent = (Element) currentElem.getParentNode();
    259                             parent.insertBefore(parent.getOwnerDocument().importNode(replacementElem, true), currentElem); // wrong doc node!!!
     255                            parent.insertBefore(cl_struct_doc.importNode(childStructure, true), currentElem);
    260256                            parent.removeChild(currentElem);
    261257                            break;
     
    263259                    }
    264260                }
    265             }
     261          } // if OID.isTop(classifier_node)
    266262            //If we have a horizontal classifier and we have NOT had the top-level node requested then we need to
    267263            //make sure we get the full list of top-level children to display (e.g. if the user has requested
    268264            //CL2.1.1 we also need to make sure we have CL2.2, CL2.3, CL2.4 etc.)
    269             else
    270             {
    271                 Element childStructure = getClassifierStructureFromID(doc, OID.getTop(classifier_node), request, collection, service_name);
    272 
    273                 String[] idParts = classifier_node.split("\\.");
    274                 String idToSearchFor = idParts[0] + "." + idParts[1];
    275 
    276                 Element replacementElem = null;
    277                 NodeList childClassifierNodes = cl_structure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
    278                 for (int i = 0; i < childClassifierNodes.getLength(); i++)
    279                 {
    280                     Element currentElem = (Element) childClassifierNodes.item(i);
    281                     if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(idToSearchFor))
    282                     {
    283                         replacementElem = currentElem;
    284                         break;
    285                     }
    286                 }
    287 
    288                 if (replacementElem != null)
    289                 {
    290                     NodeList nodesToSearch = childStructure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
    291                     for (int i = 0; i < nodesToSearch.getLength(); i++)
    292                     {
    293                         Element currentElem = (Element) nodesToSearch.item(i);
    294                         if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(idToSearchFor))
    295                         {
    296                             Element parent = (Element) currentElem.getParentNode();
    297                             parent.insertBefore(parent.getOwnerDocument().importNode(replacementElem,true), currentElem);
    298                             parent.removeChild(currentElem);
    299                             break;
    300                         }
    301                     }
    302 
    303                     cl_structure = childStructure;
    304                 }
    305             }
     265          else
     266            {
     267              // we need to get siblings of any HList nodes so the HLists remain expanded in the display
     268              // start at the bottom (the specified element) and work up teh list of parents expanding any that have hlists
     269              NodeList elems = GSXML.getNamedElements(cl_structure, GSXML.CLASS_NODE_ELEM, GSXML.NODE_ID_ATT, classifier_node);
     270              Element current_node = null;
     271              String current_id = classifier_node;
     272              if (elems != null) {
     273            // there should only be one node in the list
     274            current_node = (Element)elems.item(0);
     275              }
     276              if (current_node != null) {
     277            Element parent_node = (Element)current_node.getParentNode();
     278            while(parent_node != null && parent_node.getNodeName().equals(GSXML.CLASS_NODE_ELEM)) {
     279              if (((Element)parent_node).getAttribute(GSXML.CHILD_TYPE_ATT).equals(GSXML.HLIST)) {
     280                // get the children of the parent
     281                Element new_parent_elem = getClassifierChildrenFromID(cl_struct_doc, parent_node.getAttribute(GSXML.NODE_ID_ATT), request, collection, service_name);
     282
     283                // put current node into this new structure
     284                Element to_be_replaced = GSXML.getNamedElement(new_parent_elem, GSXML.CLASS_NODE_ELEM, GSXML.NODE_ID_ATT, current_id);
     285                if (to_be_replaced != null) {
     286                  new_parent_elem.insertBefore(current_node, to_be_replaced);
     287                  new_parent_elem.removeChild(to_be_replaced);
     288                  if (OID.isTop(parent_node.getAttribute(GSXML.NODE_ID_ATT))) {
     289                // we can't go up any further.
     290                cl_structure = new_parent_elem;
     291                parent_node = null;
     292
     293                  } else {
     294                Element next_parent = (Element)parent_node.getParentNode();
     295                next_parent.insertBefore(new_parent_elem, parent_node );
     296                next_parent.removeChild(parent_node);
     297                current_node = new_parent_elem;
     298                current_id = current_node.getAttribute(GSXML.NODE_ID_ATT);
     299                parent_node = next_parent;
     300                  }
     301                } else {
     302                  // something went wrong, we'll stop this
     303                  parent_node = null;
     304                }
     305              } else {
     306                current_node = parent_node;
     307                current_id = current_node.getAttribute(GSXML.NODE_ID_ATT);
     308                parent_node = (Element)current_node.getParentNode();
     309              }
     310            }
     311              } // if current_node != null
     312             
     313            }
    306314        }
    307315
     
    434442    }
    435443
    436     private Element getClassifierStructureFromID(Document doc, String id, Element request, String collection, String service_name)
     444    private Element getClassifierChildrenFromID(Document doc, String id, Element request, String collection, String service_name)
    437445    {
    438446        UserContext userContext = new UserContext(request);
    439447        String to = GSPath.appendLink(collection, service_name);
    440448
    441         Element firstClassifierNodeChildrenMessage = doc.createElement(GSXML.MESSAGE_ELEM);
    442         Element firstClassifierNodeChildrenRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
     449        Document msg_doc = XMLConverter.newDOM();
     450        Element firstClassifierNodeChildrenMessage = msg_doc.createElement(GSXML.MESSAGE_ELEM);
     451        Element firstClassifierNodeChildrenRequest = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
    443452        firstClassifierNodeChildrenMessage.appendChild(firstClassifierNodeChildrenRequest);
    444453
    445         Element paramList = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     454        Element paramList = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    446455        firstClassifierNodeChildrenRequest.appendChild(paramList);
    447456
    448         Element ancestorParam = doc.createElement(GSXML.PARAM_ELEM);
    449         paramList.appendChild(ancestorParam);
    450         ancestorParam.setAttribute(GSXML.NAME_ATT, "structure");
    451         ancestorParam.setAttribute(GSXML.VALUE_ATT, "ancestors");
    452 
    453         Element childrenParam = doc.createElement(GSXML.PARAM_ELEM);
     457        // Element ancestorParam = doc.createElement(GSXML.PARAM_ELEM);
     458        // paramList.appendChild(ancestorParam);
     459        // ancestorParam.setAttribute(GSXML.NAME_ATT, "structure");
     460        // ancestorParam.setAttribute(GSXML.VALUE_ATT, "ancestors");
     461
     462        Element childrenParam = msg_doc.createElement(GSXML.PARAM_ELEM);
    454463        paramList.appendChild(childrenParam);
    455464        childrenParam.setAttribute(GSXML.NAME_ATT, "structure");
    456465        childrenParam.setAttribute(GSXML.VALUE_ATT, "children");
    457466
    458         Element classifierToGetList = doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
    459         Element classifierToGet = doc.createElement(GSXML.CLASS_NODE_ELEM);
     467        Element classifierToGetList = msg_doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
     468        Element classifierToGet = msg_doc.createElement(GSXML.CLASS_NODE_ELEM);
    460469        classifierToGet.setAttribute(GSXML.NODE_ID_ATT, id);
    461470        classifierToGetList.appendChild(classifierToGet);
     
    469478        nsPath = GSPath.appendLink(nsPath, GSXML.CLASS_NODE_ELEM);
    470479        Element childStructure = (Element) GSXML.getNodeByPath(topClassifierNode, nsPath);
    471 
    472         return childStructure;
     480        return (Element)doc.importNode(childStructure, true);
    473481    }
    474482
Note: See TracChangeset for help on using the changeset viewer.