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 | |
|
20 | |
|
21 | |
|
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 | |
|
41 | |
|
42 | |
|
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 | |
|
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 | |
|
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 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
private List<Org> doOrgHierarchySearch(List<String> relatedOrgIds, List<String> orgTypes, String relationTypeKey) throws DoesNotExistException { |
114 | |
|
115 | |
|
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 | |
|
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 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
private Org findParentOrgForType(Org org, String orgTypeKey, String relationTypeKey) { |
146 | |
|
147 | 0 | if (org == null) { |
148 | 0 | return null; |
149 | |
} |
150 | |
|
151 | 0 | if (org.getType().getId().equals(orgTypeKey)) { |
152 | 0 | return org; |
153 | |
} |
154 | |
|
155 | 0 | return findParentOrgForType(organizationDao.getOrgByRelatedOrgAndType(org.getId(), relationTypeKey), orgTypeKey, relationTypeKey); |
156 | |
} |
157 | |
|
158 | |
} |