thymeleaf
package com.walmart.services.tareweight.controller;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import com.walmart.services.tareweight.model.TareWeight;
import com.walmart.services.tareweight.service.BulkDataService;
import com.walmart.services.tareweight.service.DataService;
import io.strati.metrics.annotation.Timed;
import io.strati.security.jaxrs.ws.rs.Path;
import java.util.Collection;
import java.util.List;
import javax.validation.Valid;
import org.bouncycastle.math.raw.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
/**
* Created by vn501cc on 6/25/2018.
*/
@RestController
public class TareWeightController {
@Autowired
DataService service;
@Autowired
BulkDataService bulkDataService;
@Autowired
Environment env;
//main page
@RequestMapping(value="/", method = RequestMethod.GET)
public ModelAndView welcome(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("home");
return modelAndView;
}
@GetMapping(value ="/{id}", produces = APPLICATION_JSON_VALUE)
@Timed(name = "GetTareWeightById", level1 = "HTTP", level2 = "GET", level3 = "TareWeightController")
public TareWeight getTareWeight(@PathVariable("id") String id) {
return service.findById(id)
.orElseThrow(() -> new TareWeightNotFoundException(id));
}
//check one tare weight
@RequestMapping(value="/getTareWeight", method=RequestMethod.GET)
public ModelAndView checkId(){
ModelAndView modelAndView = new ModelAndView();
TareWeight tareWeight = new TareWeight();
modelAndView.addObject("tareWeight", tareWeight );
modelAndView.setViewName("getTareWeight");
return modelAndView;
}
//show one tare weight
@RequestMapping(value = "/getTareWeight", method = RequestMethod.POST)
public ModelAndView showOne(@Valid TareWeight oldtareWeight){
ModelAndView modelAndView = new ModelAndView();
if(service.findById(oldtareWeight.getId()).isPresent()){
TareWeight tareWeight = service.findById(oldtareWeight.getId()).get();
modelAndView.addObject("tareWeightId", tareWeight.getId());
modelAndView.addObject("tareWeightWeight", tareWeight.getWeight());
modelAndView.addObject("tareWeightPercent", tareWeight.getPercent());
modelAndView.addObject("tareWeightVersion", tareWeight.getVersion());
modelAndView.addObject("tareWeightLock", tareWeight.getLock());
modelAndView.setViewName("showone");
return modelAndView;
} else{
modelAndView.addObject("errorMessage", "There isn't any tare weight data with this Id.");
modelAndView.setViewName("error");
return modelAndView;
}
}
@PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@Timed(name = "CreateTareWeight", level1 = "HTTP", level2 = "POST", level3 = "TareWeightController")
public TareWeight createTareWeight(@RequestBody TareWeight tareWeight) {
return service.createOrUpdate(tareWeight);
}
//create one tare weight data
@GetMapping(value = "/add")
public ModelAndView addTareWeight(){
ModelAndView modelAndView = new ModelAndView();
TareWeight tareWeight = new TareWeight();
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.setViewName("add");
return modelAndView;
}
//create one tare weight data and save
@PostMapping(value = "/add")
public ModelAndView createTareWeight(@Valid TareWeight tareWeight, BindingResult bindingResult){
ModelAndView modelAndView = new ModelAndView();
if(service.findById(tareWeight.getId()).isPresent()){
modelAndView.addObject("errorMessage", "Oops! It seems this tare weight data has been added already. Please try another one.");
modelAndView.setViewName("error");
return modelAndView;
//bindingResult.rejectValue("id","There is already a same tare weight data");
}
if(bindingResult.hasErrors()){
modelAndView.setViewName("add");
} else{
service.createOrUpdate(tareWeight);
modelAndView.addObject("successMessage", "tare weight data has been added successfully!");
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.setViewName("add");
}
return modelAndView;
}
@DeleteMapping(path = "/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@Timed(name = "DeleteTareWeight", level1 = "HTTP", level2 = "DELETE", level3 = "TareWeightController")
public void deleteTareWeight(@PathVariable("id") String id) {
service.delete(id);
}
@GetMapping(value= "/delete")
public ModelAndView delTareWeight(){
ModelAndView modelAndView = new ModelAndView();
TareWeight tareWeight = new TareWeight();
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.setViewName("delete");
return modelAndView;
}
//delete page
@PostMapping(value = "/delete")
public ModelAndView deleteTareWeight(@Valid TareWeight tareWeight, BindingResult bindingResult){
ModelAndView modelAndView = new ModelAndView();
if(service.findById(tareWeight.getId()).isPresent()){
service.delete(tareWeight.getId());
modelAndView.addObject("successMessage", "The data is deleted.");
modelAndView.setViewName("delete");
return modelAndView;
}
if(bindingResult.hasErrors()){
modelAndView.setViewName("delete");
}
else{
modelAndView.addObject("errorMessage","There isn't any tare weight data with this Id.");
modelAndView.setViewName("error");
}
return modelAndView;
}
@PutMapping(path = "/{id}", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
@Timed(name = "UpdateTareWeight", level1 = "HTTP", level2 = "PUT", level3 = "TareWeightController")
public TareWeight updateTareWeight(@PathVariable("id") int id, @RequestBody TareWeight tareWeight) {
return service.createOrUpdate(tareWeight);
}
//update
@GetMapping(value="/update")
public ModelAndView upTareWeight(){
ModelAndView modelAndView = new ModelAndView();
TareWeight tareWeight = new TareWeight();
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.setViewName("update");
return modelAndView;
}
@RequestMapping(value="/update", method=RequestMethod.POST)
public ModelAndView updateTareWeight(@Valid TareWeight tareWeight, BindingResult bindingResult){
ModelAndView modelAndView = new ModelAndView();
if(service.findById(tareWeight.getId()).isPresent()){
service.delete(tareWeight.getId());
service.createOrUpdate(tareWeight);
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.addObject("successMessage", "The data has been updated!");
modelAndView.setViewName("update");
return modelAndView;
}else{
modelAndView.addObject("errorMessage", "Oops! It seems this tare weight data hasn't been added yet. Please update after creating.");
modelAndView.setViewName("error");
return modelAndView;
}
}
@GetMapping(path = "/all", produces = APPLICATION_JSON_VALUE)
@Timed(name = "GetAllTareWeights", level1 = "HTTP", level2 = "GET", level3 = "TareWeightController")
public Collection<TareWeight> getAllTareWeights() {
return service.findAll();
}
@GetMapping(path = "/loadfile", produces = APPLICATION_JSON_VALUE)
@Timed(name = "LoadTareWeightData", level1 = "HTTP", level2 = "GET", level3 = "TareWeightController")
public Collection<TareWeight> loadTareWeightData() {
bulkDataService.loadBulkDataFromFile();
return service.findAll();
}
@GetMapping(value="/all")
public ModelAndView showAll(){
ModelAndView modelAndView = new ModelAndView();
List<TareWeight> list = service.findAll();
modelAndView.addObject("list", list);
modelAndView.setViewName("showAll");
return modelAndView;
}
@ResponseStatus(HttpStatus.NOT_FOUND)
class TareWeightNotFoundException extends RuntimeException {
public TareWeightNotFoundException(String id) {
super("could not find tare weight '" + id + "'.");
}
}
//error page
@RequestMapping(value="/error", method = RequestMethod.GET)
public ModelAndView error(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("error");
return modelAndView;
}
}Last updated