?? photodao.java
字號:
/*
* PhotoDAO.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.sql.SQLException;
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.ObjectNotFoundException;
import com.liusoft.dlog4j.SessionUserObject;
import com.liusoft.dlog4j.base._PhotoBase;
import com.liusoft.dlog4j.base._ReplyBean;
import com.liusoft.dlog4j.beans.AlbumBean;
import com.liusoft.dlog4j.beans.DiaryBean;
import com.liusoft.dlog4j.beans.PhotoBean;
import com.liusoft.dlog4j.beans.PhotoOutlineBean;
import com.liusoft.dlog4j.beans.PhotoReplyBean;
import com.liusoft.dlog4j.beans.SiteBean;
import com.liusoft.dlog4j.beans.TagBean;
import com.liusoft.dlog4j.search.SearchDataProvider;
import com.liusoft.dlog4j.util.DLOG4JUtils;
import com.liusoft.dlog4j.util.DateUtils;
import com.liusoft.dlog4j.util.StringUtils;
/**
* 操作照片的數據庫訪問接口
* @author Winter Lau
*/
public class PhotoDAO extends DAO implements SearchDataProvider {
/**
* 獲取具有專欄標志的Site在days天內的最熱門的照片
* @param days
* @param count
* @return
*/
public static List listHotPhotos(int days, int count){
Calendar cal = Calendar.getInstance();
DateUtils.resetTime(cal);
cal.add(Calendar.DATE, -days);
return executeNamedQuery("LIST_HOT_PHOTOS", 0, count,
new Object[]{PhotoOutlineBean.I_STATUS_NORMAL, cal.getTime(),
AlbumBean.I_TYPE_PUBLIC});
}
/**
* 返回指定網站的相片數,如果沒有指定網站則返回所有相片數
* @param site
* @return
*/
public static int getPhotoCount(int site){
String hql = "SELECT COUNT(*) FROM PhotoBean AS d WHERE d.status=?";
if(site>0){
hql += " AND d.site.id=?";
return executeStatAsInt(hql, PhotoBean.STATUS_NORMAL, site);
}
return executeStatAsInt(hql, PhotoBean.STATUS_NORMAL);
}
/**
* 得到指定照片的上一篇(用于顯示照片頁)
* @param site
* @param user
* @param album_id
* @param photo_id
* @return
*/
public static PhotoOutlineBean getPrevPhoto(SiteBean site, SessionUserObject user, int album_id, int photo_id){
if(site==null)
return null;
boolean is_owner = site.isOwner(user);
StringBuffer hql = new StringBuffer("FROM PhotoOutlineBean AS p WHERE p.status=:photo_status AND p.site.id=:site AND p.id<:photo");
if(!is_owner){
//排除用戶沒有權限訪問的分類
hql.append(" AND p.album.type=:album_type");
}
if (album_id > 0){
hql.append(" AND p.album.id=:album");
}
hql.append(" ORDER BY p.id DESC");
Session ssn = getSession();
try{
Query q = ssn.createQuery(hql.toString());
q.setInteger("photo_status", PhotoBean.STATUS_NORMAL);
q.setInteger("site", site.getId());
q.setInteger("photo", photo_id);
if(album_id > 0)
q.setInteger("album", album_id);
if(!is_owner)
q.setInteger("album_type", AlbumBean.TYPE_PUBLIC);
q.setMaxResults(1);
return (PhotoOutlineBean)q.uniqueResult();
}finally{
hql = null;
}
}
/**
* 得到指定照片的上一篇(用于顯示照片頁)
* @param site
* @param user
* @param album_id
* @param photo_id
* @return
*/
public static PhotoOutlineBean getNextPhoto(SiteBean site, SessionUserObject user, int album_id, int photo_id){
if(site==null) return null;
StringBuffer hql = new StringBuffer("FROM PhotoOutlineBean AS p WHERE p.status=:photo_status AND p.site.id=:site AND p.id>:photo");
if(user==null || !site.isOwner(user)){
//排除用戶沒有權限訪問的分類
hql.append(" AND p.album.type=:album_type");
}
if (album_id > 0){
hql.append(" AND p.album.id=:album");
}
hql.append(" ORDER BY p.id ASC");
Session ssn = getSession();
try{
Query q = ssn.createQuery(hql.toString());
q.setInteger("photo_status", PhotoBean.STATUS_NORMAL);
q.setInteger("site", site.getId());
q.setInteger("photo", photo_id);
if(album_id > 0)
q.setInteger("album", album_id);
if(!site.isOwner(user))
q.setInteger("album_type", AlbumBean.TYPE_PUBLIC);
q.setMaxResults(1);
return (PhotoOutlineBean)q.uniqueResult();
}finally{
hql = null;
}
}
/**
* 增加照片的閱讀數
* @param photo_id
* @param incCount
* @return
*/
public static void incViewCount(int photo_id, int incCount){
commitNamedUpdate("INC_PHOTO_VIEW_COUNT", incCount, photo_id);
}
/**
* 加載照片
* @param photo_id
* @return
*/
public static PhotoBean getPhotoByID(int photo_id){
if(photo_id <= 0)
return null;
return (PhotoBean)getBean(PhotoBean.class, photo_id);
}
/**
* 加載照片
* @param photo_id
* @return
*/
public static PhotoOutlineBean getPhotoOutlineByID(int photo_id){
if(photo_id <= 0)
return null;
return (PhotoOutlineBean)getBean(PhotoOutlineBean.class, photo_id);
}
/**
* 列出相片中的有效月份
* @param site_id
* @return
*/
public static List listMonths(int site_id){
return findNamedAll("PHOTO_MONTHS",site_id);
}
/**
* 根據條件列出照片
* @param site
* @param user
* @param album_id
* @param month_stamp 月份戳,例如200506表示看2005年6月份的照片
* @param fromIdx
* @param count
* @return
* @see com.liusoft.dlog4j.velocity.DLOG_VelocityTool#list_photos(SiteBean, int, int)
*/
public static List listPhotos(SiteBean site, SessionUserObject user, int album_id,
int month_stamp, int date, int fromIdx, int count) {
StringBuffer hql = new StringBuffer("FROM PhotoOutlineBean AS p WHERE p.site.id=:site");
if(album_id > 0)
hql.append(" AND (p.album.id=:album OR p.album.parent.id=:album)");
if(month_stamp > 190000 && month_stamp < 209912){
hql.append(" AND p.year=:year AND p.month=:month");
}
if(user==null || site.getOwner().getId()!=user.getId()){
hql.append(" AND p.status<>:hidden_status AND p.album.type=:owner_album");
}
if(date>0){
hql.append(" AND p.date=:date");
}
hql.append(" ORDER BY p.id DESC");
Session ssn = getSession();
try{
Query q = ssn.createQuery(hql.toString());
q.setInteger("site", site.getId());
if(album_id > 0)
q.setInteger("album", album_id);
if(month_stamp > 190000 && month_stamp < 209912){
q.setInteger("year", month_stamp / 100);
q.setInteger("month", month_stamp % 100);
}
if(user==null || site.getOwner().getId()!=user.getId()){
q.setInteger("hidden_status", PhotoBean.STATUS_PRIVATE);
q.setInteger("owner_album", AlbumBean.TYPE_PUBLIC);
}
if(date>0){
q.setInteger("date", date);
}
q.setFirstResult(fromIdx);
q.setMaxResults(count);
return q.list();
}finally{
hql = null;
}
}
/**
* 列出某個相簿的照片
* @param album
* @param fromIdx
* @param count
* @return
*/
public static List listPhotos(AlbumBean album, int fromIdx, int count){
Query q = getSession().getNamedQuery("PHOTOS_OF_ALBUM");
q.setInteger("album", album.getId());
//q.setInteger("hidden_status", PhotoBean.STATUS_PRIVATE);
if(fromIdx > 0)
q.setFirstResult(fromIdx);
if(count > 0)
q.setMaxResults(count);
return q.list();
}
/**
* 列出所有圖片(用于管理)
* @param fromIdx
* @param count
* @return
*/
public static List listPhotos(int fromIdx, int count){
String hql = "FROM PhotoOutlineBean AS p ORDER BY p.id DESC";
return executeQuery(hql, fromIdx, count, null);
}
/**
* 獲取照片數(用于管理)
* @return
*/
public static int photoCount(){
String hql = "SELECT COUNT(*) FROM PhotoOutlineBean AS p";
return executeStatAsInt(hql, null);
}
/**
* 根據條件列出照片
* @param site
* @param user
* @param album_id
* @param month_stamp 月份戳,例如200506表示看2005年6月份的照片
* @param fromIdx
* @param count
* @return
* @see com.liusoft.dlog4j.velocity.DLOG_VelocityTool#list_photos(SiteBean, int, int)
*/
public static List listPhotos(int album_id,int month_stamp, int date, int fromIdx, int count) {
StringBuffer hql = new StringBuffer("FROM PhotoOutlineBean AS p WHERE 1=1");
if(album_id > 0)
hql.append(" AND (p.album.id=:album OR p.album.parent.id=:album)");
if(month_stamp > 190000 && month_stamp < 209912){
hql.append(" AND p.year=:year AND p.month=:month");
}
hql.append(" AND p.status<>:hidden_status AND p.album.type=:owner_album");
if(date>0){
hql.append(" AND p.date=:date");
}
hql.append(" AND p.site.status=:site_status ORDER BY p.id DESC");
Session ssn = getSession();
try{
Query q = ssn.createQuery(hql.toString());
if(album_id > 0)
q.setInteger("album", album_id);
if(month_stamp > 190000 && month_stamp < 209912){
q.setInteger("year", month_stamp / 100);
q.setInteger("month", month_stamp % 100);
}
q.setInteger("hidden_status", PhotoBean.STATUS_PRIVATE);
q.setInteger("owner_album", AlbumBean.TYPE_PUBLIC);
if(date>0){
q.setInteger("date", date);
}
q.setInteger("site_status", SiteBean.STATUS_NORMAL);
q.setFirstResult(fromIdx);
q.setMaxResults(count);
return q.list();
}finally{
hql = null;
}
}
/**
* 根據條件列出照片
* @param site
* @param user
* @param album_id
* @param month_stamp 月份戳,例如200506表示看2005年6月份的照片
* @return
* @see com.liusoft.dlog4j.velocity.DLOG_VelocityTool#list_photos(SiteBean, int, int)
*/
public static int getPhotoCount(SiteBean site, SessionUserObject user, int album_id,
int month_stamp, int date) {
boolean is_owner = site.isOwner(user);
StringBuffer hql = new StringBuffer("SELECT COUNT(*) FROM PhotoBean AS p WHERE p.site.id=:site");
if(album_id > 0)
hql.append(" AND p.album.id=:album");
if(month_stamp > 190000 && month_stamp < 209912){
hql.append(" AND p.year=:year AND p.month=:month");
}
if(!is_owner){
hql.append(" AND p.status=:normal_status AND p.album.type=:public_album");
}
if(date>0){
hql.append(" AND p.date=:date");
}
Session ssn = getSession();
try{
Query q = ssn.createQuery(hql.toString());
q.setInteger("site", site.getId());
if(album_id > 0)
q.setInteger("album", album_id);
if(month_stamp > 190000 && month_stamp < 209912){
q.setInteger("year", month_stamp / 100);
q.setInteger("month", month_stamp % 100);
}
if(!is_owner){
q.setInteger("normal_status", PhotoBean.STATUS_NORMAL);
q.setInteger("public_album", AlbumBean.TYPE_PUBLIC);
}
if(date>0){
q.setInteger("date", date);
}
return ((Number)q.uniqueResult()).intValue();
}finally{
hql = null;
}
}
/**
* 刪除某張照片
* @param photo
* @throws SQLException
*/
public static void delete(_PhotoBase photo) throws SQLException{
if(photo == null)
return;
Session ssn = getSession();
try{
beginTransaction();
//釋放相冊空間
int photo_size = DLOG4JUtils.sizeInKbytes(photo.getPhotoInfo().getSize());
photo.getSite().getCapacity().incPhotoUsed(photo_size);
photo.getAlbum().incPhotoCount(-1);
photo.getUser().getCount().incPhotoCount(-1);
//遞歸所有父相簿
AlbumBean parent = photo.getAlbum().getParent();
int deep = 0;
do{
if(parent == null)
break;
deep ++;
parent.incPhotoCount(-1);
parent = parent.getParent();
}while(deep < 10);//最多遍歷十級相簿
//刪除標簽
TagDAO.deleteTagByRefId(photo.getId(), TagBean.TYPE_PHOTO);
//所有參與該相片評論者的相冊評論數減一
List rpls = photo.getReplies();
for(int i=0;rpls!=null&&i<rpls.size();i++){
PhotoReplyBean prb = (PhotoReplyBean)rpls.get(i);
if(prb.getUser()!=null){
prb.getUser().getCount().incPhotoReplyCount(-1);
}
}
//修改設置該照片為封面的相簿
executeUpdate("UPDATE AlbumBean AS a SET a.cover = NULL WHERE a.cover.id=?", photo.getId());
ssn.delete(photo);
commit();
}catch(HibernateException e){
rollback();
throw e;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -