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.Department; import by.bsuir.finance.domain.Positions; import by.bsuir.finance.domain.UserInfo; import by.bsuir.finance.domain.Users; import by.bsuir.finance.service.user.DepartmentService; import by.bsuir.finance.service.user.PositionService; import by.bsuir.finance.service.user.UserInfoService; import java.security.Principal; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; 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(value = "/admin") public class AdminController { private static final Logger log = Logger.getLogger(AppController.class.getName()); @Autowired private UserInfoService infoService; @Autowired private DepartmentService departmentService; @Autowired private PositionService positionService; @RequestMapping(value = "/users", method = RequestMethod.GET) public ModelAndView admin() { ModelAndView model = new ModelAndView(); model.setViewName("admin/users"); model.addObject("userList", infoService.findAll()); return model; } @RequestMapping(value = "/delete", method = RequestMethod.GET) @ResponseBody public ModelAndView delete(@RequestParam("idUserInfo") int idUserInfo) { ModelAndView model = new ModelAndView("admin/users"); infoService.delete(idUserInfo); return model; } @RequestMapping(value = "/edit", method = RequestMethod.GET) @ResponseBody public ModelAndView editUser(@RequestParam("idUserInfo") int idUserInfo) { log.log(Level.INFO, "edit user id = {0}", idUserInfo); ModelAndView model = new ModelAndView("admin/edit"); System.out.println("lol"); //model.addObject("user", infoRepository.findByIdUsername(((Number)idUsername).longValue())); return model; } @RequestMapping("/add") public ModelAndView add() { ModelAndView model = new ModelAndView("admin/add"); List<Department> departments = departmentService.findAll(); List<Positions> positionses = positionService.findAll(); UserInfo info = new UserInfo(); info.setDepartment(new Department()); info.setUsers(new Users()); info.setPositions(new Positions()); model.addObject("command", new UserInfo()); Map<Integer, String> depMap = new LinkedHashMap<>(); for (Department department : departments) { depMap.put(department.getIdDepartment(), department.getDepartment()); } Map<Integer, String> posMap = new LinkedHashMap<>(); for (Positions positionse : positionses) { posMap.put(positionse.getIdpositions(), positionse.getPosition()); } Map<Integer, String> roles = new LinkedHashMap<>(); roles.put(1, ""); roles.put(2, "??"); roles.put(3, ""); model.addObject("roles", roles); model.addObject("departments", depMap); model.addObject("positions", posMap); return model; } @RequestMapping(value = "/save", method = RequestMethod.GET) public String save(@ModelAttribute("Finance") UserInfo userInfo, ModelMap model, Principal principal) { log.log(Level.INFO, "role {0}", userInfo.getRole()); System.out.println(userInfo.getRole()); infoService.save(userInfo); return "redirect:users"; } }