Coverage Report - org.kuali.student.core.organizationsearch.service.impl.OrganizationHierarchySearch
 
Classes in this File Line Coverage Branch Coverage Complexity
OrganizationHierarchySearch
0%
0/57
0%
0/24
4
 
 1  
 package org.kuali.student.core.organizationsearch.service.impl;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashSet;
 5  
 import java.util.List;
 6  
 import java.util.Set;
 7  
 
 8  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 9  
 import org.kuali.student.common.exceptions.MissingParameterException;
 10  
 import org.kuali.student.common.search.dto.SearchParam;
 11  
 import org.kuali.student.common.search.dto.SearchRequest;
 12  
 import org.kuali.student.common.search.dto.SearchResult;
 13  
 import org.kuali.student.common.search.dto.SearchResultRow;
 14  
 import org.kuali.student.core.organization.dao.OrganizationDao;
 15  
 import org.kuali.student.core.organization.entity.Org;
 16  
 
 17  
 /**
 18  
  * This is a description of what this class does - pctsw don't forget to fill this in.
 19  
  * 
 20  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 21  
  */
 22  
 public class OrganizationHierarchySearch implements OrganizationSearch {
 23  
 
 24  
     private OrganizationDao organizationDao;
 25  
 
 26  
     public OrganizationHierarchySearch() {
 27  0
         super();
 28  0
     }
 29  
 
 30  
     public OrganizationDao getOrganizationDao() {
 31  0
         return organizationDao;
 32  
     }
 33  
 
 34  
     public void setOrganizationDao(OrganizationDao organizationDao) {
 35  0
         this.organizationDao = organizationDao;
 36  0
     }
 37  
 
 38  
     /**
 39  
      * This overridden method ...
 40  
      * 
 41  
      * @see org.kuali.student.core.organizationsearch.service.impl.OrganizationSearch#search(org.kuali.student.common.search.dto.SearchRequest)
 42  
      */
 43  
     @Override
 44  
     public SearchResult search(SearchRequest searchRequest) {
 45  
 
 46  0
         List<String> relatedOrgIds = null;
 47  0
         List<String> orgTypes = null;
 48  0
         String relationTypeKey = null;
 49  0
         String orgOptionalId = null;
 50  0
         for (SearchParam param : searchRequest.getParams()) {
 51  0
             if ("org.queryParam.relatedOrgIds".equals(param.getKey())) {
 52  0
                 relatedOrgIds = (List<String>) param.getValue();
 53  0
                 continue;
 54  0
             } else if ("org.queryParam.optionalOrgTypeList".equals(param.getKey())) {
 55  0
                 orgTypes = (List<String>) param.getValue();
 56  0
                 continue;
 57  0
             } else if ("org.queryParam.optionalRelationType".equals(param.getKey())) {
 58  0
                 relationTypeKey = (String) param.getValue();
 59  0
                 continue;
 60  0
             } else if ("org.queryParam.relOrgOptionalId".equals(param.getKey())) {
 61  0
                 orgOptionalId = (String) param.getValue();
 62  0
                 continue;
 63  
             }
 64  
         }
 65  
         try {
 66  0
             List<Org> orgs = null;
 67  0
             if (orgOptionalId != null) {
 68  0
                 orgs = new ArrayList<Org>();
 69  0
                 orgs.add(organizationDao.fetch(Org.class, orgOptionalId));
 70  
             } else {
 71  0
                 orgs = doOrgHierarchySearch(relatedOrgIds, orgTypes, relationTypeKey);
 72  
             }
 73  
 
 74  
             // Create a search result for the return value
 75  0
             SearchResult searchResult = new SearchResult();
 76  0
             for (Org org : orgs) {
 77  0
                 SearchResultRow resultRow = new SearchResultRow();
 78  
 
 79  
                 // Map the result cells
 80  0
                 resultRow.addCell("org.resultColumn.orgId", org.getId());
 81  0
                 resultRow.addCell("org.resultColumn.orgShortName", org.getShortName());
 82  0
                 resultRow.addCell("org.resultColumn.orgOptionalLongName", org.getLongName());
 83  0
                 resultRow.addCell("org.resultColumn.orgType", org.getType().getId());
 84  
 
 85  0
                 searchResult.getRows().add(resultRow);
 86  0
             }
 87  
 
 88  0
             return searchResult;
 89  0
         } catch (DoesNotExistException e) {
 90  0
             throw new RuntimeException("Error performing search");
 91  
         }
 92  
 
 93  
     }
 94  
 
 95  
     /**
 96  
      * This method ...
 97  
      * 
 98  
      * @param relatedOrgIds
 99  
      * @param orgTypes
 100  
      * @param relationTypeKey
 101  
      * @return
 102  
      * @throws MissingParameterException
 103  
      * @throws DoesNotExistException
 104  
      */
 105  
     private List<Org> doOrgHierarchySearch(List<String> relatedOrgIds, List<String> orgTypes, String relationTypeKey) throws DoesNotExistException {
 106  
 
 107  
         // Loop thru org types and related org ids to add the ancestors one by one.
 108  0
         Set<Org> orgs = new HashSet<Org>();
 109  0
         for (String orgTypeKey : orgTypes) {
 110  0
             for (String relatedOrgId : relatedOrgIds) {
 111  0
                 Org childOrg = null;
 112  
                 try {
 113  0
                     childOrg = organizationDao.fetch(Org.class, relatedOrgId);
 114  0
                 } catch (DoesNotExistException e) {
 115  0
                     continue;
 116  0
                 }
 117  0
                 Org parentOrg = findParentOrgForType(childOrg, orgTypeKey, relationTypeKey);
 118  
 
 119  
                 // Add parent Org to list
 120  0
                 if (parentOrg != null) {
 121  0
                     orgs.add(parentOrg);
 122  
                 }
 123  0
             }
 124  
         }
 125  
 
 126  0
         return new ArrayList<Org>(orgs);
 127  
     }
 128  
 
 129  
     /**
 130  
      * This method recursively looks for a parent organisation of a given type.
 131  
      * 
 132  
      * @param org
 133  
      * @param orgTypeKey
 134  
      * @param relationTypeKey
 135  
      * @return Org
 136  
      */
 137  
     private Org findParentOrgForType(Org org, String orgTypeKey, String relationTypeKey) {
 138  
         // If the org type is not found, return null
 139  0
         if (org == null) {
 140  0
             return null;
 141  
         }
 142  
         // Return org if type is of given type
 143  0
         if (org.getType().getId().equals(orgTypeKey)) {
 144  0
             return org;
 145  
         }
 146  
         // Recursive call
 147  0
         return findParentOrgForType(organizationDao.getOrgByRelatedOrgAndType(org.getId(), relationTypeKey), orgTypeKey, relationTypeKey);
 148  
     }
 149  
 
 150  
 }