/*
 * 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 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.servlets.format.*;

public class TopicOperations extends VeniceServlet
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */
 
  private static final String DELETE_CONFIRM_ATTR = "servlets.TopicOperations.delete.confirm";
  private static final String DELETE_CONFIRM_PARAM = "confirm";

  private static Category logger = Category.getInstance(TopicOperations.class.getName());

  /*--------------------------------------------------------------------------------
   * Overrides from class HttpServlet
   *--------------------------------------------------------------------------------
   */

  public String getServletInfo()
  {
    String rc = "TopicOperations servlet - General topic operations (freeze, archive, etc.)\n"
              + "Part of the Venice Web Communities System\n";
    return rc;

  } // end getServletInfo

  /*--------------------------------------------------------------------------------
   * Overrides from class VeniceServlet
   *--------------------------------------------------------------------------------
   */

  protected VeniceContent doVeniceGet(HttpServletRequest request, VeniceEngine engine,
				      UserContext user, RenderData rdat)
    throws ServletException, IOException, VeniceServletResult
  {
    // get the community
    CommunityContext comm = getCommunityParameter(request,user,true,"top");
    changeMenuCommunity(request,comm);
    String locator = "sig=" + comm.getCommunityID();
    String location = "sigprofile?" + locator;

    // get the conference
    ConferenceContext conf = getConferenceParameter(request,comm,true,location);
    locator += "&conf=" + conf.getConfID();
    location = "confdisp?" + locator;

    // get the topic
    TopicContext topic = getTopicParameter(request,conf,true,location);
    locator += "&top=" + topic.getTopicNumber();
    location = "confdisp?" + locator;
    
    // figure out what command we want to perform...
    String cmd = getStandardCommandParam(request);

    if (cmd.equals("HY") || cmd.equals("HN"))
    { // we want to set the hide status of the topic
      try
      { // call down to set the topic!
	topic.setHidden(cmd.equals("HY"));

      } // end try
      catch (DataException de)
      { // there was a database error
	return new ErrorBox("Database Error","Database error setting hide status: " + de.getMessage(),
			    location);

      } // end catch

      // go back to the topic view
      throw new RedirectResult(location);

    } // end if ("hide" or "show")

    if (cmd.equals("FY") || cmd.equals("FN"))
    { // we want to set the frozen status of the topic
      try
      { // call down to set the topic!
	topic.setFrozen(cmd.equals("FY"));

      } // end try
      catch (DataException de)
      { // there was a database error
	return new ErrorBox("Database Error","Database error setting freeze status: " + de.getMessage(),
			    location);

      } // end catch
      catch (AccessError ae)
      { // naughty naughty = you can't do this!
	return new ErrorBox("Access Error",ae.getMessage(),location);

      } // end catch

      // go back to the topic view
      throw new RedirectResult(location);

    } // end if ("freeze" or "unfreeze")

    if (cmd.equals("AY") || cmd.equals("AN"))
    { // we want to change the archived status of the topic
      try
      { // call down to set the topic!
	topic.setArchived(cmd.equals("AY"));

      } // end try
      catch (DataException de)
      { // there was a database error
	return new ErrorBox("Database Error","Database error setting archive status: " + de.getMessage(),
			    location);

      } // end catch
      catch (AccessError ae)
      { // naughty naughty = you can't do this!
	return new ErrorBox("Access Error",ae.getMessage(),location);

      } // end catch

      // go back to the topic view
      throw new RedirectResult(location);

    } // end if ("archive" or "unarchive")

    if (cmd.equals("DEL"))
    { // Delete Topic requires a confirmation!
      if (ConfirmBox.isConfirmed(request,DELETE_CONFIRM_ATTR,DELETE_CONFIRM_PARAM))
      { // OK, go ahead, delete the topic!
	location = "confdisp?sig=" + comm.getCommunityID() + "&conf=" + conf.getConfID();

	try
	{ // delete the bloody topic!
	  topic.delete();

	} // end try
	catch (DataException de)
	{ // there was a database error
	  return new ErrorBox("Database Error","Database error deleting topic: " + de.getMessage(),location);
	  
	} // end catch
	catch (AccessError ae)
	{ // naughty naughty = you can't do this!
	  return new ErrorBox("Access Error",ae.getMessage(),location);

	} // end catch

	// go back to the conference view
	throw new RedirectResult(location);

      } // end if (confirmed)
      else
      { // not a proper confirmation - better display one
	String message = "You are about to delete topic " + String.valueOf(topic.getTopicNumber())
	  + " from the \"" + conf.getName() + "\" conference!  Are you sure you want to do this?";
	return new ConfirmBox(request,DELETE_CONFIRM_ATTR,DELETE_CONFIRM_PARAM,"Delete Topic",message,
			      "topicops?" + locator + "&cmd=DEL",location);

      } // end else

    } // end if (delete)

    if (cmd.equals("RB"))
    { // "RB" = "Remove Bozo" (UID specified as parameter "u")
      int uid = -1;
      try
      { // get the user ID to un-bozo
	String foo = request.getParameter("u");
	if (foo!=null)
	  uid = Integer.parseInt(foo);

      } // end try
      catch (NumberFormatException nfe)
      { // just don't do anything on error
        uid = -1;

      } // end catch

      try
      { // remove "bozo" status from this user
	if (uid>0)
	  topic.setBozo(uid,false);
	setMyLocation(request,"topicops?" + locator);
	return new ManageTopic(user,comm,conf,topic);

      } // end try
      catch (DataException de)
      { // whoops!  this is a problem!
	return new ErrorBox("Database Error","Database error removing filtered user: " + de.getMessage(),
			    "topicops?" + locator);

      } // end catch

    } // end if (remove bozo)

    // unrecognized command - load the "Manage Topic menu"
    try
    { // return that "Manage Topic" page
      setMyLocation(request,"topicops?" + locator);
      return new ManageTopic(user,comm,conf,topic);

    } // end try
    catch (DataException de)
    { // whoops!  this is a problem!
      return new ErrorBox("Database Error","Database error loading manage page: " + de.getMessage(),location);

    } // end catch

  } // end doVeniceGet

} // end class TopicOperations
