📄 commentdaohbn.java
字号:
/**
* Copyright ©? 2006 广州乐言信息科技有限公司.
* All right reserved.
* Created at 2006-4-10
*/
package com.hiany.comment.dao;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.hiany.comment.domain.Comment;
/**
* CoomentDao的Hibernate 实现
*/
public class CommentDaoHbn implements CommentDao {
static Logger logger = Logger.getLogger("CommentDaoHbn");
private SessionFactory sessionFactory;
private String pageroot;
private String pageHeader = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'><html><head><meta http-equiv='Content-Type' content='text/html; charset=gb2312'><link href='../style_v1.css' rel='stylesheet' type='text/css'><body><table width=95%>";
private String pageFooter = "</table></body></html>";
private String itemTemplate = "<tr><td width=100% id=!!id!! class='text_1' bgcolor=#EEEEEE>!!name!!(ip:!!ip!!) !!commentTime!! 发表:</td></tr><tr><td class='text_1'>!!comment!!</td></tr>\n";
private int n = 5;
/*
* (non-Javadoc)
*
* @see com.hiany.comment.dao.CommentDao#save(com.hiany.comment.domain.Comment)
*/
public void save(Comment comment) {
// 保存到数据库
Session session = sessionFactory.openSession();
Transaction tsn = session.beginTransaction();
try {
session.saveOrUpdate(comment);
tsn.commit();
} catch (HibernateException e) {
tsn.rollback();
e.printStackTrace();
} finally {
session.close();
}
// 生成静态页面
this.generatePage(comment.getNewsId());
}
/**
* 生成静态页面
*
* @param comment
*/
private void generatePage(long newsId) {
// 生成静态页面 top_xxxx.html 和 all_xxxx.html
Session session = sessionFactory.openSession();
try {
String pageheader = this.getPageHeader();
String pagefooter = this.getPageFooter();
String topfilename = Comment.TOP_FILENAME;
topfilename = topfilename.replaceAll("x", new Long(newsId).toString());
OutputStream topwriter = new FileOutputStream(new File(this.getPageroot(),
topfilename));
topwriter.write(pageheader.getBytes());
String allfilename = Comment.ALL_FILENAME;
allfilename = allfilename.replaceAll("x", new Long(newsId).toString());
OutputStream allwriter = new FileOutputStream(new File(this.getPageroot(),
allfilename));
allwriter.write(pageheader.getBytes());
Criteria crit = session.createCriteria(Comment.class);
crit.add(Restrictions.eq("newsId", newsId));
crit.addOrder(Order.desc("id"));
crit.setMaxResults(1000);
for (int i = 0; i < crit.list().size(); i++) {
Comment comm = (Comment) crit.list().get(i);
String item = this.getItemTemplate();
item = item.replaceAll("!!id!!", new Long(comm.getId()).toString());
if (comm.isAnonymous()) {
if (comm.getUsername() != null) {
item = item.replaceAll("!!name!!", "游客" + comm.getUsername());
} else {
item = item.replaceAll("!!name!!", "游客");
}
} else {
item = item.replaceAll("!!name!!", comm.getNickname());
}
int p = comm.getIp().indexOf('.');
p = comm.getIp().indexOf('.', p + 1);
String ipMarked = comm.getIp().substring(0, p) + ".*.*";
item = item.replaceAll("!!ip!!", ipMarked);
item = item.replaceAll("!!commentTime!!", new SimpleDateFormat(
"yy-MM-dd hh:mm").format(new Date(comm.getCommentTime())));
item = item.replaceAll("!!comment!!", comm.getComment());
logger.debug("item:" + item);
if (i < this.getN()) {
topwriter.write(item.getBytes("GBK"));
}
allwriter.write(item.getBytes("GBK"));
}
topwriter.write(pagefooter.getBytes());
topwriter.close();
allwriter.write(pagefooter.getBytes());
allwriter.close();
} catch (IOException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
}
/**
* @return the sessionFactory
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* @param sessionFactory
* the sessionFactory to set
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* @return the pageroot
*/
public String getPageroot() {
return pageroot;
}
/**
* @param pageroot
* the pageroot to set
*/
public void setPageroot(String pageroot) {
this.pageroot = pageroot;
}
/**
* @return the itemTemplate
*/
public String getItemTemplate() {
return itemTemplate;
}
/**
* @return the n
*/
public int getN() {
return n;
}
/**
* @param ofTop
* the n to set
*/
public void setN(int ofTop) {
n = ofTop;
}
/**
* @return the pageFooter
*/
public String getPageFooter() {
return pageFooter;
}
/**
* @return the pageHeader
*/
public String getPageHeader() {
return pageHeader;
}
/*
* (non-Javadoc)
*
* @see com.hiany.comment.dao.CommentDao#delete(long)
*/
public void delete(long id) {
long newsId = 0;
Session session = sessionFactory.openSession();
Transaction tsn = session.beginTransaction();
try {
Criteria crit = session.createCriteria(Comment.class);
crit.add(Restrictions.eq("id", id));
Iterator itr = crit.list().iterator();
if(itr.hasNext()){
Comment c = (Comment)itr.next();
newsId = c.getNewsId();
session.delete(c);
}
tsn.commit();
} catch (HibernateException ex) {
tsn.rollback();
ex.printStackTrace();
}finally{
session.close();
}
if(newsId>0){
this.generatePage(newsId);
}
}
/*
* (non-Javadoc)
*
* @see com.hiany.comment.dao.CommentDao#getCommentListBeforeDays(int)
*/
public List getCommentListBeforeDays(int beforeDay, int toDay) {
List<Comment> result = new ArrayList<Comment>();
Calendar before = Calendar.getInstance(Locale.CHINA);
before.add(Calendar.DAY_OF_MONTH, 0-beforeDay);
Calendar to = Calendar.getInstance(Locale.CHINA);
to.add(Calendar.DAY_OF_MONTH, 0-toDay);
logger.debug("between:"+ before.getTimeInMillis() +","+ to.getTimeInMillis());
Session session = sessionFactory.openSession();
try {
Criteria crit = session.createCriteria(Comment.class);
crit.add(Restrictions.between("commentTime", before.getTimeInMillis(),
to.getTimeInMillis()));
crit.addOrder(Order.desc("id"));
crit.setMaxResults(1000);
Iterator itr = crit.list().iterator();
while(itr.hasNext()){
Comment c = (Comment)itr.next();
result.add(c);
}
} catch (HibernateException ex) {
ex.printStackTrace();
}finally{
session.close();
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -