| 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 | 0 |          searchRequest.setSortDirection(SortDirection.ASC); | 
  | 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 | 0 |          return new QuickViewByGivenNameSearchTypeCreator().get(); | 
  | 202 |  |      } | 
  | 203 |  |   | 
  | 204 |  |  } |