Ignore:
Timestamp:
2017-01-13T03:46:43+13:00 (7 years ago)
Author:
Georgiy Litvinov
Message:

Java code for hierarchy view in cross collection search

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/CollectionGroups.java

    r31181 r31285  
    3030    private static final String GROUP_CONTENT = "GroupCurrentContent";
    3131    private static final String UNIQUE_COLLECTIONS = "UniqueCollections";
     32    private static final String COLLECTIONS_HIERARCHY = "CollectionsHierarchy";
    3233
    3334    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.BerryBasket.class.getName());
     
    3940    }
    4041
    41     String[] _services = { GROUP_CONTENT, UNIQUE_COLLECTIONS };
     42    String[] _services = { GROUP_CONTENT, UNIQUE_COLLECTIONS, COLLECTIONS_HIERARCHY };
    4243
    4344    public boolean configure(Element info, Element extra_info) {
     
    5657        }
    5758        // Load group configuration from file
    58         readGroupConfiguration();
    59 
    60         return true;
     59        return readGroupConfiguration();
    6160    }
    6261
     
    8685        groupPath = groupPath.replaceAll("(^/+)|(/+$)", "");
    8786
    88         Element mrCollectionList = getMRCollectionList(userContext);
     87        Element mrCollectionList = getAvailableCollectionList(userContext);
    8988        if (mrCollectionList == null){
    9089            result.appendChild(doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER));
     
    9291        }
    9392        //Get current groups and collections
    94         Element groupContent = getCurrentContent(groupPath);
     93        Element groupContent = getRawCurrentContent(groupPath);
    9594   
    9695        //Get ungrouped collection list
     
    9998        // If groupContent is empty return empty collection list
    10099        if (groupContent == null) {
    101             // If config file is broken
    102             if (hierarchy == null || groupDesc == null) {
    103                 //Get ungrouped collection list
    104                 result.appendChild(doc.importNode(ungroupedCollections, true));
    105             } else {
    106                 //If config is ok, but in this group no any collection or group
    107                 result.appendChild(doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER));
    108             }
     100            result.appendChild(doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER));
    109101            return result;
    110102        }
     
    139131                    Element currentGroup = (Element) currentContent.item(i);
    140132                    String currentGroupName = currentGroup.getAttribute(GSXML.NAME_ATT);
    141                     //Get group description
    142                     Element group = getGroupDescription(currentGroupName);
    143                     //Set position value
    144                     if (group != null) {
    145                         group.setAttribute(GSXML.POSITION_ATT, String.valueOf(i));
    146                         result_group_list.appendChild(doc.importNode(group, true));
    147                     }
    148 
     133                    Element groupDescription = getGroupDescription(currentGroupName);
     134                   
     135                    groupDescription.setAttribute(GSXML.POSITION_ATT, String.valueOf(i));
     136                    result_group_list.appendChild(doc.importNode(groupDescription, true));
    149137                }
    150138
     
    152140            //Add ungrouped collections if groupPath /+ or "" or null
    153141            if (groupPath.isEmpty()) {
    154                
    155142               
    156143                //Add each ungrouped collection to the collection list in loop
     
    183170        Element resultCollections = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
    184171        result.appendChild(resultCollections);
    185         Element mrCollectionList = getMRCollectionList(userContext);
     172        Element mrCollectionList = getAvailableCollectionList(userContext);
    186173        //Collections from message router check
    187174        if (mrCollectionList == null){
     
    218205        groups = GSXML.getValue(groupParam).split(",");
    219206        for (int i = 0; i < groups.length; i++) {
    220             Element groupContent = getCurrentContent(groups[i]);
     207            Element groupContent = getRawCurrentContent(groups[i]);
    221208            //If group exists
    222209            if (groupContent != null) {
     
    241228    }
    242229   
    243     private Element getCurrentContent(String path) {
    244 
    245         if (hierarchy == null || groupDesc == null) {
    246             return null;
    247         }
    248        
    249         Document doc = XMLConverter.newDOM();
    250        
     230    protected Element processCollectionsHierarchy(Element request){
     231       
     232        Document doc = XMLConverter.newDOM();
     233        UserContext userContext = new UserContext(request);
     234        // Prepare basic response
     235        Element result = GSXML.createBasicResponse(doc, GSXML.SERVICE_TYPE_GROUPINFO);
     236        String currentPath = "";
     237        Element currentContent = getRawCurrentContent(currentPath);
     238        if (currentContent == null){
     239            return result;
     240        }
     241        Element searchableCollectionList = getSearchableCollectionList(userContext);
     242        if (searchableCollectionList == null){
     243            return result;
     244        }
     245       
     246        //Get ungrouped collection list
     247        sanitizeCurrentContent(currentContent, searchableCollectionList);
     248        addGroupInfo(currentContent, currentPath);
     249        addUngroupedCollections(currentContent, searchableCollectionList);
     250        result.appendChild(doc.importNode(currentContent, true));
     251        return result;
     252    }
     253   
     254    private void addGroupInfo(Element currentContent, String groupPath) {
     255        NodeList groups = GSXML.getChildrenByTagName(currentContent,GSXML.GROUP_ELEM);
     256        for (int i=0;i<groups.getLength();i++){
     257            Element group = (Element) groups.item(i);
     258            String name = group.getAttribute(GSXML.NAME_ATT);
     259            String newPath = groupPath + "/" + name;
     260            group.setAttribute(GSXML.PATH_ATT, newPath);
     261            Element groupDescription = getGroupDescription(name);
     262            Element titleEl = (Element) GSXML.getChildByTagName(groupDescription, GSXML.TITLE_ELEM);
     263            String title = titleEl.getTextContent();
     264            group.setAttribute(GSXML.TITLE_ELEM, title  );
     265            addGroupInfo(group, newPath);
     266        }
     267       
     268    }
     269
     270    private Element getRawCurrentContent(String path) {
     271
    251272        if (path == null) {
    252273             path = "";
    253274        }
    254        
     275
     276        Document doc = XMLConverter.newDOM();
    255277        path = path.replaceAll("(/+)", "/");
    256278        path = path.replaceAll("(^/+)|(/+$)", "");
     
    259281
    260282       
    261         Element currentView = (Element) hierarchy.cloneNode(true);
     283        Element currentContent = (Element) hierarchy.cloneNode(true);
    262284        // Get the current view
    263285        for (int i = 0; i < pathSteps.length; i++) {
    264286            if (!pathSteps[i].isEmpty()) {
    265                 currentView = GSXML.getNamedElement(currentView, GSXML.GROUP_ELEM, GSXML.NAME_ATT, pathSteps[i]);
    266                 if (currentView == null){
     287                currentContent = GSXML.getNamedElement(currentContent, GSXML.GROUP_ELEM, GSXML.NAME_ATT, pathSteps[i]);
     288                if (currentContent == null){
    267289                    break;
    268290                }
    269291            }
    270292        }
    271         if (currentView == null || !currentView.hasChildNodes()) {
     293        if (currentContent == null || !currentContent.hasChildNodes()) {
    272294            // Return to the main page
    273295            return null;
    274296        }
    275         return currentView;
    276     }
    277 
    278     private void readGroupConfiguration() {
     297        return currentContent;
     298    }
     299   
     300    private void sanitizeCurrentContent(Element currentContent, Element checkedCollectionList){
     301        if (currentContent == null){
     302            return;
     303        }
     304        NodeList nodes = currentContent.getElementsByTagName(GSXML.COLLECTION_ELEM);
     305        for (int i = 0; i < nodes.getLength(); i++) {
     306            Element element = (Element) nodes.item(i);
     307            String name = element.getAttribute(GSXML.NAME_ATT);
     308            Element checkedCollection = GSXML.getNamedElement(checkedCollectionList, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
     309            if (checkedCollection == null) {
     310                element.getParentNode().removeChild(element);
     311                i--;
     312            }
     313        }
     314    }
     315   
     316    private void addUngroupedCollections(Element currentContent, Element availableCollections){
     317        if (currentContent == null){
     318            return;
     319        }
     320        Document doc = currentContent.getOwnerDocument();
     321        NodeList collectionList = availableCollections.getElementsByTagName(GSXML.COLLECTION_ELEM);
     322        for (int i = 0; i < collectionList.getLength();i++){
     323            Element collection = (Element) collectionList.item(i);
     324            String name = collection.getAttribute(GSXML.NAME_ATT);
     325            NodeList foundCollection = GSXML.getNamedElements(currentContent, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
     326            if (foundCollection.getLength() == 0){
     327                Element ungroupedCollection = doc.createElement(GSXML.COLLECTION_ELEM);
     328                ungroupedCollection.setAttribute(GSXML.NAME_ATT, name);
     329                currentContent.appendChild(ungroupedCollection);
     330            }
     331        }
     332       
     333    }
     334
     335    private boolean readGroupConfiguration() {
    279336
    280337        File configFile = new File(GSFile.groupConfigFile(site_home));
     
    282339        if (!configFile.exists()) {
    283340            logger.info("Groups config file " + configFile.getPath() + " does not exist.");
    284             return;
     341            return false;
    285342        }
    286343        // Try to read and catch exception if it fails
     
    301358            logger.error("Error occurred while trying to remove emtpy nodes from groupConfig.xml");
    302359            e.printStackTrace();
     360            return false;
    303361        } 
    304362       
    305363        hierarchy = (Element) GSXML.getChildByTagName(content, GSXML.HIERARCHY_ELEM);
    306364        groupDesc = (Element) GSXML.getChildByTagName(content, GSXML.GROUP_DESC_ELEM);
     365        if (hierarchy == null || groupDesc == null){
     366            logger.error("Error processing groups configuration. Check groupConfig.xml");
     367            return false;
     368        }
     369        verifyGroupDescription();
     370        return true;
     371    }
     372
     373    private void verifyGroupDescription() {
     374        Document doc = groupDesc.getOwnerDocument();
     375        NodeList groups = hierarchy.getElementsByTagName(GSXML.GROUP_ELEM);
     376        for (int i = 0; i < groups.getLength(); i++) {
     377            Element group = (Element) groups.item(i);
     378            String name = group.getAttribute(GSXML.NAME_ATT);
     379            Element foundDescription = GSXML.getNamedElement(groupDesc, GSXML.GROUP_ELEM, GSXML.NAME_ATT, name);
     380            if (foundDescription == null){
     381                Element defaultDescription = doc.createElement(GSXML.GROUP_ELEM);
     382                defaultDescription.setAttribute(GSXML.NAME_ATT, name);
     383                Element groupTitle = doc.createElement(GSXML.TITLE_ELEM);
     384                groupTitle.setTextContent(name);
     385                defaultDescription.appendChild(groupTitle);
     386                groupDesc.appendChild(defaultDescription);
     387            }
     388        }
     389       
    307390    }
    308391
    309392    private Element getGroupDescription(String name) {
    310         if (groupDesc != null) {
    311             Element description = (Element) GSXML.getNamedElement(groupDesc, GSXML.GROUP_ELEM, GSXML.NAME_ATT, name);
    312             if (description != null) {
    313                 return description;
    314             }
     393        Element description = (Element) GSXML.getNamedElement(groupDesc, GSXML.GROUP_ELEM, GSXML.NAME_ATT, name);
     394        if (description == null) {
    315395            logger.error("GroupDescription is not defined. Check your groupConfig.xml");
    316         }
    317            
    318         logger.error("No group descriptions found. Check your groupConfig.xml");
    319         return null;
     396        }
     397        return description;
    320398    }
    321399
    322400    private Element getUngroupedCollections(Element mr_collection_list) {
    323401       
    324         if (groupDesc == null || hierarchy == null) {
    325             logger.error("No group descriptions in groupConfig.xml. Check your groupConfig.xml");
    326             //return mr_collection_list
    327             return mr_collection_list;
    328         }
    329402        Document doc = XMLConverter.newDOM();
    330403        // Create Set
     
    366439    private Element getPathInfo(String path) {
    367440
    368         if (hierarchy == null || groupDesc == null) {
    369             return null;
    370         }
    371        
    372441        Document doc = XMLConverter.newDOM();
    373442       
     
    387456                    pathStepDescription.setAttribute(GSXML.POSITION_ATT, String.valueOf(i));
    388457                    pathStepDescription.setAttribute(GSXML.PATH_ATT, currentPath);
    389                     pathInfo.appendChild(doc.importNode(pathStepDescription, true));
    390                 }
     458                }
     459                pathInfo.appendChild(doc.importNode(pathStepDescription, true));
    391460            }
    392461        }
     
    394463        return pathInfo;
    395464    }
    396     private Element getMRCollectionList(UserContext userContext){
     465    private Element getAvailableCollectionList(UserContext userContext){
    397466        Document doc = XMLConverter.newDOM();
    398467        // Get the message router info
     
    415484        return mr_collection_list;
    416485    }
     486    private Element getSearchableCollectionList(UserContext userContext){
     487        Document doc = XMLConverter.newDOM();
     488        Element collectionList = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
     489        // Get the message router info
     490        Element mr_info_message = doc.createElement(GSXML.MESSAGE_ELEM);
     491        Element mr_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "TextQuery" , userContext);
     492        mr_info_message.appendChild(mr_request);
     493        Element mr_info_response_message = (Element) this.router.process(mr_info_message);
     494        if (mr_info_response_message == null) {
     495            logger.error(" couldn't query the message router!");
     496            return null;
     497        }
     498        NodeList options = mr_info_response_message.getElementsByTagName(GSXML.PARAM_OPTION_ELEM);
     499        for (int i = 0; i < options.getLength(); i++) {
     500            Element option = (Element) options.item(i);
     501            String name = option.getAttribute(GSXML.NAME_ATT);
     502            if (name.equals("all")){
     503                continue;
     504            }
     505            Element collection = doc.createElement(GSXML.COLLECTION_ELEM);
     506            collection.setAttribute(GSXML.NAME_ATT, name);
     507            collectionList.appendChild(collection);
     508        }
     509        return collectionList;
     510    }
    417511}
Note: See TracChangeset for help on using the changeset viewer.