⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 useronlinehibernatedao.java

📁 天乙社区6.0是一套基于JAVA技术的网络虚拟社区
💻 JAVA
字号:
package com.laoer.bbscs.dao.hibernate;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.laoer.bbscs.dao.IUserOnlineDAO;
import com.laoer.bbscs.bean.UserOnline;
import com.laoer.bbscs.sys.*;
import java.util.*;
import org.springframework.dao.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.*;
import org.springframework.orm.hibernate.HibernateCallback;
import java.sql.SQLException;

/**
 * <p>Title: TianYi BBS</p>
 * <p>Description: TianYi BBS System</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: LAOER.COM/TIANYISOFT.NET</p>
 * @author laoer
 * @version 6.0
 */

public class UserOnlineHibernateDAO
    extends HibernateDaoSupport implements IUserOnlineDAO {

  private static final Log logger = LogFactory.getLog(UserOnlineHibernateDAO.class);

  private static final String LOAD_BY_USERNAME =
      "from UserOnline uo where uo.userName = ?";
  private static final String LOADS_ONLINE =
      "from UserOnline uo where uo.onlineTime > ? and uo.isGuest = ?";
  private static final String LOADS_FRIEND =
      "from UserOnline uo where uo.userID in (:friend) and uo.onlineTime > :time and uo.isGuest = 0";
  private static final String DEL_OUTTIME =
      "from UserOnline uo where uo.onlineTime < ?";
  private static final String DEL_USERNAME =
      "from UserOnline uo where uo.userName = ?";

  public UserOnlineHibernateDAO() {
    super();
  }

  /**
   *
   * @param uo UserOnline
   * @return UserOnline
   * @todo Implement this com.laoer.bbscs.dao.IUserOnlineDAO method
   */
  public UserOnline saveUserOnline(UserOnline uo) {
    try {
      getHibernateTemplate().saveOrUpdate(uo);
      return uo;
    }
    catch (DataAccessException ex) {
      logger.error("saveUserOnline(UserOnline uo)" + ex);
      return null;
    }
  }

  /**
   *
   * @param userName String username
   * @return UserOnline
   * @todo Implement this com.laoer.bbscs.dao.IUserOnlineDAO method
   */
  public UserOnline findUserOnlineByUserName(String userName) {
    List l = getHibernateTemplate().find(LOAD_BY_USERNAME, userName);
    if (l == null || l.size() == 0) {
      return null;
    }
    else {
      return (UserOnline) l.get(0);
    }
  }

  /**
   *
   * @param isGuest int
   * @return List
   * @todo Implement this com.laoer.bbscs.dao.IUserOnlineDAO method
   */
  public List findUserOnlines(int isGuest) {
    long atime = SysUtil.getLongTime() - Constant.ONLINETIME;
    Object[] o = {
        new Long(atime), new Integer(isGuest)};
    try {
      return this.getHibernateTemplate().find(LOADS_ONLINE, o);
    }
    catch (DataAccessException ex) {
      logger.error(ex);
      return new ArrayList();
    }
  }

  /**
   *
   * @param isGuest int
   * @return int
   * @todo Implement this com.laoer.bbscs.dao.IUserOnlineDAO method
   */
  public int getUserOnlineCount(int isGuest) {
    long atime = SysUtil.getLongTime() - Constant.ONLINETIME;
    Object[] o = {
        new Long(atime), new Integer(isGuest)};
    try {
      List l = this.getHibernateTemplate().find("select count(*) " + LOADS_ONLINE, o);
      return ( (Integer) l.get(0)).intValue();
    }
    catch (DataAccessException ex) {
      logger.error(ex);
      return 0;
    }
  }

  /**
   *
   * @param values List
   * @return List
   * @todo Implement this com.laoer.bbscs.dao.IUserOnlineDAO method
   */
  public List findFriendUserOnline(final List values) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        long atime = SysUtil.getLongTime() - Constant.ONLINETIME;
        Query q = s.createQuery(LOADS_FRIEND);
        q.setParameterList("friend", values);
        q.setLong("time", atime);
        List list = q.list();
        return list;
      }
    });
  }

  /**
   *
   * @param values List
   * @return int
   * @todo Implement this com.laoer.bbscs.dao.IUserOnlineDAO method
   */
  public int getFriendUserOnlineCount(final List values) {
    return ( (Integer) (getHibernateTemplate().execute(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        long atime = SysUtil.getLongTime() - Constant.ONLINETIME;
        Query q = s.createQuery("select count(*) " + LOADS_FRIEND);
        q.setParameterList("friend", values);
        q.setLong("time", atime);
        List l = q.list();
        if (l != null && !l.isEmpty()) {
          return (Integer) l.get(0);
        }
        else {
          return new Integer(0);
        }
      }
    }))).intValue();
  }

  /**
   *
   * @todo Implement this com.laoer.bbscs.dao.IUserOnlineDAO method
   */
  public void removeUserOnline() {
    getHibernateTemplate().execute(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        long atime = SysUtil.getLongTime() - 15 * 60000;
        s.delete(DEL_OUTTIME, new Long(atime), Hibernate.LONG);
        return null;
      }
    });
  }

  public void removeUserOnline(final String userName) {
    getHibernateTemplate().execute(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        s.delete(DEL_USERNAME, userName, Hibernate.STRING);
        return null;
      }
    });
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -