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