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

📄 forumhibernatedao.java

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

import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.laoer.bbscs.dao.IForumDAO;
import com.laoer.bbscs.bean.Forum;
import com.laoer.bbscs.sys.*;
import java.util.*;
import org.springframework.dao.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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 ForumHibernateDAO
    extends HibernateDaoSupport implements IForumDAO {

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

  public ForumHibernateDAO() {
    super();
  }

  /**
   * saveForum
   *
   * @param forum Forum
   * @return Forum
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public Forum saveForum(Forum forum) {
    try {
      this.getHibernateTemplate().saveOrUpdate(forum);
      return forum;
    }
    catch (DataAccessException ex) {
      logger.error("saveForum(Forum forum):" + ex);
      return null;
    }
  }

  /**
   * getForum
   *
   * @param id long
   * @param bid long
   * @return Forum
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public Forum getForum(final long id, final long bid) {
    return (Forum) getHibernateTemplate().execute(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String className = SysUtil.getForumClassName(bid);
        try {
          return s.get(Class.forName(className), new Long(id));
        }
        catch (ClassNotFoundException ex) {
          logger.error(ex);
          return null;
        }
        catch (HibernateException ex) {
          logger.error(ex);
          return null;
        }
      }
    });
  }

  /**
   * getForumMainNum
   *
   * @param bid long
   * @return int
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public int getForumMainNum(long bid) {
    String q = "select count(forum.id) from Forum" + SysUtil.getForumTableID(bid) +
        " forum where forum.boardID = ? and forum.isNew = 1 and forum.delSign = 0 and forum.auditing = 0";
    try {
      List l = getHibernateTemplate().find(q, new Long(bid));
      if (l != null && !l.isEmpty()) {
        return ( (Integer) l.get(0)).intValue();
      }
      else {
        return 0;
      }
    }
    catch (DataAccessException ex) {
      logger.error(ex);
      return 0;
    }
  }

  /**
   * getForumAllNum
   *
   * @param bid long
   * @param delSign short
   * @return int
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public int getForumAllNum(long bid, short delSign, short auditing) {
    String q = "select count(forum.id) from Forum" + SysUtil.getForumTableID(bid) +
        " forum where forum.boardID = ? and forum.delSign = ? and forum.auditing = ?";
    Object[] o = {
        new Long(bid), new Short(delSign), new Short(auditing)};
    try {
      List l = this.getHibernateTemplate().find(q, o);
      if (l != null && !l.isEmpty()) {
        return ( (Integer) l.get(0)).intValue();
      }
      else {
        return 0;
      }
    }
    catch (DataAccessException ex) {
      logger.error(ex);
      return 0;
    }
  }

  /**
   * getForumTopicNum
   *
   * @param bid long
   * @param ID2 long
   * @return int
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public int getForumTopicNum(long bid, long ID2) {
    String q = "select count(forum.id) from Forum" + SysUtil.getForumTableID(bid) +
        " forum where forum.id2 = ? and forum.boardID = ? and forum.delSign = 0 and forum.auditing = 0";
    Object[] o = {
        new Long(ID2), new Long(bid)};
    try {
      List l = this.getHibernateTemplate().find(q, o);
      if (l != null && !l.isEmpty()) {
        return ( (Integer) l.get(0)).intValue();
      }
      else {
        return 0;
      }
    }
    catch (DataAccessException ex) {
      logger.error(ex);
      return 0;
    }
  }

  public int getForumDelAllNum(long bid) {
    String q = "select count(forum.id) from Forum" + SysUtil.getForumTableID(bid) +
        " forum where forum.boardID = ? and forum.delSign = 1";
    try {
      List l = this.getHibernateTemplate().find(q, new Long(bid));
      if (l != null && !l.isEmpty()) {
        return ( (Integer) l.get(0)).intValue();
      }
      else {
        return 0;
      }
    }
    catch (DataAccessException ex) {
      logger.error(ex);
      return 0;
    }

  }

  /**
   * getForumMainList
   *
   * @param bid long
   * @return PageList
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public List getForumMainList(final long bid, final int firstResult, final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String q = "from Forum" + SysUtil.getForumTableID(bid) +
            " forum where forum.boardID = ? and forum.isNew = 1 and forum.delSign = 0 and forum.auditing = 0 order by forum.isTop desc,forum.lastTime desc";
        Query query = s.createQuery(q);
        query.setLong(0, bid);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        List list = query.list();
        return list;

      }
    });
  }

  /**
   * getForumAllList
   *
   * @param bid long
   * @param delSign short
   * @return PageList
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public List getForumAllList(final long bid, final short delSign, final short auditing,
                              final int firstResult, final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String q = "from Forum" + SysUtil.getForumTableID(bid) +
            " forum where forum.boardID = ? and forum.delSign = ? and forum.auditing = ? order by forum.id desc";
        Query query = s.createQuery(q);
        query.setLong(0, bid);
        query.setShort(1, delSign);
        query.setShort(2, auditing);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);

        List list = query.list();
        return list;
      }
    });

  }

  /**
   * getForumTopicList
   *
   * @param bid long
   * @param ID2 long
   * @return PageList
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public List getForumTopicList(final long bid, final long ID2, final int firstResult,
                                final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String q = "from Forum" + SysUtil.getForumTableID(bid) +
            " forum where forum.id2 = ? and forum.boardID = ? and forum.delSign = 0 and forum.auditing = 0 order by forum.id";

        Query query = s.createQuery(q);
        query.setLong(0, ID2);
        query.setLong(1, bid);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);

        List list = query.list();
        return list;
      }
    });

  }

  public List getForumTopicList(final long bid, final long ID2, final short delSign,
                                final short auditing) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String q = "from Forum" + SysUtil.getForumTableID(bid) +
            " forum where forum.id2 = ? and forum.boardID = ? and forum.delSign = ? and forum.auditing = ? order by forum.id";

        Query query = s.createQuery(q);
        query.setLong(0, ID2);
        query.setLong(1, bid);
        query.setShort(2, delSign);
        query.setShort(3, auditing);

        List list = query.list();
        return list;
      }
    });

  }

  public List getForumDelTopicList(final long bid, final long ID2) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String q = "from Forum" + SysUtil.getForumTableID(bid) +
            " forum where forum.id2 = ? and forum.boardID = ? and forum.delSign = ? order by forum.id";
        Query query = s.createQuery(q);
        query.setLong(0, ID2);
        query.setLong(1, bid);
        query.setShort(2, (short) 1);

        List list = query.list();
        return list;
      }
    });

  }

  /**
   * findForumInList
   *
   * @param bid long
   * @param values List
   * @return List
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public List findForumInList(final long bid, final List values) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String q = "from Forum" + SysUtil.getForumTableID(bid) +
            " forum where forum.id in (:values) and forum.boardID = :bid";
        Query query = s.createQuery(q);
        query.setParameterList("values", values);
        query.setLong("bid", bid);

        List list = query.list();
        return list;
      }
    });

  }

  /**
   * findForumDelAll
   *
   * @param bid long
   * @return List
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public List findForumDelAll(long bid) {
    String q = "from Forum" + SysUtil.getForumTableID(bid) +
        " forum where forum.boardID = ? and forum.delSign = 1 and forum.delTime < ?";
    Object[] o = {
        new Long(bid), new Long(SysUtil.getLongTime() - 7 * 24 * 3600 * 1000)};
    try {
      return this.getHibernateTemplate().find(q, o);

    }
    catch (DataAccessException ex) {
      logger.error(ex);
      return new ArrayList();
    }
  }

  public List findForumDelAllList(final long bid, final int firstResult, final int maxResults) {
   return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String q = "from Forum" + SysUtil.getForumTableID(bid) +
            " forum where forum.boardID = ? and forum.delSign = ?  order by forum.id desc";
        Query query = s.createQuery(q);
        query.setLong(0, bid);
        query.setShort(1, (short) 1);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);

        List list = query.list();
        return list;
      }
    });

  }

  public List findForumElite(long bid, long elite, long eliteId) {
    String q = "from Forum" + SysUtil.getForumTableID(bid) +
        " forum where forum.boardID = ? and forum.elite = ? and forum.eliteID = ?";
    Object[] o = {
        new Long(bid), new Long(elite), new Long(eliteId)};
    try {
      return this.getHibernateTemplate().find(q, o);
    }
    catch (DataAccessException ex) {
      logger.error(ex);
      return new ArrayList();
    }
  }

  /**
   * removeForum
   *
   * @param id long
   * @param bid long
   * @todo Implement this com.laoer.bbscs.dao.IForumDAO method
   */
  public void removeForum(final long id, final long bid) {
    getHibernateTemplate().execute(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String q = "from Forum" + SysUtil.getForumTableID(bid) +
            " forum where forum.id = ? and forum.boardID = ?";
        Object[] o = {
            new Long(id), new Long(bid)};
        Type[] t = {
            Hibernate.LONG, Hibernate.LONG};
        s.delete(q, o, t);
        return null;
      }
    });
  }

  public void removeForum(Forum forum) {
    try {
      getHibernateTemplate().delete(forum);
    }
    catch (DataAccessException ex) {
      logger.error(ex);
    }
  }

  public int getSearchNum(long bid, String con, String text) {
    StringBuffer sb = new StringBuffer();
    sb.append("select count(forum.id) from Forum");
    sb.append(SysUtil.getForumTableID(bid));
    sb.append(" forum where forum.boardID = ? and forum.");
    sb.append(con);
    sb.append(" like ?");
    //sb.append(" like '%");
    //sb.append(text);
    //sb.append("%'");
    try {
      Object[] o = {
          new Long(bid), "%" + text + "%"};
      List l = getHibernateTemplate().find(sb.toString(), o);
      if (l != null && !l.isEmpty()) {
        return ( (Integer) l.get(0)).intValue();
      }
      else {
        return 0;
      }
    }
    catch (DataAccessException ex) {
      logger.error(ex);
      return 0;
    }
  }

  public List getSearchList(final long bid, final String con, final String text,
                            final int firstResult,
                            final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        StringBuffer sb = new StringBuffer();
        sb.append("from Forum");
        sb.append(SysUtil.getForumTableID(bid));
        sb.append(" forum where forum.boardID = ? and forum.");
        sb.append(con);
        sb.append(" like ? order by forum.id desc");
        Query query = s.createQuery(sb.toString());
        query.setLong(0, bid);
        query.setString(1, "%" + text + "%");
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        List l = query.list();
        return l;
      }
    });
  }

}

⌨️ 快捷键说明

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