?? albumdao.java
字號(hào):
/*
* AlbumDAO.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
* http://dlog4j.sourceforge.net
*
*/
package com.liusoft.dlog4j.dao;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import com.liusoft.dlog4j.CapacityExceedException;
import com.liusoft.dlog4j.base.Orderable;
import com.liusoft.dlog4j.beans.AlbumBean;
import com.liusoft.dlog4j.beans.SiteBean;
/**
* 相簿數(shù)據(jù)庫(kù)訪問接口
*
* @author Winter Lau
*/
public class AlbumDAO extends DAO {
public final static int MAX_ALBUM_COUNT = 20;
/**
* 判斷一個(gè)相簿是否為空,包括沒有照片和子相簿
* @param album_id
* @return
*/
public static boolean isAlbumEmpty(int album_id){
if(executeNamedStatAsInt("PHOTO_COUNT_OF_ALBUM", album_id, album_id) > 0)
return false;
//看是否有子相簿
return executeNamedStatAsInt("SUB_ALBUM_COUNT", album_id)<=0;
}
/**
* 移動(dòng)相片
*
* @param site
* @param fromCat
* @param toCat
* @return 返回移動(dòng)照片的數(shù)量
* @throws SQLException
*/
public static int movePhoto(int siteid, AlbumBean fromCat,AlbumBean toCat){
if(siteid<=0 || fromCat==null || toCat==null)
throw new IllegalArgumentException();
if(fromCat.getId()==toCat.getId())
return 0;
int single_photo_count = 0;
try {
//1. 計(jì)算源相簿的所有子相簿的照片數(shù),不包括本層的照片
int photo_count = 0;
List childs = fromCat.getChilds();
for(int i=0;childs!=null&&i<childs.size();i++){
AlbumBean abean = (AlbumBean)childs.get(i);
photo_count += abean.getPhotoCount();
}
//2. 計(jì)算要移動(dòng)的照片數(shù)
single_photo_count = fromCat.getPhotoCount()-photo_count;
//3. 修改照片所在的分類
beginTransaction();
executeNamedUpdate("MOVE_PHOTO", toCat.getId(), fromCat.getId(), siteid);
//4. 修改目標(biāo)相簿的照片數(shù)
executeNamedUpdate("INC_ALBUM_PHOTO_COUNT", single_photo_count, toCat.getId(), siteid);
//5. 修改源相簿的父一級(jí)相簿的相片數(shù)
AlbumBean src_parent = fromCat.getParent();
do{
if(src_parent == null)
break;
executeNamedUpdate("INC_ALBUM_PHOTO_COUNT", -single_photo_count, src_parent.getId(), siteid);
src_parent = src_parent.getParent();
}while(true);
//6. 修改目標(biāo)相簿的父一級(jí)相簿的照片數(shù)
AlbumBean obj_parent = toCat.getParent();
do{
if(obj_parent == null)
break;
executeNamedUpdate("INC_ALBUM_PHOTO_COUNT", single_photo_count, obj_parent.getId(), siteid);
obj_parent = obj_parent.getParent();
}while(true);
//7. 重新計(jì)算源相簿的照片數(shù)
executeNamedUpdate("UPDATE_ALBUM_PHOTO_COUNT", photo_count, fromCat.getId(), siteid);
//8.提交
commit();
} catch (HibernateException e) {
rollback();
throw e;
}
return single_photo_count;
}
/**
* 根據(jù)相簿的編號(hào)獲取相簿詳細(xì)信息
*
* @param album_id
* @return
*/
public static AlbumBean getAlbumByID(int album_id) {
if(album_id < 1)
return null;
return (AlbumBean)DAO.getBean(AlbumBean.class, album_id);
}
/**
* 調(diào)整分類順序 由于在創(chuàng)建分類的時(shí)候已經(jīng)可以保證所有分類是有序的遞增的 因此直接交換兩個(gè)分類的排序值即可
*
* @param site
* @param linkid
* @param up
* 向上調(diào)整或者向下調(diào)整
* @throws SQLException
*/
public static void move(SiteBean site, int album_id, boolean up){
List objects = site.getCatalogs();
for (int i = 0; i < objects.size(); i++) {
Orderable obj = (Orderable) objects.get(i);
int sort_order = obj.getSortOrder();
if (obj.getId() == album_id) {
if (up) {
if (i > 0) {
try {
Orderable prev = (Orderable) objects.get(i - 1);
int prev_order = prev.getSortOrder();
DAO.beginTransaction();
executeNamedUpdate("UPDATE_ALBUM_ORDER", sort_order, prev.getId());
executeNamedUpdate("UPDATE_ALBUM_ORDER", prev_order, obj.getId());
commit();
} catch (HibernateException e) {
rollback();
throw e;
}
}
} else {
if (i < (objects.size() - 1)) {
try {
Orderable next = (Orderable) objects.get(i + 1);
int next_order = next.getSortOrder();
beginTransaction();
executeNamedUpdate("UPDATE_ALBUM_ORDER", sort_order, next.getId());
executeNamedUpdate("UPDATE_ALBUM_ORDER", next_order, obj.getId());
commit();
} catch (HibernateException e) {
rollback();
throw e;
}
}
}
break;
}
}
}
/**
* 添加相簿
*
* @param parentId
* @param obj
* @param pos
* @param up
* @throws SQLException
* @throws CapacityExceedException
*/
public static void create(int parentId, AlbumBean obj, int pos, int direction)
throws CapacityExceedException {
Session ssn = getSession();
int order_value = 1;
if (pos > 0) {
AlbumBean friend = getAlbumByID(pos);
order_value = friend.getSortOrder();
}
// 父節(jié)點(diǎn)
if (parentId > 0){
AlbumBean parent = (AlbumBean)DAO.getBean(AlbumBean.class, parentId);
if(parent!=null)
obj.setParent(parent);
}
obj.setSortOrder(order_value - ((direction==1) ? 1 : 0));
try {
beginTransaction();
ssn.save(obj);
// 重新讀取鏈接列表,依照順序進(jìn)行整理
List albums = findNamedAll("LIST_ALBUM",obj.getSite().getId());
if (albums.size() >= ConfigDAO.getMaxAlbumCount(obj.getSite().getId()))
throw new CapacityExceedException(albums.size());
if (albums.size() > 1) {
for (int i = 0; i < albums.size(); i++) {
Orderable lb = (Orderable) albums.get(i);
executeNamedUpdate("UPDATE_ALBUM_ORDER", i+1, lb.getId());
}
}
commit();
} catch (HibernateException e) {
rollback();
throw e;
}
}
/**
* 刪除日記分類
* 此處不能改為HQL方式執(zhí)行,因?yàn)樾枰M(jìn)行級(jí)聯(lián)刪除
* @param siteid
* @param linkid
* @return
* @throws SQLException
*/
public static void delete(int siteid, int album_id){
AlbumBean album = (AlbumBean)getBean(AlbumBean.class, album_id);
if(album!=null && album.getSite().getId()==siteid)
delete(album);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -