Changeset 31181

Show
Ignore:
Timestamp:
08.12.2016 02:02:55 (3 years ago)
Author:
litvinovg
Message:

Added java code to use groups in cross collection search

Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service
Files:
2 modified

Legend:

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

    r31166 r31181  
    22 
    33import java.io.File; 
     4 
    45import org.apache.log4j.Logger; 
    56import org.greenstone.gsdl3.util.GSFile; 
     
    1112import org.w3c.dom.Node; 
    1213import org.w3c.dom.NodeList; 
     14 
    1315import java.util.Set; 
    1416 
     
    1921 
    2022import java.util.HashSet; 
     23import java.util.Iterator; 
    2124 
    2225public class CollectionGroups extends ServiceRack { 
     
    2629 
    2730    private static final String GROUP_CONTENT = "GroupCurrentContent"; 
     31    private static final String UNIQUE_COLLECTIONS = "UniqueCollections"; 
    2832 
    2933    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.BerryBasket.class.getName()); 
     
    3539    } 
    3640 
    37     String[] _services = { GROUP_CONTENT }; 
     41    String[] _services = { GROUP_CONTENT, UNIQUE_COLLECTIONS }; 
    3842 
    3943    public boolean configure(Element info, Element extra_info) { 
     
    6064 
    6165        Document doc = XMLConverter.newDOM(); 
    62  
    6366        UserContext userContext = new UserContext(request); 
    6467 
     68      // Prepare basic response 
     69        Element result = GSXML.createBasicResponse(doc, GSXML.SERVICE_TYPE_GROUPINFO); 
     70         
     71         
    6572        // Get param list from request 
    6673        Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
     
    7986        groupPath = groupPath.replaceAll("(^/+)|(/+$)", ""); 
    8087 
    81         // Get the message router info 
    82         Element mr_info_message = doc.createElement(GSXML.MESSAGE_ELEM); 
    83         Element mr_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext); 
    84         mr_info_message.appendChild(mr_request); 
    85         Element mr_info_response_message = (Element) this.router.process(mr_info_message); 
    86         if (mr_info_response_message == null) { 
    87             logger.error(" couldn't query the message router!"); 
    88             return null; 
    89         } 
    90         Element mr_info_response = (Element) GSXML.getChildByTagName(mr_info_response_message, GSXML.RESPONSE_ELEM); 
    91         if (mr_info_response == null) { 
    92             logger.error("couldn't query the message router!"); 
    93             return null; 
    94         } 
    95  
    96         Element mr_collection_list = (Element) GSXML.getChildByTagName(mr_info_response, 
    97                 GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER); 
     88        Element mrCollectionList = getMRCollectionList(userContext); 
     89        if (mrCollectionList == null){ 
     90            result.appendChild(doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER)); 
     91            return result; 
     92        } 
    9893        //Get current groups and collections 
    9994        Element groupContent = getCurrentContent(groupPath); 
    10095     
    10196        //Get ungrouped collection list 
    102         Element ungroupedCollections = getUngroupedCollections(mr_collection_list); 
    103          
    104         // Prepare basic response 
    105         Element result = GSXML.createBasicResponse(doc, GSXML.SERVICE_TYPE_GROUPINFO); 
    106  
     97        Element ungroupedCollections = getUngroupedCollections(mrCollectionList); 
     98         
    10799        // If groupContent is empty return empty collection list 
    108100        if (groupContent == null) { 
     
    135127                    String collection_name = collection.getAttribute(GSXML.NAME_ATT); 
    136128                    // Check whether collection from current view exists in message router response 
    137                     Element checkedCollection = GSXML.getNamedElement(mr_collection_list, GSXML.COLLECTION_ELEM,GSXML.NAME_ATT, collection_name); 
     129                    Element checkedCollection = GSXML.getNamedElement(mrCollectionList, GSXML.COLLECTION_ELEM,GSXML.NAME_ATT, collection_name); 
    138130                    if (checkedCollection != null) { 
    139131                        //Set position value 
     
    184176    } 
    185177 
     178    protected Element processUniqueCollections(Element request) { 
     179        Document doc = XMLConverter.newDOM(); 
     180        UserContext userContext = new UserContext(request); 
     181        // Prepare basic response 
     182        Element result = GSXML.createBasicResponse(doc, GSXML.SERVICE_TYPE_GROUPINFO); 
     183        Element resultCollections = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER); 
     184        result.appendChild(resultCollections); 
     185        Element mrCollectionList = getMRCollectionList(userContext); 
     186        //Collections from message router check 
     187        if (mrCollectionList == null){ 
     188            logger.error("mrCollectionList is null!"); 
     189            return result; 
     190        } 
     191        //paramList check 
     192        Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
     193        if (paramList == null) { 
     194            logger.error("UniqueCollections request had no paramList."); 
     195            return result; 
     196        } 
     197        //Add collections from request 
     198        Set<String> uniq_colls = new HashSet<String>(); 
     199        Element collParam = GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSXML.COLLECTION_ELEM); 
     200        if (collParam != null){ 
     201            String colls = GSXML.getValue(collParam); 
     202            if (!colls.isEmpty()) 
     203            { 
     204                String[] _colls = colls.split(","); 
     205                for (int i=0;i < _colls.length;i++){ 
     206                    uniq_colls.add(_colls[i]); 
     207                } 
     208            } 
     209        } 
     210        //groupParam check 
     211        Element groupParam = GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSXML.GROUP_ELEM); 
     212        if (groupParam == null) { 
     213            logger.error("UniqueCollections request had no groupParam."); 
     214            return result; 
     215        } 
     216        //Add collections from groups 
     217        String[] groups = null; 
     218        groups = GSXML.getValue(groupParam).split(","); 
     219        for (int i = 0; i < groups.length; i++) { 
     220            Element groupContent = getCurrentContent(groups[i]); 
     221            //If group exists 
     222            if (groupContent != null) { 
     223                NodeList collectionNodes = GSXML.getChildrenByTagName(groupContent, GSXML.COLLECTION_ELEM); 
     224                for (int j = 0; j < collectionNodes.getLength(); j++) { 
     225                    String collName = ((Element) collectionNodes.item(j)).getAttribute(GSXML.NAME_ATT); 
     226                    uniq_colls.add(collName); 
     227                } 
     228            } 
     229     
     230        } 
     231        //Fill result collectionList  
     232        for (Iterator<String> iterator = uniq_colls.iterator(); iterator.hasNext();) { 
     233            String collectionName = (String) iterator.next(); 
     234            Element checkedCollection = GSXML.getNamedElement(mrCollectionList, GSXML.COLLECTION_ELEM,GSXML.NAME_ATT, collectionName); 
     235            if (checkedCollection != null){ 
     236                resultCollections.appendChild(doc.importNode(checkedCollection, true)); 
     237            } 
     238        } 
     239        return result; 
     240 
     241    } 
     242     
    186243    private Element getCurrentContent(String path) { 
    187244 
     
    195252             path = ""; 
    196253        } 
     254         
     255        path = path.replaceAll("(/+)", "/"); 
     256        path = path.replaceAll("(^/+)|(/+$)", ""); 
     257         
    197258        String[] pathSteps = path.split("/"); 
    198259 
     
    203264            if (!pathSteps[i].isEmpty()) { 
    204265                currentView = GSXML.getNamedElement(currentView, GSXML.GROUP_ELEM, GSXML.NAME_ATT, pathSteps[i]); 
     266                if (currentView == null){ 
     267                    break; 
     268                } 
    205269            } 
    206270        } 
     
    330394        return pathInfo; 
    331395    } 
    332  
     396    private Element getMRCollectionList(UserContext userContext){ 
     397        Document doc = XMLConverter.newDOM(); 
     398        // Get the message router info 
     399        Element mr_info_message = doc.createElement(GSXML.MESSAGE_ELEM); 
     400        Element mr_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext); 
     401        mr_info_message.appendChild(mr_request); 
     402        Element mr_info_response_message = (Element) this.router.process(mr_info_message); 
     403        if (mr_info_response_message == null) { 
     404            logger.error(" couldn't query the message router!"); 
     405            return null; 
     406        } 
     407        Element mr_info_response = (Element) GSXML.getChildByTagName(mr_info_response_message, GSXML.RESPONSE_ELEM); 
     408        if (mr_info_response == null) { 
     409            logger.error("Message router response is null!"); 
     410            return null; 
     411        } 
     412         
     413        Element mr_collection_list = (Element) GSXML.getChildByTagName(mr_info_response, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER); 
     414         
     415        return mr_collection_list; 
     416    } 
    333417} 
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/CrossCollectionSearch.java

    r30958 r31181  
    4848    protected static final String QUERY_PARAM = "query"; 
    4949    protected static final String COLLECTION_PARAM = "collection"; 
     50    protected static final String GROUP_PARAM = "group"; 
    5051  protected static final String MAXDOCS_PARAM = "maxDocs"; // matches standard maxDocs, but in this case, means max docs per collection 
    5152  protected static final String HITS_PER_PAGE_PARAM = "hitsPerPage"; 
     
    172173            return result; // Return the empty result 
    173174        } 
    174  
    175175        // get the collection list 
    176176        String[] colls_list = coll_ids_list_no_all; 
     
    184184            } 
    185185        } 
     186             
     187        colls_list = mergeGroups(userContext, param_list, colls_list); 
    186188         
    187189        String maxdocs = MAXDOCS_DEFAULT; 
     
    266268 
    267269    //     } 
     270     
     271     
     272     
    268273    protected boolean initCollectionList(String lang) 
    269274    { 
     
    489494        return result; 
    490495    } 
     496     
     497    private String[] mergeGroups(UserContext userContext, Element paramList, String[] collArray){ 
     498        Document doc = XMLConverter.newDOM(); 
     499        Element groupParam = GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GROUP_PARAM); 
     500        Element collParam = GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, COLLECTION_PARAM); 
     501        //Group param not null and coll param null or not 'all' 
     502        if (groupParam != null && (collParam == null || !GSXML.getValue(collParam).equals("all"))) 
     503        {    
     504            //GroupInfo service to get uniq collections 
     505            String uniqCollServiceName = "UniqueCollections"; 
     506            Element infoResponse = getMRInfo(userContext); 
     507            Element serviceList = (Element) GSXML.getChildByTagName(infoResponse, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER); 
     508            if (serviceList == null) {  
     509                logger.error("Service list is null!"); 
     510                return collArray; 
     511            } 
     512            Element groupInfoService = GSXML.getNamedElement(serviceList, GSXML.SERVICE_ELEM, GSXML.NAME_ATT,   uniqCollServiceName); 
     513            if (groupInfoService == null){ 
     514                logger.error("UniqueCollections service unavailable; Check your groupConfig.xml"); 
     515                return collArray; 
     516            } 
     517            Element groupQueryMessage = doc.createElement(GSXML.MESSAGE_ELEM); 
     518            Element groupQueryRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, uniqCollServiceName, userContext); 
     519            groupQueryMessage.appendChild(groupQueryRequest); 
     520            Element groupParamList = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
     521            groupQueryRequest.appendChild(groupParamList); 
     522            if (collParam != null){ 
     523                groupParamList.appendChild(doc.importNode(GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSXML.COLLECTION_ELEM), true));     
     524            } 
     525            groupParamList.appendChild(doc.importNode(GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSXML.GROUP_ELEM), true)); 
     526            Element groupQueryResult = (Element) this.router.process(groupQueryMessage); 
     527            Element groupQueryResonse = (Element) GSXML.getChildByTagName(groupQueryResult, GSXML.RESPONSE_ELEM); 
     528            Element collList = (Element) GSXML.getChildByTagName(groupQueryResonse, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER); 
     529            NodeList collections = GSXML.getChildrenByTagName(collList, GSXML.COLLECTION_ELEM); 
     530            collArray = new String[collections.getLength()]; 
     531            for (int i = 0; i < collections.getLength(); i++){ 
     532                String collName = ((Element) collections.item(i)).getAttribute(GSXML.NAME_ATT); 
     533                collArray[i] = collName; 
     534            } 
     535            return collArray; 
     536        } 
     537        return collArray; 
     538         
     539    } 
     540    private Element getMRInfo(UserContext userContext){ 
     541        Document doc = XMLConverter.newDOM(); 
     542        Element infoMessage = doc.createElement(GSXML.MESSAGE_ELEM); 
     543        Element infoRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext); 
     544        Element paramList = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
     545        infoRequest.appendChild(paramList); 
     546        GSXML.addParameterToList(paramList, GSXML.SUBSET_PARAM, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER); 
     547        infoMessage.appendChild(infoRequest); 
     548        Element responseMessage = (Element) this.router.process(infoMessage); 
     549        if (responseMessage == null) 
     550        { 
     551            logger.error("couldn't query the message router!"); 
     552            return null; 
     553        } 
     554        Element infoResponse = (Element) GSXML.getChildByTagName(responseMessage, GSXML.RESPONSE_ELEM); 
     555        if (infoResponse == null) 
     556        { 
     557            logger.error("response from message router is null!"); 
     558            return null; 
     559        } 
     560         
     561        return infoResponse; 
     562         
     563    } 
     564     
    491565}