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 | |
|
19 | |
|
20 | |
|
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 | |
|
40 | |
|
41 | |
|
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 | |
|
75 | 0 | SearchResult searchResult = new SearchResult(); |
76 | 0 | for (Org org : orgs) { |
77 | 0 | SearchResultRow resultRow = new SearchResultRow(); |
78 | |
|
79 | |
|
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 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private List<Org> doOrgHierarchySearch(List<String> relatedOrgIds, List<String> orgTypes, String relationTypeKey) throws DoesNotExistException { |
106 | |
|
107 | |
|
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 | |
|
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 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
private Org findParentOrgForType(Org org, String orgTypeKey, String relationTypeKey) { |
138 | |
|
139 | 0 | if (org == null) { |
140 | 0 | return null; |
141 | |
} |
142 | |
|
143 | 0 | if (org.getType().getId().equals(orgTypeKey)) { |
144 | 0 | return org; |
145 | |
} |
146 | |
|
147 | 0 | return findParentOrgForType(organizationDao.getOrgByRelatedOrgAndType(org.getId(), relationTypeKey), orgTypeKey, relationTypeKey); |
148 | |
} |
149 | |
|
150 | |
} |