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.controller; import com.editor.CompanyEditor; import com.editor.EmployeeEditor; import com.mailFunction.Mail; import com.model.Company; import com.model.Employee; import com.model.Preceeding; import com.model.Project; import com.model.Role; import com.model.Skill; import com.model.SkillType; import com.service.CompanyService; import com.service.EmployeeService; import com.service.PreceedingService; import com.service.ProjectService; import com.service.RoleService; import com.service.SkillRatingService; import com.service.SkillService; import com.service.SkillTypeService; import com.utility.Utility; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; 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.ModelAndView; import org.springframework.web.servlet.View; import org.springframework.web.servlet.view.RedirectView; /** * * @author guus_portegies */ @Controller @RequestMapping public class ProjectsController { @Autowired private ProjectService projectService; @Autowired private EmployeeService employeeService; @Autowired private CompanyService companyService; @Autowired private CompanyEditor companyEditor; @Autowired private EmployeeEditor employeeEditor; @Autowired private PreceedingService preceedingService; @Autowired private RoleService roleService; @Autowired SkillService ski; @Autowired SkillTypeService st; @Autowired SkillRatingService sri; @Autowired private Utility util; private String loginRedirect = "http://localhost:8080/Skillmatcher/login"; private String errorRedirect = "http://localhost:8080/Skillmatcher/error/403"; @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Company.class, this.companyEditor); binder.registerCustomEditor(Employee.class, this.employeeEditor); } @RequestMapping("/projects") public String index(HttpServletRequest request, Model model) throws IOException { if (!util.isLoggedIn(request)) { return "redirect:" + loginRedirect; } System.out.println(util.userAcces(request)); if (util.userAcces(request) > 0) { model.addAttribute("canAddProject", true); } if (util.userAcces(request) > 1) { model.addAttribute("isAdmin", true); } // if (util.userAcces(request) < 1) { // return "redirect:" + errorRedirect; // } int id = util.getUserId(request); model.addAttribute("active", "projects"); List<Project> projects = projectService.getProjects(); model.addAttribute("projects", projects); List<Company> companies = companyService.getCompanies(); model.addAttribute("companies", companies); return "projects"; } @RequestMapping(value = "/projects/archived", method = RequestMethod.GET) public ModelAndView archiveProjectView(HttpServletRequest request) { ModelAndView archiveProjectView = new ModelAndView("archivedProjects"); archiveProjectView.addObject("active", "projects"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("redirect:" + errorRedirect); } if (util.userAcces(request) > 1) { archiveProjectView.addObject("isAdmin", true); } if (util.userAcces(request) > 0) { archiveProjectView.addObject("canAddProject", true); } List<Project> projects = projectService.getArchivedProjects(); archiveProjectView.addObject("projects", projects); return archiveProjectView; } @RequestMapping(value = "/projects/archive/{id}", method = RequestMethod.GET) public ModelAndView archiveProject(@PathVariable int id, Project project, Model model, HttpServletRequest request) { ModelAndView archiveProjectView = new ModelAndView("projects"); archiveProjectView.addObject("active", "projects"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("redirect:" + errorRedirect); } if (util.userAcces(request) > 1) { archiveProjectView.addObject("isAdmin", true); } if (util.userAcces(request) > 0) { model.addAttribute("canAddProject", true); } projectService.archiveProject(project); List<Project> projects = projectService.getProjects(); archiveProjectView.addObject("projects", projects); return archiveProjectView; } @RequestMapping(value = "/projects/reArchive/{id}", method = RequestMethod.GET) public ModelAndView reArchiveProject(@PathVariable int id, Project project, Model model, HttpServletRequest request) { ModelAndView archiveProjectView = new ModelAndView("archivedProjects"); archiveProjectView.addObject("active", "projects"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("redirect:" + errorRedirect); } if (util.userAcces(request) > 1) { archiveProjectView.addObject("isAdmin", true); } if (util.userAcces(request) > 0) { model.addAttribute("canAddProject", true); } projectService.reArchiveProject(project); List<Project> projects = projectService.getArchivedProjects(); archiveProjectView.addObject("projects", projects); return archiveProjectView; } @RequestMapping(value = "/projects/add", method = RequestMethod.GET) public ModelAndView addProject(HttpServletRequest request) { ModelAndView newProject = new ModelAndView("addNewProject"); newProject.addObject("active", "projects"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("redirect:" + errorRedirect); } if (util.userAcces(request) > 1) { newProject.addObject("isAdmin", true); } List<Company> companies = companyService.getCompanies(); newProject.addObject("companies", companies); newProject.addObject("project", new Project()); return newProject; } @RequestMapping(value = "/projects/add", method = RequestMethod.POST) public ModelAndView showProjects(HttpServletRequest request, @ModelAttribute Project project) { ModelAndView projectListView = new ModelAndView("projects"); int id = util.getUserId(request); Employee e = employeeService.getEmployee(id); project.setOwner(e); if (util.userAcces(request) > 1) { projectListView.addObject("isAdmin", true); } project.setStatus(1); projectService.addProject(project); List<Project> projects = projectService.getProjects(); projectListView.addObject("projects", projects); String message = "Project toegevoegd."; projectListView.addObject("addProject", message); return projectListView; } @RequestMapping(value = "/projects/edit/{id}", method = RequestMethod.GET) public ModelAndView editProject(@PathVariable int id, HttpServletRequest request) { ModelAndView editProject = new ModelAndView("editProjects"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("redirect:" + errorRedirect); } if (util.userAcces(request) > 1) { editProject.addObject("isAdmin", true); } editProject.addObject("active", "editProject"); Project project = projectService.getProject(id); List<Company> companies = companyService.getCompanies(); editProject.addObject("project", project); editProject.addObject("companies", companies); return editProject; } @RequestMapping(value = "/projects/edit", method = RequestMethod.POST) public ModelAndView edit(@ModelAttribute Project project, HttpServletRequest request) { ModelAndView memberlistView = new ModelAndView("projects"); projectService.updateProject(project); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("redirect:" + errorRedirect); } if (util.userAcces(request) > 1) { memberlistView.addObject("isAdmin", true); } if (util.userAcces(request) > 0) { memberlistView.addObject("canAddProject", true); } List<Project> projects = projectService.getProjects(); memberlistView.addObject("projects", projects); String message = "Project gewijzigd"; memberlistView.addObject("addProject", message); return memberlistView; } @RequestMapping(value = "/projects/addEmployee/{id}", method = RequestMethod.GET) public ModelAndView addEmployee(@PathVariable int id, @ModelAttribute Project project, HttpServletRequest request) { int employeeID = (Integer) request.getSession().getAttribute("loggedIn"); Employee loggedInUser = employeeService.getEmployee(employeeID); ModelAndView projects = new ModelAndView("projects"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("redirect:" + request.getContextPath() + "/error/403"); } if (util.userAcces(request) > 1) { projects.addObject("isAdmin", true); } // Project p = projectService.getProject(id); // List<Employee> selectedEmployees = employeeService.getEmployees(); // for (Employee e : selectedEmployees) { // String message; // Mail mail = new Mail(); // message = "<p>Beste heer/mevrouw " + e.getSurname() + " ,<br><br><br></p>"; // message += "<p>U bent uitgenodigd om deel te nemen aan een project!</p>"; // message += "Klik <a href='localhost:8080/Skillmatcher/projects/edit/" + p.getId() + "'>hier</a> om het project te zien!<br><br>"; // message += "<p>Met vriendelijke groeten,<br><br><br>" + loggedInUser.getSurname() + "</p>"; // try { // mail.sendMail(e.getCompanyEmail(), "Project uitnodiging", message); // } catch (Exception ex) { // ex.printStackTrace(); // } // } String debugging = "Uitnodiging is verzonden!"; projects.addObject(debugging); return projects; } /** * Redirect to page showing members invited for the project given. * * @param id Project id * @param model * @return */ @RequestMapping(value = "/projects/viewInvitations/{id}", method = RequestMethod.GET) public ModelAndView viewInvitations(@PathVariable int id, HttpServletRequest request) { ModelAndView mav = getInvitationsModel(id); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) > 0) { mav.addObject("isManager", true); } if (util.userAcces(request) > 1) { mav.addObject("isAdmin", true); } return mav; } private ModelAndView getInvitationsModel(int projectID) { ModelAndView mav = new ModelAndView("viewInvitations"); Project project = projectService.getProject(projectID); mav.addObject("project", project); List<Preceeding> list = projectService.getAllPreceeding(project); List<Preceeding> preceeding0 = new ArrayList(); List<Preceeding> preceeding1 = new ArrayList(); List<Preceeding> preceeding2 = new ArrayList(); List<Preceeding> preceeding3 = new ArrayList(); List<Preceeding> preceeding4 = new ArrayList(); for (Preceeding p : list) { switch (p.getStatus()) { case 0: preceeding0.add(p); break; case 1: preceeding1.add(p); break; case 2: preceeding2.add(p); break; case 3: preceeding3.add(p); break; case 4: preceeding4.add(p); break; } } //model.addAttribute("preceedings", projectService.getAllPreceeding(project)); mav.addObject("preceeding0", preceeding0); mav.addObject("preceeding1", preceeding1); mav.addObject("preceeding2", preceeding2); mav.addObject("preceeding3", preceeding3); mav.addObject("preceeding4", preceeding4); mav.addObject("active", "invitations"); return mav; } @RequestMapping(value = "/accept/{id}", method = RequestMethod.GET) public ModelAndView acceptPreceeding(@PathVariable int id, HttpServletRequest request) { Preceeding p = preceedingService.getPreceeding(id); ModelAndView m = getInvitationsModel(p.getProject().getId()); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("redirect:" + errorRedirect); } if (util.userAcces(request) > 1) { m.addObject("isAdmin", true); } //Change status of the preceeding to accepted by Manager (2) if (p.getStatus() == 1) { p.setStatus(2); } preceedingService.updatePreceeding(p); if (util.userAcces(request) > 1) { m.addObject("isAdmin", true); } return m; } @RequestMapping(value = "/decline/{id}", method = RequestMethod.GET) public ModelAndView declinePreceeding(@PathVariable int id, HttpServletRequest request) { if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("redirect:" + errorRedirect); } //Change status of the preceeding to declined by Manager (4) Preceeding p = preceedingService.getPreceeding(id); if (p.getStatus() == 1) { p.setStatus(4); } preceedingService.updatePreceeding(p); ModelAndView m = getInvitationsModel(p.getProject().getId()); if (util.userAcces(request) > 1) { m.addObject("isAdmin", true); } return m; } @RequestMapping(value = "/preceeding/{id}", method = RequestMethod.GET) public ModelAndView employeeOverzicht(@PathVariable int id, Model model, HttpServletRequest request) { ModelAndView m = new ModelAndView("project_employees"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } // if (util.userAcces(request) < 1) { // return new ModelAndView("redirect:" + errorRedirect); // } if (util.userAcces(request) > 1) { m.addObject("isAdmin", true); } //get employees for project //List<Employee> employees = new ArrayList(); List<Preceeding> preceedings = preceedingService.getPreceedingInProject(id); Project p = projectService.getProject(id); model.addAttribute("project", p); model.addAttribute("preceedings", preceedings); //model.addAttribute("employees", employees); return m; } @RequestMapping(value = "/preceeding/verify/true", method = RequestMethod.POST) public View verifyTrue(Model model, HttpServletRequest request) { int projectId; try { int p = Integer.parseInt(request.getParameter("preceedingId")); projectId = Integer.parseInt(request.getParameter("projectId")); preceedingService.setStatus(p, 2); } catch (Exception e) { e.printStackTrace(); return new RedirectView(request.getContextPath() + "/projects"); } return new RedirectView(request.getContextPath() + "/preceeding/" + projectId); } @RequestMapping(value = "/preceeding/verify/false", method = RequestMethod.POST) public View verifyFalse(Model model, HttpServletRequest request) { int projectId; try { int p = Integer.parseInt(request.getParameter("preceedingId")); projectId = Integer.parseInt(request.getParameter("projectId")); preceedingService.setStatus(p, 4); } catch (Exception e) { e.printStackTrace(); return new RedirectView(request.getContextPath() + "/projects"); } return new RedirectView(request.getContextPath() + "/preceeding/" + projectId); } @RequestMapping(value = "/projects/inviteMembers/{projectId}", method = RequestMethod.GET) public ModelAndView inviteMembers(@PathVariable int projectId, Model model, HttpServletRequest request) { ModelAndView m = new ModelAndView("inviteMembers"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) < 1) { return new ModelAndView("403"); } if (util.userAcces(request) > 1) { m.addObject("isAdmin", true); } List<Employee> employees = employeeService.getEmployees(); m.addObject("employees", employees); m.addObject("projectId", projectId); List<SkillType> skillTypes = st.getSkillTypes(); m.addObject("skillTypes", skillTypes); List<Skill> alleSkills = ski.getSkills(); m.addObject("alleSkills", alleSkills); m.addObject("checked", null); List<Skill> projectskills = ski.getSkillWithProjectSkill(); m.addObject("projectskills", projectskills); List<Skill> technischeSkills = ski.getSkillWithTechnischeSkill(); m.addObject("technischeSkills", technischeSkills); return m; } @RequestMapping(value = "/projects/inviteMembers/{projectId}", method = RequestMethod.POST) public ModelAndView search(@ModelAttribute Skill skill, HttpServletRequest request, @PathVariable int projectId) { ModelAndView search = new ModelAndView("inviteMembers"); search.addObject("active", "projects"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) > 1) { search.addObject("isAdmin", true); } String[] technischeSkillscheck = request.getParameterValues("technischeSkills"); search.addObject("technischeSkillschecked", technischeSkillscheck); String[] projectskillscheck = request.getParameterValues("projectskills"); search.addObject("projectskillschecked", projectskillscheck); String region = request.getParameter("region"); search.addObject("regionQueried", region); int businessUnit = Integer.parseInt(request.getParameter("businessUnit")); search.addObject("businessUnitQueried", businessUnit); int lengthTechnischeSkill = 0; int lengthProjectSkill = 0; if (technischeSkillscheck != null) { lengthTechnischeSkill += technischeSkillscheck.length; } if (projectskillscheck != null) { lengthProjectSkill += projectskillscheck.length; } String[] skills = new String[lengthTechnischeSkill + lengthProjectSkill]; if (technischeSkillscheck != null) { System.arraycopy(technischeSkillscheck, 0, skills, 0, lengthTechnischeSkill); } if (projectskillscheck != null) { System.arraycopy(projectskillscheck, 0, skills, lengthTechnischeSkill, lengthProjectSkill); } List<Employee> list = sri.getEmployeesWithSkills(skills, region, businessUnit); search.addObject("employees", list); List<SkillType> skillTypes = st.getSkillTypes(); search.addObject("skillTypes", skillTypes); List<Skill> alleSkills = ski.getSkills(); search.addObject("alleSkills", alleSkills); List<Skill> projectskills = ski.getSkillWithProjectSkill(); search.addObject("projectskills", projectskills); List<Skill> technischeSkills = ski.getSkillWithTechnischeSkill(); search.addObject("technischeSkills", technischeSkills); return search; } @RequestMapping(value = "/projects/inviteMembers/{projectId}/{employeeId}", method = RequestMethod.POST) public View inviteMembers(@PathVariable int projectId, @PathVariable int employeeId, Model model, HttpServletRequest request) { return new RedirectView(request.getContextPath() + "/inviteMember/" + projectId + "/" + employeeId + "/" + request.getParameter("beschrijving")); } @RequestMapping(value = "inviteMember/{employeeId}/{projectId}/{beschrijving}", method = RequestMethod.GET) public ModelAndView inviteMember(@PathVariable int employeeId, @PathVariable int projectId, @PathVariable String beschrijving, Model model, HttpServletRequest request) { ModelAndView m = new ModelAndView("inviteMembers"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) > 1) { model.addAttribute("isAdmin", true); } List<Employee> employees = employeeService.getEmployees(); m.addObject("employees", employees); m.addObject("projectId", projectId); List<SkillType> skillTypes = st.getSkillTypes(); m.addObject("skillTypes", skillTypes); List<Skill> alleSkills = ski.getSkills(); m.addObject("alleSkills", alleSkills); m.addObject("checked", null); List<Skill> projectskills = ski.getSkillWithProjectSkill(); m.addObject("projectskills", projectskills); List<Skill> technischeSkills = ski.getSkillWithTechnischeSkill(); m.addObject("technischeSkills", technischeSkills); Preceeding pre = new Preceeding(); pre.setEmployee(employeeService.getEmployee(employeeId)); pre.setProject(projectService.getProject(projectId)); pre.setDescription(beschrijving); pre.setStatus(0); pre.setStartDate(new Date()); preceedingService.addPreceeding(pre); try { Employee emp = employeeService.getEmployee(employeeId); Mail mail = new Mail(); String message = "<p>Beste heer/mevrouw " + emp.getSurname() + " ,<br><br></p>"; message += "<p>U bent uitgenodigd om deel te nemen aan een project!</p>"; message += "<p>Klik op onderstaande link om uitnodiging te bekijken.<br>"; message += "<a href='http://localhost:8080/Skillmatcher/acceptInvitation/" + pre.getId() + "'>Link</a></p>"; message += "<p>Met vriendelijke groeten,<br><br>Skillmatcher</p>"; mail.sendMail(emp.getCompanyEmail(), "Uitnodiging project", message); model.addAttribute("successMessage", "Succesvol een medewerker uitgenodigd."); System.out.println("Mail sent to: " + emp.getCompanyEmail()); } catch (Exception e) { model.addAttribute("errorMessage", "Het is niet gelukt de medewerker succesvol uit te nodigen."); } model.addAttribute("projectId", projectId); model.addAttribute("employees", employees); return m; } @RequestMapping(value = "/acceptInvitation/{preceedingId}", method = RequestMethod.GET) public ModelAndView acceptInvitation(@PathVariable int preceedingId, Model model, HttpServletRequest request) { ModelAndView m = new ModelAndView("acceptInvitation"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) > 1) { model.addAttribute("isAdmin", true); } model.addAttribute("preceeding", preceedingService.getPreceeding(preceedingId)); return m; } @RequestMapping(value = "/responseOnInvite/{preceedingId}/{acceptStatus}", method = RequestMethod.GET) public ModelAndView responseOnInvite(@PathVariable int preceedingId, @PathVariable int acceptStatus, Model model, HttpServletRequest request) { System.out.println("PRECEEDING: " + preceedingId); ModelAndView m = new ModelAndView("dashboard"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) > 1) { model.addAttribute("isAdmin", true); } Preceeding pre = preceedingService.getPreceeding(preceedingId); if (acceptStatus == 0) { pre.setStatus(1); model.addAttribute("successMessage", "Je hebt de uitgenodiging succesvol geaccepteerd. De manager zal uiteindelijk beoordelen."); } else { pre.setStatus(3); model.addAttribute("successMessage", "Je hebt de uitgenodiging succesvol geweigerd."); } preceedingService.updatePreceeding(pre); m.addObject("employee", employeeService.getEmployee(util.getUserId(request))); return m; } @RequestMapping(value = "/projects/viewProject/{id}", method = RequestMethod.GET) public ModelAndView viewProject(HttpServletRequest request, @PathVariable int id) { ModelAndView mav = new ModelAndView("viewProject"); if (!util.isLoggedIn(request)) { return new ModelAndView("redirect:" + loginRedirect); } if (util.userAcces(request) > 1) { mav.addObject("isAdmin", true); } List<Preceeding> list = preceedingService.getPreceedingInProject(id); mav.addObject("employees", list); Project p = projectService.getProject(id); mav.addObject("project", p); mav.addObject("active", "projects"); return mav; } }