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.ServiceIntervalDTO; import cz.PA165.vozovyPark.dto.VehicleDTO; import cz.PA165.vozovyPark.enums.UserClassEnum; import cz.PA165.vozovyPark.service.ServiceIntervalService; import cz.PA165.vozovyPark.service.VehicleService; import editors.VehicleEditor; import java.text.ParseException; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import org.springframework.beans.factory.annotation.Autowired; 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 ServiceIntervalController { @Autowired private ServiceIntervalService serviceIntervalService; @Autowired private VehicleService vehicleService; @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { binder.registerCustomEditor(VehicleDTO.class, new VehicleEditor(this.vehicleService)); } @RequestMapping(value = "/serviceInterval/all") public String viewAllserviceIntervals(ModelMap model) { model.put("allServiceIntervals", serviceIntervalService.findAll()); return "/serviceInterval/allServiceIntervals"; } @RequestMapping(value = "/serviceInterval/{serviceIntervalId}") public String viewserviceInterval(ModelMap model, @PathVariable Long serviceIntervalId) { //retype Long to String? - where? model.put("serviceIntervalById", serviceIntervalService.getById(serviceIntervalId)); return "/serviceInterval/serviceIntervalView"; } @RequestMapping(value = "/serviceInterval/new") public String showAddServiceIntervalForm(ModelMap model) { model.put("serviceInterval", new ServiceIntervalDTO()); List<VehicleDTO> allVehicles = vehicleService.findAll(); model.put("vehicle", allVehicles); return "/serviceInterval/serviceIntervalForm"; } @RequestMapping(value = "/serviceInterval/new", method = RequestMethod.POST) public String addServiceInterval(@Valid @ModelAttribute("serviceInterval") ServiceIntervalDTO serviceInterval, BindingResult result, ModelMap model) throws ParseException { // @Valid validates model after binding user input to it if (result.hasErrors()) { List<VehicleDTO> allVehicles = vehicleService.findAll(); model.put("vehicle", allVehicles); return "/serviceInterval/serviceIntervalForm"; } serviceIntervalService.createSI(serviceInterval); return "redirect:/serviceInterval/all"; } @RequestMapping(value = "/serviceInterval/update/{serviceIntervalId}") public String showEditForm(ModelMap model, @PathVariable("serviceIntervalId") Long id) { ServiceIntervalDTO serviceInterval = serviceIntervalService.getById(id); model.put("serviceInterval", serviceInterval); model.put("userClass", UserClassEnum.values()); List<VehicleDTO> allVehicles = vehicleService.findAll(); model.put("vehicle", allVehicles); return "/serviceInterval/serviceIntervalUpdateForm"; } @RequestMapping(value = "/serviceInterval/update/{serviceIntervalId}", method = RequestMethod.POST) public String updateServiceInterval(@ModelAttribute("serviceInterval") ServiceIntervalDTO serviceInterval, @PathVariable("serviceIntervalId") Long id, BindingResult result) { if (result.hasErrors()) { return "/serviceInterval/serviceIntervalUpdateForm"; } serviceIntervalService.updateSI(serviceInterval); return "redirect:/serviceInterval/all"; } @RequestMapping(value = "/serviceInterval/delete/{serviceIntervalId}", method = RequestMethod.GET) public String deleteServiceInterval(@PathVariable("serviceIntervalId") Long id, ModelMap model) { ServiceIntervalDTO serviceInterval = serviceIntervalService.getById(id); serviceIntervalService.removeSI(serviceInterval); return "redirect:/serviceInterval/all"; } }