cz.muni.pa165.carparkapp.web.AdminController.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.pa165.carparkapp.web.AdminController.java

Source

/*
 * 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.muni.pa165.carparkapp.web;

import cz.muni.pa165.carparkapp.dto.BranchDTO;
import cz.muni.pa165.carparkapp.dto.CarDTO;
import cz.muni.pa165.carparkapp.dto.EmployeeDTO;
import cz.muni.pa165.carparkapp.service.BranchService;
import cz.muni.pa165.carparkapp.service.CarService;
import cz.muni.pa165.carparkapp.service.EmployeeService;
import java.util.List;
import java.util.Locale;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.UriComponentsBuilder;

/**
 *
 * @author coldfront
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    final static Logger log = LoggerFactory.getLogger(EmployeeController.class);

    @Autowired
    private BranchService branchService;

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private CarService carService;

    @RequestMapping("/")
    public String admin(Model model) {
        return "admin";
    }

    @ModelAttribute("users")
    public List<EmployeeDTO> getUsers(Model model) {

        //return model.addAttribute("employees", employeeService.getAllEmployees());

        return employeeService.getAllEmployees();
    }

    @RequestMapping(value = "/users/addUser", method = RequestMethod.GET)
    public String addUser(Model model) {
        log.debug("allUsers()");
        EmployeeDTO user = new EmployeeDTO();
        model.addAttribute("newUser", user);
        return "addUser";
    }

    @RequestMapping(value = "/users/delete/{id}", method = RequestMethod.POST)
    public String deleteUser(@PathVariable int id, RedirectAttributes redirectAttributes, Locale locale,
            UriComponentsBuilder uriBuilder) {
        log.debug("deleteUser({})", id);
        EmployeeDTO user = employeeService.findEmployeeById(id);
        employeeService.deleteEmployee(user);
        /*redirectAttributes.addFlashAttribute(
            "message",
            messageSource.getMessage("addCar.delete.mess", new Object[]
                    {
                        car.getRegistrationNumber()
            }, locale)
        );*/
        return "redirect:" + uriBuilder.path("/admin/users/addUser").build();
    }

    @RequestMapping(value = "/users/update", method = RequestMethod.POST)
    public String updateUser(@Valid @ModelAttribute EmployeeDTO user, BindingResult bindingResult,
            RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder, Locale locale) {
        log.debug("updateUser(locale={}, book={})", locale, user);

        System.out.println(user);

        if (user.getId() < 1) {
            user.setRole("USER");
            employeeService.addEmployee(user);
            redirectAttributes.addFlashAttribute("message",
                    messageSource.getMessage("employee.add.message", new Object[] { user.getUserName() }, locale));
        } else {
            employeeService.updateEmployee(user);
            /*redirectAttributes.addFlashAttribute(
                "message",
                messageSource.getMessage("car.updated.message", new Object[]
                        {
                            car.getRegistrationNumber()
                }, locale)
            );*/
        }
        return "redirect:" + uriBuilder.path("/admin/users/addUser").build();
    }

    @ModelAttribute("branches")
    public List<BranchDTO> allBranch() {
        log.debug("allBranches()");
        return branchService.findAllBranches();
    }

    @RequestMapping(value = "branch/addBranch", method = RequestMethod.GET)
    public String addBranch(Model model) {
        log.debug("allBranches()");
        model.addAttribute("branch", new BranchDTO());
        return "addBranch";
    }

    @RequestMapping(value = "branch/delete/{id}", method = RequestMethod.POST)
    public String delete(@PathVariable int id, RedirectAttributes redirectAttributes, Locale locale,
            UriComponentsBuilder uriBuilder) {
        log.debug("delete({})", id);
        BranchDTO branch = branchService.findBranch(id);
        branchService.deleteBranch(branch);
        redirectAttributes.addFlashAttribute("message", messageSource.getMessage("addBranch.delete.mess",
                new Object[] { branch.getCompanyNumber() }, locale));
        return "redirect:" + uriBuilder.path("/admin/branch/addBranch").build();
    }

    @RequestMapping(value = "branch/update", method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute BranchDTO branch, BindingResult bindingResult,
            RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder, Locale locale) {
        log.debug("update(locale={}, branch={})", locale, branch);
        if (branch.getId() < 1) {
            branchService.addBranch(branch);
            redirectAttributes.addFlashAttribute("message", messageSource.getMessage("branch.add.message",
                    new Object[] { branch.getCompanyNumber() }, locale));
        } else {
            branchService.updateBranch(branch);
            redirectAttributes.addFlashAttribute("message", messageSource.getMessage("branch.updated.message",
                    new Object[] { branch.getCompanyNumber() }, locale));
        }
        return "redirect:" + uriBuilder.path("/admin/branch/addBranch").build();
    }

    @ModelAttribute("cars")
    public List<CarDTO> getCars() {
        log.debug("allCars()");
        List<CarDTO> list = carService.getAllCars();
        return list;
    }

    @RequestMapping(value = "/car/addCar", method = RequestMethod.GET)
    public String addCar(Model model) {
        log.debug("allCars()");
        model.addAttribute("car", new CarDTO());
        return "addCar";
    }

    @RequestMapping(value = "/car/delete/{id}", method = RequestMethod.POST)
    public String deleteCar(@PathVariable int id, RedirectAttributes redirectAttributes, Locale locale,
            UriComponentsBuilder uriBuilder) {
        log.debug("delete({})", id);
        CarDTO car = carService.findCarById(id);
        carService.deleteCar(car);
        redirectAttributes.addFlashAttribute("message", messageSource.getMessage("addCar.delete.mess",
                new Object[] { car.getRegistrationNumber() }, locale));
        return "redirect:" + uriBuilder.path("/admin/car/addCar").build();
    }

    @RequestMapping(value = "/car/update", method = RequestMethod.POST)
    public String updateCar(@Valid @ModelAttribute CarDTO car, BindingResult bindingResult,
            RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder, Locale locale) {
        log.debug("update(locale={}, book={})", locale, car);

        if (car.getId() < 1) {
            carService.addCar(car);
            redirectAttributes.addFlashAttribute("message", messageSource.getMessage("car.add.message",
                    new Object[] { car.getRegistrationNumber() }, locale));
        } else {
            carService.updateCar(car);
            redirectAttributes.addFlashAttribute("message", messageSource.getMessage("car.updated.message",
                    new Object[] { car.getRegistrationNumber() }, locale));
        }
        return "redirect:" + uriBuilder.path("/admin/car/addCar").build();
    }

}