package cn.hmsoft.skill.enrol.control;

import cn.hmsoft.application.web.AjaxControl;
import cn.hmsoft.exception.web.LoginTimeoutException;
import cn.hmsoft.skill.data.model.school.SchoolReg;
import cn.hmsoft.skill.data.model.std.StdReg;
import cn.hmsoft.skill.helper.SkillParamHelper;

public class EnrolAbstractControl extends AjaxControl {
	/*******************
	 * 获取系统操作员，并进行判断
	 */
	protected StdReg getStdReg() {
		return getStdReg(true);
	}

	/*******************
	 * 获取系统操作员，并根据参数选择是否进行判断
	 */
	protected StdReg getStdReg(boolean check) {
		StdReg std = null;
		try {
			std = (StdReg) this.getSession().getAttribute(SkillParamHelper.SessionName_StdReg);
		} catch (Exception e) {
			throw new LoginTimeoutException();
		}
		if (check && std == null)
			throw new LoginTimeoutException();
		return std;
	}

	@Override
	public Object validRole() {
		StdReg std = this.getStdReg(true);
		if (std == null)
			throw new LoginTimeoutException();
		if (!this.getRequest().getRequestURI().endsWith("enrol/std/info/init.htm")) {
			try {
				if (std.getStd_id() - Integer.parseInt(this.getRequest().getParameter("std_id")) != 0) {
					throw new LoginTimeoutException();
				}
			} catch (Exception e) {
				throw new LoginTimeoutException();
			}
		}
		return this.getStdReg(true);
	}

	public SchoolReg getSchoolReg() {
		try {
			return (SchoolReg) this.getSession().getAttribute(SkillParamHelper.SessionName_SchoolReg);
		} catch (Exception e) {
			e.printStackTrace();
			throw new LoginTimeoutException();
		}
	}

}
