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

Java tutorial

Introduction

Here is the source code for cz.muni.pa165.carparkapp.web.EmployeeController.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.EmployeeDTO;
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 Tom
 */

@Controller
@RequestMapping("/serviceEmployee")
public class EmployeeController {

    final static Logger log = LoggerFactory.getLogger(EmployeeController.class);

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private MessageSource messageSource;

    @ModelAttribute("employees")
    public List<EmployeeDTO> allEmployee() {
        log.debug("allEmployees()");
        return employeeService.getAllEmployees();

    }

    @RequestMapping(value = "/addEmployee", method = RequestMethod.GET)
    public String addEmployee(Model model) {
        log.debug("allEmployees()");
        model.addAttribute("employee", new EmployeeDTO());
        return "addEmployee";
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public String delete(@PathVariable int id, RedirectAttributes redirectAttributes, Locale locale,
            UriComponentsBuilder uriBuilder) {
        log.debug("delete({})", id);
        EmployeeDTO employee = employeeService.findEmployeeById(id);
        employeeService.deleteEmployee(employee);
        redirectAttributes.addFlashAttribute("message", messageSource.getMessage("addEmployee.delete.mess",
                new Object[] { employee.getFirstName(), employee.getLastName() }, locale));
        return "redirect:" + uriBuilder.path("/serviceEmployee/addEmployee").build();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute EmployeeDTO employee, BindingResult bindingResult,
            RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder, Locale locale) {
        log.debug("update(locale={}, employee={})", locale, employee);
        if (employee.getId() < 1) {
            employeeService.addEmployee(employee);
            redirectAttributes.addFlashAttribute("message", messageSource.getMessage("employee.add.message",
                    new Object[] { employee.getFirstName(), employee.getLastName() }, locale));
            if (employee.getUserName() == null || employee.getUserName().isEmpty()) {
                employeeService.addEmployee(employee);
                redirectAttributes.addFlashAttribute("message",
                        messageSource.getMessage("employee.nullusername.message",
                                new Object[] { employee.getFirstName(), employee.getLastName() }, locale));
            }
        } else {

            employeeService.updateEmployee(employee);
            redirectAttributes.addFlashAttribute("message",
                    messageSource.getMessage("employee.updated.message", new Object[] {}, locale));
        }
        return "redirect:" + uriBuilder.path("/serviceEmployee/addEmployee").build();
    }

}