ash.resourcemanager.spring.controllers.AssignmentsController.java Source code

Java tutorial

Introduction

Here is the source code for ash.resourcemanager.spring.controllers.AssignmentsController.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 ash.resourcemanager.spring.controllers;

import ash.resourcemanager.hibernate.GenericDAO;
import ash.resourcemanager.hibernate.pojo.Assignment;
import ash.resourcemanager.hibernate.pojo.Employee;
import ash.resourcemanager.hibernate.pojo.Project;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Ash
 */
@Controller
@SessionAttributes
public class AssignmentsController {
    private GenericDAO<Assignment> _assignmentsDAO;
    private GenericDAO<Project> _projectsDAO;
    private GenericDAO<Employee> _employeesDAO;

    @RequestMapping(value = "assignments", method = RequestMethod.GET)

    public ModelAndView showAssignments() {
        List<Assignment> assignments = getAssignmentsDAO().getAll();
        ModelAndView modelAndView = new ModelAndView("assignments");

        modelAndView.addObject("assignments", assignments);

        return modelAndView;
    }

    @RequestMapping(value = "/assignmentForm")
    public ModelAndView showCustomerForm(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("assignmentForm");

        String method = request.getParameter("action");
        if (method.equals("add")) {
            modelAndView.addObject("command", new Assignment());
        } else if (method.equals("edit")) {
            String assignmentId = request.getParameter("id");
            Assignment assignment = getAssignmentsDAO().getById(Integer.parseInt(assignmentId));
            modelAndView.addObject("command", assignment);

        }
        modelAndView.addObject("employeesList", getEmployeesDAO().getAll());
        modelAndView.addObject("projectsList", getProjectsDAO().getAll());
        return modelAndView;
    }

    public GenericDAO<Assignment> getAssignmentsDAO() {
        if (_assignmentsDAO == null) {
            _assignmentsDAO = new GenericDAO<Assignment>(Assignment.class);
        }
        return _assignmentsDAO;
    }

    public GenericDAO<Project> getProjectsDAO() {
        if (_projectsDAO == null) {
            _projectsDAO = new GenericDAO<Project>(Project.class);
        }
        return _projectsDAO;
    }

    public GenericDAO<Employee> getEmployeesDAO() {
        if (_employeesDAO == null) {
            _employeesDAO = new GenericDAO<Employee>(Employee.class);
        }
        return _employeesDAO;
    }
}