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