/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (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.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.core.impl;

import java.sql.*;
import org.apache.log4j.*;
import com.silverwrist.venice.db.*;
import com.silverwrist.venice.core.internals.*;
import com.silverwrist.venice.svc.*;

class BackgroundTopicPurge implements Runnable
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(BackgroundTopicPurge.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private GlobalSite globalsite;           // the global site
  private EnvEngine env;                   // the environment block
  private int topicid;                     // the topic ID
  private int num_posts;                   // the number of posts in this topic
  private long max_postid;                 // the maximum post ID

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  BackgroundTopicPurge(GlobalSite globalsite, EnvEngine env, int topicid, int num_posts, long max_postid)
  {
    this.globalsite = globalsite;
    this.env = env;
    this.topicid = topicid;
    this.num_posts = num_posts;
    this.max_postid = max_postid;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Runnable
   *--------------------------------------------------------------------------------
   */

  public void run()
  {
    if (logger.isDebugEnabled())
      logger.debug("BackgroundTopicPurge running on topic #" + topicid);

    long[] postids = new long[num_posts];  // stores the post IDs
    Connection conn = null;  // pooled database connection
    Statement stmt = null;

    try
    { // get a database connection from the pool
      conn = globalsite.getConnection(null);
      stmt = conn.createStatement();

      // look up all the post IDs that are present for this topic
      StringBuffer sql = new StringBuffer("SELECT postid FROM posts WHERE topicid = ");
      sql.append(topicid).append(" AND postid <= ").append(max_postid).append(" ORDER BY postid;");
      ResultSet rs = stmt.executeQuery(sql.toString());
      int posts = 0;
      while (rs.next())
	postids[posts++] = rs.getLong(1);
      SQLUtil.shutdown(rs);

      for (int i=0; i<posts; i++)
      { // remove all references to the posts in question
	stmt.executeUpdate("DELETE FROM posts WHERE postid = " + postids[i] + ";");
	stmt.executeUpdate("DELETE FROM postdata WHERE postid = " + postids[i] + ";");
	stmt.executeUpdate("DELETE FROM postattach WHERE postid = " + postids[i] + ";");
	stmt.executeUpdate("DELETE FROM postdogear WHERE postid = " + postids[i] + ";");
	if (stmt.executeUpdate("DELETE FROM postpublish WHERE postid = " + postids[i] + ";")>0)
	  env.getEngine().unpublish(postids[i]);

      } // end for

      // all done, Bunky!
      if (logger.isDebugEnabled())
	logger.debug("BackgroundTopicPurge complete for topic #" + topicid);

    } // end try
    catch (SQLException e)
    { // on an error, just die
      logger.error("BackgroundTopicPurge FATAL EXCEPTION purging #" + topicid + ": " + e.getMessage(),e);

    } // end catch
    finally
    { // make sure we release the connection before we go
      SQLUtil.shutdown(stmt);
      SQLUtil.shutdown(conn);

    } // end finally

  } // end run

} // end class BackgroundTopicPurge
