Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cz.PA165.vozovyPark.controller; import cz.PA165.vozovyPark.dto.DriveDTO; import cz.PA165.vozovyPark.dto.UserDTO; import cz.PA165.vozovyPark.dto.VehicleDTO; import cz.PA165.vozovyPark.enums.DriveStateEnum; import cz.PA165.vozovyPark.service.DriveService; import cz.PA165.vozovyPark.service.UserService; import cz.PA165.vozovyPark.service.VehicleService; import editors.UserEditor; import editors.VehicleEditor; import java.text.ParseException; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import org.joda.time.Interval; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * * @author jzelezny */ @Controller public class DriveController { @Autowired private DriveService driveService; @Autowired private UserService userService; @Autowired private VehicleService vehicleService; @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { binder.registerCustomEditor(UserDTO.class, new UserEditor(this.userService)); binder.registerCustomEditor(VehicleDTO.class, new VehicleEditor(this.vehicleService)); //binder.registerCustomEditor(DateTime.class, new DateTimeEditor()); } @RequestMapping(value = "/drive/all") public String viewAllDrives(ModelMap model) { model.put("allDrives", driveService.findAll()); return "/drive/allDrives"; } @RequestMapping(value = "/drive/{driveId}") public String viewDrive(ModelMap model, @PathVariable Long driveId) { model.put("driveById", driveService.getById(driveId)); return "/drive/driveView"; } @RequestMapping(value = "/drive/userDrives/{userId}") public String drivesByUser(ModelMap model, @PathVariable Long userId) { UserDTO user = userService.getById(userId); model.put("driveByUser", driveService.findByUser(user)); return "/drive/userDrives"; } @RequestMapping(value = "/drive/usersDrives/") public String userDrives(ModelMap model) { List<UserDTO> userLoggedInList = userService.getByUsername(getLoggedUsername()); UserDTO user = userLoggedInList.get(0); model.put("driveByUser", driveService.findByUser(user)); return "/drive/userDrives"; } @RequestMapping(value = "/drive/new") public String showAddDriveForm(ModelMap model) { model.put("drive", new DriveDTO()); model.put("state", DriveStateEnum.values()); model.put("vehicle", vehicleService.findAll()); model.put("user", userService.getEnabledUsers()); return "/drive/driveForm"; } @RequestMapping(value = "/drive/new", method = RequestMethod.POST) public String addDrive(@Valid @ModelAttribute("drive") DriveDTO drive, BindingResult result, ModelMap model, HttpServletRequest req) throws ParseException { // @Valid validates model after binding user input to it if (result.hasErrors()) { model.put("state", DriveStateEnum.values()); model.put("user", userService.getEnabledUsers()); model.put("vehicle", vehicleService.findAll()); req.getSession().setAttribute("message", result.getAllErrors().toString()); return "/drive/driveForm"; } if (!isIntervalFree(drive)) { model.put("state", DriveStateEnum.values()); model.put("vehicle", vehicleService.findAll()); model.put("user", userService.getEnabledUsers()); req.getSession().setAttribute("message", "Selected car is not available for this date, please select a vehicle or date."); return "/drive/driveForm"; } driveService.createDrive(drive); return "redirect:/drive/all"; } @RequestMapping(value = "/drive/newUserDrive") public String showAddNewUserDriveForm(ModelMap model) { model.put("drive", new DriveDTO()); model.put("state", DriveStateEnum.values()); List<UserDTO> userLoggedInList = userService.getByUsername(getLoggedUsername()); UserDTO userLoggedIn = userLoggedInList.get(0); if (userLoggedIn.getIsAdmin()) { model.put("vehicle", vehicleService.findAll()); } else { model.put("vehicle", vehicleService.findByUserClass(userLoggedIn.getUserClass())); } //model.put("vehicle", vehicleService.findAll()); model.put("user", userService.getByUsername(getLoggedUsername())); return "/drive/driveForm"; } @RequestMapping(value = "/drive/newUserDrive", method = RequestMethod.POST) public String addNewUserDrive(@Valid @ModelAttribute("drive") DriveDTO drive, BindingResult result, ModelMap model, HttpServletRequest req) throws ParseException { // @Valid validates model after binding user input to it if (result.hasErrors()) { model.put("state", DriveStateEnum.values()); model.put("user", userService.getByUsername(getLoggedUsername())); List<UserDTO> userLoggedInList = userService.getByUsername(getLoggedUsername()); UserDTO userLoggedIn = userLoggedInList.get(0); if (userLoggedIn.getIsAdmin()) { model.put("vehicle", vehicleService.findAll()); } else { model.put("vehicle", vehicleService.findByUserClass(userLoggedIn.getUserClass())); } req.getSession().setAttribute("message", result.getAllErrors().toString()); return "/drive/driveForm"; } if (!isIntervalFree(drive)) { model.put("state", DriveStateEnum.values()); List<UserDTO> userLoggedInList = userService.getByUsername(getLoggedUsername()); UserDTO userLoggedIn = userLoggedInList.get(0); if (userLoggedIn.getIsAdmin()) { model.put("vehicle", vehicleService.findAll()); } else { model.put("vehicle", vehicleService.findByUserClass(userLoggedIn.getUserClass())); } model.put("user", userService.getByUsername(getLoggedUsername())); req.getSession().setAttribute("message", "Selected car is not available for this date, please select a vehicle or date."); return "/redirect:drive/driveForm"; } driveService.createDrive(drive); return "redirect:/drive/usersDrives/"; } @RequestMapping(value = "/drive/update/{driveId}") public String showEditForm(ModelMap model, @PathVariable("driveId") Long id) { DriveDTO drive = driveService.getById(id); model.put("drive", drive); model.put("state", DriveStateEnum.values()); model.put("user", userService.getEnabledUsers()); model.put("vehicle", vehicleService.findAll()); return "/drive/driveUpdateForm"; } @RequestMapping(value = "/drive/update/{driveId}", method = RequestMethod.POST) public String updateDrive(@ModelAttribute("drive") DriveDTO drive, @PathVariable("driveId") Long id, BindingResult result, ModelMap model) { if (result.hasErrors()) { model.put("drive", drive); model.put("state", DriveStateEnum.values()); model.put("users", userService.getEnabledUsers()); model.put("vehicle", vehicleService.findAll()); return "/drive/driveUpdateForm"; } driveService.updateDrive(drive); return "redirect:/drive/all"; } @RequestMapping(value = "/drive/delete/{driveId}", method = RequestMethod.GET) public String deleteDrive(@PathVariable("driveId") Long id, ModelMap model) { DriveDTO drive = driveService.getById(id); driveService.removeDrive(drive); return "redirect:/drive/all"; } //help functions public boolean isIntervalFree(DriveDTO drive) { Interval range = new Interval(drive.getDateFrom(), drive.getDateTo()); VehicleDTO vehicle = drive.getVehicle(); for (DriveDTO d : driveService.findAll()) { Interval i = new Interval(d.getDateFrom(), d.getDateTo()); if (range.overlaps(i) && d.getVehicle().equals(vehicle)) { return false; } } return true; } public String getLoggedUsername() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); return auth.getName(); } }