package cn.com.qmth.stmms.admin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import cn.com.qmth.stmms.admin.utils.SessionExamUtils;
import cn.com.qmth.stmms.biz.exam.model.Exam;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.biz.user.model.User;
import cn.com.qmth.stmms.common.interceptor.CommonInterceptor;
import cn.com.qmth.stmms.common.utils.RequestUtils;

/**
 * Admin Exam模块操作拦截器
 * 
 * @author LS
 * 
 */
public class AdminExamInterceptor extends CommonInterceptor {

    @Autowired
    private ExamService examService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        int examId = SessionExamUtils.getExamId(request);
        if (examId > 0) {
            User user = RequestUtils.getWebUser(request).getUser();
            Exam exam = examService.findById(examId);
            if (exam == null || !exam.getSchoolId().equals(user.getSchoolId())) {
                response.sendRedirect(request.getContextPath() + "/admin/home");
                return false;
            }
            return true;
        } else {
            response.sendRedirect(request.getContextPath() + "/admin/exam-list");
            return false;
        }
    }

}
