com.controller.SkillController.java Source code

Java tutorial

Introduction

Here is the source code for com.controller.SkillController.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.editor.SkillTypeEditor;
import com.model.Skill;
import com.model.SkillType;
import com.service.SkillService;
import com.service.SkillTypeService;
import com.utility.Utility;
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 Dries
 */
@Controller
public class SkillController {

    @Autowired
    SkillService ski;
    @Autowired
    Utility util;
    @Autowired
    SkillTypeService skilltypeservice;
    @Autowired
    private SkillTypeEditor skillTypeEditor;
    private String loginRedirect = "http://localhost:8080/Skillmatcher/login";

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(SkillType.class, this.skillTypeEditor);
    }

    @RequestMapping(value = "/admin/skill_list", method = RequestMethod.GET)
    public ModelAndView listDoGet(HttpServletRequest request, Model model) {
        ModelAndView skillList = new ModelAndView("skill_List");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) < 2) {
            return new ModelAndView("403");
        }
        if (util.userAcces(request) > 1) {
            skillList.addObject("isAdmin", true);
        }
        skillList.addObject("active", "Administratie");
        skillList.addObject("skills", ski.getSkills());
        return skillList;
    }

    @RequestMapping(value = "/admin/skill_add", method = RequestMethod.GET)
    public ModelAndView skillAddPage(HttpServletRequest request) {
        ModelAndView addSkill = new ModelAndView("addSkill");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) < 2) {
            return new ModelAndView("403");
        }
        if (util.userAcces(request) > 1) {
            addSkill.addObject("isAdmin", true);
        }
        Skill skill = new Skill();
        addSkill.addObject("active", "Administratie");
        addSkill.addObject("skillTypes", skilltypeservice.getSkillTypes());
        addSkill.addObject("skill", skill);
        addSkill.addObject("submitValueButton", "Save");
        addSkill.addObject("skillSetTitle", "Skill add");
        return addSkill;
    }

    @RequestMapping(value = "/admin/skill_edit/{id}", method = RequestMethod.GET)
    public ModelAndView updateSkill(@PathVariable int id, HttpServletRequest request) {
        ModelAndView editSkill = new ModelAndView("editSkill");
        if (!util.isLoggedIn(request)) {
            return new ModelAndView("redirect:" + loginRedirect);
        }
        if (util.userAcces(request) < 2) {
            return new ModelAndView("403");
        }
        if (util.userAcces(request) > 1) {
            editSkill.addObject("isAdmin", true);
        }
        editSkill.addObject("skillTypes", skilltypeservice.getSkillTypes());
        Skill skill = ski.getSkill(id);
        editSkill.addObject("skill", skill);
        editSkill.addObject("submitValueButton", "Update");
        editSkill.addObject("skillSetTitle", "Skill update");
        editSkill.addObject("active", "Administratie");
        return editSkill;
    }

    @RequestMapping(value = "admin/skill_add/save", method = RequestMethod.POST)
    public View saveSkill(HttpServletRequest request, @ModelAttribute Skill skill) {
        skill.setStatus(1);
        ski.addSkill(skill);
        return new RedirectView(request.getContextPath() + "/admin/skill_list");
    }

    @RequestMapping(value = "admin/skill_edit/save", method = RequestMethod.POST)
    public View updateSkill(HttpServletRequest request, @ModelAttribute Skill skill) {
        skill.setStatus(1);
        ski.updateSkill(skill);
        return new RedirectView(request.getContextPath() + "/admin/skill_list");
    }
}