Coverage Report - org.kuali.student.core.personsearch.service.impl.QuickViewByGivenName
 
Classes in this File Line Coverage Branch Coverage Complexity
QuickViewByGivenName
0%
1/108
0%
0/66
10.25
QuickViewByGivenName$1
0%
0/6
0%
0/4
10.25
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may
 3  
  * not use this file except in compliance with the License. You may obtain a copy of the License at
 4  
  * http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed
 5  
  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 6  
  * implied. See the License for the specific language governing permissions and limitations under the License.
 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 PRINCIPAL_NAME_PARAM = "person.queryParam.personPrincipalName";
 35  
     final static public String AFFILIATION_PARAM = "person.queryParam.personAffiliation";
 36  
     final static public String EXCLUDED_USER_ID = "person.queryParam.excludedUserId";
 37  
 
 38  
     final static public String PERSON_ID_RESULT = "person.resultColumn.PersonId";
 39  
     final static public String ENTITY_ID_RESULT = "person.resultColumn.EntityId";
 40  
     final static public String DISPLAY_NAME_RESULT = "person.resultColumn.DisplayName";// Smith, John (jsmith)
 41  
     final static public String GIVEN_NAME_RESULT = "person.resultColumn.GivenName";// Smith, John
 42  
     final static public String PRINCIPAL_NAME_RESULT = "person.resultColumn.PrincipalName";
 43  
 
 44  
     final static private String KIM_PERSON_AFFILIATION_TYPE_CODE = "affiliationTypeCode";
 45  
 
 46  
     final static private String KIM_PRINCIPALS_PRINCIPALNAME = "principals.principalName";
 47  
     final static private String KIM_PRINCIPALS_PRINCIPALID = "principals.principalId";
 48  
     final static private String KIM_PERSON_FIRST_NAME = "names.firstName";
 49  
     final static private String KIM_PERSON_MIDDLE_NAME = "names.middleName";
 50  
     final static private String KIM_PERSON_LAST_NAME = "names.lastName";
 51  
 
 52  
     private List<Person> findPersons(final IdentityManagementService identityService, final SearchRequest searchRequest) {
 53  0
         String nameSearch = null;
 54  0
         String principalNameSearch = null;
 55  0
         String affilSearch = null;
 56  0
         String idSearch = null;
 57  0
         String excludedUserId = null;
 58  0
         for (SearchParam param : searchRequest.getParams()) {
 59  0
             String value = (String) param.getValue();
 60  0
             if (!value.isEmpty()) {
 61  0
                 if (value.indexOf("%") == -1 && value.indexOf("*") == -1) {
 62  0
                     value = "*" + value + "*";
 63  
                 }
 64  0
                 if (NAME_PARAM.equals(param.getKey())) {
 65  0
                     if (nameSearch != null) {
 66  0
                         nameSearch += "|" + value;
 67  
                     } else {
 68  0
                         nameSearch = value;
 69  
                     }
 70  0
                 } else if (PRINCIPAL_NAME_PARAM.equals(param.getKey())) {
 71  0
                     if (principalNameSearch != null) {
 72  0
                             principalNameSearch += "|" + value;
 73  
                     } else {
 74  0
                             principalNameSearch = value;
 75  
                     }
 76  0
                 } else if (AFFILIATION_PARAM.equals(param.getKey())) {
 77  0
                     if (affilSearch != null) {
 78  0
                         affilSearch += "|" + value;
 79  
                     } else {
 80  0
                         affilSearch = value;
 81  
                     }
 82  0
                 } else if (ID_PARAM.equals(param.getKey())) {
 83  0
                     if (idSearch != null) {
 84  0
                         idSearch += "|" + param.getValue();
 85  
                     } else {
 86  0
                         idSearch = param.getValue().toString();
 87  
                     }
 88  0
                 } else if (EXCLUDED_USER_ID.equals(param.getKey())) {
 89  0
                     excludedUserId = (String) param.getValue();
 90  
                 }
 91  
             }
 92  0
         }
 93  0
         final List<Map<String, String>> searches = new ArrayList<Map<String, String>>();
 94  0
         if (idSearch != null) {
 95  0
             Map<String, String> criteria = new HashMap<String, String>();
 96  0
             criteria.put(KIM_PRINCIPALS_PRINCIPALID, idSearch);
 97  0
             searches.add(criteria);
 98  0
         } else if (principalNameSearch != null) {
 99  0
             Map<String, String> criteria = new HashMap<String, String>();
 100  0
             criteria.put(KIM_PRINCIPALS_PRINCIPALNAME, principalNameSearch);
 101  0
             searches.add(criteria);
 102  0
         } else if (nameSearch != null) {
 103  0
             Map<String, String> principalNameCriteria = new HashMap<String, String>();
 104  0
             principalNameCriteria.put(KIM_PRINCIPALS_PRINCIPALNAME, nameSearch);
 105  0
             searches.add(principalNameCriteria);
 106  
 
 107  0
             Map<String, String> firstNameCriteria = new HashMap<String, String>();
 108  0
             firstNameCriteria.put(KIM_PERSON_FIRST_NAME, nameSearch);
 109  0
             searches.add(firstNameCriteria);
 110  
 
 111  0
             Map<String, String> middleNameCriteria = new HashMap<String, String>();
 112  0
             middleNameCriteria.put(KIM_PERSON_MIDDLE_NAME, nameSearch);
 113  0
             searches.add(middleNameCriteria);
 114  
 
 115  0
             Map<String, String> lastNameCriteria = new HashMap<String, String>();
 116  0
             lastNameCriteria.put(KIM_PERSON_LAST_NAME, nameSearch);
 117  0
             searches.add(lastNameCriteria);
 118  
         }
 119  
 
 120  0
         if (affilSearch != null) {// TODO what is this search for?
 121  0
             if (searches.isEmpty()) {
 122  0
                 searches.add(new HashMap<String, String>());
 123  
             }
 124  0
             for (Map<String, String> search : searches) {
 125  0
                 search.put(KIM_PERSON_AFFILIATION_TYPE_CODE, affilSearch);
 126  
             }
 127  
         }
 128  
 
 129  0
         if (searches.isEmpty()) {
 130  0
             searches.add(new HashMap<String, String>());
 131  
         }
 132  
 
 133  0
         final Map<String, Person> persons = new HashMap<String, Person>();
 134  0
         for (Map<String, String> criteria : searches) {
 135  0
             criteria.putAll(PersonSearchServiceImpl.PERSON_CRITERIA);
 136  0
             final List<? extends Person> foundPeople = findPeopleInternal(identityService, criteria, false);
 137  0
             for (Person person : foundPeople) {
 138  
                 // Ignore the current user.
 139  0
                 if (excludedUserId != null && excludedUserId.equals(person.getPrincipalId())) {
 140  0
                     continue;
 141  
                 }
 142  0
                 persons.put(person.getEntityId(), person);
 143  
             }
 144  0
         }
 145  0
         return new ArrayList<Person>(persons.values());
 146  
     }
 147  
 
 148  
     @Override
 149  
     public SearchResult search(final IdentityManagementService identityService, final SearchRequest searchRequest) {
 150  0
         final SearchResult result = new SearchResult();
 151  0
         searchRequest.setSortDirection(SortDirection.ASC);
 152  
         
 153  0
         List<Person> persons = findPersons(identityService, searchRequest);
 154  
         // TODO finish sorting
 155  0
         if (searchRequest.getSortDirection() != null) {
 156  0
             final int direction = (searchRequest.getSortDirection().equals(SortDirection.ASC) ? 1 : -1);
 157  0
             Collections.sort(persons, new Comparator<Person>() {
 158  
 
 159  
                 @Override
 160  
                 public int compare(Person o1, Person o2) {
 161  0
                     if (DISPLAY_NAME_RESULT.equals(searchRequest.getSortColumn())) {
 162  0
                         return o1.getName().compareToIgnoreCase(o2.getName()) * direction;
 163  0
                     } else if (ENTITY_ID_RESULT.equals(searchRequest.getSortColumn())) {
 164  0
                         return o1.getPrincipalId().compareToIgnoreCase(o2.getPrincipalId()) * direction;
 165  
                     } else {
 166  
                         // TODO Recognize alternate sort columns
 167  0
                         return 0;
 168  
                     }
 169  
                 }
 170  
             });
 171  
         }
 172  
 
 173  
         //Implement pagination.
 174  0
         int startAt = (null != searchRequest && null != searchRequest.getStartAt()) ? searchRequest.getStartAt() : 0;
 175  0
         int maxResult = (null != searchRequest && null != searchRequest.getMaxResults()) ? startAt
 176  
                 + searchRequest.getMaxResults() : persons.size();
 177  0
         for (int i = startAt; (i < persons.size() && i < maxResult); i++) {
 178  0
             Person person = persons.get(i);
 179  0
             final SearchResultRow resultRow = new SearchResultRow();
 180  0
             resultRow.setCells(new ArrayList<SearchResultCell>());
 181  
 
 182  0
             SearchResultCell cell = new SearchResultCell();
 183  0
             cell.setKey(ENTITY_ID_RESULT);
 184  0
             cell.setValue(person.getEntityId());
 185  0
             resultRow.getCells().add(cell);
 186  
 
 187  0
             cell = new SearchResultCell();
 188  0
             cell.setKey(PERSON_ID_RESULT);
 189  0
             cell.setValue(person.getPrincipalId());
 190  0
             resultRow.getCells().add(cell);
 191  
 
 192  0
             cell = new SearchResultCell();
 193  0
             cell.setKey(PRINCIPAL_NAME_RESULT);
 194  0
             cell.setValue(person.getPrincipalName());
 195  0
             resultRow.getCells().add(cell);
 196  
 
 197  0
             cell = new SearchResultCell();
 198  0
             cell.setKey(GIVEN_NAME_RESULT);
 199  0
             cell.setValue(person.getName());
 200  0
             resultRow.getCells().add(cell);
 201  
 
 202  0
             cell = new SearchResultCell();
 203  0
             cell.setKey(DISPLAY_NAME_RESULT);
 204  0
             cell.setValue(person.getName() + " (" + person.getPrincipalName() + ")");
 205  0
             resultRow.getCells().add(cell);
 206  
 
 207  0
             result.getRows().add(resultRow);
 208  
 
 209  
         }
 210  
 
 211  0
         result.setStartAt(searchRequest.getStartAt());
 212  0
         if (searchRequest.getNeededTotalResults()) {
 213  0
             result.setTotalResults(persons.size());
 214  
         } else {
 215  0
             result.setTotalResults(result.getRows().size());
 216  
         }
 217  
 
 218  0
         return result;
 219  
     }
 220  
 
 221  
     @Override
 222  
     public SearchTypeInfo getType() {
 223  0
         return new QuickViewByGivenNameSearchTypeCreator().get();
 224  
     }
 225  
 
 226  
 }