com.liusoft.dlog4j.dao.PhotoDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.liusoft.dlog4j.dao.PhotoDAO.java

Source

/*
 *  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.ArrayList;
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.FckUploadFileBeanBase;
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.FckUploadFileBean;
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.db.HibernateUtils;
import com.liusoft.dlog4j.util.DLOG4JUtils;
import com.liusoft.dlog4j.util.DateUtils;
import com.liusoft.dlog4j.util.StringUtils;

import dlog.common.search.SearchDataProvider;
import dlog.common.search.SearchEnabled;

/**
 * 
 * 
 * @author Winter Lau
 */
public class PhotoDAO extends DAO implements SearchDataProvider {

    /**
     * 
     * 
     * @param site_id
     * @param parent
     * @return
     */
    public static List listAlbums(int site_id, AlbumBean parent) {
        StringBuffer hql = new StringBuffer("FROM AlbumBean a WHERE a.site.id=?");
        if (parent != null) {
            hql.append(" AND a.parent.id=");
            hql.append(parent.getId());
        } else {
            hql.append(" AND a.parent IS NULL");
        }
        hql.append(" ORDER BY a.sortOrder");
        return findAllCacheable("query.albums_of_site", hql.toString(), site_id);
    }

    /**
     * Sitedays
     * 
     * @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 executeNamedQueryCacheable("query.hot_photos", "LIST_HOT_PHOTOS", 0, count,
                PhotoOutlineBean.STATUS_NORMAL, cal.getTime(), AlbumBean.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
     *            ,20050620056
     * @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);
    }

    /**
     * 
     * 
     * @return
     */
    public static int photoCount() {
        String hql = "SELECT COUNT(*) FROM PhotoOutlineBean AS p";
        return executeStatAsInt(hql);
    }

    /**
     * 
     * 
     * @param site
     * @param user
     * @param album_id
     * @param month_stamp
     *            ,20050620056
     * @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)");
        else {
            // hql.append(" AND (TO_DAYS(NOW()) - TO_DAYS(p.site.createTime) >
            // 2)");
        }
        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());
            q.setCacheable(true).setCacheRegion("query.new_photos");
            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
     *            ,20050620056
     * @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;
        }
    }

    /**
     * 
     * 
     * @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();
            evict(PhotoBean.class, photo.getId());
            evict(PhotoOutlineBean.class, photo.getId());
        } 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;
        }
    }

    /**
     *  FIXME: photo/show.vm
     * 
     * @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());
            q.setCacheable(true);
            q.setCacheRegion("query.photo_calendar");
            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)
     */
    @SuppressWarnings("unchecked")
    public List<SearchEnabled> fetchAfter(Date beginTime) throws Exception {
        return findNamedAll("LIST_PHOTO_AFTER", beginTime, PhotoBean.STATUS_NORMAL, AlbumBean.TYPE_PRIVATE);
    }

    public void finish() {
        HibernateUtils.closeSession();
    }

    /**
     * 
     * 
     * @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.setCacheable(true).setCacheRegion("query.new_replies_of_site");
        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, date, _ReplyBean.STATUS_NORMAL,
                PhotoBean.STATUS_NORMAL, AlbumBean.TYPE_PRIVATE);
    }

    // ==================  DLOG 3.5===============

    public static List diaryRelating(SiteBean bean, int fromidx, int page) {
        StringBuffer hql = new StringBuffer();
        // hql.append(" FROM FckUploadFileBean as fub");
        // hql.append(" WHERE fub.site.id = ?");

        hql.append(" SELECT f.id,( SELECT d.title FROM DiaryBean as d WHERE d.id=f.refId),f.uri,f.site.id ");
        hql.append(" FROM FckUploadFileBean as f ");
        hql.append(" WHERE f.site.id=? AND f.refType=? ");

        List objs = executeQuery(hql.toString(), fromidx, page, bean.getId(),
                FckUploadFileBeanBase.FILE_TYPE_IMAGE);
        FckUploadFileBean fbean;
        List fs = new ArrayList();
        for (int i = 0; i < objs.size(); i++) {
            int id = ((Number) ((Object[]) objs.get(i))[0]).intValue();
            String name1 = (String) ((Object[]) objs.get(i))[1];
            String name2 = (String) ((Object[]) objs.get(i))[2];
            int sid = ((Number) ((Object[]) objs.get(i))[3]).intValue();
            fbean = new FckUploadFileBean();
            fbean.setId(id);
            fbean.setSavePath(name1);
            fbean.setUri(name2);
            SiteBean sb = new SiteBean();
            sb.setId(sid);
            fbean.setSite(sb);
            fs.add(fbean);
        }
        return fs;
    }

    public static int diaryRelatingCount(SiteBean bean) {
        StringBuffer hql = new StringBuffer();
        hql.append(" SELECT COUNT(*) ");
        hql.append(" FROM FckUploadFileBean as fub");
        hql.append(" WHERE fub.site.id = ?");
        int count = Integer.parseInt(uniqueResult(hql.toString(), bean.getId()).toString());
        return count;

    }

    public static List list_albums_view(SiteBean bean) {
        // 
        StringBuffer hql1 = new StringBuffer();
        hql1.append(" FROM AlbumBean as ab");
        hql1.append(" WHERE ab.site.id = ?");
        List list = executeQuery(hql1.toString(), -1, -1, bean.getId());
        // 
        StringBuffer hql2 = new StringBuffer();
        hql2.append(" FROM FckUploadFileBean as fub");
        hql2.append(" WHERE fub.site.id = ? ");
        //hql2.append(" GROUP BY  fub.site.id ");
        hql2.append(" ORDER BY  fub.id desc ");
        FckUploadFileBean fBean = (FckUploadFileBean) uniqueResult(hql2.toString(), bean.getId());
        //   
        int count = diaryRelatingCount(bean);

        // 
        AlbumBean albumBean = new AlbumBean();
        PhotoBean cover = new PhotoBean();
        String previewURL = "";
        if (fBean != null)
            previewURL = fBean.getUri();
        // 
        cover.setPreviewURL(previewURL);
        albumBean.setId(0);
        // 
        albumBean.setName("");
        // 
        albumBean.setPhotoCount(count);
        //   4
        albumBean.setType(4);
        // 
        albumBean.setCover(cover);

        list.add(0, albumBean);
        return list;
    }

}