Search in sources :

Example 1 with TopicDAO

use of net.jforum.dao.TopicDAO in project jforum2 by rafaelsteil.

the class GenericForumDAO method getLastPostInfo.

private LastPostInfo getLastPostInfo(int forumId, boolean tryFix) {
    LastPostInfo lpi = new LastPostInfo();
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.lastPostInfo"));
        p.setInt(1, forumId);
        rs = p.executeQuery();
        if (rs.next()) {
            lpi.setUsername(rs.getString("username"));
            lpi.setUserId(rs.getInt("user_id"));
            SimpleDateFormat df = new SimpleDateFormat(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
            lpi.setPostDate(df.format(rs.getTimestamp("post_time")));
            lpi.setPostId(rs.getInt("post_id"));
            lpi.setTopicId(rs.getInt("topic_id"));
            lpi.setPostTimeMillis(rs.getTimestamp("post_time").getTime());
            lpi.setTopicReplies(rs.getInt("topic_replies"));
            lpi.setHasInfo(true);
            // Check if the topic is consistent
            TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
            Topic t = tm.selectById(lpi.getTopicId());
            if (t.getId() == 0) {
                // Hm, that's not good. Try to fix it
                tm.fixFirstLastPostId(lpi.getTopicId());
            }
            tryFix = false;
        } else if (tryFix) {
            rs.close();
            rs = null;
            p.close();
            p = null;
            int postId = this.getMaxPostId(forumId);
            p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.latestTopicIdForfix"));
            p.setInt(1, forumId);
            rs = p.executeQuery();
            if (rs.next()) {
                int topicId;
                topicId = rs.getInt("topic_id");
                rs.close();
                rs = null;
                p.close();
                p = null;
                // Topic
                p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.fixLatestPostData"));
                p.setInt(1, postId);
                p.setInt(2, topicId);
                p.executeUpdate();
                p.close();
                p = null;
                // Forum
                p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.fixForumLatestPostData"));
                p.setInt(1, postId);
                p.setInt(2, forumId);
                p.executeUpdate();
            }
        }
        return (tryFix ? this.getLastPostInfo(forumId, false) : lpi);
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) TopicDAO(net.jforum.dao.TopicDAO) PreparedStatement(java.sql.PreparedStatement) Topic(net.jforum.entities.Topic) LastPostInfo(net.jforum.entities.LastPostInfo) SimpleDateFormat(java.text.SimpleDateFormat) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 2 with TopicDAO

use of net.jforum.dao.TopicDAO in project jforum2 by rafaelsteil.

the class GenericForumDAO method moveTopics.

/**
	 * @see net.jforum.dao.ForumDAO#moveTopics(java.lang.String[], int, int)
	 */
public void moveTopics(String[] topics, int fromForumId, int toForumId) {
    PreparedStatement p = null;
    PreparedStatement t = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.moveTopics"));
        t = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.setForumByTopic"));
        p.setInt(1, toForumId);
        p.setInt(2, fromForumId);
        t.setInt(1, toForumId);
        TopicDAO tdao = DataAccessDriver.getInstance().newTopicDAO();
        Forum f = this.selectById(toForumId);
        for (int i = 0; i < topics.length; i++) {
            int topicId = Integer.parseInt(topics[i]);
            p.setInt(3, topicId);
            t.setInt(2, topicId);
            p.executeUpdate();
            t.executeUpdate();
            tdao.setModerationStatusByTopic(topicId, f.isModerated());
        }
        this.decrementTotalTopics(fromForumId, topics.length);
        this.incrementTotalTopics(toForumId, topics.length);
        this.setLastPost(fromForumId, this.getMaxPostId(fromForumId));
        this.setLastPost(toForumId, this.getMaxPostId(toForumId));
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(p);
        DbUtils.close(t);
    }
}
Also used : SQLException(java.sql.SQLException) TopicDAO(net.jforum.dao.TopicDAO) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException) Forum(net.jforum.entities.Forum)

Example 3 with TopicDAO

use of net.jforum.dao.TopicDAO in project jforum2 by rafaelsteil.

the class ForumAction method delete.

// Delete
public void delete() {
    String[] ids = this.request.getParameterValues("forum_id");
    ForumDAO forumDao = DataAccessDriver.getInstance().newForumDAO();
    TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
    if (ids != null) {
        for (int i = 0; i < ids.length; i++) {
            int forumId = Integer.parseInt(ids[i]);
            topicDao.deleteByForum(forumId);
            forumDao.delete(forumId);
            Forum f = new Forum(ForumRepository.getForum(forumId));
            ForumRepository.removeForum(f);
        }
        SecurityRepository.clean();
        RolesRepository.clear();
    }
    this.list();
}
Also used : ForumDAO(net.jforum.dao.ForumDAO) TopicDAO(net.jforum.dao.TopicDAO) Forum(net.jforum.entities.Forum)

Example 4 with TopicDAO

use of net.jforum.dao.TopicDAO in project jforum2 by rafaelsteil.

the class TopicsCommon method deleteTopic.

/**
	 * Deletes a topic.
	 * This method will remove the topic from the database,
	 * clear the entry frm the cache and update the last 
	 * post info for the associated forum.
	 * @param topicId The topic id to remove
	 * @param fromModeration boolean 
     * @param forumId int
	 */
public static synchronized void deleteTopic(int topicId, int forumId, boolean fromModeration) {
    TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
    Topic topic = new Topic();
    topic.setId(topicId);
    topic.setForumId(forumId);
    topicDao.delete(topic, fromModeration);
    if (!fromModeration) {
        // Updates the Recent Topics if it contains this topic
        TopicRepository.loadMostRecentTopics();
        // Updates the Hottest Topics if it contains this topic
        TopicRepository.loadHottestTopics();
        TopicRepository.clearCache(forumId);
        topicDao.removeSubscriptionByTopic(topicId);
    }
}
Also used : TopicDAO(net.jforum.dao.TopicDAO) Topic(net.jforum.entities.Topic)

Example 5 with TopicDAO

use of net.jforum.dao.TopicDAO in project jforum2 by rafaelsteil.

the class TopicsCommon method topicsByForum.

/**
	 * List all first 'n' topics of a given forum.
	 * This method returns no more than <code>ConfigKeys.TOPICS_PER_PAGE</code>
	 * topics for the forum. 
	 * 
	 * @param forumId The forum id to which the topics belongs to
	 * @param start The start fetching index
	 * @return <code>java.util.List</code> containing the topics found.
	 */
public static List topicsByForum(int forumId, int start) {
    TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
    int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
    List topics;
    // Try to get the first's page of topics from the cache
    if (start == 0 && SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
        topics = TopicRepository.getTopics(forumId);
        if (topics.size() == 0 || !TopicRepository.isLoaded(forumId)) {
            synchronized (MUTEXT) {
                if (topics.size() == 0 || !TopicRepository.isLoaded(forumId)) {
                    topics = tm.selectAllByForumByLimit(forumId, start, topicsPerPage);
                    TopicRepository.addAll(forumId, topics);
                }
            }
        }
    } else {
        topics = tm.selectAllByForumByLimit(forumId, start, topicsPerPage);
    }
    return topics;
}
Also used : TopicDAO(net.jforum.dao.TopicDAO) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TopicDAO (net.jforum.dao.TopicDAO)21 Topic (net.jforum.entities.Topic)14 List (java.util.List)10 PostDAO (net.jforum.dao.PostDAO)10 Post (net.jforum.entities.Post)7 AttachmentCommon (net.jforum.view.forum.common.AttachmentCommon)6 ArrayList (java.util.ArrayList)5 ForumDAO (net.jforum.dao.ForumDAO)5 Forum (net.jforum.entities.Forum)5 Iterator (java.util.Iterator)4 ModerationLog (net.jforum.entities.ModerationLog)4 User (net.jforum.entities.User)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 PollDAO (net.jforum.dao.PollDAO)3 Poll (net.jforum.entities.Poll)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2