Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

MUMPFL-83 Search optionally by first name #28

Merged
merged 2 commits into from
Jul 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package edu.wisc.my.profile.model;

public class SearchTerm{

private String firstName;
private String lastName;

/**
* Constructs a SearchTerm
*/
public SearchTerm(){
}

/**
* Constructs a SearchTerm with firstName and lastName
* @param firstName
* @param lastName
*/
public SearchTerm(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}

/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import java.util.List;

import edu.wisc.my.profile.model.SearchTerm;
import edu.wisc.my.profile.model.User;

public interface SearchUsersService {

/**
* Get a list of users based on search term
* @param searchTerm
* @return an alphabetized list based on last name, first name
* @param username of requester
* @param manifestGroups to authorize by
* @param searchTerm search criteria
* @return an alphabetized list based on last name, first name. Will return
* empty list if searchTerm is null or searchTerm.last name is empty
*/
List<User> getUsers(String username, String manifestGroups, String searchTerm);
List<User> getUsers(String username, String manifestGroups, SearchTerm searchTerm);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import java.util.List;

import edu.wisc.my.profile.model.SearchTerm;
import edu.wisc.my.profile.model.User;

public interface LocalUserDao {

/**
* Gets the List of users that matches the search term
*
* @param searchTerm
* @return An alphabetized list of users based on userId. Empty for no search hits
* @return An alphabetized list of users based on userId. Empty for no
* search hits or if searchTerm has no last name criteria or null
*/
public List<User> getUsersBySearchTerm(String searchTerm);
public List<User> getUsersBySearchTerm(SearchTerm searchTerm);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import edu.wisc.my.profile.local.dao.LocalUserDao;
import edu.wisc.my.profile.model.SearchTerm;
import edu.wisc.my.profile.model.User;


Expand All @@ -21,9 +23,10 @@ public class LocalUserDaoImpl implements LocalUserDao {

@SuppressWarnings("unchecked")
@Override
public List<User> getUsersBySearchTerm(String searchTerm){
if(searchTerm == null){
searchTerm = "";
public List<User> getUsersBySearchTerm(SearchTerm searchTerm){
List<User> filteredUsers = new ArrayList<User>();
if(searchTerm == null || StringUtils.isEmpty(searchTerm.getLastName())){
return filteredUsers;
}
ObjectMapper mapper = new ObjectMapper();
ClassLoader classLoader = getClass().getClassLoader();
Expand All @@ -34,10 +37,12 @@ public List<User> getUsersBySearchTerm(String searchTerm){
} catch (Exception e) {
logger.error("Issue during search for users", e);
}
List<User> filteredUsers = new ArrayList<User>();

for(User u: listUsers){
if(u.getLastName().toLowerCase().contains(searchTerm.toLowerCase())){
filteredUsers.add(u);
if(u.getLastName().toLowerCase().contains(searchTerm.getLastName().toLowerCase())){
if(StringUtils.isEmpty(searchTerm.getLastName()) || u.getFirstName().toLowerCase().contains(searchTerm.getFirstName().toLowerCase())){
filteredUsers.add(u);
}
}
}
Collections.sort(filteredUsers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.stereotype.Service;

import edu.wisc.my.profile.local.dao.LocalUserDao;
import edu.wisc.my.profile.model.SearchTerm;
import edu.wisc.my.profile.model.User;

@Service
Expand All @@ -28,7 +29,7 @@ public void setAdminGroup(String group) {
}

@Override
public List<User> getUsers(String username, String manifestGroups, String searchTerm) {
public List<User> getUsers(String username, String manifestGroups, SearchTerm searchTerm) {
if(StringUtils.isNotBlank(adminGroup) && manifestGroups.contains(adminGroup)) {
logger.info("User {} looked up users using this search term: {}.",username, searchTerm);
return dao.getUsersBySearchTerm(searchTerm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import java.util.List;

import edu.wisc.my.profile.model.SearchTerm;
import edu.wisc.my.profile.model.User;

public interface SearchUsersMiddlewareDao{

/**
* Gets the List of users that matches the search term
* @param searchTerm
* @return An alphabetized list of users based on Last Name, first name. Empty for no search hits
* @return An alphabetized list of users based on userId. Empty for no
* search hits or if searchTerm has no last name criteria or null
*/
public List<User> getUsersBySearchTerm(String searchTerm);
public List<User> getUsersBySearchTerm(SearchTerm searchTerm);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;

import edu.wisc.my.profile.model.SearchTerm;
import edu.wisc.my.profile.model.User;


Expand All @@ -23,12 +26,18 @@ public SearchUsersMiddlewareDaoImpl(DataSource ds) {

@Override
@SuppressWarnings("unchecked")
public List<User> getUsersBySearchTerm(String searchTerm) {
public List<User> getUsersBySearchTerm(SearchTerm searchTerm) {
List<User> userList = new ArrayList<User>();
//Return empty list if search term is null or no last name searched
if(searchTerm == null || StringUtils.isEmpty(searchTerm.getLastName())){
return userList;
}

SearchUsersProcedure sup = new SearchUsersProcedure(datasource);
Map<String, Object> result = new HashMap<String, Object>();
result = sup.searchUsers(null, searchTerm.toUpperCase());
result = sup.searchUsers(searchTerm.getFirstName(), searchTerm.getLastName());
Iterator<Entry<String, Object>> entries = result.entrySet().iterator();
List<User> userList = new ArrayList<User>();

while (entries.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entries.next();
//the value is the list of users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.stereotype.Service;

import edu.wisc.my.profile.dao.SearchUsersMiddlewareDao;
import edu.wisc.my.profile.model.SearchTerm;
import edu.wisc.my.profile.model.User;

@Service
Expand All @@ -28,7 +29,7 @@ public void setAdminGroup(String group) {
}

@Override
public List<User> getUsers(String username, String manifestGroups, String searchTerm) {
public List<User> getUsers(String username, String manifestGroups, SearchTerm searchTerm) {
if(StringUtils.isNotBlank(adminGroup) && manifestGroups.contains(adminGroup)) {
logger.info("User {} looked up users using this search term: {}.",username, searchTerm);
return searchUsersDao.getUsersBySearchTerm(searchTerm);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package edu.wisc.my.profile.web;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -20,10 +18,10 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import edu.wisc.my.profile.model.ContactInformation;
import edu.wisc.my.profile.model.SearchTerm;
import edu.wisc.my.profile.model.User;
import edu.wisc.my.profile.service.EmergencyContactInformationService;
import edu.wisc.my.profile.service.LocalContactInformationService;
Expand Down Expand Up @@ -57,14 +55,17 @@ public void setUsernameAttr(String attr) {
}

@RequestMapping(method = RequestMethod.GET, value="/searchUsers")
public @ResponseBody void getUsers(HttpServletRequest request, @RequestParam("searchTerm") String searchTerm, HttpServletResponse response) {
public @ResponseBody void getUsers(HttpServletRequest request, @RequestParam("searchTerms") JSONObject searchTerms, HttpServletResponse response) {
try {
String username = request.getHeader(usernameAttribute);
String manifestGroups = request.getHeader(manifestAttribute);
if(StringUtils.isBlank(username)) {
logger.warn("User hit admin service w/o username set");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
SearchTerm searchTerm = new SearchTerm();
searchTerm.setFirstName(searchTerms.optString("firstName"));
searchTerm.setLastName(searchTerms.optString("lastName"));
List<User> users = searchUsersService.getUsers(username, manifestGroups, searchTerm);
JSONObject jsonToReturn = new JSONObject();
JSONArray userList = new JSONArray();
Expand Down
27 changes: 27 additions & 0 deletions my-profile-webapp/src/main/webapp/css/my-app.less
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,30 @@ form .form-help {
.warn-icon {
padding:5px;
}

.name-form {
width:80%;
margin:0 auto;
.firstname {
width:49%;
display: inline-block;
label {
display:block;
}
input {
display:block;
margin:0 auto;
}
}
.lastname {
width:49%;
display: inline-block;
label {
display:block;
}
input {
display:block;
margin:0 auto;
}
}
}
5 changes: 3 additions & 2 deletions my-profile-webapp/src/main/webapp/my-app/lec/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ define(['angular'], function(angular) {
$scope.empty = false;
$scope.searching = false;
$scope.result = [];
$scope.searchTerm = "";
$scope.firstName ="";
$scope.lastName = "";
$scope.searchResultLimitIncrementor=10;
$scope.searchResultLimit = $scope.searchResultLimitIncrementor;
};
Expand All @@ -39,7 +40,7 @@ define(['angular'], function(angular) {
$scope.searching=true;
$scope.result = [];
$scope.searchResultLimit = $scope.searchResultLimitIncrementor;
lecService.searchUsers($scope.searchTerm).then(function(result){
lecService.searchUsers($scope.firstName, $scope.lastName).then(function(result){
$scope.result = result.data;
angular.forEach($scope.result.addresses, function(value, key, obj){
value.edit = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
<div class='info-card center'>
<div class='admin-lookup-form'>
<form name='form' novalidate>
<span class='bold'>Search by student's last name</span>
<div>
<input type='text' required ng-model='searchTerm' class='center' placeholder="e.g. Johnson">
<div class="name-form">
<div class="firstname">
<label for='firstName'>First Name (optional)</label>
<input type='text' id='firstName' ng-model='firstName' class='center' placeholder="e.g. John">
</div>
<div class="lastname">
<label for='lastName'>Last Name (required)</label>
<input type='text' required id='lastName' ng-model='lastName' class='center' placeholder="e.g. Johnson">
</div>
</div>
<div>
<button class='btn btn-primary' ng-click="search()" ng-show='form.$valid'>Search</button>
Expand Down
5 changes: 3 additions & 2 deletions my-profile-webapp/src/main/webapp/my-app/lec/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ define(['angular'], function(angular) {

//search

var searchUsers = function(searchTerm){
return $http.get('/profile/api/localContactInfo/searchUsers?searchTerm=' + searchTerm).success(
var searchUsers = function(firstName, lastName){
var searchTerms = {"firstName":firstName, "lastName":lastName};
return $http.get('/profile/api/localContactInfo/searchUsers?searchTerms=' + JSON.stringify(searchTerms)).success(
function(data, status) { //success function
return data.data;
}).error(function(data, status) { // failure function
Expand Down