Skip to content

Instantly share code, notes, and snippets.

@efleming969
Created April 15, 2015 18:52
Show Gist options
  • Select an option

  • Save efleming969/ccf6999fe1d893c16cec to your computer and use it in GitHub Desktop.

Select an option

Save efleming969/ccf6999fe1d893c16cec to your computer and use it in GitHub Desktop.
package example.web;
import example.domain.PermissionDeniedException;
import example.domain.Person;
import example.domain.SearchUi;
import example.domain.Thing;
import example.service.ThingService;
import example.service.PersonService;
import example.service.ThingConstants;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
@Controller("searchController")
@RequestMapping("/protected")
@SessionAttributes("searchUi")
public class SearchController {
private static Logger LOG = Logger.getLogger(SearchController.class);
@Autowired private PersonService personService;
@Autowired private ThingService thingService;
@Autowired private MessageSource messageSource;
@RequestMapping(value="/completedEvals.htm",method=RequestMethod.GET)
public ModelAndView processCompletedEvals() {
LOG.debug("Calling Completed Evals Controller");
ModelAndView mav = new ModelAndView("/protected/completedEvals");
mav.addObject("searchUi", new SearchUi());
return mav;
}
@RequestMapping(value="/searchCompletedThings.htm",method=RequestMethod.POST)
public ModelAndView listCompletedEvals(@ModelAttribute("searchUi") SearchUi searchUi, HttpServletRequest request) {
LOG.info("Calling Completed Evals Controller for SearchType["+searchUi.getHistorySearch()+"]");
Person currentUser = getCurrentUser();
validateInput(searchUi);
List<Thing> things = null;
// Search all
if (searchUi.getHistorySearch().equalsIgnoreCase(ThingConstants.SEARCH_ALL)) {
//person with special or administrative role can view all records
if (request.isUserInRole(ThingConstants.ROLE_SPECIAL) || request.isUserInRole(OhssConstants.ROLE_REVIEWER)) {
things = thingService.findAllCompleted(getCurrentLocationCode(request.getSession(false)));
} else {
//view only records that you are supervisor
things = thingService.findCompletedBySupervisor(currentUser);
}
}
// Search by last name
if (searchUi.getHistorySearch().equalsIgnoreCase(ThingConstants.SEARCH_MEMBER)){
if (request.isUserInRole(ThingConstants.ROLE_SPECIAL) || request.isUserInRole(OhssConstants.ROLE_REVIEWER)) {
LOG.debug("Search By Name with special role: " + searchUi.getName());
things = thingService.findCompletedByRef(searchUi.getRef(), getCurrentLocationCode(request.getSession(false)));
} else {
LOG.debug("Search By Name no special role" + searchUi.getName());
things = thingService.findCompletedByRefAndSupervisor(currentUser.getRef(), searchUi.getRef());
}
}
// Search by date
if(searchUi.getHistorySearch().equalsIgnoreCase(ThingConstants.SEARCH_BY_DATE)) {
//Calculating endDate; Adding 1 day
Calendar calendar = Calendar.getInstance();
calendar.setTime(searchUi.getEndDate());
calendar.add(Calendar.DATE, 1);
Date endDate = calendar.getTime();
LOG.debug("Search By Date; calculated endDate: " +endDate);
//person with special or administrative role can view all records
if (request.isUserInRole(ThingConstants.ROLE_SPECIAL) || request.isUserInRole(OhssConstants.ROLE_REVIEWER)) {
LOG.debug("Search By Date: " + searchUi.getStartDate() + " " + searchUi.getEndDate());
things = thingService.findCompletedByDate(searchUi.getStartDate(), endDate, getCurrentLocationCode(request.getSession(false)));
} else {
LOG.debug("Search By Date: " + searchUi.getStartDate() + " " + searchUi.getEndDate());
things = thingService.findCompletedByDateAndSupervisor(currentUser, searchUi.getStartDate(), endDate);
}
}
List<Thing> uniqueThings = new ArrayList<>();
if (things != null) {
Set<Person> people = new HashSet<>();
for (Thing thing : things) {
LOG.debug("ThingID["+thing.getId()+"]; Member[" + thing.getMember().getFullNameWithPrefix()+"]");
if (people.contains(thing.getMember())) {
LOG.debug("Person " + thing.getMember().getFullName() + " is already in the list");
} else {
uniqueThings.add(thing);
people.add(thing.getMember());
LOG.debug("Person " + thing.getMember().getFullName() + " need to be added to the list");
}
}
}
ModelAndView mav = new ModelAndView("/protected/completedEvals");
mav.addObject("things", uniqueThings);
return mav;
}
private void validateInput(SearchUi searchUi) {
if (searchUi.getHistorySearch().equalsIgnoreCase(ThingConstants.SEARCH_MEMBER) && StringUtils.isBlank(searchUi.getRef())) {
throw new IllegalArgumentException("Reference is required to search by Member.");
}
if (searchUi.getHistorySearch().equalsIgnoreCase(ThingConstants.SEARCH_BY_DATE)) {
if (searchUi.getStartDate() == null || searchUi.getEndDate() == null) {
throw new IllegalArgumentException("StartDate and EndDate are required to search by Date.");
}
if (searchUi.getStartDate().getTime() > searchUi.getEndDate().getTime()) {
throw new IllegalArgumentException("StartDate should be less than EndDate.");
}
}
}
@RequestMapping("/completedRiskEvalsDetail")
public ModelAndView listCompletedEvalsDetail(@ModelAttribute("searchUi") SearchUi searchUi, @RequestParam("id") Long id, HttpServletRequest request) {
LOG.debug("Calling Completed Evals Controller");
Person currentUser = getCurrentUser();
//no errors on entry page
ModelAndView mav = new ModelAndView("/protected/completedEvalsDetails");
Person member = personService.getById(id);
if(!(((request.isUserInRole(ThingConstants.ROLE_SPECIAL) || request.isUserInRole(OhssConstants.ROLE_REVIEWER) || thingService.isSupervisor(currentUser.getRef()))
&& getCurrentLocationCode(request.getSession(false)).equals(member.getLocation().getLocationCode())) || currentUser.getId().equals(id))) {
throw new PermissionDeniedException(messageSource.getMessage("error.view.permission_denied", null, Locale.getDefault()));
}
boolean canViewCopyRiskEval = false;
List<Thing> thingList;
if(!(request.isUserInRole(ThingConstants.ROLE_SPECIAL) || request.isUserInRole(OhssConstants.ROLE_REVIEWER))) {
//Supervisor
thingList = thingService.findCompletedPerMemberBySupervisor(member, currentUser);
canViewCopyRiskEval = true;
} else {
thingList = thingService.findCompletedPerMember(member);
for(Thing thing: thingList) {
if(thing.getSupervisor().getRef().equalsIgnoreCase(currentUser.getRef())) {
canViewCopyRiskEval = true;
break;
}
}
}
mav.addObject("searchUi", searchUi);
mav.addObject("things", thingList);
mav.addObject("canViewCopyRiskEval", canViewCopyRiskEval);
return mav;
}
@RequestMapping("/inProgressEvals.htm")
public ModelAndView findInProgressEvals(HttpServletRequest request, @RequestParam(required = false) List<Long> deactivateRAs, @RequestParam(required = false) String deactivateComments) {
LOG.debug("Calling In Progress Evals Controller");
Person currentUser = getCurrentUser();
ModelAndView mav = new ModelAndView("/protected/inProgressEvals");
if (deactivateRAs != null && !deactivateRAs.isEmpty()) {
for (Long raId : deactivateRAs) {
LOG.info("Deactivate RA ID = [" + raId + "]; Comments = [" + deactivateComments + "]");
Thing thing = thingService.getById(raId);
if (thing != null)
thingService.deactivate(thing, deactivateComments);
}
}
List<Thing> thingIncomplete;
if(!(request.isUserInRole(ThingConstants.ROLE_SPECIAL) || request.isUserInRole(OhssConstants.ROLE_REVIEWER))) {
thingIncomplete = thingService.findIncompleteBySupervisorLogin(currentUser.getRef());
} else {
thingIncomplete = thingService.findIncompleteByLocationAndStatus(getCurrentLocationCode(request.getSession(false)), ThingConstants.STATUS_ACTIVE);
}
mav.addObject("thingIncomplete", thingIncomplete);
return mav;
}
@RequestMapping("/archivedEvals.htm")
public ModelAndView findArchivedEvals(HttpServletRequest request, @RequestParam(required = false) List<Long> restoreRAs) {
LOG.debug("Calling Archived Evals Controller");
if(!(request.isUserInRole(ThingConstants.ROLE_SPECIAL) || request.isUserInRole(OhssConstants.ROLE_REVIEWER))) {
throw new PermissionDeniedException(messageSource.getMessage("error.view.permission_denied", null, Locale.getDefault()));
}
if(restoreRAs != null && !restoreRAs.isEmpty()) {
for(Long raId : restoreRAs) {
LOG.info("Restore RA ID = [" + raId + "]");
Thing thing = thingService.getById(raId);
if(thing != null)
thingService.activate(thing);
}
}
ModelAndView mav = new ModelAndView("/protected/archivedEvals");
List<Thing> inactiveEvals = thingService.findIncompleteByLocationAndStatus(getCurrentLocationCode(request.getSession(false)), ThingConstants.STATUS_INACTIVE);
mav.addObject("inactiveEvals", inactiveEvals);
return mav;
}
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat(messageSource.getMessage("dateFormat", null, Locale.getDefault()));
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment