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 com.portal.controller; import javax.servlet.http.HttpServletRequest; import com.portal.dto.EmployeeDTO; import com.portal.entity.Department; import com.portal.entity.Employee; import com.portal.entity.Position; import com.portal.service.PortalService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ExceptionHandler; 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.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; /** * * @author Evgen */ @Controller @RequestMapping("/admin") public class AdminController { PortalService portalService; public PortalService getPortalService() { return portalService; } @Autowired public void setPortalService(PortalService portalService) { this.portalService = portalService; } @RequestMapping("") public String showMenu() { return "admin/menu"; } @RequestMapping(value = "/departments", method = RequestMethod.GET) public String showDepartments(ModelMap model) { model.addAttribute("departments", portalService.getAllDepartments()); return "admin/departments"; } @RequestMapping(value = "/positions", method = RequestMethod.GET) public String showPositions(ModelMap model) { model.addAttribute("positions", portalService.getAllPositions()); return "admin/positions"; } @RequestMapping(value = "/employes", method = RequestMethod.GET) public String showEmployes(ModelMap model) { model.addAttribute("employes", portalService.getAllEmployes()); model.addAttribute("positions", portalService.getAllPositions()); model.addAttribute("departments", portalService.getAllDepartments()); model.addAttribute("employeedto", new EmployeeDTO()); return "admin/employes"; } @RequestMapping(value = "/employes/update/{id}", method = RequestMethod.GET) public String showConcreteEmployee(ModelMap model, @PathVariable("id") int id) { model.addAttribute("positions", portalService.getAllPositions()); model.addAttribute("departments", portalService.getAllDepartments()); Employee employee = portalService.getEmployee(id); EmployeeDTO dto = new EmployeeDTO(); dto.setName(employee.getName()); dto.setDepartment(employee.getDepartment().getId()); dto.setPosition(employee.getPosition().getId()); dto.setPhone(employee.getPhone()); dto.setEmail(employee.getEmail()); dto.setDescription(employee.getDescription()); dto.setAvatar(employee.getAvatar()); model.addAttribute("employeedto", dto); model.addAttribute("id", id); return "admin/employes/update"; } @RequestMapping(value = "/departments/{action}", method = RequestMethod.POST) public String submitDepartments(@PathVariable String action, @RequestParam(value = "id", required = false) Integer id, @RequestParam(value = "name", required = false) String name) throws Exception { switch (action) { case "create": if (name != null && name.length() > 0) { Department department = new Department(name); portalService.saveDepartment(department); } break; case "update": if (id != null && name != null && name.length() > 0) { Department department = new Department(name); department.setId(id); portalService.saveDepartment(department); } break; case "delete": if (id != null) { portalService.deleteDepartment(id); } break; default: throw new AssertionError(); } return "redirect:/admin/departments"; } @RequestMapping(value = "/positions/{action}", method = RequestMethod.POST) public String submitPositions(@PathVariable String action, @RequestParam(value = "id", required = false) Integer id, @RequestParam(value = "name", required = false) String name) throws Exception { switch (action) { case "create": if (name != null && name.length() > 0) { Position position = new Position(name); portalService.savePosition(position); } break; case "update": if (id != null && name != null && name.length() > 0) { Position position = new Position(name); position.setId(id); portalService.savePosition(position); } break; case "delete": if (id != null) { portalService.deletePosition(id); } break; default: throw new AssertionError(); } return "redirect:/admin/positions"; } @RequestMapping(value = "/employes/create", method = RequestMethod.POST) public String submitCreateEmployee(HttpServletRequest request, @ModelAttribute("employeedto") EmployeeDTO employeedto, @RequestParam(value = "avatarFile", required = false) MultipartFile avatar) throws Exception { Employee employee = new Employee(); if (!avatar.isEmpty() && avatar.getContentType().equals("image/jpeg")) { portalService.saveFile( request.getServletContext().getRealPath("/template/img/") + "/" + avatar.getOriginalFilename(), avatar); employee.setAvatar(avatar.getOriginalFilename()); } if (employeedto.getName() == null || employeedto.getName().trim().length() == 0 || employeedto.getPosition() == 0 || employeedto.getDepartment() == 0) { return "redirect:/admin/employes"; } employee.setName(employeedto.getName()); employee.setDepartment(portalService.getDepartment(employeedto.getDepartment())); employee.setPosition(portalService.getPosition(employeedto.getPosition())); employee.setPhone(employeedto.getPhone()); employee.setEmail(employeedto.getEmail()); employee.setDescription(employeedto.getDescription()); portalService.saveEmployee(employee); return "redirect:/admin/employes"; } @RequestMapping(value = "/employes/update", method = RequestMethod.POST) public String submitUpdateEmployee(HttpServletRequest request, @RequestParam("id") int id, @ModelAttribute("employeedto") EmployeeDTO employeedto, @RequestParam(value = "avatarFile", required = false) MultipartFile avatar) throws Exception { // Employee employee=new Employee(); Employee employee = portalService.getEmployee(id); if (!avatar.isEmpty() && avatar.getContentType().equals("image/jpeg")) { portalService.saveFile( request.getServletContext().getRealPath("/template/img/") + "/" + avatar.getOriginalFilename(), avatar); employee.setAvatar(avatar.getOriginalFilename()); } // employee.setId(id); employee.setName(employeedto.getName()); employee.setDepartment(portalService.getDepartment(employeedto.getDepartment())); employee.setPosition(portalService.getPosition(employeedto.getPosition())); employee.setPhone(employeedto.getPhone()); employee.setEmail(employeedto.getEmail()); employee.setDescription(employeedto.getDescription()); portalService.saveEmployee(employee); return "redirect:/admin/employes"; } @RequestMapping(value = "/employes/delete", method = RequestMethod.POST) public String submitDeleteEmployee(@RequestParam("id") int id) { portalService.deleteEmployee(id); return "redirect:/admin/employes"; } @ExceptionHandler(Exception.class) public ModelAndView handleExceptions(Exception ex) { ModelAndView view = new ModelAndView(); view.addObject("exception", ex); view.setViewName("exception"); return view; } }