<commit_before>/*
 * Copyright (c) <NAME>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms,
 * with or without modification, are permitted provided
 * that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above
 * copyright notice, this list of conditions and the
 * following  disclaimer.
 * 2)  Redistributions in binary form must reproduce the
 * above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * 3) Neither the name of "<NAME>" nor
 * the names of its contributors may be used to endorse
 * or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 *
 * This file creation date: 30/03/2003 / 02:37:20
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.dao.generic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.DataAccessDriver;
import net.jforum.dao.TopicDAO;
import net.jforum.entities.Forum;
import net.jforum.entities.ForumStats;
import net.jforum.entities.LastPostInfo;
import net.jforum.entities.ModeratorInfo;
import net.jforum.entities.Topic;
import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;

/**
 * @author <NAME>
 * @author <NAME>
 * @version $Id: GenericForumDAO.java,v 1.19 2006/02/01 00:37:56 rafaelsteil Exp $
 */
public class GenericForumDAO extends AutoKeys implements net.jforum.dao.ForumDAO 
{
	/**
	 * @see net.jforum.dao.ForumDAO#selectById(int)
	 */
	public Forum selectById(int forumId) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.selectById"));
		p.setInt(1, forumId);

		ResultSet rs = p.executeQuery();

		Forum f = new Forum();

		if (rs.next()) {
			f = this.fillForum(rs);
		}

		rs.close();
		p.close();

		return f;
	}

	protected Forum fillForum(ResultSet rs) throws Exception {
		Forum f = new Forum();

		f.setId(rs.getInt("forum_id"));
		f.setIdCategories(rs.getInt("categories_id"));
		f.setName(rs.getString("forum_name"));
		f.setDescription(rs.getString("forum_desc"));
		f.setOrder(rs.getInt("forum_order"));
		f.setTotalTopics(rs.getInt("forum_topics"));
		f.setLastPostId(rs.getInt("forum_last_post_id"));
		f.setModerated(rs.getInt("moderated") > 0);
		f.setTotalPosts(this.countForumPosts(f.getId()));

		return f;
	}
	
	protected int countForumPosts(int forumId) throws Exception
	{
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("ForumModel.countForumPosts"));
		p.setInt(1, forumId);
		ResultSet rs = p.executeQuery();
		rs.next();

		int total = rs.getInt(1);

		rs.close();
		p.close();

		return total;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.selectAll"));
		List l = new ArrayList();

		ResultSet rs = p.executeQuery();

		while (rs.next()) {
			l.add(this.fillForum(rs));
		}

		rs.close();
		p.close();

		return l;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#setOrderUp(Forum, Forum)
	 */
	public Forum setOrderUp(Forum forum, Forum related) throws Exception 
	{
		return this.changeForumOrder(forum, related, true);
	}
	
	/**
	 * @see net.jforum.dao.ForumDAO#setOrderDown(Forum, Forum)
	 */
	public Forum setOrderDown(Forum forum, Forum related) throws Exception {
		return this.changeForumOrder(forum, related, false);
	}
	
	private Forum changeForumOrder(Forum forum, Forum related, boolean up) throws Exception
	{
		int tmpOrder = related.getOrder();
		related.setOrder(forum.getOrder());
		forum.setOrder(tmpOrder);

		// ******** 
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.setOrderById"));
		p.setInt(1, forum.getOrder());
		p.setInt(2, forum.getId());
		p.executeUpdate();
		p.close();
		
		// ********
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.setOrderById"));
		p.setInt(1, related.getOrder());
		p.setInt(2, related.getId());
		p.executeUpdate();
		p.close();
		
		return this.selectById(forum.getId());
	}

	/**
	 * @see net.jforum.dao.ForumDAO#delete(int)
	 */
	public void delete(int forumId) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.delete"));
		p.setInt(1, forumId);

		p.executeUpdate();

		p.close();
	}

	/**
	 * @see net.jforum.dao.ForumDAO#update(net.jforum.Forum)
	 */
	public void update(Forum forum) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.update"));

		p.setInt(1, forum.getCategoryId());
		p.setString(2, forum.getName());
		p.setString(3, forum.getDescription());
		p.setInt(4, forum.isModerated() ? 1 : 0);
		p.setInt(5, forum.getId());

		// Order, TotalTopics and LastPostId must be updated using the
		// respective methods
		p.executeUpdate();
		p.close();
	}

	/**
	 * @see net.jforum.dao.ForumDAO#addNew(net.jforum.Forum)
	 */
	public int addNew(Forum forum) throws Exception {
		// Gets the higher order
		PreparedStatement pOrder = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.getMaxOrder"));
		ResultSet rs = pOrder.executeQuery();

		if (rs.next()) {
			forum.setOrder(rs.getInt(1) + 1);
		}

		rs.close();
		pOrder.close();

		// Updates the order
		PreparedStatement p = this.getStatementForAutoKeys("ForumModel.addNew");

		p.setInt(1, forum.getCategoryId());
		p.setString(2, forum.getName());
		p.setString(3, forum.getDescription());
		p.setInt(4, forum.getOrder());
		p.setInt(5, forum.isModerated() ? 1 : 0);

		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("ForumModel.lastGeneratedForumId"));
		int forumId = this.executeAutoKeysQuery(p);

		p.close();

		forum.setId(forumId);
		return forumId;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#setLastPost(int, int)
	 */
	public void setLastPost(int forumId, int postId) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.updateLastPost"));

		p.setInt(1, postId);
		p.setInt(2, forumId);

		p.executeUpdate();

		p.close();
	}

	/**
	 * @see net.jforum.dao.ForumDAO#setTotalTopics(int)
	 */
	public void incrementTotalTopics(int forumId, int count) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.incrementTotalTopics"));
		p.setInt(1, count);
		p.setInt(2, forumId);
		p.executeUpdate();
		p.close();
	}

	/**
	 * @see net.jforum.dao.ForumDAO#setTotalTopics(int)
	 */
	public void decrementTotalTopics(int forumId, int count) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.decrementTotalTopics"));
		p.setInt(1, count);
		p.setInt(2, forumId);
		p.executeUpdate();
		p.close();

		// If there are no more topics, then clean the
		// last post id information
		int totalTopics = this.getTotalTopics(forumId);
		if (totalTopics < 1) {
			this.setLastPost(forumId, 0);
		}
	}
	
	private LastPostInfo getLastPostInfo(int forumId, boolean tryFix) throws Exception
	{
		LastPostInfo lpi = new LastPostInfo();

		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.lastPostInfo"));
		p.setInt(1, forumId);

		ResultSet 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) {
			p.close();
			rs.close();
			
			int postId = this.getMaxPostId(forumId);
			
			p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.latestTopicIdForfix"));
			p.setInt(1, forumId);
			rs = p.executeQuery();
			
			int topicId = -1;
			
			if (rs.next()) {
				topicId = rs.getInt("topic_id");
				
				rs.close();
				p.close();
				
				// Topic
				p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.fixLatestPostData"));
				p.setInt(1, postId);
				p.setInt(2, topicId);
				p.executeUpdate();
				p.close();
				
				// Forum
				p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.fixForumLatestPostData"));
				p.setInt(1, postId);
				p.setInt(2, forumId);
				p.executeUpdate();
			}
		}

		p.close();
		rs.close();
		
		return (tryFix ? this.getLastPostInfo(forumId, false) : lpi);
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getLastPostInfo(int)
	 */
	public LastPostInfo getLastPostInfo(int forumId) throws Exception 
	{
		return this.getLastPostInfo(forumId, true);
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getModeratorList(int)
	 */
	public List getModeratorList(int forumId) throws Exception
	{
		List l = new ArrayList();

		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("ForumModel.getModeratorList"));
		p.setInt(1, forumId);

		ResultSet rs = p.executeQuery();
		
		while (rs.next()) {
			ModeratorInfo mi = new ModeratorInfo();
			
			mi.setId(rs.getInt("id"));
			mi.setName(rs.getString("name"));
			
			l.add(mi);
		}
		
		rs.close();
		p.close();
		
		return l;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getTotalMessages()
	 */
	public int getTotalMessages() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("ForumModel.totalMessages"));
		ResultSet rs = p.executeQuery();
		rs.next();

		int total = rs.getInt("total_messages");
		
		p.close();
		rs.close();

		return total;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getTotalTopics(int)
	 */
	public int getTotalTopics(int forumId) throws Exception {
		int total = 0;
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.getTotalTopics"));
		p.setInt(1, forumId);
		ResultSet rs = p.executeQuery();

		if (rs.next()) {
			total = rs.getInt(1);
		}

		p.close();
		rs.close();

		return total;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getMaxPostId(int)
	 */
	public int getMaxPostId(int forumId) throws Exception {
		int id = -1;

		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.getMaxPostId"));
		p.setInt(1, forumId);<commit_msg>*** empty log message ***<commit_after>/*
 * Copyright (c) <NAME>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms,
 * with or without modification, are permitted provided
 * that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above
 * copyright notice, this list of conditions and the
 * following  disclaimer.
 * 2)  Redistributions in binary form must reproduce the
 * above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * 3) Neither the name of "<NAME>" nor
 * the names of its contributors may be used to endorse
 * or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 *
 * This file creation date: 30/03/2003 / 02:37:20
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.dao.generic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.DataAccessDriver;
import net.jforum.dao.TopicDAO;
import net.jforum.entities.Forum;
import net.jforum.entities.ForumStats;
import net.jforum.entities.LastPostInfo;
import net.jforum.entities.ModeratorInfo;
import net.jforum.entities.Topic;
import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;

/**
 * @author <NAME>
 * @author <NAME>
 * @version $Id: GenericForumDAO.java,v 1.20 2006/02/01 23:27:05 rafaelsteil Exp $
 */
public class GenericForumDAO extends AutoKeys implements net.jforum.dao.ForumDAO 
{
	/**
	 * @see net.jforum.dao.ForumDAO#selectById(int)
	 */
	public Forum selectById(int forumId) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.selectById"));
		p.setInt(1, forumId);

		ResultSet rs = p.executeQuery();

		Forum f = new Forum();

		if (rs.next()) {
			f = this.fillForum(rs);
		}

		rs.close();
		p.close();

		return f;
	}

	protected Forum fillForum(ResultSet rs) throws Exception {
		Forum f = new Forum();

		f.setId(rs.getInt("forum_id"));
		f.setIdCategories(rs.getInt("categories_id"));
		f.setName(rs.getString("forum_name"));
		f.setDescription(rs.getString("forum_desc"));
		f.setOrder(rs.getInt("forum_order"));
		f.setTotalTopics(rs.getInt("forum_topics"));
		f.setLastPostId(rs.getInt("forum_last_post_id"));
		f.setModerated(rs.getInt("moderated") > 0);
		f.setTotalPosts(this.countForumPosts(f.getId()));

		return f;
	}
	
	protected int countForumPosts(int forumId) throws Exception
	{
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("ForumModel.countForumPosts"));
		p.setInt(1, forumId);
		ResultSet rs = p.executeQuery();
		rs.next();

		int total = rs.getInt(1);

		rs.close();
		p.close();

		return total;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.selectAll"));
		List l = new ArrayList();

		ResultSet rs = p.executeQuery();

		while (rs.next()) {
			l.add(this.fillForum(rs));
		}

		rs.close();
		p.close();

		return l;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#setOrderUp(Forum, Forum)
	 */
	public Forum setOrderUp(Forum forum, Forum related) throws Exception 
	{
		return this.changeForumOrder(forum, related, true);
	}
	
	/**
	 * @see net.jforum.dao.ForumDAO#setOrderDown(Forum, Forum)
	 */
	public Forum setOrderDown(Forum forum, Forum related) throws Exception {
		return this.changeForumOrder(forum, related, false);
	}
	
	private Forum changeForumOrder(Forum forum, Forum related, boolean up) throws Exception
	{
		int tmpOrder = related.getOrder();
		related.setOrder(forum.getOrder());
		forum.setOrder(tmpOrder);

		// ******** 
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.setOrderById"));
		p.setInt(1, forum.getOrder());
		p.setInt(2, forum.getId());
		p.executeUpdate();
		p.close();
		
		// ********
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.setOrderById"));
		p.setInt(1, related.getOrder());
		p.setInt(2, related.getId());
		p.executeUpdate();
		p.close();
		
		return this.selectById(forum.getId());
	}

	/**
	 * @see net.jforum.dao.ForumDAO#delete(int)
	 */
	public void delete(int forumId) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.delete"));
		p.setInt(1, forumId);

		p.executeUpdate();

		p.close();
	}

	/**
	 * @see net.jforum.dao.ForumDAO#update(net.jforum.Forum)
	 */
	public void update(Forum forum) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.update"));

		p.setInt(1, forum.getCategoryId());
		p.setString(2, forum.getName());
		p.setString(3, forum.getDescription());
		p.setInt(4, forum.isModerated() ? 1 : 0);
		p.setInt(5, forum.getId());

		// Order, TotalTopics and LastPostId must be updated using the
		// respective methods
		p.executeUpdate();
		p.close();
	}

	/**
	 * @see net.jforum.dao.ForumDAO#addNew(net.jforum.Forum)
	 */
	public int addNew(Forum forum) throws Exception {
		// Gets the higher order
		PreparedStatement pOrder = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.getMaxOrder"));
		ResultSet rs = pOrder.executeQuery();

		if (rs.next()) {
			forum.setOrder(rs.getInt(1) + 1);
		}

		rs.close();
		pOrder.close();

		// Updates the order
		PreparedStatement p = this.getStatementForAutoKeys("ForumModel.addNew");

		p.setInt(1, forum.getCategoryId());
		p.setString(2, forum.getName());
		p.setString(3, forum.getDescription());
		p.setInt(4, forum.getOrder());
		p.setInt(5, forum.isModerated() ? 1 : 0);

		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("ForumModel.lastGeneratedForumId"));
		int forumId = this.executeAutoKeysQuery(p);

		p.close();

		forum.setId(forumId);
		return forumId;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#setLastPost(int, int)
	 */
	public void setLastPost(int forumId, int postId) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.updateLastPost"));

		p.setInt(1, postId);
		p.setInt(2, forumId);

		p.executeUpdate();

		p.close();
	}

	/**
	 * @see net.jforum.dao.ForumDAO#setTotalTopics(int)
	 */
	public void incrementTotalTopics(int forumId, int count) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.incrementTotalTopics"));
		p.setInt(1, count);
		p.setInt(2, forumId);
		p.executeUpdate();
		p.close();
	}

	/**
	 * @see net.jforum.dao.ForumDAO#setTotalTopics(int)
	 */
	public void decrementTotalTopics(int forumId, int count) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.decrementTotalTopics"));
		p.setInt(1, count);
		p.setInt(2, forumId);
		p.executeUpdate();
		p.close();

		// If there are no more topics, then clean the
		// last post id information
		int totalTopics = this.getTotalTopics(forumId);
		if (totalTopics < 1) {
			this.setLastPost(forumId, 0);
		}
	}
	
	private LastPostInfo getLastPostInfo(int forumId, boolean tryFix) throws Exception
	{
		LastPostInfo lpi = new LastPostInfo();

		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.lastPostInfo"));
		p.setInt(1, forumId);

		ResultSet 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();
			p.close();
			
			int postId = this.getMaxPostId(forumId);
			
			p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.latestTopicIdForfix"));
			p.setInt(1, forumId);
			rs = p.executeQuery();
			
			int topicId = -1;
			
			if (rs.next()) {
				topicId = rs.getInt("topic_id");
				
				rs.close();
				p.close();
				
				// Topic
				p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.fixLatestPostData"));
				p.setInt(1, postId);
				p.setInt(2, topicId);
				p.executeUpdate();
				p.close();
				
				// Forum
				p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.fixForumLatestPostData"));
				p.setInt(1, postId);
				p.setInt(2, forumId);
				p.executeUpdate();
			}
		}

		rs.close();
		p.close();
		
		return (tryFix ? this.getLastPostInfo(forumId, false) : lpi);
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getLastPostInfo(int)
	 */
	public LastPostInfo getLastPostInfo(int forumId) throws Exception 
	{
		return this.getLastPostInfo(forumId, true);
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getModeratorList(int)
	 */
	public List getModeratorList(int forumId) throws Exception
	{
		List l = new ArrayList();

		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("ForumModel.getModeratorList"));
		p.setInt(1, forumId);

		ResultSet rs = p.executeQuery();
		
		while (rs.next()) {
			ModeratorInfo mi = new ModeratorInfo();
			
			mi.setId(rs.getInt("id"));
			mi.setName(rs.getString("name"));
			
			l.add(mi);
		}
		
		rs.close();
		p.close();
		
		return l;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getTotalMessages()
	 */
	public int getTotalMessages() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("ForumModel.totalMessages"));
		ResultSet rs = p.executeQuery();
		rs.next();

		int total = rs.getInt("total_messages");
		
		rs.close();
		p.close();

		return total;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getTotalTopics(int)
	 */
	public int getTotalTopics(int forumId) throws Exception {
		int total = 0;
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.getTotalTopics"));
		p.setInt(1, forumId);
		ResultSet rs = p.executeQuery();

		if (rs.next()) {
			total = rs.getInt(1);
		}

		rs.close();
		p.close();

		return total;
	}

	/**
	 * @see net.jforum.dao.ForumDAO#getMaxPostId(int)
	 */
	public int getMaxPostId(int forumId) throws Exception {
		int id = -1;

		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.getMaxPostId"));
		p.setInt(1, forumId);
{'fraction_non_alphanumeric': 0.1349951440595662, 'fraction_numerical': 0.006555519585626416, 'mean_word_length': 4.382923110433457, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2643660', 'n_tokens_mistral': 9191, 'n_tokens_neox': 8194, 'n_words': 1699}