?? photodao.java
字號:
}
/**
* 更新照片信息
* @param new_album_id
* @param photo
* @param newKeyword
* @param cover 是否設置為封面
* @throws ObjectNotFoundException
* @throws IllegalAccessException
*/
public static void update(int new_album_id, PhotoBean photo, String newKeyword, boolean cover)
throws ObjectNotFoundException, IllegalAccessException {
if(photo==null || new_album_id <1)
return;
try{
beginTransaction();
if(photo.getAlbum().getId() != new_album_id){
//清除原有相簿的封面屬性
if (photo.getAlbum().getCover() != null
&& photo.getAlbum().getCover().getId() == photo.getId())
photo.getAlbum().setCover(null);
AlbumBean new_album = AlbumDAO.getAlbumByID(new_album_id);
if(new_album == null)
throw new ObjectNotFoundException(String.valueOf(new_album));
if(new_album.getSite().getId()!=photo.getSite().getId())
throw new IllegalAccessException(new_album.getName());
//修改新相簿的相片數 (增一)
AlbumBean parent = new_album;
int deep = 0;
do{
if(parent == null)
break;
deep ++;
parent.incPhotoCount(1);
parent = parent.getParent();
}while(deep < 10);//最多遍歷十級相簿
//修改舊相簿的相片數(減一)
parent = photo.getAlbum();
deep = 0;
do{
if(parent == null)
break;
deep ++;
parent.incPhotoCount(-1);
parent = parent.getParent();
}while(deep < 10);//最多遍歷十級相簿
photo.setAlbum(new_album);
}
//設置相簿封面
if(cover){
photo.getAlbum().setCover(photo);
}
if (photo.getAlbum().getType() == AlbumBean.TYPE_PUBLIC
&& photo.getStatus() != PhotoBean.STATUS_PRIVATE) {
if(!StringUtils.equals(photo.getKeyword(), newKeyword)){
TagDAO.deleteTagByRefId(photo.getId(), DiaryBean.TYPE_PHOTO);
//更新標簽
photo.setKeyword(newKeyword);
List tags = photo.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(photo.getSite());
tag.setRefId(photo.getId());
tag.setRefType(DiaryBean.TYPE_PHOTO);
tag.setName((String)tags.get(i));
//System.out.println("************************ tagName: "+tag.getName());
photo.getTags().add(tag);
tag_count ++;
}
}
}
}
else{
//對于有訪問權限控制的相簿中的照片,刪除其對應的標簽
TagDAO.deleteTagByRefId(photo.getId(), DiaryBean.TYPE_PHOTO);
}
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
/**
* 寫照片信息到數據庫
* @param album
* @param photo
* @param cover
* @throws IllegalAccessException
* @throws ObjectNotFoundException
*/
public static void create(AlbumBean album, PhotoBean photo, boolean cover)
throws IllegalAccessException, ObjectNotFoundException
{
if(photo==null || album ==null)
throw new IllegalArgumentException();
if(album.getSite().getId()!=photo.getSite().getId())
throw new IllegalAccessException(album.getName());
photo.setAlbum(album);
Calendar cal = Calendar.getInstance();
photo.setYear(cal.get(Calendar.YEAR));
photo.setMonth((cal.get(Calendar.MONTH)+1));
photo.setDate(cal.get(Calendar.DATE));
photo.setCreateTime(cal.getTime());
Session ssn = getSession();
try{
beginTransaction();
//修改site的已用相冊空間
int photo_site = DLOG4JUtils.sizeInKbytes(photo.getPhotoInfo().getSize());
photo.getSite().getCapacity().incPhotoUsed(photo_site);
//修改相簿的相片數
album.setPhotoCount(album.getPhotoCount()+1);
if(cover)
album.setCover(photo);
//遞歸所有父相簿
AlbumBean parent = album.getParent();
int deep = 0;
do{
if(parent == null)
break;
deep ++;
parent.incPhotoCount(1);
parent = parent.getParent();
}while(deep < 10);//最多遍歷十級相簿
photo.getUser().getCount().incPhotoCount(1);
ssn.save(photo);
if (album.getType() == AlbumBean.TYPE_PUBLIC
&& photo.getStatus() != PhotoBean.STATUS_PRIVATE) {
List tags = photo.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(photo.getSite());
tag.setRefId(photo.getId());
tag.setRefType(DiaryBean.TYPE_PHOTO);
tag.setName(tag_name);
ssn.save(tag);
tag_count ++;
}
}
}
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
/**
* 獲取某個網站當前所有照片的大小總和
* @param sid
* @param album_id
* @return
*/
public static int getTotalPhotoSize(int sid, int album_id){
if(sid < 1)
return -1;
StringBuffer hql = new StringBuffer("SELECT SUM(p.photoInfo.size) FROM PhotoBean AS p WHERE p.site.id=?");
if(album_id > 0)
hql.append(" AND p.album.id=?");
Session ssn = getSession();
Query q = ssn.createQuery(hql.toString());
q.setInteger(0, sid);
if(album_id > 0)
q.setInteger(1, album_id);
try{
Number size = (Number)q.uniqueResult();
return (size!=null)?size.intValue():0;
}finally{
hql = null;
}
}
/**
* 獲取某個網站當前所有照片總數
* @param sid
* @param album_id
* @return
*/
public static int getTotalPhotoCount(int sid, int album_id, int month){
if(sid < 1)
return -1;
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 >= 190001 && month <= 209912){
hql.append(" AND p.year = :year AND p.month = :month");
}
Session ssn = getSession();
Query q = ssn.createQuery(hql.toString());
q.setInteger("site", sid);
if(album_id > 0)
q.setInteger("album", album_id);
if(month >= 190001 && month <= 209912){
q.setInteger("year", month / 100);
q.setInteger("month", month % 100);
}
try{
Number size = (Number)q.uniqueResult();
return (size!=null)?size.intValue():0;
}finally{
hql = null;
}
}
/**
* 統計指定月份每天的照片數
* @param site
* @param loginUser
* @param month
* @return
*/
public static int[] statCalendarPhotoCount(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];
//查詢出當月的所有照片進行統計
boolean is_owner = site.isOwner(user);
StringBuffer hql = new StringBuffer("SELECT j.createTime FROM PhotoBean AS j WHERE j.createTime>=:beginTime AND j.createTime<:endTime AND j.site.id=:site");
if(!is_owner){
//排除用戶沒有權限訪問的分類
hql.append(" AND j.status=:status AND j.album.type=:album_type");
}
try{
Session ssn = getSession();
Query q = ssn.createQuery(hql.toString()).setCacheable(true);
q.setTimestamp("beginTime", firstDate.getTime());
q.setTimestamp("endTime", nextMonthFirstDate.getTime());
q.setInteger("site", site.getId());
if(!is_owner){
q.setInteger("status", PhotoBean.STATUS_NORMAL);
q.setInteger("album_type", AlbumBean.TYPE_PUBLIC);
}
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;
}
}
/**
* @see com.liusoft.dlog4j.search.SearchDataProvider#fetchAfter(Date)
*/
public List fetchAfter(Date beginTime) throws Exception {
return findNamedAll("LIST_PHOTO_AFTER", new Object[]{beginTime,
PhotoBean.I_STATUS_NORMAL, AlbumBean.I_TYPE_PRIVATE});
}
/**
* 返回指定站點的相片評論總數
* @param site
* @return
*/
public static int getPhotoReplyCount(int site){
String hql = "SELECT COUNT(*) FROM PhotoReplyBean AS d WHERE d.status=?";
if(site>0){
hql += " AND d.site.id=?";
return executeStatAsInt(hql, PhotoReplyBean.STATUS_NORMAL, site);
}
return executeStatAsInt(hql, PhotoReplyBean.STATUS_NORMAL);
}
/**
* 獲取相冊評論總數(p_replies.vm)
* @param site
* @param user
* @param fromIdx
* @param count
* @return
*/
public static int getPhotoReplyCount(SiteBean site, SessionUserObject user){
boolean is_owner = site.isOwner(user);
StringBuffer hql = new StringBuffer("SELECT COUNT(*) FROM PhotoReplyBean AS r WHERE r.status=? AND r.site.id=?");
if(!is_owner){
//排除用戶沒有權限訪問的分類
hql.append(" AND r.photo.album.type=? AND r.photo.status=?");
return executeStatAsInt(hql.toString(),PhotoReplyBean.STATUS_NORMAL,site.getId(),AlbumBean.TYPE_PUBLIC,PhotoBean.STATUS_NORMAL);
}
return executeStatAsInt(hql.toString(),PhotoReplyBean.STATUS_NORMAL,site.getId());
}
/**
* 列出最新照片評論
* @param site
* @param user
* @param fromIdx
* @param count
* @return
*/
public static List listPhotoReplies(SiteBean site, int fromIdx, int count, SessionUserObject user){
boolean is_owner = site.isOwner(user);
StringBuffer hql = new StringBuffer("FROM PhotoReplyBean AS r WHERE r.status=:status AND r.site.id=:site AND r.photo.status=:photo_status");
if(!is_owner){
//排除用戶沒有權限訪問的分類
hql.append(" AND r.photo.album.type=:album_type");
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", PhotoReplyBean.STATUS_NORMAL);
q.setInteger("photo_status", PhotoBean.STATUS_NORMAL);
q.setInteger("site", site.getId());
if(!is_owner){
q.setInteger("album_type", AlbumBean.TYPE_PUBLIC);
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 listPhotoReplies(int photo_id, int fromIdx, int count){
return executeNamedQuery("REPLIES_OF_PHOTO",fromIdx,count,photo_id);
}
/**
* 創建照片評論,自動更新對應照片的評論數
* 當評論數超過最大的允許評論數后自動鎖貼
* @param reply
*/
public static void createPhotoReply(PhotoReplyBean reply){
try{
Session ssn = getSession();
int max_reply_count = ConfigDAO.getMaxReplyCount(reply.getSite().getId());
beginTransaction();
reply.getPhoto().incReplyCount(1);
if(reply.getPhoto().getReplyCount()>=max_reply_count)
reply.getPhoto().setLock(1);
reply.getPhoto().setLastReplyTime(new Date());
if(reply.getUser()!=null)
reply.getUser().getCount().incPhotoReplyCount(1);
ssn.save(reply);
commit();
}catch(HibernateException e){
rollback();
throw e;
}
}
/**
* 刪除照片評論,自動減少對應照片的評論數
* @param reply
*/
public static void deletePhotoReply(PhotoReplyBean reply){
Session ssn = getSession();
try{
beginTransaction();
reply.getPhoto().incReplyCount(-1);
if(reply.getUser()!=null)
reply.getUser().getCount().incPhotoReplyCount(-1);
ssn.delete(reply);
commit();
}catch(HibernateException e){
rollback();
}
}
/**
* 讀取某個時間點以后的所有正常的評論(SearchEnginePlugIn::buildReplyIndex)
* @param date
* @return
* @throws Exception
*/
public static List listPhotoRepliesAfter(Date date){
return executeNamedQuery("LIST_PHOTO_REPLIES_AFTER", -1, -1, new Object[]{date,
_ReplyBean.I_STATUS_NORMAL, PhotoBean.I_STATUS_NORMAL,
AlbumBean.I_TYPE_PRIVATE});
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -