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

📄 elantopicdaoimpl.java

📁 struts+hibernate BBS mysql数据库 功能基本齐全
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.elan.forum.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.elan.db.ElHbnDB;
import com.elan.forum.dao.TopicDAO;
import com.elan.forum.model.ForumTopic;
import com.elan.forum.model.Forumpiece;
import com.elan.forum.model.Forumtopicreply;
import com.elan.forum.model.Forumtopictype;
import com.elan.forum.model.Forummodule;
import com.elan.forum.model.view.Forumnewtopic;
import com.elan.forum.util.Constents;

public class ElanTopicDAOImpl implements TopicDAO {
	private static ElanTopicDAOImpl elanTopicDAOImpl = null;

	private ElanTopicDAOImpl() {
	}

	static {
		elanTopicDAOImpl = new ElanTopicDAOImpl();
	}

	public static ElanTopicDAOImpl newInstance() {
		return elanTopicDAOImpl;
	}

	public int addForumtopic(ForumTopic forumtopic) {
		return 0;
	}

	public int deleteForumtopic(int id) {
		return 0;
	}

	public int deleteForumtopic(ForumTopic forumtopic) {
		return 0;
	}

	public List<ForumTopic> getAllTopic() {
		List<ForumTopic> aList = null;
		Session session = ElHbnDB.getSession();
		Query query = session.createQuery("from Forumtopic");
		aList = query.list();
		return aList;
	}

	public int modifyForumtopic(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int modifyForumtopic(ForumTopic forumtopic) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ForumTopic findForumtopic(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * 发表新文章 (non-Javadoc)
	 * 
	 * @see com.elan.forum.dao.TopicDAO#postTopic(com.elan.forum.model.Forumtopic)
	 */
	public int postTopic(ForumTopic topic) {
		Session session = ElHbnDB.getSession();
		session.save(topic);
		session.flush();
		return Constents.CURRENT_OPERATION_SUCCESS;
	}

	public Object[] getTopicById(int id) {
		Session session = ElHbnDB.getSession();
		Query query = session
				.createQuery("select id, title, text, createTime, click, reply, topicType, moduleId, moduleName, pieceId, pieceName, author from Forumtopic where id = "
						+ id);
		return (Object[]) query.list().get(0);
	}

	public List<Forumtopictype> getAllTopicType() {
		Session session = ElHbnDB.getSession();
		Query query = session.createQuery("from Forumtopictype");
		return query.list();
	}

	public List<Forummodule> getForumModule() {
		Session session = ElHbnDB.getSession();
		String sql = "from Forummodule";
		Query query = session.createQuery(sql);
		return query.list();
	}

	/*
	 * 获取Module信息,方法内部自己处理事物,专门用于jsp 中直接调用 (non-Javadoc)
	 * 
	 * @see com.elan.forum.dao.TopicDAO#getForumModuleByTransaction()
	 */
	public List<Forummodule> getForumModuleByTransaction() {
		Session session = ElHbnDB.getSession();
		Transaction tx = session.beginTransaction();
		Query query;
		try {
			String sql = "from Forummodule";
			query = session.createQuery(sql);
			tx.commit();
		} catch (RuntimeException re) {
			// do something
			tx.rollback();
			return null;
		} finally {
			ElHbnDB.closeSession();
		}
		return query.list();
	}

	/*
	 * 获取module文章,首页不建议,因为他是返回很多个文章对象应该采用getForumnewtopic方法获取 (non-Javadoc)
	 * 
	 * @see com.elan.forum.dao.TopicDAO#getForumModuleTopic(java.util.HashMap)
	 */
	@SuppressWarnings("unchecked")
	public HashMap getForumModuleTopic(HashMap fmaMap) {
		Session session = ElHbnDB.getSession();
		Transaction tx = session.beginTransaction();
		List<Forummodule> mlist;
		try {
			String msql = "from Forummodule";
			String asql = "from Forummodule where id = ?";
			mlist = session.createQuery(msql).list();// m list
			for (int m = 0; m < mlist.size(); m++) {
				for (int i = 0; i < mlist.size(); i++) {
					Query query = session.createQuery(asql);
					query.setParameter(0, ((Forummodule) mlist.get(i)).getId());
					fmaMap.put(((Forummodule) mlist.get(m)).getName(), query
							.list());
				}
			}
		} catch (RuntimeException re) {
			// do something
			tx.rollback();
		} finally {
			ElHbnDB.closeSession();
		}
		return fmaMap;
	}

	/*
	 * 获取mudule文章,只返回最新发表的文章 (non-Javadoc) 这里自己处理事物
	 * 
	 * @see com.elan.forum.dao.TopicDAO#getForumnewtopic()
	 */
	@SuppressWarnings("unchecked")
	public List<Forumnewtopic> getForumnewtopic() {
		Session session = ElHbnDB.getSession();
		Transaction tx = session.beginTransaction();
		try {
			String sql = "from Forumnewtopic";
			Query query = session.createQuery(sql);
			tx.commit();
			return query.list();
		} catch (RuntimeException re) {
			tx.rollback();
		} finally {
			ElHbnDB.closeSession();
		}
		return null;// error
	}

	public List<ForumTopic> findPiece(Integer pieceId) {
		List<ForumTopic> list = null;
		Session session = ElHbnDB.getSession();
		try {
			String hql = "from ForumTopic where pieceId = ?  and essence = 0 and top = 0 and locked = 0 order by id desc";
			Query query = session.createQuery(hql);
			query.setParameter(0, pieceId);
			list = query.list();
		} catch (RuntimeException re) {
			// to do
			re.printStackTrace();
		}
		return list;
	}

	public ForumTopic findPieceTopic(Integer topicId) {
		Session session = ElHbnDB.getSession();
		ForumTopic fa = null;
		try {
			String hql = "from ForumTopic where id = ? and locked = 0";
			Query query = session.createQuery(hql);
			query.setParameter(0, topicId);
			query.uniqueResult();
			fa = (ForumTopic) query.list().get(0);
			return fa;
		} catch (RuntimeException re) {
			re.printStackTrace();
		}
		return fa;
	}

	public List<Forumtopicreply> findTopicRegly(Integer topicId) {
		Session session = ElHbnDB.getSession();
		List<Forumtopicreply> list = null;
		try {
			String hql = "from Forumtopicreply where topicId = ? and locked = 0";
			Query query = session.createQuery(hql);
			query.setParameter(0, topicId);
			list = query.list();
			if (list == null) {
			}
			return list;
		} catch (RuntimeException re) {
			// to do
			re.printStackTrace();
		}
		return list;
	}

	/*
	 * 获取最后回复的帖子 (non-Javadoc)
	 * 
	 * @see com.elan.forum.dao.TopicDAO#getLastTopicReplyById(java.lang.Integer)
	 */
	public Forumtopicreply getLastTopicReplyById(Integer topicReplyId) {
		Session session = ElHbnDB.getSession();
		List<Forumtopicreply> list = null;
		String hql = "select MAX(id) from Forumtopicreply where topicId = "
				+ topicReplyId + " and locked = 0";
		Integer id = (Integer) session.createQuery(hql).uniqueResult();
		Query query = session
				.createQuery("from Forumtopicreply where topicId = ? and id = ? and locked = 0");
		query.setParameter(0, topicReplyId);
		query.setParameter(1, id);
		list = (List<Forumtopicreply>) query.list();
		if (list.size() == 0) {
			return null;
		}
		return (Forumtopicreply) query.list().get(0);
	}

	/*
	 * 保存回复的帖子 (non-Javadoc)
	 * 
	 * @see com.elan.forum.dao.TopicDAO#replyTopic(com.elan.forum.model.Forumtopicreply)
	 */
	public boolean replyTopic(Forumtopicreply forumTopicReply) {
		Session session = ElHbnDB.getSession();
		try {
			session.save(forumTopicReply);
		} catch (RuntimeException re) {
			re.printStackTrace();
			return false;
		}
		return true;
	}

	/*
	 * 根据TopicReplyId查找回复帖子 (non-Javadoc)
	 * 
	 * @see com.elan.forum.dao.TopicDAO#findTopicReplyById(java.lang.Integer)
	 */

	public Forumtopicreply findTopicReplyById(Integer topicReplyId) {
		Session session = ElHbnDB.getSession();
		String hql = "from Forumtopicreply where id = ? and locked = 0";
		Query query = session.createQuery(hql);
		query.setParameter(0, topicReplyId);
		List<Forumtopicreply> list = query.list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 获取全部的piece 下面的topic
	 * 
	 * @param pieceId
	 * @return
	 */
	public List getForumTopicByPieceId(Integer pieceId) {
		List list = null;
		Session session = ElHbnDB.getSession();
		String hql = "from ForumTopic where pieceId = ? and locked = 0";
		Query query = session.createQuery(hql);
		return query.list();
	}

	/**
	 * 根据pageSize获取page获取list
	 * 
	 * @param page
	 * @param pageSize
	 * @param pieceId
	 * @return
	 */
	public List<Forumtopicreply> getForumTopicByPieceId(Integer page,
			Integer pageSize, Integer pieceId) {
		List<Forumtopicreply> list = null;
		Session session = ElHbnDB.getSession();
		String hql = "From ForumTopic where pieceId = ? and essence = 0 and top = 0 and locked = 0 order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter(0, pieceId);
		query.setFirstResult((page - 1) * pageSize);
		query.setMaxResults(pageSize);
		return query.list();
	}

	/**
	 * 获取哪个piece有新帖子,以天为单位来计数时间间隔 (non-Javadoc)
	 * 
	 * @see com.elan.forum.dao.TopicDAO#getForumPieceIsNew()
	 */
	public byte[] getForumPieceIsNew() {
		byte[] isNew = null;
		Session session = ElHbnDB.getSession();
		Transaction tx = session.beginTransaction();
		try {
			Query query = session.createQuery("from Forumpiece ");
			List list = query.list();
			if (list.size() > 0) {
				isNew = new byte[list.size()];
			}
			for (int i = 0; i < list.size(); i++) {
				// isNew[i] = ((Forumpiece) list.get(i)).getIsNewTopic()
				// .booleanValue() ? Byte.valueOf("0").byteValue() :
				// Byte.valueOf("1").byteValue();
				if (((Forumpiece) list.get(i)).getIsNewTopic().booleanValue()) {
					isNew[i] = 1;
				} else {
					isNew[i] = 0;
				}
			}
		} catch (RuntimeException re) {
			tx.rollback();
			re.printStackTrace();
		} finally {
			ElHbnDB.closeSession();
		}
		return isNew;
	}

	/**
	 * 获取总Topic的记录 (non-Javadoc)
	 * 
	 * @see com.elan.forum.dao.TopicDAO#getForumTopicCount()
	 */
	public Long getForumTopicCount() {
		Session session = ElHbnDB.getSession();
		String hql = "select Count(*) From ForumTopic and locked = 0";
		Long l = ((Long) session.createQuery(hql).uniqueResult());
		return l;
	}

	/**
	 * 获取pieceId下面的topic数
	 * 
	 * @param pieceId
	 * @return Long
	 */
	public Long getForumPieceTopicCountById(Integer pieceId) {
		Session session = ElHbnDB.getSession();
		String hql = "select Count(*) From ForumTopic where pieceId = "
				+ pieceId + " and locked = 0";
		Long l = ((Long) session.createQuery(hql).uniqueResult());
		return l;
	}

	/**
	 * 获取页数
	 * 
	 * @param pageSize
	 * @return
	 */
	public Long getForumPieceTopicCountById(Long pageSize) {
		Long pageCount = new Long(0);
		Long topicCount = getForumTopicCount();
		if (topicCount % pageSize == 0) {
			pageCount = topicCount / pageSize;
		} else {
			pageCount = (topicCount / pageSize) + 1;
		}
		return pageCount;
	}

	/**
	 * @param topicId
	 * @return Boolean
	 */
	public Boolean isNewReply(Integer topicId) {
		Boolean isNew = Boolean.valueOf(false);
		Session session = ElHbnDB.getSession();
		Forumtopicreply ftr = getLastTopicReplyById(topicId);
		Calendar cld = Calendar.getInstance();
		int theDay = cld.get(Calendar.DATE);
		Timestamp topicReplyDay = ftr.getReplyTime();
		int topicDay = topicReplyDay.getDate();
		if (theDay == topicDay) {
			isNew = Boolean.valueOf(true);
		} else {
			isNew = Boolean.valueOf(false);
		}
		return isNew;
	}

	/**
	 * @param ..
	 * @param ..
	 * @param ..
	 *            (non-Javadoc)
	 * @see com.elan.forum.dao.TopicDAO#getForumPieceTopic(java.lang.Integer,
	 *      java.lang.Integer, java.lang.Integer)
	 */
	public Map getForumPieceTopic(Integer page, Integer pageSize,
			Integer pieceId) {
		Map map = new HashMap();
		// 设置piece下面的topicCount
		Long topicCount = this.getForumPieceTopicCountById(pieceId);
		Long pageCount = new Long(0);
		map.put("topicCount", topicCount);

		if (0 == topicCount % pageSize) {
			pageCount = topicCount / pageSize;
		} else {
			pageCount = topicCount / pageSize + 1;
		}
		if (page > pageCount) {
			page = pageCount.intValue();
		}
		// 设置pageCount
		map.put("page", page);
		map.put("pageCount", pageCount);
		// 设置piece下面的topic
		List list = this.getForumTopicByPieceId(page, pageSize, pieceId);
		map.put("list", list);
		// 设置是否有新的回复
		Boolean[] isNewReplyArray = new Boolean[list.size()];
		for (int i = 0; i < list.size(); i++) {
			ForumTopic ft = (ForumTopic) list.get(i);
			Forumtopicreply ftr = getLastTopicReplyById(ft.getId());
			if (ftr != null) {
				isNewReplyArray[i] = isNewReply(ftr.getTopicId());
			} else {
				isNewReplyArray[i] = false;
			}

		}
		map.put("isNewReplyArray", isNewReplyArray);
		return map;
	}

⌨️ 快捷键说明

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