1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
package org.kuali.student.core.personsearch.service.impl; |
10 | |
|
11 | |
import java.util.ArrayList; |
12 | |
import java.util.Collections; |
13 | |
import java.util.Comparator; |
14 | |
import java.util.HashMap; |
15 | |
import java.util.List; |
16 | |
import java.util.Map; |
17 | |
|
18 | |
import org.kuali.rice.kim.bo.Person; |
19 | |
import org.kuali.rice.kim.service.IdentityManagementService; |
20 | |
import org.kuali.student.common.search.dto.SearchParam; |
21 | |
import org.kuali.student.common.search.dto.SearchRequest; |
22 | |
import org.kuali.student.common.search.dto.SearchResult; |
23 | |
import org.kuali.student.common.search.dto.SearchResultCell; |
24 | |
import org.kuali.student.common.search.dto.SearchResultRow; |
25 | |
import org.kuali.student.common.search.dto.SearchTypeInfo; |
26 | |
import org.kuali.student.common.search.dto.SortDirection; |
27 | |
|
28 | 1 | public final class QuickViewByGivenName extends PersonSearch implements SearchOperation { |
29 | |
public static final String SEARCH_TYPE = "person.search.personQuickViewByGivenName"; |
30 | |
public static final String CRITERIA_TYPE = "person.search.personByGivenName"; |
31 | |
public static final String RESULT_TYPE = "person.search.personQuickView"; |
32 | |
final static public String NAME_PARAM = "person.queryParam.personGivenName"; |
33 | |
final static public String ID_PARAM = "person.queryParam.personId"; |
34 | |
final static public String AFFILIATION_PARAM = "person.queryParam.personAffiliation"; |
35 | |
final static public String EXCLUDED_USER_ID = "person.queryParam.excludedUserId"; |
36 | |
|
37 | |
final static public String PERSON_ID_RESULT = "person.resultColumn.PersonId"; |
38 | |
final static public String ENTITY_ID_RESULT = "person.resultColumn.EntityId"; |
39 | |
final static public String DISPLAY_NAME_RESULT = "person.resultColumn.DisplayName"; |
40 | |
final static public String GIVEN_NAME_RESULT = "person.resultColumn.GivenName"; |
41 | |
final static public String PRINCIPAL_NAME_RESULT = "person.resultColumn.PrincipalName"; |
42 | |
|
43 | |
final static private String KIM_PERSON_AFFILIATION_TYPE_CODE = "affiliationTypeCode"; |
44 | |
|
45 | |
final static private String KIM_PRINCIPALS_PRINCIPALNAME = "principals.principalName"; |
46 | |
final static private String KIM_PRINCIPALS_PRINCIPALID = "principals.principalId"; |
47 | |
final static private String KIM_PERSON_FIRST_NAME = "names.firstName"; |
48 | |
final static private String KIM_PERSON_MIDDLE_NAME = "names.middleName"; |
49 | |
final static private String KIM_PERSON_LAST_NAME = "names.lastName"; |
50 | |
|
51 | |
private List<Person> findPersons(final IdentityManagementService identityService, final SearchRequest searchRequest) { |
52 | 0 | String nameSearch = null; |
53 | 0 | String affilSearch = null; |
54 | 0 | String idSearch = null; |
55 | 0 | String excludedUserId = null; |
56 | 0 | for (SearchParam param : searchRequest.getParams()) { |
57 | 0 | String value = (String) param.getValue(); |
58 | 0 | if (!value.isEmpty()) { |
59 | 0 | if (value.indexOf("%") == -1 && value.indexOf("*") == -1) { |
60 | 0 | value = "*" + value + "*"; |
61 | |
} |
62 | 0 | if (NAME_PARAM.equals(param.getKey())) { |
63 | 0 | if (nameSearch != null) { |
64 | 0 | nameSearch += "|" + value; |
65 | |
} else { |
66 | 0 | nameSearch = value; |
67 | |
} |
68 | 0 | } else if (AFFILIATION_PARAM.equals(param.getKey())) { |
69 | 0 | if (affilSearch != null) { |
70 | 0 | affilSearch += "|" + value; |
71 | |
} else { |
72 | 0 | affilSearch = value; |
73 | |
} |
74 | 0 | } else if (ID_PARAM.equals(param.getKey())) { |
75 | 0 | if (idSearch != null) { |
76 | 0 | idSearch += "|" + param.getValue(); |
77 | |
} else { |
78 | 0 | idSearch = param.getValue().toString(); |
79 | |
} |
80 | 0 | } else if (EXCLUDED_USER_ID.equals(param.getKey())) { |
81 | 0 | excludedUserId = (String) param.getValue(); |
82 | |
} |
83 | |
} |
84 | 0 | } |
85 | 0 | final List<Map<String, String>> searches = new ArrayList<Map<String, String>>(); |
86 | 0 | if (idSearch != null) { |
87 | 0 | Map<String, String> criteria = new HashMap<String, String>(); |
88 | 0 | criteria.put(KIM_PRINCIPALS_PRINCIPALID, idSearch); |
89 | 0 | searches.add(criteria); |
90 | 0 | } else if (nameSearch != null) { |
91 | 0 | Map<String, String> principalNameCriteria = new HashMap<String, String>(); |
92 | 0 | principalNameCriteria.put(KIM_PRINCIPALS_PRINCIPALNAME, nameSearch); |
93 | 0 | searches.add(principalNameCriteria); |
94 | |
|
95 | 0 | Map<String, String> firstNameCriteria = new HashMap<String, String>(); |
96 | 0 | firstNameCriteria.put(KIM_PERSON_FIRST_NAME, nameSearch); |
97 | 0 | searches.add(firstNameCriteria); |
98 | |
|
99 | 0 | Map<String, String> middleNameCriteria = new HashMap<String, String>(); |
100 | 0 | middleNameCriteria.put(KIM_PERSON_MIDDLE_NAME, nameSearch); |
101 | 0 | searches.add(middleNameCriteria); |
102 | |
|
103 | 0 | Map<String, String> lastNameCriteria = new HashMap<String, String>(); |
104 | 0 | lastNameCriteria.put(KIM_PERSON_LAST_NAME, nameSearch); |
105 | 0 | searches.add(lastNameCriteria); |
106 | |
} |
107 | |
|
108 | 0 | if (affilSearch != null) { |
109 | 0 | if (searches.isEmpty()) { |
110 | 0 | searches.add(new HashMap<String, String>()); |
111 | |
} |
112 | 0 | for (Map<String, String> search : searches) { |
113 | 0 | search.put(KIM_PERSON_AFFILIATION_TYPE_CODE, affilSearch); |
114 | |
} |
115 | |
} |
116 | |
|
117 | 0 | if (searches.isEmpty()) { |
118 | 0 | searches.add(new HashMap<String, String>()); |
119 | |
} |
120 | |
|
121 | 0 | final Map<String, Person> persons = new HashMap<String, Person>(); |
122 | 0 | for (Map<String, String> criteria : searches) { |
123 | 0 | criteria.putAll(PersonSearchServiceImpl.PERSON_CRITERIA); |
124 | 0 | final List<? extends Person> foundPeople = findPeopleInternal(identityService, criteria, false); |
125 | 0 | for (Person person : foundPeople) { |
126 | |
|
127 | 0 | if (excludedUserId != null && excludedUserId.equals(person.getPrincipalId())) { |
128 | 0 | continue; |
129 | |
} |
130 | 0 | persons.put(person.getEntityId(), person); |
131 | |
} |
132 | 0 | } |
133 | 0 | return new ArrayList<Person>(persons.values()); |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public SearchResult search(final IdentityManagementService identityService, final SearchRequest searchRequest) { |
138 | 0 | final SearchResult result = new SearchResult(); |
139 | |
|
140 | 0 | List<Person> persons = findPersons(identityService, searchRequest); |
141 | |
|
142 | 0 | if (searchRequest.getSortDirection() != null) { |
143 | 0 | final int direction = (searchRequest.getSortDirection().equals(SortDirection.ASC) ? 1 : -1); |
144 | 0 | Collections.sort(persons, new Comparator<Person>() { |
145 | |
|
146 | |
@Override |
147 | |
public int compare(Person o1, Person o2) { |
148 | 0 | if (DISPLAY_NAME_RESULT.equals(searchRequest.getSortColumn())) { |
149 | 0 | return o1.getName().compareToIgnoreCase(o2.getName()) * direction; |
150 | 0 | } else if (ENTITY_ID_RESULT.equals(searchRequest.getSortColumn())) { |
151 | 0 | return o1.getPrincipalId().compareToIgnoreCase(o2.getPrincipalId()) * direction; |
152 | |
} else { |
153 | |
|
154 | 0 | return 0; |
155 | |
} |
156 | |
} |
157 | |
}); |
158 | |
} |
159 | |
|
160 | 0 | for (Person person : persons) { |
161 | 0 | final SearchResultRow resultRow = new SearchResultRow(); |
162 | 0 | resultRow.setCells(new ArrayList<SearchResultCell>()); |
163 | |
|
164 | 0 | SearchResultCell cell = new SearchResultCell(); |
165 | 0 | cell.setKey(ENTITY_ID_RESULT); |
166 | 0 | cell.setValue(person.getEntityId()); |
167 | 0 | resultRow.getCells().add(cell); |
168 | |
|
169 | 0 | cell = new SearchResultCell(); |
170 | 0 | cell.setKey(PERSON_ID_RESULT); |
171 | 0 | cell.setValue(person.getPrincipalId()); |
172 | 0 | resultRow.getCells().add(cell); |
173 | |
|
174 | 0 | cell = new SearchResultCell(); |
175 | 0 | cell.setKey(PRINCIPAL_NAME_RESULT); |
176 | 0 | cell.setValue(person.getPrincipalName()); |
177 | 0 | resultRow.getCells().add(cell); |
178 | |
|
179 | 0 | cell = new SearchResultCell(); |
180 | 0 | cell.setKey(GIVEN_NAME_RESULT); |
181 | 0 | cell.setValue(person.getName()); |
182 | 0 | resultRow.getCells().add(cell); |
183 | |
|
184 | 0 | cell = new SearchResultCell(); |
185 | 0 | cell.setKey(DISPLAY_NAME_RESULT); |
186 | 0 | cell.setValue(person.getName() + "(" + person.getPrincipalName() + ")"); |
187 | 0 | resultRow.getCells().add(cell); |
188 | |
|
189 | 0 | result.getRows().add(resultRow); |
190 | |
|
191 | 0 | } |
192 | |
|
193 | 0 | result.setStartAt(searchRequest.getStartAt()); |
194 | 0 | result.setTotalResults(result.getRows().size()); |
195 | 0 | return result; |
196 | |
} |
197 | |
|
198 | |
@Override |
199 | |
public SearchTypeInfo getType() { |
200 | 0 | return new QuickViewByGivenNameSearchTypeCreator().get(); |
201 | |
} |
202 | |
|
203 | |
} |