package com.decker.Essentials.User;

import java.net.HttpCookie;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Timestamp;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

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

import org.sipc.se.dao.factory.OperateFactory;

public class User
{
    private HttpServletRequest Requset;
    private HttpServletResponse Response;
    private ResultSet AboutUser;

    public User(HttpServletRequest request, HttpServletResponse response)
    {
	this.Requset = request;
	this.Response = response;
	if ((request.getAttribute("username") != null) && (request.getAttribute("password") != null))
	{
	    this.AboutUser = ReceiveDataFromUser((String) request.getAttribute("username"), ConvertMD5((String) request.getAttribute("password")));
	}

    }

    public UserType GetUserType()
    {
	try
	{
	    if (this.AboutUser == null )
		return UserType.Anonymous;
	    switch (this.AboutUser.getInt("InRole"))
	    {
	    case 0:
		return UserType.SystemAdmin;
	    case 1:
		return UserType.Teacher;
	    case 2:
		return UserType.Student;
	    case 3:
		return UserType.Anonymous;
	    default:
		return UserType.Anonymous;
	    }
	} catch (Exception e)
	{
	    e.printStackTrace();
	    return UserType.Anonymous;
	}

    }

    public Date GetLastLogin()
    {
	try
	{
	    if (this.AboutUser == null )
	    return new Date();
	    
	    return this.AboutUser.getDate("LastLoginDate");
	    
	} catch (SQLException e)
	{
	    e.printStackTrace();
	    return new Date();
	}
    }
    
    public static ResultSet ReceiveDataFromUser(String username, String MD5password)
    {
	String sqlString = String.format("SELECT * FROM Essentials.User WHERE UserID = '%s' and Password = '%s' ", username, MD5password);
	ResultSet resultSet;
	try
	{
	    resultSet = OperateFactory.getInstance().getDAOInstance().doQuery(sqlString);
	    if (!resultSet.first())
	    {
		return null;
	    } else
	    {
		return resultSet;
	    }
	} catch (Exception e)
	{
	    e.printStackTrace();
	}
	return null;
    }

    public static String ConvertMD5(String string)
    {
	try
	{
	    return new String(MessageDigest.getInstance("MD5").digest(string.getBytes()));
	} catch (NoSuchAlgorithmException e)
	{
	    e.printStackTrace();
	    return null;
	}
    }

}

@@ -768,16 +768,394 @@
 e;%0D%0A%09if 
+(request.getCookies().length != 0)%0D%0A%09%7B%0D%0A%09    for (Cookie cookie : request.getCookies())%0D%0A%09    %7B%0D%0A%09%09if (cookie.getName() == %22identification%22)%0D%0A%09%09%7B%0D%0A%09%09    String%5B%5D arg = cookie.getValue().split(%22%7C%7C%22);%0D%0A%09%09    try%0D%0A%09%09    %7B%0D%0A%09%09%09this.AboutUser = ReceiveDataFromUser(arg%5B0%5D, arg%5B1%5D);%0D%0A%09%09    %7D catch (Exception e)%0D%0A%09%09    %7B%0D%0A%09%09%09this.AboutUser = null;%0D%0A%09%09    %7D%0D%0A%0D%0A%09%09%7D%0D%0A%09    %7D%0D%0A%09%7D else if 
 ((reques
@@ -1380,24 +1380,26 @@
 sword%22)));%0D%0A
+%0D%0A
 %09%7D%0D%0A%0D%0A    %7D%0D
@@ -1421,17 +1421,17 @@
 serType 
-G
+g
 etUserTy
@@ -1480,25 +1480,24 @@
 User == null
- 
 )%0D%0A%09%09return 
@@ -1922,9 +1922,9 @@
 ate 
-G
+g
 etLa
@@ -1986,17 +1986,13 @@
 null
- 
 )%0D%0A%09
-    
+%09
 retu
@@ -2007,21 +2007,16 @@
 ate();%0D%0A
-%09    
 %0D%0A%09    r
@@ -2063,21 +2063,16 @@
 ate%22);%0D%0A
-%09    
 %0D%0A%09%7D cat
@@ -2159,20 +2159,16 @@
 %0A    %7D%0D%0A
-    
 %0D%0A    pu

Change method name to lower initial and add session indentification