Java tutorial
/* * 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 by.bsuir.finance.controllers; import by.bsuir.finance.domain.Contract; import by.bsuir.finance.domain.DefaultSalary; import by.bsuir.finance.domain.Salary; import by.bsuir.finance.domain.UserInfo; import by.bsuir.finance.domain.Payments; import by.bsuir.finance.exceptions.UserInfoNotFoundException; import by.bsuir.finance.service.contract.ContractService; import by.bsuir.finance.service.salary.DefaultSalaryService; import by.bsuir.finance.service.salary.SalaryService; import by.bsuir.finance.service.user.UserInfoService; import by.bsuir.finance.service.user.UsersService; import java.security.Principal; import java.util.Date; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; /** * * @author igor */ @Controller @RequestMapping("/accountant") public class AccountantController { private static final Logger log = Logger.getLogger(AccountantController.class.getName()); @Autowired private UserInfoService infoService; @Autowired private SalaryService salaryService; @Autowired private UsersService userService; @Autowired private ContractService contractService; @Autowired private DefaultSalaryService defaultSalaryService; @Autowired private PaymentsService paymentsService; @RequestMapping(value = "/details", method = RequestMethod.GET) @ResponseBody public ModelAndView details(@RequestParam("idUserInfo") int idUserInfo) { ModelAndView model = new ModelAndView("accountant/details"); UserInfo info = null; try { info = infoService.findById(idUserInfo); } catch (UserInfoNotFoundException e) { log.log(Level.SEVERE, e.getMessage()); } log.log(Level.INFO, "info : {0}", info); model.addObject("command", info); model.addObject("userinfo", info); return model; } @RequestMapping(value = "/increase") public ModelAndView accrue() { ModelAndView model = new ModelAndView("accountant/increase"); List<UserInfo> infos = infoService.findAll(); model.addObject("infos", infos); return model; } @RequestMapping(value = "/save", method = RequestMethod.GET) @ResponseBody public String save(@ModelAttribute("Finance") UserInfo userInfo, ModelMap model, Principal principal) { log.log(Level.INFO, "userInfo : {0} username : {1} id : {2}", new Object[] { userInfo.getIdUserInfo(), userInfo, null }); return "redirect:increase"; } @RequestMapping(value = "/saveall") public String saveAll() { log.info("saveall"); List<UserInfo> infos = infoService.findAll(); for (UserInfo info : infos) { List<DefaultSalary> defsalaries = defaultSalaryService.findAll(); for (DefaultSalary defsalary : defsalaries) { if ((defsalary.getDepartment().getIdDepartment() == info.getDepartment().getIdDepartment()) && (defsalary.getPositions().getIdpositions() == info.getPositions().getIdpositions())) { Salary salary = new Salary(); salary.setDateTime(new Date()); salary.setDefaultSalary(defsalary); salary.setSalary(defsalary.getDefaultSalary() * salary.getCoefficient()); salary.setUsers(userService.findByUsername(getUserName())); salaryService.saveSalary(salary); log.log(Level.INFO, "salary : {0}", salary); } } } return "redirect:increase"; } public String getUserName() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String name = auth.getName(); //get logged in username return name; } @RequestMapping(value = "contracts") public ModelAndView contracts() { ModelAndView model = new ModelAndView("accountant/contracts"); List<Contract> contracts = contractService.findAll(); model.addObject("contracts", contracts); return model; } @RequestMapping(value = "payments") public ModelAndView payments() { ModelAndView model = new ModelAttribute("accountant/payments"); List<Payments> payments = paymentsService.findAll(); model.addObject("payments", payments); return model; } @RequestMapping(value = "pay", method = RequestMethod.GET) @ResponseBody public ModelAndView details(@RequestParam("idContract") int idContract) { ModelAndView model = new ModelAndView("accountant/pay"); model.addObject("command", new Payments()); Contract contract = contractService.findByIdContract(idContract); model.addObject("contract", contract); return model; } @RequestMapping(value = "pay/${idContract}") @ResponseBody public String pay(@PathVariable("idContract") int idContract, @ModelAttribute("Finance") Payments payments) { payments.setContract(contractService.findByIdContract(idContract)); paymentsService.save(payments); return "redirect:payments"; } }