com.controller.ProfilesController.java Source code

Java tutorial

Introduction

Here is the source code for com.controller.ProfilesController.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 com.controller;

import com.PDF.Resume;
import com.editor.EducationEditor;
import com.editor.SkillEditor;
import com.model.Certificate;
import com.model.Education;
import com.model.Employee;
import com.model.EmployeeHasEducation;
import com.model.Preceeding;
import com.model.Project;
import com.model.Skill;
import com.model.SkillRating;
import com.model.SkillType;
import com.service.CertificateService;
import com.service.EducationService;
import com.service.EmployeeService;
import com.service.PreceedingService;
import com.service.ProjectService;
import com.service.SkillRatingService;
import com.service.SkillService;
import com.service.SkillTypeService;
import com.utility.Utility;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
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 ProfilesController {

    @Autowired
    EmployeeService empService;

    @Autowired
    ProjectService projectService;

    @Autowired
    SkillService ski;

    @Autowired
    SkillTypeService st;

    @Autowired
    SkillRatingService sri;

    @Autowired
    CertificateService certService;

    @Autowired
    SkillService skillService;

    @Autowired
    PreceedingService proceedingService;

    @Autowired
    EducationService educationService;

    @Autowired
    EducationEditor educationEditor;

    @Autowired
    SkillEditor skillEditor;

    @Autowired
    Utility util;

    private String loginRedirect = "http://localhost:8080/Skillmatcher/login";

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Education.class, this.educationEditor);
        binder.registerCustomEditor(Skill.class, this.skillEditor);
    }

    @RequestMapping(value = "/profiles", method = RequestMethod.GET)
    public String index(HttpServletRequest request, Model model) {
        model.addAttribute("active", "profiles");
        if (!util.isLoggedIn(request)) {
            return "redirect:/login";
        }
        if (util.userAcces(request) > 1) {
            model.addAttribute("isAdmin", true);
        }
        List<Employee> employees = empService.getEmployees();
        model.addAttribute("employees", employees);

        List<SkillType> skillTypes = st.getSkillTypes();
        model.addAttribute("skillTypes", skillTypes);

        List<Skill> alleSkills = ski.getSkills();
        model.addAttribute("alleSkills", alleSkills);

        model.addAttribute("checked", null);

        List<Skill> projectskills = ski.getSkillWithProjectSkill();
        model.addAttribute("projectskills", projectskills);

        List<Skill> technischeSkills = ski.getSkillWithTechnischeSkill();
        model.addAttribute("technischeSkills", technischeSkills);

        return "profiles";
    }

    @RequestMapping(value = "/profiles", method = RequestMethod.POST)
    public String search(HttpServletRequest request, Model model) {
        model.addAttribute("active", "profiles");
        if (!util.isLoggedIn(request)) {
            return "redirect:" + loginRedirect;
        }
        if (util.userAcces(request) > 1) {
            model.addAttribute("isAdmin", true);
        }
        String[] technischeSkillscheck = request.getParameterValues("technischeSkills");
        model.addAttribute("technischeSkillschecked", technischeSkillscheck);

        String[] projectskillscheck = request.getParameterValues("projectskills");
        model.addAttribute("projectskillschecked", projectskillscheck);

        String region = request.getParameter("region");
        model.addAttribute("regionQueried", region);

        int businessUnit = Integer.parseInt(request.getParameter("businessUnit"));
        model.addAttribute("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);
        model.addAttribute("employees", list);

        List<SkillType> skillTypes = st.getSkillTypes();
        model.addAttribute("skillTypes", skillTypes);

        List<Skill> alleSkills = ski.getSkills();
        model.addAttribute("alleSkills", alleSkills);

        List<Skill> projectskills = ski.getSkillWithProjectSkill();
        model.addAttribute("projectskills", projectskills);

        List<Skill> technischeSkills = ski.getSkillWithTechnischeSkill();
        model.addAttribute("technischeSkills", technischeSkills);

        return "profiles";
    }

    @RequestMapping(value = "/profiles/createResume", method = RequestMethod.GET)
    public void createResume(HttpServletRequest request, HttpServletResponse response)
            throws ParseException, IOException, COSVisitorException {
        String imagePath = request.getContextPath() + "/resources/images/logo-infosupport.png";
        int id = (Integer) request.getSession().getAttribute("loggedIn");
        Employee emp = empService.getEmployee(id);
        List<SkillRating> skillList = sri.getSkillRatingUserCv(id);

        List<Preceeding> preceedings = new ArrayList();
        for (Preceeding p : proceedingService.getPreceedingInEmployeeCv(
                empService.getEmployee((Integer) request.getSession().getAttribute("loggedIn")))) {
            preceedings.add(p);
        }
        List<EmployeeHasEducation> educations = educationService.getEmployeeEducationsCv(id);
        ArrayList<Certificate> certificateList = (ArrayList<Certificate>) certService.getCertificateesCv(id);
        Resume resume = new Resume(preceedings, emp, (ArrayList<EmployeeHasEducation>) educations, certificateList,
                (ArrayList<SkillRating>) skillList, imagePath);
        ByteArrayOutputStream output = resume.createResume();

        // Force to download
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/force-download");
        responseHeaders.add("Content-Disposition", "attachment; filename=\"cv.pdf\"");
        response.getOutputStream().flush();
        response.getOutputStream().write(output.toByteArray());

    }

    @RequestMapping(value = "/profiles/createResume/{id}", method = RequestMethod.GET)
    public void createResumeView(@PathVariable int id, HttpServletRequest request, HttpServletResponse response)
            throws ParseException, IOException, COSVisitorException {
        String imagePath = request.getContextPath() + "//resources//images//logo-infosupport.png";
        Employee emp = empService.getEmployee(id);
        List<SkillRating> skillList = sri.getSkillRatingUserCv(id);
        System.out.println("size: " + skillList.size());
        List<Preceeding> preceedings = new ArrayList();
        for (Preceeding p : proceedingService.getPreceedingInEmployeeCv(empService.getEmployee(id))) {
            preceedings.add(p);
        }
        List<EmployeeHasEducation> educations = educationService.getEmployeeEducationsCv(id);
        ArrayList<Certificate> certificateList = (ArrayList<Certificate>) certService.getCertificateesCv(id);
        Resume resume = new Resume(preceedings, emp, (ArrayList<EmployeeHasEducation>) educations, certificateList,
                (ArrayList<SkillRating>) skillList, imagePath);
        ByteArrayOutputStream output = resume.createResume();

        // Force to download
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/force-download");
        responseHeaders.add("Content-Disposition", "attachment; filename=\"cv.pdf\"");
        response.getOutputStream().flush();
        response.getOutputStream().write(output.toByteArray());
        // ToDo: Open the pdf in a new tab

    }

    //    @RequestMapping(value = "/profiles/search", method = RequestMethod.GET)
    //    public String searchUser(@RequestParam String skillselector, HttpServletRequest request, Model model) {
    //        List<Employee> list = sri.getEmployeesWithSkill(Integer.parseInt(skillselector));
    //        model.addAttribute("skillsEmployees", list);
    //
    //        List<SkillType> specifiedSkillTypes = st.getSkillTypes();
    //        model.addAttribute("specifiedSkillTypes", specifiedSkillTypes);
    //        return "search";
    //    }
    @RequestMapping(value = "/profiles/edit", method = RequestMethod.GET)
    public String doGet(HttpServletRequest request, Model model) {
        model.addAttribute("active", "profilesEdit");
        if (!util.isLoggedIn(request)) {
            return "redirect:" + loginRedirect;
        }
        if (util.userAcces(request) > 1) {
            model.addAttribute("isAdmin", true);
        }
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        System.out.println(dateFormat.format(date));
        int profileId;
        try {
            profileId = (Integer) request.getSession().getAttribute("loggedIn");
        } catch (NullPointerException E) {
            model.addAttribute("errorLogin", "Sessie verlopen, log opnieuw in");
            return new String("redirect:" + loginRedirect);
        }
        Employee employee;
        HttpSession session = request.getSession();

        if ((request.getParameter("profileId")).equals(null)) {
            employee = empService.getEmployee((Integer) request.getSession().getAttribute("selectedUser"));
        } else {
            employee = empService.getEmployee(Integer.parseInt(request.getParameter("profileId")));
            session.setAttribute("selectedUser", Integer.parseInt(request.getParameter("profileId")));

        }

        //Employee employee = esi.getEmployee(Integer.parseInt(request.getParameter("profileId")));
        ArrayList<Skill> sk = (ArrayList) ski.getSkills();
        String dateString = dateFormat.format(date);
        List<SkillRating> skillRatings = sri
                .getSkillRatingUser(Integer.parseInt(request.getParameter("profileId")));
        model.addAttribute("empSkills", skillRatings);
        model.addAttribute("skills", sk);
        model.addAttribute("date", dateString);
        model.addAttribute("employee", employee);
        return "editProfiles";
    }

    @RequestMapping(value = "/profiles/view", method = RequestMethod.GET)
    public String viewProfile(HttpServletRequest request, Model model) {
        model.addAttribute("active", "profilesEdit");
        if (!util.isLoggedIn(request)) {
            return "redirect:" + loginRedirect;
        }
        if (util.userAcces(request) > 1) {
            model.addAttribute("isAdmin", true);
        } else {
            model.addAttribute("isAdmin", false);
        }
        if (util.userAcces(request) == 1) {
            model.addAttribute("isManager", true);
        } else {
            model.addAttribute("isManager", false);
        }

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        System.out.println(dateFormat.format(date));

        HttpSession session = request.getSession();

        Employee employee = empService.getEmployee(Integer.parseInt(request.getParameter("profileId")));
        ArrayList<Skill> sk = (ArrayList) ski.getSkills();
        String dateString = dateFormat.format(date);
        List<SkillRating> skillRatings = sri
                .getSkillRatingUser(Integer.parseInt(request.getParameter("profileId")));
        List<Project> projects = new ArrayList();
        List<Certificate> certificates = certService
                .getCertificatees((Integer) request.getSession().getAttribute("loggedIn"));
        model.addAttribute("certificates", certificates);
        for (Preceeding p : proceedingService.getPreceedingInEmployee(employee)) {
            projects.add(p.getProject());
        }
        model.addAttribute("empSkills", skillRatings);
        model.addAttribute("projects", projects);
        model.addAttribute("skills", sk);
        model.addAttribute("date", dateString);
        model.addAttribute("employee", employee);
        model.addAttribute("educations", educationService.getEmployeeEducations(employee.getId()));
        return "viewProfile";
    }

    //    @RequestMapping(value = "/profiles/edit/pers", method = RequestMethod.POST)
    //    public View personaliaUpdate(HttpServletRequest request, Model model) {
    //        Employee employee = empService.getEmployee((Integer) request.getSession().getAttribute("selectedUser"));
    //        employee.setSurname(request.getParameter("name"));
    //        employee.setCompanyEmail(request.getParameter("companyEmail"));
    //        employee.setCompanyPhone(request.getParameter("companyPhone"));
    //        employee.setPersonalEmail(request.getParameter("personalEmail"));
    //        employee.setPersonalPhone(request.getParameter("personalPhone"));
    //        employee.setCity(request.getParameter("stad"));
    //        String gender = request.getParameter("gender");
    //        employee.setGender(gender);
    //        System.out.println("employee " + employee.getGender());
    //        employee.setAddress(request.getParameter("adres"));
    //        employee.setZipCode(request.getParameter("postcode"));
    //        employee.setRegion(request.getParameter("regio"));
    //
    //        empService.updateEmployee(employee);
    //        return new RedirectView(request.getContextPath() + "/profiles/edit?profileId=" + (Integer) request.getSession().getAttribute("selectedUser"));
    //    }
    @RequestMapping(value = "/profiles/edit/skl", method = RequestMethod.POST)
    public View skillAdd(HttpServletRequest request, Model model) {
        if (util.userAcces(request) > 1) {
            model.addAttribute("isAdmin", true);
        }
        Employee employee = empService.getEmployee((Integer) request.getSession().getAttribute("loggedIn"));
        Date date = new Date();
        List<SkillRating> skills = employee.getSkills();
        Skill skil = ski.getSkill(Integer.parseInt(request.getParameter("skillrating")));
        SkillRating skr = new SkillRating();
        skr.setAdded(date);
        skr.setUpdated(date);
        skr.setEmployee(employee);
        skr.setRating(Integer.parseInt(request.getParameter("skillRatingNumber")));
        skr.setSkill(skil);
        skr.setExpYears(Integer.parseInt(request.getParameter("jarenErvaring")));
        skills.add(skr);
        employee.setSkills(skills);
        empService.updateEmployee(employee);
        sri.addSkillRating(skr);
        return new RedirectView(request.getContextPath() + "/profiles/edit/newSkill");
        //return new RedirectView(request.getContextPath()+"/profiles/edit?profileId="+Integer.parseInt(request.getParameter("profileId")));
    }

    @RequestMapping(value = "/profiles/edit/sklUpdate", method = RequestMethod.POST)
    public View skillRatingUpdate(HttpServletRequest request, Model model) {
        Skill skil = ski.getSkill(Integer.parseInt(request.getParameter("skillrateUpdate")));
        SkillRating skr = sri.getSkillRating(Integer.parseInt(request.getParameter("skillRatingId")));
        skr.setUpdated(new Date());
        skr.setRating(Integer.parseInt(request.getParameter("skillRatingNumber")));
        skr.setSkill(skil);
        sri.updateSkillRating(skr);
        //return new RedirectView(request.getContextPath()+"/profiles/edit?profileId="+Integer.parseInt(request.getParameter("profileId")));
        return new RedirectView(request.getContextPath() + "/profiles/edit/newSkill");
    }

    @RequestMapping(value = "profiles/requestskill", method = RequestMethod.GET)
    public ModelAndView requestSkill(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("requestskill");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }

        if (util.userAcces(request) > 1) {
            mav.addObject("isAdmin", true);
        }
        mav.addObject("active", "skillRequest");
        return mav;
    }

    @RequestMapping(value = "profiles/requestskill/new", method = RequestMethod.GET)
    public ModelAndView requestnewSkill(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("requestskill");
        if (!request.getParameter("naam").equals("")) {
            Skill skill = new Skill();
            skill.setName(request.getParameter("naam"));
            skill.setDescription(request.getParameter("opmerkingen"));
            skill.setStatus(0);
            skillService.addSkill(skill);
            mav.addObject("successMessage",
                    "De nieuwe skill in succesvol aangevraagd, de administrator zal hier naar kijken.");
        } else {
            mav.addObject("errorMessage", "Skill kon niet worden toegevoegd, vul aub alle velden in.");
        }
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }

        if (util.userAcces(request) > 1) {
            mav.addObject("isAdmin", true);
        }
        return mav;
    }

    /**
     * TEST TEST
     */
    @RequestMapping(value = "profiles/skills", method = RequestMethod.GET)
    public ModelAndView profileSkills(HttpServletRequest request) {
        ModelAndView model = new ModelAndView("skillList");

        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            model.addObject("isAdmin", true);
        }

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();

        Employee employee = empService.getEmployee(util.getUserId(request));

        ArrayList<Skill> sk = (ArrayList) ski.getSkills();
        String dateString = dateFormat.format(date);
        List<SkillRating> skillRatings = sri.getSkillRatingUser(util.getUserId(request));

        model.addObject("empSkills", skillRatings);
        model.addObject("skills", sk);
        model.addObject("date", dateString);
        model.addObject("employee", employee);
        model.addObject("active", "Vaardigheid");
        return model;
    }

    @RequestMapping("profiles/skills/new")
    public ModelAndView profileAddSkill(HttpServletRequest request) {
        ModelAndView addSkill = new ModelAndView("newSkillRating");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            addSkill.addObject("isAdmin", true);
        }
        addSkill.addObject("skillRating", new SkillRating());
        addSkill.addObject("skills", skillService.getSkills());
        addSkill.addObject("voegtoe", "toevoegen");
        addSkill.addObject("newOrUpdate", "new");
        addSkill.addObject("active", "Vaardigheid");
        return addSkill;
    }

    @RequestMapping("profiles/skills/update/{id}")
    public ModelAndView profileUpdateSkill(HttpServletRequest request, @PathVariable int id) {
        ModelAndView addSkill = new ModelAndView("newSkillRating");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            addSkill.addObject("isAdmin", true);
        }
        addSkill.addObject("skillRating", sri.getSkillRating(id));
        addSkill.addObject("skills", skillService.getSkills());
        addSkill.addObject("voegtoe", "aanpassen");
        addSkill.addObject("wijzigen", "Wijzigen");
        addSkill.addObject("newOrUpdate", ("update/" + id));
        addSkill.addObject("active", "Vaardigheid");
        return addSkill;
    }

    @RequestMapping(value = "profiles/skills/update/{id}", method = RequestMethod.POST)
    public ModelAndView profileUpdateSkillSave(HttpServletRequest request, @PathVariable int id,
            @ModelAttribute SkillRating skill) {
        ModelAndView addSkill = new ModelAndView("newSkillRating");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            addSkill.addObject("isAdmin", true);
        }
        addSkill.addObject("skillRating", sri.getSkillRating(id));
        addSkill.addObject("skills", skillService.getSkills());
        addSkill.addObject("voegtoe", "aanpassen");
        addSkill.addObject("newOrUpdate", ("update/" + id));
        addSkill.addObject("message", "Vaardigheid geupdate.");
        Employee employee = util.getEmployee(request);
        skill.setEmployee(employee);
        skill.setUpdated(new Date());
        skill.setAdded(sri.getSkillRating(id).getAdded());
        sri.updateSkillRating(skill);
        return addSkill;
    }

    @RequestMapping("profiles/skills/delete/{id}")
    public ModelAndView deleteSkillRating(HttpServletRequest request, @PathVariable int id) {
        ModelAndView overView = new ModelAndView("skillList");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            overView.addObject("isAdmin", true);
        }
        sri.deleteSkillRating(id);
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();

        Employee employee = empService.getEmployee(util.getUserId(request));

        ArrayList<Skill> sk = (ArrayList) ski.getSkills();
        String dateString = dateFormat.format(date);
        List<SkillRating> skillRatings = sri.getSkillRatingUser(util.getUserId(request));

        overView.addObject("empSkills", skillRatings);
        overView.addObject("skills", sk);
        overView.addObject("date", dateString);
        overView.addObject("employee", employee);
        overView.addObject("active", "Vaardigheid");
        return overView;
    }

    @RequestMapping(value = "profiles/skills/new", method = RequestMethod.POST)
    public ModelAndView profileAddedSkill(HttpServletRequest request, @ModelAttribute SkillRating skill) {
        ModelAndView addSkill = new ModelAndView("skillList");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            addSkill.addObject("isAdmin", true);
        }

        System.out.println(skill.getSkill().getName());
        Employee employee = util.getEmployee(request);

        skill.setEmployee(employee);
        skill.setAdded(new Date());
        skill.setUpdated(new Date());
        List<SkillRating> skills = employee.getSkills();

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();

        ArrayList<Skill> sk = (ArrayList) ski.getSkills();
        String dateString = dateFormat.format(date);
        List<SkillRating> skillRatings = sri.getSkillRatingUser(util.getUserId(request));

        for (SkillRating s : skills) {
            System.out.println(s.getSkill().getName());
            if (s.getSkill().getId() == skill.getSkill().getId()) {
                ModelAndView alreadyAdded = new ModelAndView("newSkillRating");
                System.out.println("you already have this skill it won't get added");
                alreadyAdded.addObject("empSkills", skillRatings);
                alreadyAdded.addObject("skills", sk);
                alreadyAdded.addObject("date", dateString);
                alreadyAdded.addObject("employee", employee);
                alreadyAdded.addObject("active", "Vaardigheid");
                alreadyAdded.addObject("message", "U heeft deze vaardigheid al");
                return alreadyAdded;
            }
        }
        try {
            sri.addSkillRating(skill);
            skillRatings = sri.getSkillRatingUser(util.getUserId(request));
            addSkill.addObject("empSkills", skillRatings);
            addSkill.addObject("skills", sk);
            addSkill.addObject("date", dateString);
            addSkill.addObject("employee", employee);
            addSkill.addObject("active", "Vaardigheid");
            addSkill.addObject("message", "Vaardigheid toegevoegd");
        } catch (Exception ex) {
            ex.printStackTrace();
            addSkill.addObject("message", "Er is iets fout gegaan");
            return new ModelAndView("newSkillRating");
        }
        return addSkill;
    }

    /**
     * TEST TEST
     */
    @RequestMapping(value = "profiles/edit/newSkill", method = RequestMethod.GET)
    public ModelAndView newSkillGet(HttpServletRequest request, Model model) {
        ModelAndView skillRating = new ModelAndView("editSkills");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }

        if (util.userAcces(request) > 1) {
            skillRating.addObject("isAdmin", true);
        }

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();

        System.out.println("profileId=" + util.getUserId(request));
        //        request.setAttribute("profileId", request.getParameter("profileId"));
        Employee employee = empService.getEmployee(util.getUserId(request));
        //        Employee employee = esi.getEmployee(Integer.parseInt(request.getAttribute("selectedUser").toString()));
        ArrayList<Skill> sk = (ArrayList) ski.getSkills();
        String dateString = dateFormat.format(date);
        List<SkillRating> skillRatings = sri.getSkillRatingUser(util.getUserId(request));
        skillRating.addObject("empSkills", skillRatings);
        skillRating.addObject("skills", sk);
        skillRating.addObject("date", dateString);
        skillRating.addObject("employee", employee);

        return skillRating;
    }

    @RequestMapping(value = "profiles/certificates/new", method = RequestMethod.GET)
    public ModelAndView newCertificate(Certificate certificate, HttpServletRequest request) {
        ModelAndView certificateList = new ModelAndView("newCertificate");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }

        if (util.userAcces(request) > 1) {
            certificateList.addObject("isAdmin", true);
        }
        certificateList.addObject("active", "newEducation");
        return certificateList;
    }

    @RequestMapping(value = "profiles/certificates", method = RequestMethod.GET)
    public ModelAndView newCertificate(HttpServletRequest request) {
        ModelAndView certificateList = new ModelAndView("certificateList");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }

        if (util.userAcces(request) > 1) {
            certificateList.addObject("isAdmin", true);
        }
        certificateList.addObject("active", "cerfiticateList");
        List<Certificate> certificates = certService
                .getCertificatees((Integer) request.getSession().getAttribute("loggedIn"));
        certificateList.addObject("certificates", certificates);
        return certificateList;
    }

    @RequestMapping(value = "profiles/certificates/delete/{id}")
    public ModelAndView removeCertificate(Model model, HttpServletRequest request, Certificate cert) {
        ModelAndView certificateList = new ModelAndView("certificateList");
        certService.deleteCertificate(cert.getId());
        if (util.userAcces(request) > 1) {
            certificateList.addObject("isAdmin", true);
        }
        List<Certificate> list = certService
                .getCertificatees((Integer) request.getSession().getAttribute("loggedIn"));
        certificateList.addObject("certificates", list);
        certificateList.addObject("certificateMessage", "Certificaat verwijderd.");
        return certificateList;
    }

    @RequestMapping(value = "profiles/certificates/add", method = RequestMethod.POST)
    public ModelAndView addCertificate(Certificate cert, HttpServletRequest request) throws ParseException {
        ModelAndView addCertificate = new ModelAndView("certificateList");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            addCertificate.addObject("isAdmin", true);
        }
        cert.setEmployee(empService.getEmployee((Integer) request.getSession().getAttribute("loggedIn")));
        certService.addCertificate(cert);
        List<Certificate> certificates = certService
                .getCertificatees((Integer) request.getSession().getAttribute("loggedIn"));
        addCertificate.addObject("certificates", certificates);
        addCertificate.addObject("certificateMessage", "Certificaat toegevoegd.");
        return addCertificate;
    }

    @RequestMapping(value = "profiles/certificates/addToCv/{id}", method = RequestMethod.POST)
    public ModelAndView addToCvCertificate(@ModelAttribute Certificate certificate, HttpServletRequest request) {
        ModelAndView EducationList = new ModelAndView("certificateList");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            EducationList.addObject("isAdmin", true);
        }
        certService.updateCertificateCv(certificate, true);
        List<Certificate> list = certService
                .getCertificatees((Integer) request.getSession().getAttribute("loggedIn"));
        EducationList.addObject("certificates", list);
        return EducationList;
    }

    @RequestMapping(value = "profiles/certificates/removeFromCv/{id}", method = RequestMethod.POST)
    public ModelAndView removeFromCvCertificate(@ModelAttribute Certificate certificate,
            HttpServletRequest request) {
        ModelAndView EducationList = new ModelAndView("certificateList");
        certService.updateCertificateCv(certificate, false);
        if (util.userAcces(request) > 1) {
            EducationList.addObject("isAdmin", true);
        }
        List<Certificate> list = certService
                .getCertificatees((Integer) request.getSession().getAttribute("loggedIn"));
        EducationList.addObject("certificates", list);
        return EducationList;
    }

    @RequestMapping(value = "profiles/certificates/edit/{id}", method = RequestMethod.GET)
    public ModelAndView editCertificate(@PathVariable int id, HttpServletRequest request) {
        ModelAndView editCertificate = new ModelAndView("editCertificate");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }

        if (util.userAcces(request) > 1) {
            editCertificate.addObject("isAdmin", true);
        }
        editCertificate.addObject("active", "editCertificate");
        Certificate certificate = certService.getCertificate(id);
        editCertificate.addObject("certificate", certificate);

        return editCertificate;
    }

    @RequestMapping(value = "profiles/certificates/edit", method = RequestMethod.POST)
    public ModelAndView editCertificatePost(Certificate certificate, HttpServletRequest request) {
        ModelAndView cerfiticateList = new ModelAndView("certificateList");
        cerfiticateList.addObject("active", "editCertificate");
        certService.updateCertificate(certificate);
        if (util.userAcces(request) > 1) {
            cerfiticateList.addObject("isAdmin", true);
        }
        List<Certificate> list = certService
                .getCertificatees((Integer) request.getSession().getAttribute("loggedIn"));
        cerfiticateList.addObject("certificates", list);
        cerfiticateList.addObject("certificateMessage", "Certificaat aangepast.");
        return cerfiticateList;
    }
    /*Education*/

    @RequestMapping(value = "profiles/educations", method = RequestMethod.GET)
    public ModelAndView educations(HttpServletRequest request) {
        ModelAndView educationList = new ModelAndView("educationList");
        educationList.addObject("active", "educationList");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }

        if (util.userAcces(request) > 1) {
            educationList.addObject("isAdmin", true);
        }
        List<EmployeeHasEducation> educations = educationService
                .getEmployeeEducations((Integer) request.getSession().getAttribute("loggedIn"));
        educationList.addObject("active", "educationList");
        educationList.addObject("educations", educations);
        return educationList;
    }

    @RequestMapping(value = "profiles/educations/addToCv/{id}", method = RequestMethod.POST)
    public ModelAndView addToCv(@ModelAttribute EmployeeHasEducation education, HttpServletRequest request) {
        ModelAndView EducationList = new ModelAndView("educationList");
        educationService.updateCvEducation(education, true);
        if (util.userAcces(request) > 1) {
            EducationList.addObject("isAdmin", true);
        }
        List<EmployeeHasEducation> list = educationService
                .getEmployeeEducations((Integer) request.getSession().getAttribute("loggedIn"));
        EducationList.addObject("educations", list);
        return EducationList;
    }

    @RequestMapping(value = "profiles/educations/removeFromCv/{id}", method = RequestMethod.POST)
    public ModelAndView removeFromCv(@ModelAttribute EmployeeHasEducation education, HttpServletRequest request) {
        ModelAndView EducationList = new ModelAndView("educationList");
        educationService.updateCvEducation(education, false);
        List<EmployeeHasEducation> list = educationService
                .getEmployeeEducations((Integer) request.getSession().getAttribute("loggedIn"));
        EducationList.addObject("educations", list);
        return EducationList;
    }

    @RequestMapping(value = "profiles/educations/new", method = RequestMethod.GET)
    public ModelAndView newEducation(EmployeeHasEducation education, HttpServletRequest request) {
        ModelAndView educationList = new ModelAndView("newEducation");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }

        if (util.userAcces(request) > 1) {
            educationList.addObject("isAdmin", true);
        }
        List<Education> list = educationService.getEducations();
        educationList.addObject("educations", list);
        educationList.addObject("active", "newEducation");
        return educationList;
    }

    @RequestMapping(value = "profiles/educations/delete/{id}")
    public ModelAndView removeEducation(HttpServletRequest request, EmployeeHasEducation education) {
        ModelAndView EducationList = new ModelAndView("educationList");
        educationService.deleteEducation(education.getId());
        if (util.userAcces(request) > 1) {
            EducationList.addObject("isAdmin", true);
        }
        List<EmployeeHasEducation> list = educationService
                .getEmployeeEducations((Integer) request.getSession().getAttribute("loggedIn"));
        EducationList.addObject("educations", list);
        return EducationList;
    }

    @RequestMapping(value = "profiles/educations/add", method = RequestMethod.POST)
    public ModelAndView addEducation(@ModelAttribute EmployeeHasEducation hasEducation, HttpServletRequest request)
            throws ParseException {
        ModelAndView addEducation = new ModelAndView("educationList");
        if (util.userAcces(request) > 1) {
            addEducation.addObject("isAdmin", true);
        }
        hasEducation.setEmployee(empService.getEmployee((Integer) request.getSession().getAttribute("loggedIn")));
        educationService.addEmployeeEducation(hasEducation);
        List<EmployeeHasEducation> educations = educationService
                .getEmployeeEducations((Integer) request.getSession().getAttribute("loggedIn"));
        addEducation.addObject("educations", educations);
        addEducation.addObject("educationMessage", "Diploma toegevoegd");
        return addEducation;
    }

    @RequestMapping(value = "profiles/educations/edit/{id}", method = RequestMethod.GET)
    public ModelAndView editEducation(@PathVariable int id, HttpServletRequest request) throws ParseException {

        EmployeeHasEducation education = educationService.getEmployeeEducation(id);
        ModelAndView addEducation = new ModelAndView("editEducation");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            addEducation.addObject("isAdmin", true);
        }
        List<Education> list = educationService.getEducations();
        addEducation.addObject("educations", list);
        addEducation.addObject("employeeHasEducation", education);
        addEducation.addObject("editEducation", "active");
        return addEducation;
    }

    @RequestMapping(value = "profiles/educations/edit", method = RequestMethod.POST)
    public ModelAndView editEducationPost(EmployeeHasEducation education, HttpServletRequest request) {
        ModelAndView addEducation = new ModelAndView("educationList");
        educationService.updateEducation(education);
        if (util.userAcces(request) > 1) {
            addEducation.addObject("isAdmin", true);
        }
        List<EmployeeHasEducation> educations = educationService
                .getEmployeeEducations((Integer) request.getSession().getAttribute("loggedIn"));
        addEducation.addObject("educations", educations);
        addEducation.addObject("educationMessage", "Diploma gewijzigd");
        return addEducation;
    }

    @RequestMapping(value = "profiles/educations/newEducation", method = RequestMethod.GET)
    public ModelAndView requestNewEducation(HttpServletRequest request, @ModelAttribute Education Education) {
        ModelAndView newEducation = new ModelAndView("requestNewEducation");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            newEducation.addObject("isAdmin", true);
        }
        newEducation.addObject("active", "requestNewEducation");
        return newEducation;

    }

    @RequestMapping(value = "profiles/education/newEducation/requestNew", method = RequestMethod.POST)
    public ModelAndView requestEducation(@ModelAttribute Education education, HttpServletRequest request) {
        ModelAndView addEducation = new ModelAndView("educationList");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            addEducation.addObject("isAdmin", true);
        }
        educationService.addEducation(education);
        List<EmployeeHasEducation> educations = educationService
                .getEmployeeEducations((Integer) request.getSession().getAttribute("loggedIn"));
        addEducation.addObject("educations", educations);
        addEducation.addObject("educationMessage", "Verzoek verstuurd");
        return addEducation;
    }

    /*end Education*/
    /* Projects */
    @RequestMapping(value = "profiles/projects")
    public ModelAndView projectList(HttpServletRequest request) {
        ModelAndView projectList = new ModelAndView("projectList");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            projectList.addObject("isAdmin", true);
        }
        List<Preceeding> preceedings = new ArrayList();
        for (Preceeding p : proceedingService.getPreceedingInEmployee(
                empService.getEmployee((Integer) request.getSession().getAttribute("loggedIn")))) {
            preceedings.add(p);
        }
        projectList.addObject("preceedings", preceedings);
        return projectList;
    }

    @RequestMapping(value = "profiles/projects/addToCv/{id}", method = RequestMethod.POST)
    public ModelAndView addToCvProject(@ModelAttribute Preceeding preceeding, HttpServletRequest request) {
        proceedingService.setCvStatus(preceeding.getId(), true);
        ModelAndView projectList = new ModelAndView("projectList");
        List<Preceeding> preceedings = new ArrayList();
        for (Preceeding p : proceedingService.getPreceedingInEmployee(
                empService.getEmployee((Integer) request.getSession().getAttribute("loggedIn")))) {
            preceedings.add(p);
        }
        if (util.userAcces(request) > 1) {
            projectList.addObject("isAdmin", true);
        }
        projectList.addObject("preceedings", preceedings);

        return projectList;
    }

    @RequestMapping(value = "profiles/projects/removeFromCv/{id}", method = RequestMethod.POST)
    public ModelAndView removeFromCvProject(@ModelAttribute Preceeding preceeding, HttpServletRequest request) {
        ModelAndView projectList = new ModelAndView("projectList");
        proceedingService.setCvStatus(preceeding.getId(), false);
        List<Preceeding> preceedings = new ArrayList();
        for (Preceeding p : proceedingService.getPreceedingInEmployee(
                empService.getEmployee((Integer) request.getSession().getAttribute("loggedIn")))) {
            preceedings.add(p);
        }
        if (util.userAcces(request) > 1) {
            projectList.addObject("isAdmin", true);
        }
        projectList.addObject("preceedings", preceedings);

        return projectList;
    }

    /* EndProjects */
    @RequestMapping(value = "profiles/edit/newSkill", method = RequestMethod.POST)
    public ModelAndView newSkill(HttpServletRequest request, Model model) {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        int profileId;
        try {
            profileId = (Integer) request.getSession().getAttribute("loggedIn");
        } catch (NullPointerException E) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            model.addAttribute("isAdmin", true);
        }
        //request.setAttribute("profileId", request.getParameter("profileId"));
        Employee employee = empService.getEmployee((Integer) request.getSession().getAttribute("loggedIn"));
        ArrayList<Skill> sk = (ArrayList) ski.getSkills();
        String dateString = dateFormat.format(date);
        List<SkillRating> skillRatings = sri
                .getSkillRatingUser((Integer) request.getSession().getAttribute("loggedIn"));
        model.addAttribute("empSkills", skillRatings);
        model.addAttribute("skills", sk);
        model.addAttribute("date", dateString);
        model.addAttribute("employee", employee);
        return new ModelAndView("editSkills");
    }

    @RequestMapping(value = "profiles/skills/addToCv/{id}", method = RequestMethod.GET)
    public ModelAndView addToCvSkill(@ModelAttribute SkillRating skill, HttpServletRequest request) {
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }

        ModelAndView skillList = new ModelAndView("skillList");
        if (util.userAcces(request) > 1) {
            skillList.addObject("isAdmin", true);
        }
        sri.updateSkillRatingCv(skill, true);
        ArrayList<Skill> sk = (ArrayList) ski.getSkills();
        List<SkillRating> skillRatings = sri
                .getSkillRatingUser((Integer) request.getSession().getAttribute("loggedIn"));
        skillList.addObject("skills", sk);
        skillList.addObject("empSkills", skillRatings);
        return skillList;
    }

    @RequestMapping(value = "profiles/skills/removeFromCv/{id}", method = RequestMethod.GET)
    public ModelAndView removeFromCvSkill(@ModelAttribute SkillRating skill, HttpServletRequest request) {
        ModelAndView skillList = new ModelAndView("skillList");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) > 1) {
            skillList.addObject("isAdmin", true);
        }
        sri.updateSkillRatingCv(skill, false);
        ArrayList<Skill> sk = (ArrayList) ski.getSkills();
        List<SkillRating> skillRatings = sri
                .getSkillRatingUser((Integer) request.getSession().getAttribute("loggedIn"));

        skillList.addObject("skills", sk);
        skillList.addObject("empSkills", skillRatings);
        return skillList;
    }

    @RequestMapping(value = "/database/vul", method = RequestMethod.GET)
    public View databaseFiller(HttpServletRequest request) {
        fillEmployees();
        addSkills();
        addProjects();
        return new RedirectView(request.getContextPath());
    }

    private void fillEmployees() {
        List<Employee> employees = new LinkedList();

        employees.add(new Employee("Dries Meerman", "020345348", "0204302589", "dries.meerman@hva.nl",
                "driesmeerman@hotmail.com", "cornelis trooststraat 29", "1072JA", "Amsterdam", "Noord-Holland",
                "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Jeffrey Veer", "23452345", "2345234", "jeffrey.Veer@hva.nl",
                "jeffreyveer@hotmail.com", "Duivendrechtsekade 29", "1025AA", "Amsterdam", "Noord-Holland", "Man",
                "testgezicht.jpg", true, true));
        employees.add(new Employee("Guus Portegies", "23452345", "2345234", "Guus.Portegies@hva.nl",
                "GuusPortegies@hotmail.com", "Duivendrechtsekade 29", "1025AA", "Amsterdam", "Noord-Holland", "Man",
                "testgezicht.jpg", true, true));
        employees.add(new Employee("Angela van der Veer", "23452345", "2345234", "angela.van.der.veer@hva.nl",
                "angelaveer@hotmail.com", "Duivendrechtsekade 29", "1025AA", "Amsterdam", "Noord-Holland", "Man",
                "testgezicht.jpg", true, true));
        employees.add(new Employee("Jason Mansoor", "23452345", "2345234", "jason.mansoor@hva.nl",
                "jasonmansoor@hotmail.com", "Duivendrechtsekade 29", "1025AA", "Amsterdam", "Noord-Holland", "Man",
                "testgezicht.jpg", true, true));
        employees.add(new Employee("Alex Zijlstra", "23452345", "2345234", "alex.zijlstra@hva.nl",
                "alexzijlstra@hotmail.com", "Duivendrechtsekade 29", "1025AA", "Amsterdam", "Noord-Holland", "Man",
                "testgezicht.jpg", true, true));
        employees.add(new Employee("Guido Schmidt", "23452345", "2345234", "Guido.schmidt@hva.nl",
                "Guidoschmidt@hotmail.com", "Duivendrechtsekade 29", "1025AA", "Amsterdam", "Noord-Holland", "Man",
                "testgezicht.jpg", true, true));

        employees.add(new Employee("Jan van Dijk", "23452345", "2345234", null, null, "kalverstraat 29", "1082KL",
                "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Piet van Dijk", "23452345", "2345234", null, null, "kalverstraat 29", "1082KL",
                "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Jan de Graaf", "23452345", "2345234", null, null, "kalverstraat 29", "1082KL",
                "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Karel Visser", "23452345", "2345234", null, null, "kalverstraat 29", "1082KL",
                "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Henk Bakker", "23452345", "2345234", null, null, "kalverstraat 29", "1082KL",
                "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Kevin Uithof", "23452345", "2345234", null, null, "kalverstraat 29", "1082KL",
                "Amsterdam", "Noord-Holland", "Vrouw", "testgezicht.jpg", true, true));
        employees.add(new Employee("Sara Visser", "23452345", "2345234", null, null, "kalverstraat 29", "1082KL",
                "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Juliet Molenhof", "23452345", "2345234", null, null, "kalverstraat 29",
                "1082KL", "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Alicia Achterhof", "23452345", "2345234", null, null, "kalverstraat 29",
                "1082KL", "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Dafne Leeuw", "23452345", "2345234", null, null, "kalverstraat 29", "1082KL",
                "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Francien Jansen", "23452345", "2345234", null, null, "kalverstraat 29",
                "1082KL", "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));
        employees.add(new Employee("Lindsey Overdijk", "23452345", "2345234", null, null, "kalverstraat 29",
                "1082KL", "Amsterdam", "Noord-Holland", "Man", "testgezicht.jpg", true, true));

        for (Employee employee : employees) {
            empService.addEmployee(employee);
        }
        System.out.println("added Employees");
    }

    private void addSkills() {
        List<Skill> skills = new LinkedList();
        skills.add(new Skill("Java", "Oracle programming language", "software"));
        skills.add(new Skill("c#", "Microsoft programming language", "software"));
        skills.add(new Skill("Swift", "Apple programming language", "software"));
        skills.add(new Skill("Python", "scripting language", "software"));

        for (Skill s : skills) {
            ski.addSkill(s);
        }
        System.out.println("added skills");

    }

    private void addProjects() {
        List<Project> projects = new LinkedList();
        //        projects.add(new Project("infosupport app", "zorgen dat mensen elkaar kunnen vinden", "05-05-05", "05-05-07", "infosupport", null));
        //        projects.add(new Project("ing app", "bankierapp", "05-04-05", "05-04-07", "ing", null));
        //        projects.add(new Project("AH bestel snel", "online bestel app", "02-04-05", "08-04-14", "alberthein", null));
        for (Project p : projects) {
            projectService.addProject(p);
        }
    }
}