CollegeController.java

package edu.rit.coopeval.controller;

import edu.rit.coopeval.authentication.beans.User;
import edu.rit.coopeval.model.College;
import edu.rit.coopeval.model.Department;
import edu.rit.coopeval.model.Term;
import edu.rit.coopeval.dao.*;
import edu.rit.coopeval.viewmodel.college.CollegeViewModel;
import edu.rit.coopeval.viewmodel.college.DepartmentViewModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by anusharma on 3/5/15.
 */

@RestController
@Scope("session")
@RequestMapping(value ="/services/CollegeService")
public class CollegeController {

    @Autowired
    private DepartmentRepository departmentRepo;
    @Autowired
    private CollegeRepository collegeRepo;
    @Autowired
    private DepartmentUserJoinRepository deptUserJoinRepo;
    @Autowired
    private EvaluationRepository evalRepo;
    @Autowired
    private TermRepository termRepo;

    @RequestMapping(value = "/addDepartment", method = RequestMethod.PUT)
    public DepartmentViewModel addDepartment(@RequestBody DepartmentViewModel deptModel, HttpSession session){
        User currentUser = (User) session.getAttribute("currentUser");
        Department dept = new Department();
        if(departmentRepo.getDepartment(deptModel.getDepartmentName()) == null) {
            dept.setDepartmentName(deptModel.getDepartmentName());
            dept.setDepartmentCode(deptModel.getDepartmentCode());
            dept.setCollegeByCollegeId(deptModel.getCollegeByCollegeId());
            dept = departmentRepo.save(dept);
        }
        return new DepartmentViewModel(dept);
    }

    @RequestMapping(value = "/deleteDepartment", method = RequestMethod.DELETE)
    public DepartmentViewModel removeDepartment(@RequestParam Long departmentId, HttpServletResponse res) throws IllegalArgumentException {
        //if department exists & there are no attached evaluations and department users
        HttpServletResponse response = res;
        DepartmentViewModel deptModel = new DepartmentViewModel();
        if(departmentRepo.findByDepartmentId(departmentId) != null && evalRepo.findByDepartmentId(departmentId) == null && deptUserJoinRepo.findDepartmentUserJoinByDepartmentId(departmentId).isEmpty()){
            deptModel.setCollegeByCollegeId(departmentRepo.findByDepartmentId(departmentId).getCollegeByCollegeId());
            deptModel.setDepartmentId(departmentId);
            deptModel.setDepartmentName(departmentRepo.findByDepartmentId(departmentId).getDepartmentName());
            deptModel.setDepartmentCode(departmentRepo.findByDepartmentId(departmentId).getDepartmentCode());
            departmentRepo.delete(departmentRepo.findByDepartmentId(departmentId));

        }else {
            throw new IllegalArgumentException("Invalid Delete Request");
        }
        return deptModel;
    }

    @RequestMapping(value = "/getAllDepartments", method = RequestMethod.GET)
    public List<DepartmentViewModel> getAllDepartments(@RequestParam String collegeId){
        List<DepartmentViewModel> allDepartments = new ArrayList<DepartmentViewModel>();
        for(Department department: departmentRepo.findDepartmentsByCollegeId(Long.parseLong(collegeId))){
            allDepartments.add(new DepartmentViewModel(department));
        }
        return allDepartments;
    }

    @RequestMapping(value = "/addCollege", method = RequestMethod.PUT)
    public CollegeViewModel addCollege(@RequestBody CollegeViewModel collegeModel, HttpSession session){
        User currentUser = (User) session.getAttribute("currentUser");
        College college = new College();
        if(collegeRepo.getCollege(collegeModel.getCollegeName()) == null) {
            college.setCollegeName(collegeModel.getCollegeName());
            college = collegeRepo.save(college);
        }
        return new CollegeViewModel(college);
    }
    @RequestMapping(value = "/deleteCollege", method = RequestMethod.DELETE)
    public CollegeViewModel removeCollege(@RequestParam Long collegeId, HttpServletResponse response) throws IllegalArgumentException{
        //if college exists & no attached departments
        CollegeViewModel collegeModel = new CollegeViewModel();
        if (collegeRepo.getCollegeById(collegeId) != null && departmentRepo.findDepartmentsByCollegeId(collegeId).isEmpty()) {
            collegeModel.setCollegeName(collegeRepo.getCollegeById(collegeId).getCollegeName());
            collegeModel.setCollegeId(collegeId);
            collegeRepo.delete(collegeRepo.getCollegeById(collegeId));
        }
        else{
            throw new IllegalArgumentException("Invalid Delete Request");
        }
        return collegeModel;
    }

    @RequestMapping(value = "/getAllColleges", method = RequestMethod.GET)
    public List<CollegeViewModel> getAllColleges(){
        List<CollegeViewModel> allColleges = new ArrayList<CollegeViewModel>();
        for(College college1: collegeRepo.findAll()){
            allColleges.add(new CollegeViewModel(college1));
        }
        return allColleges;
    }

    @RequestMapping(value = "/getFutureTerms", method = RequestMethod.GET)
    public List<String> getFutureTerms(){
        Term currentTerm = termRepo.findByIsCurrentTerm("1");
        List<String> futureTerms = new ArrayList<String>();
        for(Term futureTerm: termRepo.findGreaterThanTermCode(currentTerm.getTermCode())){
            futureTerms.add(futureTerm.getTermCode());
        }
        return futureTerms;
    }


    @ExceptionHandler({IllegalArgumentException.class})
    void handleBadRequests(HttpServletResponse response) throws IOException {
        response.sendError(HttpStatus.FORBIDDEN.value());
    }
}