?? diarydao.java
字號:
/*
* DiaryDAO.java
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Author: Winter Lau (javayou@gmail.com)
* http://dlog4j.sourceforge.net
*/
package com.liusoft.dlog4j.dao;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import com.liusoft.dlog4j.SessionUserObject;
import com.liusoft.dlog4j.base._ReplyBean;
import com.liusoft.dlog4j.base._BeanBase;
import com.liusoft.dlog4j.beans.DiaryOutlineBean;
import com.liusoft.dlog4j.beans.DiaryReplyBean;
import com.liusoft.dlog4j.beans.TagBean;
import com.liusoft.dlog4j.beans.BookmarkBean;
import com.liusoft.dlog4j.beans.CatalogBean;
import com.liusoft.dlog4j.beans.DiaryBean;
import com.liusoft.dlog4j.beans.SiteBean;
import com.liusoft.dlog4j.beans.UserBean;
import com.liusoft.dlog4j.search.SearchDataProvider;
import com.liusoft.dlog4j.util.DateUtils;
/**
* 日記數據庫訪問接口
*
* @author liudong
*/
public class DiaryDAO extends DAO implements SearchDataProvider {
public final static int MAX_RESULT_COUNT = 100; // 返回最大的記錄數
/**
* 設置精華日記
* @param diary_id
* @param elite
* @return
*/
public static int markDiaryAsElite(int diary_id, boolean elite){
return commitNamedUpdate("MODIFY_TYPE_OF_DIARY", elite?DiaryBean.TYPE_ELITE:0, diary_id);
}
/**
* 列出最新的日記
* @param fromIdx
* @param count
* @return
*/
public static List listNewArticles(int fromIdx, int count){
return executeNamedQuery("LIST_NEW_DIARY", fromIdx, count,
DiaryOutlineBean.STATUS_NORMAL, CatalogBean.TYPE_OWNER);
}
/**
* 獲取具有專欄標志的Site在days天內的最熱門的文章,不包括文章內容
* @param days
* @param count
* @return
*/
public static List listHotArticles(int days, int count){
Calendar cal = Calendar.getInstance();
DateUtils.resetTime(cal);
cal.add(Calendar.DATE, -days);
return executeNamedQuery("LIST_HOT_DIARY2", 0, count,
new Object[]{DiaryOutlineBean.I_STATUS_NORMAL, cal.getTime(),
CatalogBean.I_TYPE_OWNER});
}
/**
* 獲取某個Site在days天內的最熱門的文章,不包括文章內容
* @param days
* @param count
* @return
*/
public static List listHotArticles(int siteid, int days, int count){
Calendar cal = Calendar.getInstance();
DateUtils.resetTime(cal);
cal.add(Calendar.DATE, -days);
return executeNamedQuery("LIST_HOT_DIARY", 0, count, new Object[] {
new Integer(siteid), DiaryOutlineBean.I_STATUS_NORMAL,
cal.getTime(), CatalogBean.I_TYPE_OWNER });
}
/**
* 獲取某個Site在days天以前最熱門的文章,不包括文章內容
* @param days
* @param count
* @return
*/
public static List listHotArticlesBefore(int siteid, int days, int count){
Calendar cal = Calendar.getInstance();
DateUtils.resetTime(cal);
cal.add(Calendar.DATE, - days + 1);
return executeNamedQuery("LIST_HOT_DIARY3", 0, count, new Object[] {
new Integer(siteid), DiaryOutlineBean.I_STATUS_NORMAL,
cal.getTime(), CatalogBean.I_TYPE_OWNER });
}
/**
* 返回指定站點的日記總數,如果沒有指定站點則返回所有日記數
* @param site
* @return
*/
public static int getDiaryCount(int site){
String hql = "SELECT COUNT(*) FROM DiaryBean AS d WHERE d.status=?";
if(site>0){
hql += " AND d.site.id=?";
return executeStatAsInt(hql, DiaryBean.STATUS_NORMAL, site);
}
return executeStatAsInt(hql, DiaryBean.STATUS_NORMAL);
}
/**
* 從垃圾箱中恢復日記
* @param log
*/
public static void unDelete(DiaryOutlineBean log){
Session ssn = getSession();
try{
beginTransaction();
log.setStatus(DiaryBean.STATUS_NORMAL);
log.getCatalog().incArticleCount(1);
log.getOwner().getCount().incArticleCount(1);
//所有參與該日記評論者的日記評論數加一
List rpls = log.getReplies();
for(int i=0;rpls!=null && i<rpls.size();i++){
DiaryReplyBean prb = (DiaryReplyBean)rpls.get(i);
if(prb.getUser()!=null)
prb.getUser().getCount().incArticleReply(1);
}
ssn.update(log);
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
/**
* 鎖貼
* @param log
*/
public static void lock(int log_id){
setLock(log_id, 1);
}
/**
* 解鎖
* @param log
*/
public static void unlock(int log_id){
setLock(log_id, 0);
}
/**
* 鎖定/解鎖日記
* @param log
*/
protected static void setLock(int log_id, int lock){
commitNamedUpdate("LOCK_DIARY", lock, log_id);
}
/**
* 統計指定月份每天的日記數
* @param site
* @param loginUser
* @param month
* @return
*/
public static int[] statCalendarLogs(SiteBean site, SessionUserObject user, Calendar month)
{
Calendar firstDate = (Calendar)month.clone();
firstDate.set(Calendar.DATE,1);
DateUtils.resetTime(firstDate);
Calendar nextMonthFirstDate = (Calendar)firstDate.clone();
nextMonthFirstDate.add(Calendar.MONTH,1);
//計算指定月份有多少天
Calendar tempCal = (Calendar)nextMonthFirstDate.clone();
tempCal.add(Calendar.DATE,-1);
int dateCount = tempCal.get(Calendar.DATE);
int[] logCounts = new int[dateCount+1];
//查詢出當月的所有日記進行統計
StringBuffer hql = new StringBuffer("SELECT j.writeTime FROM DiaryBean AS j WHERE j.writeTime>=:beginTime AND j.writeTime<:endTime AND j.status=:status AND j.site.id=:site");
if(!site.isOwner(user)){
//排除用戶沒有權限訪問的分類
hql.append(" AND (j.catalog.type<>:cat_type");
if(user != null)
hql.append(" OR (j.catalog.type=:cat_type AND j.catalog.id IN (SELECT p.key.catalog FROM CatalogPermBean AS p WHERE p.key.user=:user))");
hql.append(')');
}
Session ssn = getSession();
try{
Query q = ssn.createQuery(hql.toString()).setCacheable(true);
q.setTimestamp("beginTime", firstDate.getTime());
q.setTimestamp("endTime", nextMonthFirstDate.getTime());
q.setInteger("status", DiaryBean.STATUS_NORMAL);
q.setInteger("site", site.getId());
if(!site.isOwner(user)){
q.setInteger("cat_type", CatalogBean.TYPE_OWNER);
if(user != null)
q.setInteger("user", user.getId());
}
int total = 0;
Iterator logs = q.list().iterator();
while(logs.hasNext()){
tempCal.setTime((Date)logs.next());
int date = tempCal.get(Calendar.DATE);
logCounts[date]++;
total ++;
}
logCounts[0] = total;
return logCounts;
}finally{
hql = null;
firstDate = null;
nextMonthFirstDate = null;
tempCal = null;
}
}
/**
* 返回某站的被刪除的日記數
* @param site
* @param user
* @return
*/
public static int getTrashCount(int site_id){
return executeNamedStat("DIARY_COUNT_BY_STATUS", site_id, DiaryBean.STATUS_DELETED).intValue();
}
/**
* 返回某站的被刪除的所有日記
* @param site
* @param user
* @return
*/
public static List listTrash(int site_id){
return listTrash(site_id, -1, -1);
}
public static List listTrash(int site_id, int fromIdx, int count){
return executeNamedQuery("LIST_DIARY_BY_STATUS",fromIdx,count,site_id, DiaryBean.STATUS_DELETED);
}
/**
* 返回某人的日記草稿數
* @param site
* @param user
* @return
*/
public static int getDraftCount(SiteBean site, int userid){
return executeNamedStatAsInt("DRAFT_COUNT", site.getId(),userid, DiaryBean.STATUS_DRAFT);
}
/**
* 返回某人的所有草稿件
* @param site
* @param user
* @return
*/
public static List listDrafts(SiteBean site, int userid){
return listDrafts(site, userid, -1, -1);
}
public static List listDrafts(SiteBean site, int userid, int fromIdx, int count){
return executeNamedQuery("LIST_DRAFT",fromIdx,count,site.getId(),userid,DiaryBean.STATUS_DRAFT);
}
/**
* 增加日記的閱讀數
* @param log_id
* @param incCount
* @return
*/
public static void incViewCount(int log_id, int incCount){
commitNamedUpdate("INC_DIARY_VIEW_COUNT",
new Object[] { new Integer(incCount),
new Timestamp(System.currentTimeMillis()),
new Integer(log_id) });
}
/**
* 把日記置為刪除狀態
* @param log_id
*/
public static void delete(DiaryBean log){
try{
beginTransaction();
log.getCatalog().incArticleCount(-1);
log.getOwner().getCount().incArticleCount(-1);
//所有參與該日記評論者的日記評論數減一
List rpls = log.getReplies();
for(int i=0;i<rpls.size();i++){
DiaryReplyBean prb = (DiaryReplyBean)rpls.get(i);
if(prb.getUser()!=null)
prb.getUser().getCount().incArticleReply(-1);
}
log.setStatus(DiaryBean.STATUS_DELETED);
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
/**
* 徹底刪除日記包括其評論(不做任何權限判斷)
* @param log_id
* @throws SQLException
* @throws IOException
*/
public static void forceDelete(int log_id) throws Exception{
Session ssn = getSession();
try{
DiaryBean log = (DiaryBean)ssn.load(DiaryBean.class, new Integer(log_id));
beginTransaction();
//如果是正常日記則對應的分類日記數減一
if(log.getStatus()==DiaryBean.STATUS_NORMAL){
log.getCatalog().incArticleCount(-1);
}
//刪除標簽
TagDAO.deleteTagByRefId(log_id, TagBean.TYPE_DIARY);
//刪除附件
FCKUploadFileDAO.deleteFilesByRef(ssn, log.getSite().getId(), log_id,
DiaryBean.TYPE_DIARY);
//所有參與該相片評論者的相冊評論數減一
cleanupReplies(ssn, log_id);
//刪除日記
ssn.delete(log);
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
/**
* 徹底刪除某個網站的垃圾箱
* @param site_id
* @throws SQLException
* @throws IOException
*/
public static void cleanupTrash(int site_id) throws Exception{
List logs = findNamedAll("QUERY_TRASH_BEFORE_CLEANUP",site_id, DiaryBean.STATUS_DELETED);
if(logs!=null && logs.size()>0){
try{
Session ssn = getSession();
beginTransaction();
for(int i=0;i<logs.size();i++){
DiaryOutlineBean log = (DiaryOutlineBean)logs.get(i);
//刪除該日記的所有評論
cleanupReplies(ssn, log.getId());
//刪除標簽
TagDAO.deleteTagByRefId(log.getId(), TagBean.TYPE_DIARY);
//刪除附件
FCKUploadFileDAO.deleteFilesByRef(ssn, site_id, log.getId(), DiaryBean.TYPE_DIARY);
//刪除日記
ssn.delete(log);
}
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
}
/**
* 刪除日記評論,自動減少對應日記的評論數
* @param reply
*/
private static int cleanupReplies(Session ssn, int log_id){
return executeNamedUpdate("DELETE_REPLIES_OF_DIARY", log_id);
}
/**
* 判斷用戶是否有編輯某篇日記的權限
* @param user
* @param diary
* @return
*/
public static boolean canUserEditDiary(SessionUserObject user, DiaryBean diary){
if(user==null || diary==null || user.getStatus()!=UserBean.STATUS_NORMAL)
return false;
if(diary.getOwner().getId()==user.getId())
return true;
if(diary.getSite().isOwner(user))
return true;
return false;
}
/**
* 得到指定日記的上一篇(用于顯示日記頁)
* @param site
* @param user
* @param cat_id
* @param log_id
* @return
*/
public static DiaryOutlineBean getPrevDiary(SiteBean site, SessionUserObject user, int cat_id, int log_id){
if(site==null)
return null;
StringBuffer hql = new StringBuffer("FROM DiaryOutlineBean AS j WHERE j.site.id=:site AND j.status=:status AND j.id<:diary");
if(!site.isOwner(user)){
//排除用戶沒有權限訪問的分類
hql.append(" AND (j.catalog.type<>:type");
if(user != null)
hql.append(" OR (j.catalog.type=:type AND j.catalog.id IN (SELECT p.key.catalog FROM CatalogPermBean AS p WHERE p.key.user=:user))");
hql.append(')');
}
if (cat_id > 0){
hql.append(" AND j.catalog.id=:catalog");
}
hql.append(" ORDER BY j.id DESC");
Session ssn = getSession();
try{
Query q = ssn.createQuery(hql.toString());
q.setInteger("site", site.getId());
q.setInteger("status", DiaryBean.STATUS_NORMAL);
q.setInteger("diary", log_id);
if(cat_id > 0)
q.setInteger("catalog", cat_id);
if(!site.isOwner(user)){
q.setInteger("type", CatalogBean.TYPE_OWNER);
if(user != null)
q.setInteger("user", user.getId());
}
q.setMaxResults(1);
return (DiaryOutlineBean)q.uniqueResult();
}finally{
hql = null;
}
}
/**
* 得到指定日記的上一篇(用于顯示日記頁)
* @param site
* @param user
* @param cat_id
* @param log_id
* @return
*/
public static DiaryOutlineBean getNextDiary(SiteBean site, SessionUserObject user, int cat_id, int log_id){
if(site==null) return null;
StringBuffer hql = new StringBuffer("FROM DiaryOutlineBean AS j WHERE j.site.id=:site AND j.status=:status AND j.id>:diary");
if(!site.isOwner(user)){
//排除用戶沒有權限訪問的分類
hql.append(" AND (j.catalog.type<>:type");
if(user != null)
hql.append(" OR (j.catalog.type=:type AND j.catalog.id IN (SELECT p.key.catalog FROM CatalogPermBean AS p WHERE p.key.user=:user))");
hql.append(')');
}
if (cat_id > 0){
hql.append(" AND j.catalog.id=:catalog");
}
hql.append(" ORDER BY j.id ASC");
Session ssn = getSession();
try{
Query q = ssn.createQuery(hql.toString());
q.setInteger("site", site.getId());
q.setInteger("status", DiaryBean.STATUS_NORMAL);
q.setInteger("diary", log_id);
if(cat_id > 0)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -