EvaluationModel.java

package edu.rit.coopeval.evaluation;

import edu.rit.coopeval.authentication.beans.User;
import edu.rit.coopeval.model.EmployerQuestionAnswer;
import edu.rit.coopeval.model.Evaluation;
import edu.rit.coopeval.model.EvaluationApproval;
import edu.rit.coopeval.model.StudentQuestionAnswer;
import edu.rit.coopeval.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Date;

/**
 * EvaluationModel contains all of the business logic when communicating with the database
 *
 * Created by cteisd on 9/28/15.
 */
@Component
public class EvaluationModel {

    protected User currentUser;

    @Autowired(required = true)
    private EvaluationApprovalStatusRepository evalApprovalStatusRepo;

    @Autowired(required = true)
    private EvaluationApprovalRepository evalApprovalRepo;

    @Autowired(required = true)
    private EvaluationRepository evalRepo;

    @Autowired(required = true)
    private EvaluationStatusRepository evalStatusRepo;

    @Autowired(required = true)
    private StudentQuestionAnswerRepository studentRepo;

    @Autowired(required = true)
    private EmployerQuestionAnswerRepository employerRepo;

    /**
     * Approve the evaluation
     *
     * @param eval the evaluation that is going to be approved
     */
    public void approve(Evaluation eval){
        EvaluationApproval evaluationApproval = eval.getEvaluationApprovalByEvaluationId();
        evaluationApproval.setEvaluatorUid(currentUser.getUid());
        evaluationApproval.setApprovedBy(currentUser.getUserName());
        evaluationApproval.setLastUpdatedDate(new Timestamp(new Date().getTime()));
        evaluationApproval.setEvaluationApprovalStatusByEvaluationApprovalStatusId(evalApprovalStatusRepo.findByName("Approved"));
        evalApprovalRepo.save(evaluationApproval);
    }

    /**
     * Reject the evaluation
     *
     * @param eval the evaluation that is going to be rejected
     */
    public void reject(Evaluation eval){
        EvaluationApproval evaluationApproval = eval.getEvaluationApprovalByEvaluationId();
        evaluationApproval.setEvaluatorUid(currentUser.getUid());
        evaluationApproval.setLastUpdatedDate(new Timestamp(new Date().getTime()));
        evaluationApproval.setEvaluationApprovalStatusByEvaluationApprovalStatusId(evalApprovalStatusRepo.findByName("Rejected"));
        evalApprovalRepo.save(evaluationApproval);
        this.updateStatus(eval, EvaluationType.STUDENT, "Saved");
    }

    /**
     * Save the evaluation
     *
     * @param eval the evaluation to be saved
     * @param type the type of evaluation
     */
    public void save(Evaluation eval, EvaluationType type){
        this.updateAnswers(eval, type);
        this.updateStatus(eval, type, "Saved");
    }

    /**
     * Submit the evaluation
     *
     * @param eval the evaluation to be submitted
     * @param type the type of the evaluation
     */
    public void submit(Evaluation eval, EvaluationType type){
        this.updateAnswers(eval, type);
        this.updateStatus(eval, type, "Submitted");
    }

    /**
     * Update the answers for the evaluation
     *
     * @param eval the evaluation that is going to be updated
     * @param type the type of evaluation
     */
    public void updateAnswers(Evaluation eval, EvaluationType type){

        switch (type) {
            case STUDENT:
                for(StudentQuestionAnswer qa: eval.getStudentQuestionAnswersByEvaluationId()){
                    this.studentRepo.save(qa);
                }
                break;

            case EMPLOYER:
                for(EmployerQuestionAnswer qa: eval.getEmployerQuestionAnswersByEvaluationId()){
                    this.employerRepo.save(qa);
                }
                break;
        }
    }

    /**
     * Update the status of the evaluation
     *
     * @param eval the evaluation to be updated
     * @param type the type of the evaluation
     * @param status the status the evaluation should be updated too
     */
    public void updateStatus(Evaluation eval, EvaluationType type, String status){

        Date date = new Date();
        switch (type) {
            case STUDENT:
                eval.setEvaluationStatusByStudentEvaluationStatusId(evalStatusRepo.findByName(status));
                eval.setStudentLastUpdatedDate(new Timestamp(date.getTime()));
                break;

            case EMPLOYER:
                eval.setEvaluationStatusByEmployerEvaluationStatusId(evalStatusRepo.findByName(status));
                eval.setEmployerLastUpdatedDate(new Timestamp(date.getTime()));
                break;
        }
        evalRepo.save(eval);

    }

    public User getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }

}