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

Java tutorial

Introduction

Here is the source code for cz.muni.pa165.carparkapp.web.PersonalInfoController.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.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cz.muni.pa165.carparkapp.dto.EmployeeDTO;
import java.security.Principal;
import java.util.Locale;
import javax.validation.Valid;
import org.springframework.context.MessageSource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.UriComponentsBuilder;

/**
 *
 * @author Martin
 */

@Controller
@RequestMapping("/employee")
public class PersonalInfoController {
    final static Logger log = LoggerFactory.getLogger(EmployeeController.class);

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private MessageSource messageSource;

    @RequestMapping(method = RequestMethod.GET)
    public String getEmployee(Model model, Principal principal) {
        String userName = principal.getName();//(String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();      

        EmployeeDTO result = null;
        for (EmployeeDTO e : employeeService.getAllEmployees()) {
            if (e.getUserName().equals(userName))
                result = e;
        }

        log.debug("getEmployee()");
        model.addAttribute("empl", employeeService.findEmployeeById(result.getId()));
        return "employee";
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update_form(@PathVariable int id, Model model) {
        EmployeeDTO employee = employeeService.findEmployeeById(id);
        model.addAttribute("employee", employee);
        log.debug("update_form(model={})", model);
        return "employeeEdit";
    }

    @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={}, customer={})", locale, employee);
        if (bindingResult.hasErrors()) {
            log.debug("binding errors");
            for (ObjectError ge : bindingResult.getGlobalErrors()) {
                log.debug("ObjectError: {}", ge);
            }
            for (FieldError fe : bindingResult.getFieldErrors()) {
                log.debug("FieldError: {}", fe);
            }
            return employee.getId() < 1 ? "employee" : "employee/update";
        }
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        EmployeeDTO emp = null;

        for (EmployeeDTO employee1 : employeeService.getAllEmployees()) {
            if (employee1.getUserName().equals(name))
                emp = employee1;
        }
        employee.setUserName(name);
        employee.setPassword(emp.getPassword());
        employeeService.updateEmployee(employee);
        redirectAttributes.addFlashAttribute("message", messageSource.getMessage("employee.updated",
                new Object[] { employee.getFirstName(), employee.getLastName(), employee.getId() }, locale));
        return "redirect:" + uriBuilder.path("/employee").build();
    }

}