📄 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 + -