Search in sources :

Example 6 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericForumDAO method countForumPosts.

protected int countForumPosts(int forumId) {
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.countForumPosts"));
        p.setInt(1, forumId);
        rs = p.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        }
        return 0;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 7 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericForumDAO method setLastPost.

/**
	 * @see net.jforum.dao.ForumDAO#setLastPost(int, int)
	 */
public void setLastPost(int forumId, int postId) {
    PreparedStatement p = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.updateLastPost"));
        p.setInt(1, postId);
        p.setInt(2, forumId);
        p.executeUpdate();
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(p);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 8 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericForumDAO method addNew.

/**
	 * @see net.jforum.dao.ForumDAO#addNew(net.jforum.entities.Forum)
	 */
public int addNew(Forum forum) {
    // Gets the higher order
    PreparedStatement pOrder = null;
    ResultSet rs = null;
    try {
        pOrder = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.getMaxOrder"));
        rs = pOrder.executeQuery();
        if (rs.next()) {
            forum.setOrder(rs.getInt(1) + 1);
        }
        rs.close();
        rs = null;
        pOrder.close();
        pOrder = null;
        pOrder = this.getStatementForAutoKeys("ForumModel.addNew");
        pOrder.setInt(1, forum.getCategoryId());
        pOrder.setString(2, forum.getName());
        pOrder.setString(3, forum.getDescription());
        pOrder.setInt(4, forum.getOrder());
        pOrder.setInt(5, forum.isModerated() ? 1 : 0);
        this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("ForumModel.lastGeneratedForumId"));
        int forumId = this.executeAutoKeysQuery(pOrder);
        forum.setId(forumId);
        return forumId;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, pOrder);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 9 with DatabaseException

use of net.jforum.exceptions.DatabaseException 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 10 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericForumDAO method isUserSubscribed.

public boolean isUserSubscribed(int forumId, int userId) {
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.isUserSubscribed"));
        stmt.setInt(1, forumId);
        stmt.setInt(2, userId);
        rs = stmt.executeQuery();
        return rs.next();
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, stmt);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Aggregations

DatabaseException (net.jforum.exceptions.DatabaseException)264 PreparedStatement (java.sql.PreparedStatement)255 SQLException (java.sql.SQLException)254 ResultSet (java.sql.ResultSet)138 List (java.util.List)64 ArrayList (java.util.ArrayList)63 Timestamp (java.sql.Timestamp)17 User (net.jforum.entities.User)15 Iterator (java.util.Iterator)12 Post (net.jforum.entities.Post)9 Date (java.util.Date)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Topic (net.jforum.entities.Topic)8 Connection (java.sql.Connection)5 Bookmark (net.jforum.entities.Bookmark)5 KarmaStatus (net.jforum.entities.KarmaStatus)4 SimpleDateFormat (java.text.SimpleDateFormat)3 PrivateMessage (net.jforum.entities.PrivateMessage)3 Ranking (net.jforum.entities.Ranking)3