package manager;
import java.sql.*;
import java.text.*;
import java.util.*;
import org.hibernate.*;
import model.*;
import java.util.Date;
public class UserInfoManager
{
public UserInfo userLogin(String username, String password)
{
Session session = HibernateUtil.getCurrentSession();
UserInfo user = null;
try
{
String hql = "from UserInfo u where u.username='"+username+"' and u.password='"+password+"'";
user = (UserInfo)session.createQuery(hql).uniqueResult();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
HibernateUtil.closeSession(session);
}
return user;
}
public UserInfo getOneUser(long uid)
{
Session session = HibernateUtil.getCurrentSession();
UserInfo user = null;
try
{
String hql = "from UserInfo u where u.id=" + uid;
user = (UserInfo)session.createQuery(hql).uniqueResult();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
HibernateUtil.closeSession(session);
}
return user;
}
public void updateUserInfo(long uid)
{
Session session = HibernateUtil.getCurrentSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
UserInfo user = getOneUser(uid);
user.setLogintime(getCurrentTime());
session.update(user, uid);
tx.commit();
}
catch(Exception ex)
{
if(tx!=null)
tx.rollback();
ex.printStackTrace();
}
finally
{
HibernateUtil.closeSession(session);
}
}
public List getUserList()
{
Session session = HibernateUtil.getCurrentSession();
List list = new ArrayList();
try
{
String hql = "from UserInfo";
list = session.createQuery(hql).list();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
HibernateUtil.closeSession(session);
}
return list;
}
public String getCurrentTime()
{
Calendar c = new GregorianCalendar();
StringBuffer sb = new StringBuffer();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
sb.append(year).append("-").append(month<10?("0"+month):month).append("-").append(day<10?("0"+day):day).append(" ");
sb.append(hour<10?("0"+hour):hour).append(":").append(minute<10?("0"+minute):minute).append(":").append(second<10?("0"+second):second);
return sb.toString();
}
public void delOnlineUser()
{
Session session = HibernateUtil.getCurrentSession();
Transaction tx = null;
List list = getUserList();
if(list!=null)
{
Iterator iter = list.iterator();
while(iter.hasNext())
{
tx = session.beginTransaction();
UserInfo user = (UserInfo)iter.next();
long userTime = getDateTime(user.getLogintime());
long currentTime = getDateTime(getCurrentTime());
if((currentTime-userTime)>1200000)
{
session.delete(user);
}
tx.commit();
}
}
}
public long getDateTime(String currentTime)
{
long time = 0;
try
{
SimpleDateFormat format = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
format.applyPattern("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(currentTime);
time = date.getTime();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return time;
}
public static void main(String[] args)throws Exception
{
new UserInfoManager().delOnlineUser();
}
}