/**
* ReplyDAO.java of DLOG4J_E40
* 作者:liudong
* 时间: 2008-1-14
* 项目主页: <a href="http://www.dlog4j.com/">http://www.dlog4j.com</a>
*/
package com.dlog4j.dao;
import static com.dlog4j.dao.GenericDAO.commitNamedUpdate;
import static com.dlog4j.dao.GenericDAO.commitUpdate;
import static com.dlog4j.dao.GenericDAO.executeNamedUpdate;
import static com.dlog4j.dao.GenericDAO.executeQuery;
import static com.dlog4j.dao.GenericDAO.executeStatAsInt;
import static com.dlog4j.dao.GenericDAO.executeUpdate;
import static com.dlog4j.dao.GenericDAO.findAll;
import static com.dlog4j.dao.GenericDAO.getBean;
import static com.dlog4j.dbaccess.HibernateUtils.commit;
import static com.dlog4j.dbaccess.HibernateUtils.getSession;
import static com.dlog4j.dbaccess.HibernateUtils.rollback;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.dlog4j.beans.DiaryBean;
import com.dlog4j.beans.DiaryReplyBean;
import com.dlog4j.beans.PhotoBean;
import com.dlog4j.beans.PhotoReplyBean;
import com.dlog4j.beans.ReplyBean;
import com.dlog4j.beans.SiteBean;
import com.dlog4j.beans.UserBean;
import com.dlog4j.dbaccess.HibernateUtils;
/**
* 评论数据库接口,包括日记和相册的在一起
*
* @author liudong
*/
public abstract class ReplyDAO
{
private static final int NUM2 = 2;
private static final int NUM1000 = 1000;
/**
* 删除某用户的所有日记评论
*
* @param site site
* @param diary diary
* @param reply reply
* @return int
* @author yuanliang
*/
public static final int deleteDiaryRepliesViaAuthor(String site,
long diary, long reply)
{
DiaryReplyBean rplBean = getBean(DiaryReplyBean.class,
new DiaryReplyBean(site, diary, reply));
if (rplBean == null)
{
return -1;
}
org.hibernate.Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
String sql = "select r.diary ,count(r.diary) from dlog_diary_reply r where author='" + rplBean.getAuthor() +"' and site_name='" + site + "' group by diary";
updateDiaryReplyCount(sql,session,site);
transaction.commit();
String hql = "DELETE FROM DiaryReplyBean WHERE site=? AND author=?";
return commitUpdate(hql, site, rplBean.getAuthor());
}
/**
* 删除某IP的所有日记评论
*
* @param site site
* @param diary diary
* @param reply reply
* @author yuanliang
* @return int
*/
public static final int deleteDiaryRepliesViaIp(String site, long diary,
long reply)
{
DiaryReplyBean rplBean = getBean(DiaryReplyBean.class,
new DiaryReplyBean(site, diary, reply));
if (rplBean == null)
{
return -1;
}
org.hibernate.Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
String sql = "select r.diary ,count(r.diary) from dlog_diary_reply r where client_ip='" + rplBean.getClientIp() +"' and site_name='" + site + "' group by diary";
updateDiaryReplyCount(sql,session,site);
transaction.commit();
String hql = "DELETE FROM DiaryReplyBean WHERE site=? AND clientIp=?";
return commitUpdate(hql, site, rplBean.getClientIp());
}
/**
* 删除某用户的所有照片评论
*
* @param site site
* @param photo photo
* @param reply reply
* @return int
* @author yuanliang
*/
@SuppressWarnings("unchecked")
public static final int deletePhotoRepliesViaAuthor(String site,
long photo, long reply)
{
PhotoReplyBean rplBean = getBean(PhotoReplyBean.class,
new PhotoReplyBean(site, photo, reply));
if (rplBean == null)
{
return -1;
}
org.hibernate.Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
String sql = "select r.photo ,count(r.photo) from dlog_photo_reply r where author='" + rplBean.getAuthor() +"' and site_name='" + site + "' group by photo";
updatePhotoReplyCount(sql,session,site);
/*
* List<Object[]> listobj = session.createSQLQuery(sql).list();
* for(int i=0;i<listobj.size();i++){
List<Object []> li = new ArrayList();
li.add(listobj.get(i));
for(Object[] o:li){
long id = Long.parseLong(o[0].toString());
int authorRelpyCount = Integer.parseInt(o[1].toString());
String hql = "UPDATE PhotoBean SET replyCount=replyCount-" + authorRelpyCount + " WHERE id= ? and site=?" ;
session.createQuery(hql).setLong(0, id).setString(1, site).executeUpdate();
}
}*/
transaction.commit();
String hql = "DELETE FROM PhotoReplyBean WHERE site=? AND author=?";
return commitUpdate(hql, site, rplBean.getAuthor());
}
/**
* 删除某用户的所有照片评论
*
* @param site site
* @param photo photo
* @param reply reply
* @return int
* @author yuanliang
*/
@SuppressWarnings("unchecked")
public static final int deletePhotoRepliesViaIp(String site, long photo,
long reply)
{
PhotoReplyBean rplBean = getBean(PhotoReplyBean.class,
new PhotoReplyBean(site, photo, reply));
if (rplBean == null)
{
return -1;
}
org.hibernate.Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
String sql = "select r.photo ,count(r.photo) from dlog_photo_reply r where client_ip='" + rplBean.getClientIp() +"' and site_name='" + site + "' group by photo";
/* List<Object[]> listobj = session.createSQLQuery(sql).list();
for(int i=0;i<listobj.size();i++){
List<Object []> li = new ArrayList();
li.add(listobj.get(i));
for(Object[] o:li){
long id = Long.parseLong(o[0].toString());
int authorRelpyCount = Integer.parseInt(o[1].toString());
String hql = "UPDATE PhotoBean SET replyCount=replyCount-" + authorRelpyCount + " WHERE id= ? and site=?" ;
session.createQuery(hql).setLong(0, id).setString(1, site).executeUpdate();
}
}*/
updatePhotoReplyCount(sql,session,site);
transaction.commit();
String hql = "DELETE FROM PhotoReplyBean WHERE site=? AND clientIp=?";
return commitUpdate(hql, site, rplBean.getClientIp());
}
/**
* updatePhotoReplyCount
*
* @param sql
* @param session
* @param site
* @author yuanliang
*/
@SuppressWarnings("unchecked")
private static final void updatePhotoReplyCount(String sql,Session session,String site){
List<Object[]> listobj = session.createSQLQuery(sql).list();
for(int i=0;i<listobj.size();i++){
List<Object []> li = new ArrayList();
li.add(listobj.get(i));
for(Object[] o:li){
long id = Long.parseLong(o[0].toString());
int authorOrIpRelpyCount = Integer.parseInt(o[1].toString());
String hql = "UPDATE PhotoBean SET replyCount=replyCount-" + authorOrIpRelpyCount + " WHERE id= ? and site=?" ;
session.createQuery(hql).setLong(0, id).setString(1, site).executeUpdate();
}
}
}
/**
* updatePhotoReplyCount
*
* @param sql
* @param session
* @param site
* @author yuanliang
*/
@SuppressWarnings("unchecked")
private static final void updateDiaryReplyCount(String sql,Session session,String site){
List<Object[]> listobj = session.createSQLQuery(sql).list();
for(int i=0;i<listobj.size();i++){
List<Object []> li = new ArrayList();
li.add(listobj.get(i));
for(Object[] o:li){
long id = Long.parseLong(o[0].toString());
int authorOrIpRelpyCount = Integer.parseInt(o[1].toString());
String hql = "UPDATE DiaryBean SET replyCount=replyCount-" + authorOrIpRelpyCount + " WHERE id= ? and site=?" ;
session.createQuery(hql).setLong(0, id).setString(1, site).executeUpdate();
}
}
}
/**
* 列出评论
*
* @param <T> <T>
* @param rplClass rplClass
* @param from from
* @param size size
* @param site site
* @return List
*/
public static final <T extends ReplyBean> List<T> list(Class<T> rplClass,
String site, int from, int size)
{
StringBuilder hql = new StringBuilder("FROM ");
hql.append(rplClass.getSimpleName());
hql.append(" WHERE site=? ORDER BY id DESC");
return executeQuery(rplClass, hql.toString(), from, size, site);
}
/**
* 总评论数
*
* @param <T> <T>
* @param rplClass rplClass
* @param site site
* @return <T>
*/
public static final <T> int getReplyCount(Class<T> rplClass, String site)
{
StringBuilder hql = new StringBuilder("SELECT COUNT(*) FROM ");
hql.append(rplClass.getSimpleName());
hql.append(" WHERE site=?");
return executeStatAsInt(hql.toString(), site);
}
/**
* 列出某篇日记的评论
*
* @param site site
* @param diary diary
* @param from from
* @param size size
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listDiaryReplies(String site,
long diary, int from, int size)
{
String hql = "FROM DiaryReplyBean WHERE site=? AND diary=? ORDER BY id";
return executeQuery(DiaryReplyBean.class, hql, from, size, site, diary);
}
/**
* listDiaryReplies
*
* @param diary diary
* @param from from
* @param size size
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listDiaryReplies(long diary,
int from, int size)
{
String hql = "FROM DiaryReplyBean WHERE diary=? ORDER BY id";
return executeQuery(DiaryReplyBean.class, hql, from, size, diary);
}
/**
* listDiaryreplies
*
* @param reply reply
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listDiaryreplies(long reply)
{
String hql = "FROM DiaryReplyBean WHERE id=?";
return executeQuery(DiaryReplyBean.class, hql, -1, -1, reply);
}
/**
* listMyDiaryReplies
*
* @param user user
* @param fromidx fromidx
* @param count count
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listMyDiaryReplies(String user,
int fromidx, int count)
{
String hql = "FROM DiaryReplyBean WHERE user=? ORDER BY createTime DESC";
return executeQuery(DiaryReplyBean.class, hql, fromidx, count, user);
}
/**
* 列出某张照片的评论
*
* @param site site
* @param photo photo
* @param from from
* @param size size
* @return List<PhotoReplyBean>
*/
public static final List<PhotoReplyBean> listPhotoReplies(String site,
long photo, int from, int size)
{
String hql = "FROM PhotoReplyBean WHERE site=? AND photo=? ORDER BY id";
return executeQuery(PhotoReplyBean.class, hql, from, size, site, photo);
}
/**
* listMyPhotoReplies
*
* @param user user
* @param fromidx fromidx
* @param count count
* @return List<PhotoReplyBean>
*/
public static final List<PhotoReplyBean> listMyPhotoReplies(String user,
int fromidx, int count)
{
String hql = "FROM PhotoReplyBean WHERE user=? ORDER BY createTime DESC";
return executeQuery(PhotoReplyBean.class, hql, fromidx, count, user);
}
/**
* getReplyCountOfUser
*
* @param <T> <T>
* @param rplBean rplBean
* @param user user
* @return int
*/
public static final <T extends ReplyBean> int getReplyCountOfUser(
Class<T> rplBean, String user)
{
String hql = "SELECT COUNT(*) FROM " + rplBean.getSimpleName()
+ " WHERE user=?";
return executeStatAsInt(hql, user);
}
/**
* 发表日记评论
*
* @param site site
* @param user user
* @param diary diary
* @param rbean rbean
*/
public static final void addDiaryReply(SiteBean site, UserBean user,
DiaryBean diary, DiaryReplyBean rbean)
{
rbean.setSite(site.getName());
rbean.setDiary(diary.getId());
rbean.setId(genId(rbean, site.getCreateTime()));
rbean.setUser((user != null) ? user.getName() : null);
rbean.setCreateTime(new Date());
try
{
HibernateUtils.beginTransaction();
getSession().save(rbean);
String hql = "UPDATE DiaryBean SET replyCount=replyCount+1,lastReplyTime=? WHERE site=? AND id=?";
int ec = executeUpdate(hql,
new Date(),
site.getName(),
diary.getId());
if (ec > 0)
{
// 更新缓存中的日记评论�
DiaryDAO.updateDiaryReplyCountInCache(diary, 1);
}
commit();
}
catch (HibernateException e)
{
rollback();
throw e;
}
}
/**
* 发表照片评论
*
* @param site site
* @param user user
* @param photo photo
* @param rbean rbean
*/
public static final void addPhotoReply(SiteBean site, UserBean user,
PhotoBean photo, PhotoReplyBean rbean)
{
rbean.setSite(site.getName());
rbean.setPhoto(photo.getId());
rbean.setId(genId(rbean, site.getCreateTime()));
rbean.setUser((user != null) ? user.getName() : null);
rbean.setCreateTime(new Date());
try
{
HibernateUtils.beginTransaction();
getSession().save(rbean);
String hql = "UPDATE PhotoBean SET replyCount = replyCount+1, lastReplyTime=? "
+ " WHERE site=? AND id=?";
int ec = executeUpdate(hql,
new Date(),
site.getName(),
photo.getId());
if (ec > 0)
{
// 更新缓存中的日记评论�
PhotoDAO.updatePhotoReplyCountInCache(photo, 1);
}
commit();
}
catch (HibernateException e)
{
rollback();
throw e;
}
}
/**
* 删除日记评论
*
* @param content content
* @return int
*/
public static final int deleteDiaryReplyByContent(String content)
{
String hql = "SELECT site,diary,id FROM DiaryReplyBean WHERE content LIKE ?";
String hql2 = "UPDATE DiaryBean SET replyCount=replyCount-1 WHERE site=? AND id=?";
List<Object[]> rpls = findAll(Object[].class, hql, '%' + content + '%');
int rc = 0;
try
{
HibernateUtils.beginTransaction();
for (Object[] rpl : rpls)
{
if (executeUpdate(hql2, rpl[0], rpl[1]) > 0)
{
DiaryDAO.updateDiaryReplyCountInCache(new DiaryBean(
(String) rpl[0], (Long) rpl[1]), -1);
}
executeNamedUpdate("DELETE_DIARY_REPLY",
rpl[0],
rpl[1],
rpl[NUM2]);
rc++;
}
commit();
}
catch (HibernateException e)
{
rollback();
throw e;
}
return rc;
}
/**
* 删除照片评论
*
* @param content content
* @return int
*/
public static final int deletePhotoReplyByContent(String content)
{
String hql = "SELECT site,photo,id FROM PhotoReplyBean WHERE content LIKE ?";
String hql2 = "UPDATE PhotoBean SET replyCount=replyCount-1 WHERE site=? AND id=?";
List<Object[]> rpls = findAll(Object[].class, hql, '%' + content + '%');
int rc = 0;
try
{
HibernateUtils.beginTransaction();
for (Object[] rpl : rpls)
{
if (executeUpdate(hql2, rpl[0], rpl[1]) > 0)
{
PhotoDAO.updatePhotoReplyCountInCache(new PhotoBean(
(String) rpl[0], (Long) rpl[1]), -1);
}
executeNamedUpdate("DELETE_PHOTO_REPLY",
rpl[0],
rpl[1],
rpl[NUM2]);
rc++;
}
commit();
}
catch (HibernateException e)
{
rollback();
throw e;
}
return rc;
}
/**
* 删除日记评论
*
* @param site site
* @param user user
* @param diary diary
* @param reply reply
* @return boolean
*/
public static final boolean deleteDiaryReply(SiteBean site, UserBean user,
long diary, long reply)
{
boolean dr = false;
if (user != null)
{
if (site.isOwner(user))
{
dr = commitNamedUpdate("DELETE_DIARY_REPLY",
site.getName(),
diary,
reply) > 0;
}
else
{
DiaryReplyBean r = getBean(DiaryReplyBean.class,
new DiaryReplyBean(site.getName(), diary, reply));
if (r != null
&& StringUtils.equals(user.getName(), r.getUser()))
{
dr = commitNamedUpdate("DELETE_DIARY_REPLY",
site.getName(),
diary,
reply) > 0;
}
}
if (dr)
{
// 重新统计评论�
String hql = "SELECT COUNT(*) FROM DiaryReplyBean WHERE site=? AND diary=?";
int rc = executeStatAsInt(hql, site.getName(), diary);
hql = "UPDATE DiaryBean SET replyCount=? WHERE site=? AND id=?";
int ec = commitUpdate(hql, rc, site.getName(), diary);
if (ec > 0)
{
// 更新缓存中的日记评论�
DiaryDAO.updateDiaryReplyCountInCache(new DiaryBean(
site.getName(), diary), -1);
}
}
}
return dr;
}
/**
* 删除某篇日记的所有评论
*
* @param site site
* @param diary diary
* @return int
*/
protected static final int deleteDiaryReplies(String site, long diary)
{
String hql = "DELETE FROM DiaryReplyBean WHERE site=? AND diary=?";
return executeUpdate(hql, site, diary);
}
/**
* 删除照片评论
*
* @param site site
* @param user user
* @param photo photo
* @param reply reply
* @return boolean
*/
public static final boolean deletePhotoReply(SiteBean site, UserBean user,
long photo, long reply)
{
boolean dr = false;
if (user != null)
{
if (site.isOwner(user))
{
dr = commitNamedUpdate("DELETE_PHOTO_REPLY",
site.getName(),
photo,
reply) > 0;
}
else
{
PhotoReplyBean r = getBean(PhotoReplyBean.class,
new PhotoReplyBean(site.getName(), photo, reply));
if (r != null
&& StringUtils.equals(user.getName(), r.getUser()))
{
dr = commitNamedUpdate("DELETE_PHOTO_REPLY",
site.getName(),
photo,
reply) > 0;
}
}
if (dr)
{
// 重新统计评论�
String hql = "SELECT COUNT(*) FROM PhotoReplyBean WHERE site=? AND photo=?";
int rc = executeStatAsInt(hql, site.getName(), photo);
hql = "UPDATE PhotoBean SET replyCount=? WHERE site=? AND id=?";
commitUpdate(hql, rc, site.getName(), photo);
}
}
return dr;
}
/**
* 生成唯一的评论编号(并保证此ID是递增的)
*
* @param rbean rbean
* @param baseTime baseTime
* @return long long
*/
static final long genId(ReplyBean rbean, Date baseTime)
{
int rbeanContent = rbean.getContent().hashCode();
if (rbeanContent == Integer.MIN_VALUE)
{
rbeanContent = Integer.MAX_VALUE + 1;
}
long id = (System.currentTimeMillis() - baseTime.getTime()) / NUM1000;
id *= NUM1000;
return id + Math.abs(rbeanContent) % NUM1000;
}
/**
* the method for get list Diary replies by repaly or diary in DB
* @author lyuan
* @param reply long
* @param size int
* @param diary long
* @param from int
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listDiaryreplies(long reply, long diary, int from, int size)
{
String hql = "FROM DiaryReplyBean WHERE id=? and diary=? ";
return executeQuery(DiaryReplyBean.class, hql, from, size, reply, diary);
}
}
今天在导入一个工程时,编译并打包到Tomcat后,发现出现java.lang.UnsupportedClassVersionError: Bad version number in .class file异常,检查了一下我的myEclipse,发现不知道啥时候不小心将编译器改成JDK6.0了,那个工程是从同事的机上拷贝过来的,用的编译器是JDK5.0,试了一下,果然是这个问题引起。
那次在Linux上部署工程时也出现过因为版本不同引起的问题,那时我们用的IDE的编译器是JDK5.0,而那台Linux装的是JDK6.0,部署后发现很多功能都出错,看来有些东西还是得注意一下啊。
附,在myEclipse中改变编译器的方法:Project->Properties->Java Compiler->Configure Workspace Setting,在弹出的页面中可以进行设置。
* ReplyDAO.java of DLOG4J_E40
* 作者:liudong
* 时间: 2008-1-14
* 项目主页: <a href="http://www.dlog4j.com/">http://www.dlog4j.com</a>
*/
package com.dlog4j.dao;
import static com.dlog4j.dao.GenericDAO.commitNamedUpdate;
import static com.dlog4j.dao.GenericDAO.commitUpdate;
import static com.dlog4j.dao.GenericDAO.executeNamedUpdate;
import static com.dlog4j.dao.GenericDAO.executeQuery;
import static com.dlog4j.dao.GenericDAO.executeStatAsInt;
import static com.dlog4j.dao.GenericDAO.executeUpdate;
import static com.dlog4j.dao.GenericDAO.findAll;
import static com.dlog4j.dao.GenericDAO.getBean;
import static com.dlog4j.dbaccess.HibernateUtils.commit;
import static com.dlog4j.dbaccess.HibernateUtils.getSession;
import static com.dlog4j.dbaccess.HibernateUtils.rollback;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.dlog4j.beans.DiaryBean;
import com.dlog4j.beans.DiaryReplyBean;
import com.dlog4j.beans.PhotoBean;
import com.dlog4j.beans.PhotoReplyBean;
import com.dlog4j.beans.ReplyBean;
import com.dlog4j.beans.SiteBean;
import com.dlog4j.beans.UserBean;
import com.dlog4j.dbaccess.HibernateUtils;
/**
* 评论数据库接口,包括日记和相册的在一起
*
* @author liudong
*/
public abstract class ReplyDAO
{
private static final int NUM2 = 2;
private static final int NUM1000 = 1000;
/**
* 删除某用户的所有日记评论
*
* @param site site
* @param diary diary
* @param reply reply
* @return int
* @author yuanliang
*/
public static final int deleteDiaryRepliesViaAuthor(String site,
long diary, long reply)
{
DiaryReplyBean rplBean = getBean(DiaryReplyBean.class,
new DiaryReplyBean(site, diary, reply));
if (rplBean == null)
{
return -1;
}
org.hibernate.Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
String sql = "select r.diary ,count(r.diary) from dlog_diary_reply r where author='" + rplBean.getAuthor() +"' and site_name='" + site + "' group by diary";
updateDiaryReplyCount(sql,session,site);
transaction.commit();
String hql = "DELETE FROM DiaryReplyBean WHERE site=? AND author=?";
return commitUpdate(hql, site, rplBean.getAuthor());
}
/**
* 删除某IP的所有日记评论
*
* @param site site
* @param diary diary
* @param reply reply
* @author yuanliang
* @return int
*/
public static final int deleteDiaryRepliesViaIp(String site, long diary,
long reply)
{
DiaryReplyBean rplBean = getBean(DiaryReplyBean.class,
new DiaryReplyBean(site, diary, reply));
if (rplBean == null)
{
return -1;
}
org.hibernate.Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
String sql = "select r.diary ,count(r.diary) from dlog_diary_reply r where client_ip='" + rplBean.getClientIp() +"' and site_name='" + site + "' group by diary";
updateDiaryReplyCount(sql,session,site);
transaction.commit();
String hql = "DELETE FROM DiaryReplyBean WHERE site=? AND clientIp=?";
return commitUpdate(hql, site, rplBean.getClientIp());
}
/**
* 删除某用户的所有照片评论
*
* @param site site
* @param photo photo
* @param reply reply
* @return int
* @author yuanliang
*/
@SuppressWarnings("unchecked")
public static final int deletePhotoRepliesViaAuthor(String site,
long photo, long reply)
{
PhotoReplyBean rplBean = getBean(PhotoReplyBean.class,
new PhotoReplyBean(site, photo, reply));
if (rplBean == null)
{
return -1;
}
org.hibernate.Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
String sql = "select r.photo ,count(r.photo) from dlog_photo_reply r where author='" + rplBean.getAuthor() +"' and site_name='" + site + "' group by photo";
updatePhotoReplyCount(sql,session,site);
/*
* List<Object[]> listobj = session.createSQLQuery(sql).list();
* for(int i=0;i<listobj.size();i++){
List<Object []> li = new ArrayList();
li.add(listobj.get(i));
for(Object[] o:li){
long id = Long.parseLong(o[0].toString());
int authorRelpyCount = Integer.parseInt(o[1].toString());
String hql = "UPDATE PhotoBean SET replyCount=replyCount-" + authorRelpyCount + " WHERE id= ? and site=?" ;
session.createQuery(hql).setLong(0, id).setString(1, site).executeUpdate();
}
}*/
transaction.commit();
String hql = "DELETE FROM PhotoReplyBean WHERE site=? AND author=?";
return commitUpdate(hql, site, rplBean.getAuthor());
}
/**
* 删除某用户的所有照片评论
*
* @param site site
* @param photo photo
* @param reply reply
* @return int
* @author yuanliang
*/
@SuppressWarnings("unchecked")
public static final int deletePhotoRepliesViaIp(String site, long photo,
long reply)
{
PhotoReplyBean rplBean = getBean(PhotoReplyBean.class,
new PhotoReplyBean(site, photo, reply));
if (rplBean == null)
{
return -1;
}
org.hibernate.Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
String sql = "select r.photo ,count(r.photo) from dlog_photo_reply r where client_ip='" + rplBean.getClientIp() +"' and site_name='" + site + "' group by photo";
/* List<Object[]> listobj = session.createSQLQuery(sql).list();
for(int i=0;i<listobj.size();i++){
List<Object []> li = new ArrayList();
li.add(listobj.get(i));
for(Object[] o:li){
long id = Long.parseLong(o[0].toString());
int authorRelpyCount = Integer.parseInt(o[1].toString());
String hql = "UPDATE PhotoBean SET replyCount=replyCount-" + authorRelpyCount + " WHERE id= ? and site=?" ;
session.createQuery(hql).setLong(0, id).setString(1, site).executeUpdate();
}
}*/
updatePhotoReplyCount(sql,session,site);
transaction.commit();
String hql = "DELETE FROM PhotoReplyBean WHERE site=? AND clientIp=?";
return commitUpdate(hql, site, rplBean.getClientIp());
}
/**
* updatePhotoReplyCount
*
* @param sql
* @param session
* @param site
* @author yuanliang
*/
@SuppressWarnings("unchecked")
private static final void updatePhotoReplyCount(String sql,Session session,String site){
List<Object[]> listobj = session.createSQLQuery(sql).list();
for(int i=0;i<listobj.size();i++){
List<Object []> li = new ArrayList();
li.add(listobj.get(i));
for(Object[] o:li){
long id = Long.parseLong(o[0].toString());
int authorOrIpRelpyCount = Integer.parseInt(o[1].toString());
String hql = "UPDATE PhotoBean SET replyCount=replyCount-" + authorOrIpRelpyCount + " WHERE id= ? and site=?" ;
session.createQuery(hql).setLong(0, id).setString(1, site).executeUpdate();
}
}
}
/**
* updatePhotoReplyCount
*
* @param sql
* @param session
* @param site
* @author yuanliang
*/
@SuppressWarnings("unchecked")
private static final void updateDiaryReplyCount(String sql,Session session,String site){
List<Object[]> listobj = session.createSQLQuery(sql).list();
for(int i=0;i<listobj.size();i++){
List<Object []> li = new ArrayList();
li.add(listobj.get(i));
for(Object[] o:li){
long id = Long.parseLong(o[0].toString());
int authorOrIpRelpyCount = Integer.parseInt(o[1].toString());
String hql = "UPDATE DiaryBean SET replyCount=replyCount-" + authorOrIpRelpyCount + " WHERE id= ? and site=?" ;
session.createQuery(hql).setLong(0, id).setString(1, site).executeUpdate();
}
}
}
/**
* 列出评论
*
* @param <T> <T>
* @param rplClass rplClass
* @param from from
* @param size size
* @param site site
* @return List
*/
public static final <T extends ReplyBean> List<T> list(Class<T> rplClass,
String site, int from, int size)
{
StringBuilder hql = new StringBuilder("FROM ");
hql.append(rplClass.getSimpleName());
hql.append(" WHERE site=? ORDER BY id DESC");
return executeQuery(rplClass, hql.toString(), from, size, site);
}
/**
* 总评论数
*
* @param <T> <T>
* @param rplClass rplClass
* @param site site
* @return <T>
*/
public static final <T> int getReplyCount(Class<T> rplClass, String site)
{
StringBuilder hql = new StringBuilder("SELECT COUNT(*) FROM ");
hql.append(rplClass.getSimpleName());
hql.append(" WHERE site=?");
return executeStatAsInt(hql.toString(), site);
}
/**
* 列出某篇日记的评论
*
* @param site site
* @param diary diary
* @param from from
* @param size size
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listDiaryReplies(String site,
long diary, int from, int size)
{
String hql = "FROM DiaryReplyBean WHERE site=? AND diary=? ORDER BY id";
return executeQuery(DiaryReplyBean.class, hql, from, size, site, diary);
}
/**
* listDiaryReplies
*
* @param diary diary
* @param from from
* @param size size
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listDiaryReplies(long diary,
int from, int size)
{
String hql = "FROM DiaryReplyBean WHERE diary=? ORDER BY id";
return executeQuery(DiaryReplyBean.class, hql, from, size, diary);
}
/**
* listDiaryreplies
*
* @param reply reply
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listDiaryreplies(long reply)
{
String hql = "FROM DiaryReplyBean WHERE id=?";
return executeQuery(DiaryReplyBean.class, hql, -1, -1, reply);
}
/**
* listMyDiaryReplies
*
* @param user user
* @param fromidx fromidx
* @param count count
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listMyDiaryReplies(String user,
int fromidx, int count)
{
String hql = "FROM DiaryReplyBean WHERE user=? ORDER BY createTime DESC";
return executeQuery(DiaryReplyBean.class, hql, fromidx, count, user);
}
/**
* 列出某张照片的评论
*
* @param site site
* @param photo photo
* @param from from
* @param size size
* @return List<PhotoReplyBean>
*/
public static final List<PhotoReplyBean> listPhotoReplies(String site,
long photo, int from, int size)
{
String hql = "FROM PhotoReplyBean WHERE site=? AND photo=? ORDER BY id";
return executeQuery(PhotoReplyBean.class, hql, from, size, site, photo);
}
/**
* listMyPhotoReplies
*
* @param user user
* @param fromidx fromidx
* @param count count
* @return List<PhotoReplyBean>
*/
public static final List<PhotoReplyBean> listMyPhotoReplies(String user,
int fromidx, int count)
{
String hql = "FROM PhotoReplyBean WHERE user=? ORDER BY createTime DESC";
return executeQuery(PhotoReplyBean.class, hql, fromidx, count, user);
}
/**
* getReplyCountOfUser
*
* @param <T> <T>
* @param rplBean rplBean
* @param user user
* @return int
*/
public static final <T extends ReplyBean> int getReplyCountOfUser(
Class<T> rplBean, String user)
{
String hql = "SELECT COUNT(*) FROM " + rplBean.getSimpleName()
+ " WHERE user=?";
return executeStatAsInt(hql, user);
}
/**
* 发表日记评论
*
* @param site site
* @param user user
* @param diary diary
* @param rbean rbean
*/
public static final void addDiaryReply(SiteBean site, UserBean user,
DiaryBean diary, DiaryReplyBean rbean)
{
rbean.setSite(site.getName());
rbean.setDiary(diary.getId());
rbean.setId(genId(rbean, site.getCreateTime()));
rbean.setUser((user != null) ? user.getName() : null);
rbean.setCreateTime(new Date());
try
{
HibernateUtils.beginTransaction();
getSession().save(rbean);
String hql = "UPDATE DiaryBean SET replyCount=replyCount+1,lastReplyTime=? WHERE site=? AND id=?";
int ec = executeUpdate(hql,
new Date(),
site.getName(),
diary.getId());
if (ec > 0)
{
// 更新缓存中的日记评论�
DiaryDAO.updateDiaryReplyCountInCache(diary, 1);
}
commit();
}
catch (HibernateException e)
{
rollback();
throw e;
}
}
/**
* 发表照片评论
*
* @param site site
* @param user user
* @param photo photo
* @param rbean rbean
*/
public static final void addPhotoReply(SiteBean site, UserBean user,
PhotoBean photo, PhotoReplyBean rbean)
{
rbean.setSite(site.getName());
rbean.setPhoto(photo.getId());
rbean.setId(genId(rbean, site.getCreateTime()));
rbean.setUser((user != null) ? user.getName() : null);
rbean.setCreateTime(new Date());
try
{
HibernateUtils.beginTransaction();
getSession().save(rbean);
String hql = "UPDATE PhotoBean SET replyCount = replyCount+1, lastReplyTime=? "
+ " WHERE site=? AND id=?";
int ec = executeUpdate(hql,
new Date(),
site.getName(),
photo.getId());
if (ec > 0)
{
// 更新缓存中的日记评论�
PhotoDAO.updatePhotoReplyCountInCache(photo, 1);
}
commit();
}
catch (HibernateException e)
{
rollback();
throw e;
}
}
/**
* 删除日记评论
*
* @param content content
* @return int
*/
public static final int deleteDiaryReplyByContent(String content)
{
String hql = "SELECT site,diary,id FROM DiaryReplyBean WHERE content LIKE ?";
String hql2 = "UPDATE DiaryBean SET replyCount=replyCount-1 WHERE site=? AND id=?";
List<Object[]> rpls = findAll(Object[].class, hql, '%' + content + '%');
int rc = 0;
try
{
HibernateUtils.beginTransaction();
for (Object[] rpl : rpls)
{
if (executeUpdate(hql2, rpl[0], rpl[1]) > 0)
{
DiaryDAO.updateDiaryReplyCountInCache(new DiaryBean(
(String) rpl[0], (Long) rpl[1]), -1);
}
executeNamedUpdate("DELETE_DIARY_REPLY",
rpl[0],
rpl[1],
rpl[NUM2]);
rc++;
}
commit();
}
catch (HibernateException e)
{
rollback();
throw e;
}
return rc;
}
/**
* 删除照片评论
*
* @param content content
* @return int
*/
public static final int deletePhotoReplyByContent(String content)
{
String hql = "SELECT site,photo,id FROM PhotoReplyBean WHERE content LIKE ?";
String hql2 = "UPDATE PhotoBean SET replyCount=replyCount-1 WHERE site=? AND id=?";
List<Object[]> rpls = findAll(Object[].class, hql, '%' + content + '%');
int rc = 0;
try
{
HibernateUtils.beginTransaction();
for (Object[] rpl : rpls)
{
if (executeUpdate(hql2, rpl[0], rpl[1]) > 0)
{
PhotoDAO.updatePhotoReplyCountInCache(new PhotoBean(
(String) rpl[0], (Long) rpl[1]), -1);
}
executeNamedUpdate("DELETE_PHOTO_REPLY",
rpl[0],
rpl[1],
rpl[NUM2]);
rc++;
}
commit();
}
catch (HibernateException e)
{
rollback();
throw e;
}
return rc;
}
/**
* 删除日记评论
*
* @param site site
* @param user user
* @param diary diary
* @param reply reply
* @return boolean
*/
public static final boolean deleteDiaryReply(SiteBean site, UserBean user,
long diary, long reply)
{
boolean dr = false;
if (user != null)
{
if (site.isOwner(user))
{
dr = commitNamedUpdate("DELETE_DIARY_REPLY",
site.getName(),
diary,
reply) > 0;
}
else
{
DiaryReplyBean r = getBean(DiaryReplyBean.class,
new DiaryReplyBean(site.getName(), diary, reply));
if (r != null
&& StringUtils.equals(user.getName(), r.getUser()))
{
dr = commitNamedUpdate("DELETE_DIARY_REPLY",
site.getName(),
diary,
reply) > 0;
}
}
if (dr)
{
// 重新统计评论�
String hql = "SELECT COUNT(*) FROM DiaryReplyBean WHERE site=? AND diary=?";
int rc = executeStatAsInt(hql, site.getName(), diary);
hql = "UPDATE DiaryBean SET replyCount=? WHERE site=? AND id=?";
int ec = commitUpdate(hql, rc, site.getName(), diary);
if (ec > 0)
{
// 更新缓存中的日记评论�
DiaryDAO.updateDiaryReplyCountInCache(new DiaryBean(
site.getName(), diary), -1);
}
}
}
return dr;
}
/**
* 删除某篇日记的所有评论
*
* @param site site
* @param diary diary
* @return int
*/
protected static final int deleteDiaryReplies(String site, long diary)
{
String hql = "DELETE FROM DiaryReplyBean WHERE site=? AND diary=?";
return executeUpdate(hql, site, diary);
}
/**
* 删除照片评论
*
* @param site site
* @param user user
* @param photo photo
* @param reply reply
* @return boolean
*/
public static final boolean deletePhotoReply(SiteBean site, UserBean user,
long photo, long reply)
{
boolean dr = false;
if (user != null)
{
if (site.isOwner(user))
{
dr = commitNamedUpdate("DELETE_PHOTO_REPLY",
site.getName(),
photo,
reply) > 0;
}
else
{
PhotoReplyBean r = getBean(PhotoReplyBean.class,
new PhotoReplyBean(site.getName(), photo, reply));
if (r != null
&& StringUtils.equals(user.getName(), r.getUser()))
{
dr = commitNamedUpdate("DELETE_PHOTO_REPLY",
site.getName(),
photo,
reply) > 0;
}
}
if (dr)
{
// 重新统计评论�
String hql = "SELECT COUNT(*) FROM PhotoReplyBean WHERE site=? AND photo=?";
int rc = executeStatAsInt(hql, site.getName(), photo);
hql = "UPDATE PhotoBean SET replyCount=? WHERE site=? AND id=?";
commitUpdate(hql, rc, site.getName(), photo);
}
}
return dr;
}
/**
* 生成唯一的评论编号(并保证此ID是递增的)
*
* @param rbean rbean
* @param baseTime baseTime
* @return long long
*/
static final long genId(ReplyBean rbean, Date baseTime)
{
int rbeanContent = rbean.getContent().hashCode();
if (rbeanContent == Integer.MIN_VALUE)
{
rbeanContent = Integer.MAX_VALUE + 1;
}
long id = (System.currentTimeMillis() - baseTime.getTime()) / NUM1000;
id *= NUM1000;
return id + Math.abs(rbeanContent) % NUM1000;
}
/**
* the method for get list Diary replies by repaly or diary in DB
* @author lyuan
* @param reply long
* @param size int
* @param diary long
* @param from int
* @return List<DiaryReplyBean>
*/
public static final List<DiaryReplyBean> listDiaryreplies(long reply, long diary, int from, int size)
{
String hql = "FROM DiaryReplyBean WHERE id=? and diary=? ";
return executeQuery(DiaryReplyBean.class, hql, from, size, reply, diary);
}
}
今天在导入一个工程时,编译并打包到Tomcat后,发现出现java.lang.UnsupportedClassVersionError: Bad version number in .class file异常,检查了一下我的myEclipse,发现不知道啥时候不小心将编译器改成JDK6.0了,那个工程是从同事的机上拷贝过来的,用的编译器是JDK5.0,试了一下,果然是这个问题引起。
那次在Linux上部署工程时也出现过因为版本不同引起的问题,那时我们用的IDE的编译器是JDK5.0,而那台Linux装的是JDK6.0,部署后发现很多功能都出错,看来有些东西还是得注意一下啊。
附,在myEclipse中改变编译器的方法:Project->Properties->Java Compiler->Configure Workspace Setting,在弹出的页面中可以进行设置。