package cn.com.qmth.stmms.common.domain;

import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.common.enums.UserType;
import cn.com.qmth.stmms.common.session.model.StmmsSession;

/**
 * 已登录学生用户抽象类，除基本状态信息外，还加上了学生用户类型
 * 
 * @author luoshi
 * 
 */
public class WebStudent extends WebUser {

    // 标识该学生用户是否已注册到User表中；否则为针对某次考试的临时用户
    private boolean registered;

    public WebStudent(Integer id, boolean registered) {
        super(id, UserType.STUDENT);
        this.registered = registered;
    }

    public static WebStudent buildFromSession(StmmsSession session) {
        WebStudent ws = null;
        WebUser wu = WebUser.buildFromSession(session);
        if (wu != null && wu.isType(UserType.STUDENT)) {
            String registered = StringUtils.trimToNull(session.getParameter("_web_student_type_"));
            ws = new WebStudent(wu.getId(), "1".equals(registered));
        }
        return ws;
    }

    public void writeToSession(StmmsSession session) {
        super.writeToSession(session);
        session.setParameter("_web_student_type_", registered ? "1" : "0");
    }

    public boolean isRegistered() {
        return registered;
    }

}
