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

import javax.servlet.http.HttpServletRequest;

import cn.com.qmth.stmms.biz.exam.model.Exam;
import cn.com.qmth.stmms.common.session.model.StmmsSession;
import cn.com.qmth.stmms.common.utils.RequestUtils;

public class SessionExamUtils {

    public static final String EXAM_ID_KEY = "examId";

    public static final String EXAM_NAME_KEY = "examName";

    public static final String FORBIDDEN_INFO_KEY = "forbiddenInfo";

    public static final String FORBIDDEN_SCORE_KEY = "forbiddenScore";

    public static int getExamId(HttpServletRequest request) {
        int examId = 0;
        try {
            examId = Integer.valueOf(RequestUtils.getSession(request).getParameter(EXAM_ID_KEY));
        } catch (Exception e) {
            // log.error("考试-基础参数设置-examId类型转换出错", e);
        }
        if (examId == 0) {
            try {
                examId = Integer.valueOf(request.getParameter(EXAM_ID_KEY));
            } catch (Exception e) {
                // log.error("考试-基础参数设置-examId类型转换出错", e);
            }
        }
        return examId;
    }

    public static void clearExamId(HttpServletRequest request) {
        StmmsSession session = RequestUtils.getSession(request);
        session.setParameter(EXAM_ID_KEY, null);
        session.setParameter(EXAM_NAME_KEY, null);
        session.setParameter(FORBIDDEN_INFO_KEY, null);
        session.setParameter(FORBIDDEN_SCORE_KEY, null);
        request.removeAttribute(EXAM_ID_KEY);
        request.removeAttribute(EXAM_NAME_KEY);
        request.removeAttribute(FORBIDDEN_INFO_KEY);
        request.removeAttribute(FORBIDDEN_SCORE_KEY);
    }

    public static void setExamId(HttpServletRequest request, Exam exam) {
        StmmsSession session = RequestUtils.getSession(request);
        session.setParameter(EXAM_ID_KEY, String.valueOf(exam.getId()));
        session.setParameter(FORBIDDEN_INFO_KEY, String.valueOf(exam.isForbiddenInfo()));
        session.setParameter(FORBIDDEN_SCORE_KEY, String.valueOf(exam.isForbiddenScore()));
    }
}
