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

Java tutorial

Introduction

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

Source

/*
 *  BBSForumDAO.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.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.liusoft.dlog4j.CapacityExceedException;
import com.liusoft.dlog4j.base.Orderable;
import com.liusoft.dlog4j.beans.ForumBean;
import com.liusoft.dlog4j.beans.SiteBean;

/**
 * 
 * 
 * @author Winter Lau
 */
public class BBSForumDAO extends DAO {

    /**
     * 
     * 
     * @param forum_id
     * @return
     */
    public static ForumBean getForumByID(int forum_id) {
        if (forum_id < 0)
            return null;
        return (ForumBean) getBean(ForumBean.class, forum_id);
    }

    /**
     * 
     * @param forum_id
     * @return
     */
    public static int lockForumByID(int site_id, int forum_id) {
        return changeForumStatus(site_id, forum_id, ForumBean.STATUS_LOCKED);
    }

    /**
     * 
     * @param forum_id
     * @return
     */
    public static int hideForumByID(int site_id, int forum_id) {
        return changeForumStatus(site_id, forum_id, ForumBean.STATUS_HIDDEN);
    }

    /**
     * 
     * @param forum_id
     * @return
     */
    protected static int changeForumStatus(int site_id, int forum_id, int status) {
        return commitNamedUpdate("CHANGE_FORUM_STATUS", status, forum_id, site_id);
    }

    /**
     * 
     * 
     * @param obj
     * @param pos
     * @param up
     * @throws CapacityExceedException
     */
    public static void createForum(ForumBean obj, int pos, boolean up) throws CapacityExceedException {
        Session ssn = getSession();
        int order_value = 1;
        if (pos > 0) {
            ForumBean friend = (ForumBean) ssn.get(ForumBean.class, new Integer(pos));
            order_value = friend.getSortOrder();
        }
        obj.setSortOrder(order_value - (up ? 1 : 0));
        try {
            beginTransaction();
            ssn.save(obj);
            // 
            Query q = ssn.getNamedQuery("LIST_FORUMS");
            q.setInteger(0, obj.getSite().getId());
            List links = q.list();
            if (links.size() >= ConfigDAO.getMaxCatalogCount(obj.getSite().getId()))
                throw new CapacityExceedException(links.size());
            if (links.size() > 1) {
                for (int i = 0; i < links.size(); i++) {
                    Orderable lb = (Orderable) links.get(i);
                    executeNamedUpdate("UPDATE_FORUM_ORDER", i + 1, lb.getId());
                }
            }
            commit();
        } catch (HibernateException e) {
            rollback();
            throw e;
        }
    }

    /**
     * 
     * 
     * @param site_id
     * @param forum_id
     * @return
     */
    public static int deleteForumByID(int site_id, int forum_id) {
        return commitNamedUpdate("DELETE_FORUM", site_id, forum_id);
    }

    /**
     *  
     *  
     * 
     * @param site
     * @param linkid
     * @param up 
     */
    public static void move(SiteBean site, int cat_id, boolean up) {
        List objects = site.getForums();
        for (int i = 0; i < objects.size(); i++) {
            Orderable obj = (Orderable) objects.get(i);
            int sort_order = obj.getSortOrder();
            if (obj.getId() == cat_id) {
                if (up) {
                    if (i > 0) {
                        try {
                            Orderable prev = (Orderable) objects.get(i - 1);
                            int prev_order = prev.getSortOrder();
                            beginTransaction();
                            executeNamedUpdate("UPDATE_FORUM_ORDER", sort_order, prev.getId());
                            executeNamedUpdate("UPDATE_FORUM_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_FORUM_ORDER", sort_order, next.getId());
                            executeNamedUpdate("UPDATE_FORUM_ORDER", next_order, obj.getId());
                            commit();
                        } catch (HibernateException e) {
                            rollback();
                            throw e;
                        }
                    }
                }
                break;
            }
        }
    }

}