⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 topichibernatedao.java

📁 javaBB,一套很不錯的JSP源碼,特共享給大家
💻 JAVA
字号:
package org.javabb.dao.hibernate;

import java.util.Date;
import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import org.javabb.dao.entity.ITopicDAO;
import org.javabb.vo.AnswerNotify;
import org.javabb.vo.AnswerNotifyPK;
import org.javabb.vo.FavUserTopic;
import org.javabb.vo.Forum;
import org.javabb.vo.Topic;
import org.springframework.orm.hibernate.HibernateCallback;

/*
 * Copyright 2004 JavaFree.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * $Id: TopicHibernateDAO.java,v 1.21.10.9 2007/08/31 21:04:43 daltoncamargo Exp $
 * @author Dalton Camargo - <a href="mailto:dalton@javabb.org">dalton@javabb.org </a> <br>
 * @author Ronald Tetsuo Miura
 */
public class TopicHibernateDAO extends HibernateDAO implements ITopicDAO {
	
	/**
     * @see org.javabb.dao.entity.ITopicDAO#findLastTopic()
     */
    public Topic findLastTopic() {
        String sql = " from {vo}Topic as p " + " order by p.idTopic desc";
        Topic t = null;
        List lst = getList(sql, 0, 1);

        if (!lst.isEmpty()) {
            t = (Topic) lst.get(0);
        }

        lst = null;

        return t;
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#findCountOfTopicsByForum(org.javabb.vo.Forum)
     */
    public Integer findCountOfTopicsByForum(Forum forum) {
        String sql = "Topic as t " + " where t.forum.idForum =" + forum.getIdForum();

        return this.countRowsOfTable(sql, "t.idTopic");
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#load(java.lang.Long)
     */
    public Topic load(Long id) {
        return (Topic) getHibernateTemplate().load(Topic.class, id);
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#create(org.javabb.vo.Topic)
     */
    public Long create(Topic topic) {
        return (Long) getHibernateTemplate().save(topic);
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#delete(java.lang.Long)
     */
    public void delete(Long topicId) {
        getHibernateTemplate().delete(load(topicId));
    }

    /**
     * @param topicId
     */
    public void deleteAllPostOfTopic(Long topicId) {
        // TODO pode ser usada dele玢o em cascata aqui?
        Topic topic = load(topicId);
        this.getHibernateTemplate().deleteAll(topic.getPosts());
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#lockTopic(org.javabb.vo.Topic, java.lang.Integer)
     */
    public void lockTopic(Topic topic, Integer lock) {
    	topic = load(topic.getId());
    	topic.setTopicStatus(lock);
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#moveTopic(org.javabb.vo.Topic, java.lang.Long)
     */
    public void moveTopic(Topic topic, Long idForumDest) {
    	
    	String sql = "update jbb_topics set id_forum=" + idForumDest
            + " where id_forum="
            + topic.getForum().getIdForum()
            + " and id_topic="
            + topic.getIdTopic();
        this.executeSQL(sql);
        
        
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#findAll()
     */
    public List findAll() {
        return findAll(Topic.class, "o.idTopic", ALL_PAGES, 0);
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#countTopicsByForum(java.lang.Long)
     */
    public int countTopicsByForum(Long forumId, Integer forumModel) {
        return countRowsWhere(Topic.class,
            "o.idTopic",
            "o.forum.idForum=? and o.topicModel=?",
            new Object[] { forumId, forumModel});
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#findByForum(java.lang.Long, Long, int, int)
     */
    public List findByForum(Long forumId, Integer forumModel, int pageNumber, int itemsPerPage) {
        /*
        
    	List topics = find(Topic.class,
                "o.forum.idForum=? and o.topicModel=?",
	            new Object[] { forumId, forumModel },
	            "o.lastPostDate desc, o.idTopic desc",
	            pageNumber,
	            itemsPerPage);
*/
        
        String sql = " SELECT o FROM Topic as o " +
        " inner join fetch o.user as user " +
        " inner join fetch o.forum as forum " +
        " inner join fetch forum.category as cat " +
        " WHERE o.forum.idForum=? and o.topicModel=? " +
        " ORDER BY o.lastPostDate desc, o.idTopic desc " +
        "  ";

        List topics = find(sql,  new Object[] { forumId, forumModel },pageNumber,itemsPerPage);
	            
        return topics;
        
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#findByForum(java.lang.Long, Long)
     */    
    public List findByForum(Long forumId, Integer forumModel) {
        
        return find(Topic.class,
            "o.forum.idForum=? and o.topicModel=?",
            new Object[] { forumId, forumModel },
            "o.lastPostDate desc, o.idTopic desc",
            ALL_PAGES, 0);
    }
    
    
    /**
     * @see org.javabb.dao.entity.ITopicDAO#findPostedAfter(java.util.Date, int, int)
     */
    public List findPostedAfter(Date date, int pageNumber, int itemsPerPage) {

    	return find(Topic.class,
            "o.lastPostDate >= ?",
            new Object[] { date },
            "o.lastPostDate DESC",
            pageNumber,
            itemsPerPage);
    }

    /**
     * @see org.javabb.dao.entity.ITopicDAO#countPostedAfter(java.util.Date)
     */
    public int countPostedAfter(Date date) {
        if (logger.isDebugEnabled()) {
            logger.debug("count posts after " + date);
        }
        return countRowsWhere(Topic.class,
            "o.idTopic",
            "o.lastPostDate >= ?",
            new Object[] { date });
    }
    
	/**
	 * @see org.javabb.dao.entity.ITopicDAO#update(org.javabb.vo.Topic)
	 */
	public void update( Topic topic ) {
		getHibernateTemplate().update(topic);
	}    
    
    /**
     * @see org.javabb.dao.entity.ITopicDAO#public List wathTopicByTopicUser(Long, Long)
     */
    public List wathTopicByTopicUser(Long userId, Long topicId) {
        return find(AnswerNotify.class,
                "o.user.idUser = ? AND o.topic.idTopic = ?",
                new Object[] { userId, topicId },
                "o.user.idUser asc",
                ALL_PAGES, 0);
    }
    
    
    public List favoriteTopicByTopicUser(Long userId, Long topicId) {
        return find(FavUserTopic.class,
                "o.user.idUser = ? AND o.topic.idTopic = ?",
                new Object[] { userId, topicId },
                "o.user.idUser asc",
                ALL_PAGES, 0);
    }
	
    
    public List favoriteTopicByTopic(Long topicId) {
        return find(FavUserTopic.class,
                "o.topic.idTopic = ?",
                new Object[] { topicId },
                "o.topic.idTopic asc",
                ALL_PAGES, 0);
    }    
    
    /**
     * @see org.javabb.dao.entity.ITopicDAO#public void insertWatchTopicUser(Long, Long)     
     */
    public void insertWatchTopicUser(Long topicId, Long userId){
        AnswerNotify answerNotify = new AnswerNotify(new AnswerNotifyPK(topicId, userId));
        this.getHibernateTemplate().save(answerNotify);
    }
    
    /**
     * @see org.javabb.dao.entity.ITopicDAO#public void deleteWatchTopicUser(Long, Long)
     */
    public void deleteWatchTopicUser(Long topicId, Long userId){
        AnswerNotify answerNotify = new AnswerNotify(new AnswerNotifyPK(topicId, userId));
        this.getHibernateTemplate().delete(answerNotify);
    }
    
    /**
     * @see org.javabb.dao.entity.ITopicDAO#public List wathTopicByUser(Long)
     */
    public List wathTopicByUser(Long userId) {
        return find(AnswerNotify.class,
                "o.user.idUser = ?",
                new Object[] { userId },
                "o.user.idUser asc",
                ALL_PAGES, 0);
    }
    

    public List favoriteTopicByUser(Long userId) {
        return find(FavUserTopic.class,
                "o.user.idUser = ?",
                new Object[] { userId },
                "o.user.idUser asc",
                ALL_PAGES, 0);
    }
    
    
    /**
     * @see org.javabb.dao.entity.ITopicDAO#public List wathTopicByTopic(Long)
     */
    public List wathTopicByTopic(Long topicId) {
        return find(AnswerNotify.class,
                "o.topic.idTopic = ?",
                new Object[] { topicId },
                "o.topic.idTopic asc",
                ALL_PAGES, 0);
    }
    
	/**
	 * @see org.javabb.dao.entity.ITopicDAO#findLastTopics(int)
	 */
	public List findLastTopics(int limit) {
        String sql = " SELECT o FROM Topic as o " +
        " inner join fetch o.user as user " +
        " inner join fetch o.forum as forum " +
        " ORDER BY o.lastPostId desc ";
        return find(sql,  new Object[] {}, 1, limit);
	}

	/**
	 * Favorite Topics listed by Rank
	 */
    public List favoriteTopics() {
		HibernateCallback callback = new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {
		    	String hql = "SELECT distinct f.topic.idTopic from FavUserTopic as f order by f.topic.idTopic desc";
		    	List favs =  find(hql,  new Object[] {}, ALL_PAGES, 50);
		    	
		    	if (!favs.isEmpty()) {
		    		hql = "SELECT t from Topic as t where t.idTopic in (";
					for (int i = 0; i < favs.size(); i++) {
						Long fav = (Long)favs.get(i);
						hql += fav + ",";
					}
					hql = hql.substring(0, hql.length() - 1);
					hql += ")";

					favs = session.find(hql);
				}
				return favs;
			}
		};    	
		return getHibernateTemplate().executeFind(callback);
    }
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -