?? elantopicdaoimpl.java
字號:
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;
}
/*
* 發(fā)表新文章 (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信息,方法內(nèi)部自己處理事物,專門用于jsp 中直接調(diào)用 (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文章,首頁不建議,因為他是返回很多個文章對象應(yīng)該采用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文章,只返回最新發(fā)表的文章 (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;
}
/*
* 獲取最后回復(fù)的帖子 (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);
}
/*
* 保存回復(fù)的帖子 (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;
}
/*
* 根據(jù)TopicReplyId查找回復(fù)帖子 (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();
}
/**
* 根據(jù)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有新帖子,以天為單位來計數(shù)時間間隔 (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數(shù)
*
* @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;
}
/**
* 獲取頁數(shù)
*
* @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();
// 設(shè)置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();
}
// 設(shè)置pageCount
map.put("page", page);
map.put("pageCount", pageCount);
// 設(shè)置piece下面的topic
List list = this.getForumTopicByPieceId(page, pageSize, pieceId);
map.put("list", list);
// 設(shè)置是否有新的回復(fù)
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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -