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

Java tutorial

Introduction

Here is the source code for cz.muni.pa165.carparkapp.web.LoansController.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.dto.LoanDTO;
import cz.muni.pa165.carparkapp.service.EmployeeService;
import cz.muni.pa165.carparkapp.service.LoanService;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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 Martin
 */

@Controller
@RequestMapping("/loans")
public class LoansController {

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

    @Autowired
    private LoanService loanService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private MessageSource messageSource;

    @ModelAttribute("loans")
    public List<LoanDTO> allEmployee() {
        log.debug("getListOfLoans()");
        return loanService.getListOfLoanes();
    }

    @RequestMapping(method = RequestMethod.GET)
    public String getEmployee(Model model) {
        log.debug("getListOfLoans()");
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        EmployeeDTO emp = null;

        for (EmployeeDTO employee : employeeService.getAllEmployees()) {
            if (employee.getUserName().equals(name))
                emp = employee;
        }
        List<LoanDTO> list = loanService.getLoansByEmployee(emp);
        model.addAttribute("loans", list);
        return "loans";
    }

    @RequestMapping(value = "/return/{id}", method = RequestMethod.POST)
    public String update(@PathVariable int id, UriComponentsBuilder uriBuilder,
            RedirectAttributes redirectAttributes, Locale locale) {
        LoanDTO loan = loanService.getLoanById(id);
        log.debug("update(locale={}, loan={})", locale, loan);
        Date date = new Date();
        loan.setEndDate(date);
        loanService.updateLoan(loan);
        redirectAttributes.addFlashAttribute("message",
                messageSource.getMessage("loan.returned", new Object[] { loan.getCar().getId() }, locale));
        return "redirect:" + uriBuilder.path("/loans").build();
    }
}