public class SignInfoDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(SignInfoDAO.class);
	// property constants
	public static final String SIGN_DETAIL = "signDetail";
	public static final String SIGN_TIMES = "signTimes";
	public static final String LAST_SIGN_PHOTO = "lastSignPhoto";

	public void save(SignInfo transientInstance) {
		log.debug("saving SignInfo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	public void saveSingle(SignInfo transientInstance) {
		log.debug("saving SignInfo instance");
		Session session  = getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			if(tx != null){
				tx.rollback();
			}
			throw re;
		}
	}
	public boolean updateSingle(int id,String signDetail, int signTimes, String lastSignPhoto){
		Session session = getSession();
		session.beginTransaction();
		String hqlString="update SignInfo  set signDetail=:signDetail , signTimes=:signTimes , lastSignPhoto=:lastSignPhoto where signID=:id";
		Query query = session.createQuery(hqlString);
		query.setString("signDetail", signDetail);
		query.setInteger("signTimes", signTimes);
		query.setString("lastSignPhoto", lastSignPhoto);
		query.setInteger("id", id);
		try {
			query.executeUpdate();
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			log.debug("update signInfo fail!! ");
			e.printStackTrace();
			return false;
		}
		
	}

	public void delete(SignInfo persistentInstance) {
		log.debug("deleting SignInfo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SignInfo findById(java.lang.Integer id) {
		log.debug("getting SignInfo instance with id: " + id);
		try {
			SignInfo instance = (SignInfo) getSession().get(
					"com.hufeiya.DAO.SignInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SignInfo instance) {
		log.debug("finding SignInfo instance by example");
		try {
			List results = getSession()
					.createCriteria(SignInfo.class)
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SignInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from SignInfo as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findBySignDetail(Object signDetail) {
		return findByProperty(SIGN_DETAIL, signDetail);
	}

	public List findBySignTimes(Object signTimes) {
		return findByProperty(SIGN_TIMES, signTimes);
	}

	public List findByLastSignPhoto(Object lastSignPhoto) {
		return findByProperty(LAST_SIGN_PHOTO, lastSignPhoto);
	}

	public List findAll() {
		log.debug("finding all SignInfo instances");
		try {
			String queryString = "from SignInfo";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public SignInfo merge(SignInfo detachedInstance) {
		log.debug("merging SignInfo instance");
		try {
			SignInfo result = (SignInfo) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SignInfo instance) {
		log.debug("attaching dirty SignInfo instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SignInfo instance) {
		log.debug("attaching clean SignInfo instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
} public class StudentDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
	// property constants
	public static final String CLIENT_ID = "clientId";
	public static final String STUDENT_NAME = "studentName";
	public static final String STUDENT_NO = "studentNo";
	public static final String CLASS_NAME = "className";

	public void save(Student transientInstance) {
		log.debug("saving Student instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Student findById(java.lang.Integer id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student) getSession().get(
					"com.hufeiya.DAO.Student", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List results = getSession()
					.createCriteria(Student.class)
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Student instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Student as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByClientId(Object clientId) {
		return findByProperty(CLIENT_ID, clientId);
	}

	public List findByStudentName(Object studentName) {
		return findByProperty(STUDENT_NAME, studentName);
	}

	public List findByStudentNo(Object studentNo) {
		return findByProperty(STUDENT_NO, studentNo);
	}

	public List findByClassName(Object className) {
		return findByProperty(CLASS_NAME, className);
	}

	public List findAll() {
		log.debug("finding all Student instances");
		try {
			String queryString = "from Student";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Student result = (Student) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Student instance) {
		log.debug("attaching clean Student instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
} public class UserDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(UserDAO.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String PASS = "pass";
	public static final String USER_NO = "userNo";
	public static final String PHONE = "phone";
	public static final String USER_TYPE = "userType";

	public void save(User transientInstance) {
		log.debug("saving User instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(User persistentInstance) {
		log.debug("deleting User instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public User findById(java.lang.Integer id) {
		log.debug("getting User instance with id: " + id);
		try {
			User instance = (User) getSession().get(User.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(User instance) {
		log.debug("finding User instance by example");
		try {
			List results = getSession().createCriteria(User.class)
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding User instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from User as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByPass(Object pass) {
		return findByProperty(PASS, pass);
	}

	public List findByUserNo(Object userNo) {
		return findByProperty(USER_NO, userNo);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByUserType(Object userType) {
		return findByProperty(USER_TYPE, userType);
	}

	public List findAll() {
		log.debug("finding all User instances");
		try {
			String queryString = "from User";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public User merge(User detachedInstance) {
		log.debug("merging User instance");
		try {
			User result = (User) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(User instance) {
		log.debug("attaching dirty User instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(User instance) {
		log.debug("attaching clean User instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
} public class SignInfo implements java.io.Serializable {

	// Fields

	private Integer signId;
	private Student student;
	private Course course;
	private String signDetail;
	private Integer signTimes;
	private String lastSignPhoto;

	// Constructors

	/** default constructor */
	public SignInfo() {
	}

	/** minimal constructor */
	public SignInfo(Student student, Course course) {
		this.student = student;
		this.course = course;
	}

	/** full constructor */
	public SignInfo(Student student, Course course, String signDetail,
			Integer signTimes, String lastSignPhoto) {
		this.student = student;
		this.course = course;
		this.signDetail = signDetail;
		this.signTimes = signTimes;
		this.lastSignPhoto = lastSignPhoto;
	}
	/** constructor for update*/
	public SignInfo( String signDetail,
			Integer signTimes, String lastSignPhoto) {
		this.signDetail = signDetail;
		this.signTimes = signTimes;
		this.lastSignPhoto = lastSignPhoto;
	}

	// Property accessors

	public Integer getSignId() {
		return this.signId;
	}

	public void setSignId(Integer signId) {
		this.signId = signId;
	}

	public Student getStudent() {
		return this.student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Course getCourse() {
		return this.course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public String getSignDetail() {
		return this.signDetail;
	}

	public void setSignDetail(String signDetail) {
		this.signDetail = signDetail;
	}

	public Integer getSignTimes() {
		return this.signTimes;
	}

	public void setSignTimes(Integer signTimes) {
		this.signTimes = signTimes;
	}

	public String getLastSignPhoto() {
		return this.lastSignPhoto;
	}

	public void setLastSignPhoto(String lastSignPhoto) {
		this.lastSignPhoto = lastSignPhoto;
	}

} public class Student implements java.io.Serializable {

	// Fields

	private Integer sid;
	private StudentSheet studentSheet;
	private String clientId;
	private String studentName;
	private String studentNo;
	private String className;
	private Set signInfos = new HashSet(0);

	// Constructors

	/** default constructor */
	public Student() {
	}

	/** full constructor */
	public Student(StudentSheet studentSheet, String clientId,
			String studentName, String studentNo, String className,
			Set signInfos) {
		this.studentSheet = studentSheet;
		this.clientId = clientId;
		this.studentName = studentName;
		this.studentNo = studentNo;
		this.className = className;
		this.signInfos = signInfos;
	}

	// Property accessors

	public Integer getSid() {
		return this.sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public StudentSheet getStudentSheet() {
		return this.studentSheet;
	}

	public void setStudentSheet(StudentSheet studentSheet) {
		this.studentSheet = studentSheet;
	}

	public String getClientId() {
		return this.clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getStudentName() {
		return this.studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	public String getStudentNo() {
		return this.studentNo;
	}

	public void setStudentNo(String studentNo) {
		this.studentNo = studentNo;
	}

	public String getClassName() {
		return this.className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public Set getSignInfos() {
		return this.signInfos;
	}

	public void setSignInfos(Set signInfos) {
		this.signInfos = signInfos;
	}

} public class User implements java.io.Serializable {

	// Fields

	private Integer uid;
	private String username;
	private String pass;
	private String userNo;
	private String phone;
	private Boolean userType;
	private Set studentSheets = new HashSet(0);
	private Set courses = new HashSet(0);

	// Constructors

	/** default constructor */
	public User() {
	}

	/** full constructor */
	public User(String username, String pass, String userNo, String phone,
			Boolean userType, Set studentSheets, Set courses) {
		this.username = username;
		this.pass = pass;
		this.userNo = userNo;
		this.phone = phone;
		this.userType = userType;
		this.studentSheets = studentSheets;
		this.courses = courses;
	}
	
	/**Login constructor*/
	public User(String phone,String pass){
		this.phone = phone;
		this.pass = pass;
	}
	// Property accessors

	public Integer getUid() {
		return this.uid;
	}

	public void setUid(Integer uid) {
		this.uid = uid;
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPass() {
		return this.pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getUserNo() {
		return this.userNo;
	}

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}

	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Boolean getUserType() {
		return this.userType;
	}

	public void setUserType(Boolean userType) {
		this.userType = userType;
	}

	public Set getStudentSheets() {
		return this.studentSheets;
	}

	public void setStudentSheets(Set studentSheets) {
		this.studentSheets = studentSheets;
	}

	public Set getCourses() {
		return this.courses;
	}

	public void setCourses(Set courses) {
		this.courses = courses;
	}

} public class JsonCourse {

		// Fields

		private Integer cid;
		private Integer uid;
		private String teacherName;
		private String courseName;
		private String startDates;
		private Integer numberOfWeeks;
		private Map<Integer, JsonSignInfo> signInfos = new HashMap<>();

		// Constructors

		/** default constructor */
		public JsonCourse() {
		}

		/** full constructor */
		public JsonCourse(String courseName, String startDates,
				Integer numberOfWeeks, Map signInfos) {
			this.courseName = courseName;
			this.startDates = startDates;
			this.numberOfWeeks = numberOfWeeks;
			this.signInfos = signInfos;
		}
		
		public JsonCourse(Course course){
			this.cid = course.getCid();
			this.uid = course.getUser().getUid();
			this.teacherName = course.getUser().getUsername();
			this.courseName = course.getCourseName();
			this.startDates = course.getStartDates();
			this.numberOfWeeks = course.getNumberOfWeeks();
			Set<SignInfo> temp = course.getSignInfos();
			for(SignInfo signInfo:temp){
				JsonSignInfo jsonSignInfo = new JsonSignInfo(signInfo);
				this.signInfos.put(jsonSignInfo.getSignId(), jsonSignInfo);
			}
			
		}
		
		/**used for student user*/
		public JsonCourse(Course course,JsonSignInfo jsonSignInfo){
			this.cid = course.getCid();
			this.teacherName = course.getUser().getUsername();
			this.courseName = course.getCourseName();
			this.startDates = course.getStartDates();
			this.numberOfWeeks = course.getNumberOfWeeks();
			this.getSignInfos().put(jsonSignInfo.getSignId(), jsonSignInfo);
			
		}


		public Integer getCid() {
			return this.cid;
		}

		public void setCid(Integer cid) {
			this.cid = cid;
		}

		public String getCourseName() {
			return this.courseName;
		}

		public void setCourseName(String courseName) {
			this.courseName = courseName;
		}

		public String getStartDates() {
			return this.startDates;
		}

		public void setStartDates(String startDates) {
			this.startDates = startDates;
		}

		public Integer getNumberOfWeeks() {
			return this.numberOfWeeks;
		}

		public void setNumberOfWeeks(Integer numberOfWeeks) {
			this.numberOfWeeks = numberOfWeeks;
		}

		public Map<Integer, JsonSignInfo> getSignInfos() {
			return this.signInfos;
		}

		public void setSignInfos(Map<Integer, JsonSignInfo> signInfos) {
			this.signInfos = signInfos;
		}

		public Integer getUid() {
			return uid;
		}

		public void setUid(Integer uid) {
			this.uid = uid;
		}

		public String getTeacherName() {
			return teacherName;
		}

		public void setTeacherName(String teacherName) {
			this.teacherName = teacherName;
		}

	} public class JsonSignInfo {

	// Fields

	private Integer signId;
	private String studentName;
	private String studengNo;
	private String signDetail;
	private Integer signTimes;
	private String lastSignPhoto;

	// Constructors

	/** default constructor */
	public JsonSignInfo() {
	}


	/** full constructor */
	public JsonSignInfo(String studentName,String studentNo, String signDetail,
			Integer signTimes, String lastSignPhoto) {
		this.setStudentName(studentName);
		this.setStudengNo(studentNo);
		this.signDetail = signDetail;
		this.signTimes = signTimes;
		this.lastSignPhoto = lastSignPhoto;
	}
	
	public JsonSignInfo(SignInfo s) {
		this.setSignId(s.getSignId());
		this.setStudentName(s.getStudent().getStudentName());
		this.setStudengNo(s.getStudent().getStudentNo());
		this.signDetail = s.getSignDetail();
		this.signTimes = s.getSignTimes();
		this.lastSignPhoto = s.getLastSignPhoto();
	}
	
	// Property accessors

	public Integer getSignId() {
		return this.signId;
	}

	public void setSignId(Integer signId) {
		this.signId = signId;
	}

	public String getSignDetail() {
		return this.signDetail;
	}

	public void setSignDetail(String signDetail) {
		this.signDetail = signDetail;
	}

	public Integer getSignTimes() {
		return this.signTimes;
	}

	public void setSignTimes(Integer signTimes) {
		this.signTimes = signTimes;
	}

	public String getLastSignPhoto() {
		return this.lastSignPhoto;
	}

	public void setLastSignPhoto(String lastSignPhoto) {
		this.lastSignPhoto = lastSignPhoto;
	}


	public String getStudentName() {
		return studentName;
	}


	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}


	public String getStudengNo() {
		return studengNo;
	}


	public void setStudengNo(String studengNo) {
		this.studengNo = studengNo;
	}

} public class JsonUser {

	// Fields

	private Integer uid;
	private String username;
	private String pass;
	private String userNo;
	private String phone;
	private Boolean userType;
	private Map<Integer, JsonCourse>jsonCoursesMap = new HashMap<Integer, JsonCourse>();
	// Constructors

	/** default constructor */
	public JsonUser() {
	}

	/** full constructor */
	public JsonUser(String username, String pass, String userNo, String phone,
			Boolean userType) {
		this.username = username;
		this.pass = pass;
		this.userNo = userNo;
		this.phone = phone;
		this.userType = userType;
	}
	
	public JsonUser(User user){
		this.uid = user.getUid();
		this.username = user.getUsername();
		this.pass = user.getPass();
		this.userNo = user.getUserNo();
		this.phone = user.getPhone();
		this.userType = user.getUserType();
		Set<Course> tempCouses = user.getCourses();
		for(Course course:tempCouses){
			JsonCourse tempJsonCourse = new JsonCourse(course);
			this.jsonCoursesMap.put(tempJsonCourse.getCid(), tempJsonCourse);
		}
	}

	// Property accessors

	public Integer getUid() {
		return this.uid;
	}

	public void setUid(Integer uid) {
		this.uid = uid;
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPass() {
		return this.pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}


	public Map<Integer, JsonCourse> getJsonCoursesMap() {
		return jsonCoursesMap;
	}

	public void setJsonCoursesMap(Map<Integer, JsonCourse> jsonCoursesMap) {
		this.jsonCoursesMap = jsonCoursesMap;
	}

	public String getUserNo() {
		return this.userNo;
	}

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}

	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Boolean getUserType() {
		return this.userType;
	}

	public void setUserType(Boolean userType) {
		this.userType = userType;
	}

} public class AESUtils {
	static String IV = "AAAAAAAAAAAAAAAA";
	// If my users are more than 9000W.It  breaks.Hope it be true
	static final int PADDING = 10000000; 
	static String encryptionKey = "0123456789abcdef";
	static String STRINGPADDING = "\0\0\0\0\0\0\0\0";
	static final int INVALID_VALUE = -1;

	private static byte[] encrypt(String plainText, String encryptionKey)
			throws Exception {
		Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
		SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"),
				"AES");
		cipher.init(Cipher.ENCRYPT_MODE, key,
				new IvParameterSpec(IV.getBytes("UTF-8")));
		return cipher.doFinal(plainText.getBytes("UTF-8"));
	}

	private static String decrypt(byte[] cipherText, String encryptionKey)
			throws Exception {
		Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
		SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"),
				"AES");
		cipher.init(Cipher.DECRYPT_MODE, key,
				new IvParameterSpec(IV.getBytes("UTF-8")));
		return new String(cipher.doFinal(cipherText), "UTF-8");
	}

	public static String encrypt(int target) {
		target += PADDING;
		String targetString = Integer.toString(target);
		targetString += STRINGPADDING;
		try {
			return new String(Base64.encodeBase64(encrypt(targetString,
					encryptionKey)));

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static int decrypt(String encryptedString) {
		byte[] decodedBytes;
		decodedBytes = Base64.decodeBase64(encryptedString);
		try {
			String rawString = decrypt(decodedBytes, encryptionKey);
			rawString = rawString.substring(0, 8);
			return Integer.parseInt(rawString) - PADDING;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return INVALID_VALUE;
		} catch (Exception e) {
			e.printStackTrace();
			return INVALID_VALUE;
		}
	}

} public class CookieUtils {
	private static final Boolean IS_STUDENT = true;
	public static final int INVALID_VALUE = -1;
	public static void addCookieToRespose(HttpServletResponse response, int id) {
		Cookie cookie = new Cookie("uid", AESUtils.encrypt(id));
		System.out.println(cookie.getValue());//debug the cookie 
		response.addCookie(cookie);
	}

	public static  int getUidFromCookies(Cookie[] cookies) {
		int decryptedCookieValue = INVALID_VALUE;
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("uid")) {
					int tempDecrypted = AESUtils.decrypt(cookie.getValue());
					if (tempDecrypted != INVALID_VALUE) {
						decryptedCookieValue = tempDecrypted;
						break;
					}
				}
			}
		}
		return decryptedCookieValue;
	}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.hufeiya.DAO.SignInfoDAO;
import com.hufeiya.DAO.StudentDAO;
import com.hufeiya.DAO.UserDAO;
import com.hufeiya.entity.SignInfo;
import com.hufeiya.entity.Student;
import com.hufeiya.entity.User;
import com.hufeiya.jsonObject.JsonCourse;
import com.hufeiya.jsonObject.JsonSignInfo;
import com.hufeiya.jsonObject.JsonUser;
import com.hufeiya.utils.AESUtils;
import com.hufeiya.utils.CookieUtils;
import org.apache.tomcat.util.codec.binary.Base64;
package com.hufeiya.controller;





public class LoginServlet extends HttpServlet {
	private static final Boolean IS_STUDENT = true;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		res.setContentType("text/html;charset=UTF-8");
		res.setCharacterEncoding("UTF-8");
		PrintWriter out = res.getWriter();
		UserDAO userDAO = new UserDAO();
		User user = null;
		int uidFromCookies = CookieUtils.getUidFromCookies(req.getCookies());
		if (uidFromCookies != CookieUtils.INVALID_VALUE) {
			user = userDAO.findById(uidFromCookies);
			System.out.println("用户id:" + user.getUid());
		} else {
			String phone = req.getParameter("phone");
			String pass = req.getParameter("pass");
			if (phone == null || pass == null) {
				out.print("[\"false\"]");
				out.close();
				return;
			}
			User example = new User(phone, pass);
			List resultUserList = userDAO.findByExample(example);
			// cannot find the User with target password
			if (resultUserList == null || resultUserList.size() == 0) {
				out.print("[\"false\"]");
				out.close();
				return;
			}
			user = (User) resultUserList.get(0);
			CookieUtils.addCookieToRespose(res, user.getUid());
		}
		JsonUser jsonUser = userToJsonUser(user);
		final Gson gson = new Gson();
		final String json = gson.toJson(jsonUser);
		out.print("[\"true\"," + json + "]");
		out.close();
		
	}

	public JsonUser userToJsonUser(User user) {
		JsonUser jsonUser = new JsonUser(user);
		if (user.getUserType() == IS_STUDENT) {
			Student example = new Student();
			example.setStudentNo(user.getUserNo());
			List<Student> students = new StudentDAO().findByExample(example);
			// This student haven't joined a course
			if (students == null || students.size() == 0) {
				jsonUser.setJsonCoursesMap(null);
				return jsonUser;
			}
			Map<Integer,JsonCourse> jsonCourses = new HashMap<Integer, JsonCourse>();
			//Student student = students.get(0);
			for(Student student : students){
				List<SignInfo> signInfos = new SignInfoDAO().findByProperty(