?? diarydao.java
字號:
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 journal
* @param add_bookmark
* @throws HibernateException
* @throws SQLException
*/
public static void create(DiaryBean journal, boolean add_bookmark){
try{
Session ssn = getSession();
beginTransaction();
if(journal.getStatus()==DiaryBean.STATUS_NORMAL){
journal.getCatalog().incArticleCount(1);
journal.getOwner().getCount().incArticleCount(1);
}
ssn.save(journal);
if(journal.getCatalog().getType()==CatalogBean.TYPE_GENERAL){
//只有公開分類中的日記才可以設置標簽
List tags = journal.getKeywords();
if(tags!=null && tags.size()>0){
int tag_count = 0;
for(int i=0;i<tags.size();i++){
if(tag_count>=MAX_TAG_COUNT)
break;
String tag_name = (String)tags.get(i);
if(tag_name.getBytes().length > MAX_TAG_LENGTH)
continue;
TagBean tag = new TagBean();
tag.setSite(journal.getSite());
tag.setRefId(journal.getId());
tag.setRefType(DiaryBean.TYPE_DIARY);
tag.setName(tag_name);
ssn.save(tag);
tag_count ++;
}
}
}
if(add_bookmark){
BookmarkBean bmb = new BookmarkBean();
bmb.setOwner(journal.getOwner());
bmb.setSite(journal.getSite());
bmb.setCreateTime(new Date());
bmb.setParentId(journal.getId());
bmb.setParentType(_BeanBase.TYPE_DIARY);
bmb.setTitle(journal.getTitle());
journal.getOwner().getCount().incBookmarkCount(1);
ssn.save(bmb);
}
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
/**
* 修改日記
* @param diary
*/
public static void update(DiaryBean diary, boolean updateTags){
try{
beginTransaction();
if(updateTags){
TagDAO.deleteTagByRefId(diary.getId(), DiaryBean.TYPE_DIARY);
if(diary.getCatalog().getType()==CatalogBean.TYPE_GENERAL){
List tags = diary.getKeywords();
if(tags!=null && tags.size()>0){
int tag_count = 0;
for(int i=0;i<tags.size();i++){
if(tag_count>=MAX_TAG_COUNT)
break;
String tag_name = (String)tags.get(i);
if(tag_name.getBytes().length > MAX_TAG_LENGTH)
continue;
TagBean tag = new TagBean();
tag.setSite(diary.getSite());
tag.setRefId(diary.getId());
tag.setRefType(DiaryBean.TYPE_DIARY);
tag.setName((String)tags.get(i));
diary.getTags().add(tag);
tag_count ++;
}
}
}
}
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
/**
* 根據(jù)日記的編號獲取日記詳細信息
* @param article_id
* @return
*/
public static DiaryBean getDiaryByID(int article_id){
if(article_id < 0)
return null;
return (DiaryBean)getBean(DiaryBean.class, article_id);
}
/**
* 根據(jù)日記的編號獲取日記概要信息
* @param article_id
* @return
*/
public static DiaryOutlineBean getDiaryOutlineByID(int article_id){
if(article_id < 0)
return null;
return (DiaryOutlineBean)getBean(DiaryOutlineBean.class, article_id);
}
/**
* 獲取日記所在的分類
* @param log_id
* @return
*/
public static CatalogBean getCatalogByDiary(int log_id){
if(log_id < 0)
return null;
return (CatalogBean)namedUniqueResult("CATALOG_OF_DIARY", log_id);
}
/**
* 獲取所有日記數(shù),不包括隱藏的
* @return
*/
public static int getPublicDiaryCount(){
return executeNamedStat("PUBLIC_DIARY_COUNT", DiaryBean.STATUS_NORMAL, CatalogBean.TYPE_OWNER).intValue();
}
/**
* 獲取指定網(wǎng)站指定分類的日記數(shù)
* @param site
* @param user
* @param catalog_id
* @param year
* @param month
* @param date
* @return
*/
public static int getDiaryCount(SiteBean site, SessionUserObject user, int catalog_id, int year, int month, int date){
StringBuffer hql = new StringBuffer("SELECT COUNT(*) FROM DiaryBean AS a WHERE a.status=:status AND a.site.id=:site");
if(!site.isOwner(user)){
//排除用戶沒有權限訪問的分類
hql.append(" AND (a.catalog.type<>:cat_type");
if(user != null)
hql.append(" OR (a.catalog.type=:cat_type AND a.catalog.id IN (SELECT p.key.catalog FROM CatalogPermBean AS p WHERE p.key.user=:user))");
hql.append(')');
}
if (catalog_id > 0)
hql.append(" AND a.catalog.id=:catalog");
if(year > 0 || month > 0 || date > 0){
hql.append(" AND a.writeTime >= :beginTime AND a.writeTime < :endTime");
}
try {
Session ssn = getSession();
Query q = ssn.createQuery(hql.toString());
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());
}
}
if (catalog_id > 0) {
q.setInteger("catalog", catalog_id);
}
if(year > 0 || month > 0 || date > 0){
Calendar[] cals = genTimeParams(year,month,date);
q.setTimestamp("beginTime", cals[0].getTime());
q.setTimestamp("endTime", cals[1].getTime());
}
return ((Number) q.uniqueResult()).intValue();
} finally {
hql = null;
}
}
/**
* 獲取時間段的前后兩個時間點
* @param year
* @param month
* @param date
* @return
*/
protected static Calendar[] genTimeParams(int year, int month, int date){
Calendar[] params = new Calendar[2];
if(year>0&&month>0&&date>0){//查詢某天
params[0] = DateUtils.getDateBegin(year,month,date);
params[1] = (Calendar)params[0].clone();
params[1].add(Calendar.DATE,1);
}
else
if(year>0&&month>0){//查詢某月
params[0] = DateUtils.getDateBegin(year,month,1);
params[1] = (Calendar)params[0].clone();
params[1].add(Calendar.MONTH,1);
}
else
if(year>0){//查詢某年
params[0] = DateUtils.getDateBegin(year,1,1);
params[1] = (Calendar)params[0].clone();
params[1].add(Calendar.YEAR,1);
}
return params;
}
/**
* 獲取指定網(wǎng)站指定分類的日記
* @param site
* @param user
* @param catalog_id
* @param year
* @param month
* @param date
* @param fromIdx
* @param count
* @return
*/
public static List listDiary(int year,int month,int date,int fromIdx, int count, boolean withContent){
StringBuffer hql = new StringBuffer("FROM ");
hql.append(withContent?"DiaryBean":"DiaryOutlineBean");
hql.append(" AS a WHERE a.status=:status");
//排除訪問受限的分類
hql.append(" AND (a.catalog.type<>:cat_type)");
if(year > 0 || month > 0 || date > 0){
hql.append(" AND a.writeTime >= :beginTime AND a.writeTime < :endTime");
}
hql.append(" ORDER BY a.id DESC");
try {
Session ssn = getSession();
Query q = ssn.createQuery(hql.toString());
q.setInteger("status", DiaryBean.STATUS_NORMAL);
q.setInteger("cat_type", CatalogBean.TYPE_OWNER);
if(year > 0 || month > 0 || date > 0){
Calendar[] cals = genTimeParams(year,month,date);
q.setTimestamp("beginTime", cals[0].getTime());
q.setTimestamp("endTime", cals[1].getTime());
}
if(fromIdx>0)
q.setFirstResult(fromIdx);
if(count>0)
q.setMaxResults(count);
return q.list();
} finally {
hql = null;
}
}
/**
* 獲取指定網(wǎng)站指定分類的日記
* @param site
* @param user
* @param catalog_id
* @param year
* @param month
* @param date
* @param fromIdx
* @param count
* @return
*/
public static List listDiary(SiteBean site, SessionUserObject user, int catalog_id, int year,int month,int date,
int fromIdx, int count, boolean withContent){
StringBuffer hql = new StringBuffer("FROM ");
hql.append(withContent?"DiaryBean":"DiaryOutlineBean");
hql.append(" AS a WHERE a.status=:status AND a.site.id=:site");
//超級管理員也不能看其他人網(wǎng)站的隱藏目錄(2006-5-22 by Winter Lau)
if(user==null || site.getOwner().getId() != user.getId()){
//排除用戶沒有權限訪問的分類
hql.append(" AND (a.catalog.type<>:cat_type");
if(user != null)
hql.append(" OR (a.catalog.type=:cat_type AND a.catalog.id IN (SELECT p.key.catalog FROM CatalogPermBean AS p WHERE p.key.user=:user))");
hql.append(')');
}
if (catalog_id > 0)
hql.append(" AND a.catalog.id=:catalog");
if(year > 0 || month > 0 || date > 0){
hql.append(" AND a.writeTime >= :beginTime AND a.writeTime < :endTime");
}
hql.append(" ORDER BY a.id DESC");
try {
Session ssn = getSession();
Query q = ssn.createQuery(hql.toString());
q.setInteger("status", DiaryBean.STATUS_NORMAL);
q.setInteger("site", site.getId());
if(user==null || site.getOwner().getId() != user.getId()){
q.setInteger("cat_type", CatalogBean.TYPE_OWNER);
if(user != null){
q.setInteger("user", user.getId());
}
}
if (catalog_id > 0) {
q.setInteger("catalog", catalog_id);
}
if(year > 0 || month > 0 || date > 0){
Calendar[] cals = genTimeParams(year,month,date);
q.setTimestamp("beginTime", cals[0].getTime());
q.setTimestamp("endTime", cals[1].getTime());
}
if(fromIdx>0)
q.setFirstResult(fromIdx);
if(count>0)
q.setMaxResults(count);
return q.list();
} finally {
hql = null;
}
}
/**
* 讀取某個時間點以后的所有正常的日記(SearchEnginePlugIn::buildLogIndex)
* @param date
* @param max_count
* @return
* @throws Exception
*/
public static List listDiaryAfter(Date date, int max_count){
return executeNamedQuery("LIST_DIARY_AFTER", 0, max_count, new Object[]{date,
DiaryBean.I_STATUS_NORMAL, new Integer(CatalogBean.TYPE_OWNER)});
}
/**
* 增加文章的引用數(shù)
* @param catalog_id
* @param incCount
* @return
* @throws SQLException
*/
static int incTrackBackCount(Session ssn, int log_id, int incCount){
Query q = ssn.getNamedQuery("INC_DIARY_TB_COUNT");
q.setInteger(0, incCount);
q.setInteger(1, log_id);
return q.executeUpdate();
}
/* (non-Javadoc)
* @see com.liusoft.dlog4j.search.SearchDataProvider#fetchAfter(java.util.Date)
*/
public List fetchAfter(Date beginTime) throws Exception {
return DiaryDAO.listDiaryAfter(beginTime, -1);
}
/**
* 列出最新日記評論
* @param site
* @param user
* @param fromIdx
* @param count
* @return
*/
public static List listDiaryReplies(SiteBean site, int fromIdx, int count, SessionUserObject user){
StringBuffer hql = new StringBuffer("FROM DiaryReplyBean AS r WHERE r.status=:status AND r.site.id=:site AND r.diary.status=:diary_status");
if(!site.isOwner(user)){
//排除用戶沒有權限訪問的分類
hql.append(" AND (r.diary.catalog.type<>:cat_type");
if(user != null)
hql.append(" OR (r.diary.catalog.type=:cat_type AND r.diary.catalog.id IN (SELECT p.key.catalog FROM CatalogPermBean AS p WHERE p.key.user=:userid))");
hql.append(')');
hql.append(" AND (r.ownerOnly = 0 OR r.user.id = :userid)");
}
hql.append(" ORDER BY r.id DESC");
Session ssn = getSession();
Query q = ssn.createQuery(hql.toString());
q.setInteger("status", DiaryReplyBean.STATUS_NORMAL);
q.setInteger("site", site.getId());
q.setInteger("diary_status", DiaryOutlineBean.STATUS_NORMAL);
if(!site.isOwner(user)){
q.setInteger("cat_type", CatalogBean.TYPE_OWNER);
q.setInteger("userid", (user!=null)?user.getId():-1);
}
if(fromIdx>0)
q.setFirstResult(fromIdx);
if(count>0)
q.setMaxResults(count);
return q.list();
}
/**
* 分頁列出某篇日記的評論
* @param log_id
* @param fromIdx
* @param count
* @return
*/
public static List listDiaryReplies(int log_id, int fromIdx, int count, boolean reverse){
String hql_name = reverse?"LIST_REPLIES_OF_DIARY":"LIST_REPLIES_OF_DIARY2";
return executeNamedQuery(hql_name, fromIdx, count, log_id);
}
/**
* 讀取某個時間點以后的所有正常的評論(SearchEnginePlugIn::buildReplyIndex)
* @param date
* @return
* @throws Exception
*/
public static List listDiaryRepliesAfter(Date date){
return findNamedAll("LIST_DIARY_REPLIES", new Object[]{date, _ReplyBean.I_STATUS_NORMAL, CatalogBean.I_TYPE_OWNER});
}
/**
* 獲取評論總數(shù)(j_replies.vm)
* @param site
* @param user
* @return
*/
public static int getDiaryReplyCount(SiteBean site, SessionUserObject user){
StringBuffer hql = new StringBuffer("SELECT COUNT(*) FROM DiaryReplyBean AS r WHERE r.status=? AND r.site.id=?");
if(!site.isOwner(user)){
//排除用戶沒有權限訪問的分類
hql.append(" AND (r.diary.catalog.type<>?");
if(user != null)
hql.append(" OR (r.diary.catalog.type=? AND r.diary.catalog.id IN (SELECT p.key.catalog FROM CatalogPermBean AS p WHERE p.key.user=?))");
hql.append(')');
}
Session ssn = getSession();
Query q = ssn.createQuery(hql.toString());
q.setInteger(0, DiaryReplyBean.STATUS_NORMAL);
q.setInteger(1, site.getId());
if(!site.isOwner(user)){
q.setInteger(2, CatalogBean.TYPE_OWNER);
if(user != null){
q.setInteger(3, CatalogBean.TYPE_OWNER);
q.setInteger(4, user.getId());
}
}
return ((Number)q.uniqueResult()).intValue();
}
/**
* 返回指定站點的日記評論總數(shù)
* @param site
* @return
*/
public static int getDiaryReplyCount(int site){
String hql = "SELECT COUNT(*) FROM DiaryReplyBean AS d WHERE d.status=?";
if(site>0){
hql += " AND d.site.id=?";
return executeStatAsInt(hql, DiaryReplyBean.STATUS_NORMAL, site);
}
return executeStatAsInt(hql, DiaryReplyBean.STATUS_NORMAL);
}
/**
* 刪除日記評論,自動減少對應日記的評論數(shù)
* @param reply
*/
public static void deleteDiaryReply(DiaryReplyBean reply){
Session ssn = getSession();
try{
beginTransaction();
if(reply.getDiary()!=null)
reply.getDiary().incReplyCount(-1);
if(reply.getUser()!=null)
reply.getUser().getCount().incArticleReply(-1);
ssn.delete(reply);
commit();
}catch(HibernateException e){
rollback();
}
}
/**
* 創(chuàng)建日記評論,自動更新對應日記的評論數(shù)
* 當評論數(shù)超過最大的允許評論數(shù)后自動鎖貼
* @param reply
*/
public static void createDiaryReply(DiaryReplyBean reply){
try{
Session ssn = getSession();
int max_reply_count = ConfigDAO.getMaxReplyCount(reply.getSite().getId());
beginTransaction();
reply.getDiary().incReplyCount(1);
if(reply.getDiary().getReplyCount()>=max_reply_count && max_reply_count > 0)
reply.getDiary().setLock(1);
reply.getDiary().setLastReplyTime(new Date());
if(reply.getUser()!=null)
reply.getUser().getCount().incArticleReply(1);
ssn.save(reply);
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -