/*
 * 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 java.util.*;
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 PostOperations extends VeniceServlet
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String NUKE_CONFIRM_ATTR = "servlets.PostOperations.nuke.confirm";
  private static final String NUKE_CONFIRM_PARAM = "confirm";

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

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

  public String getServletInfo()
  {
    String rc = "PostOperations servlet - General post operations (hide, scribble, 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;

    // get the message
    TopicMessageContext msg = getMessageParameter(request,topic,true,location);
    location = "confdisp?" + locator + "&p1=" + msg.getPostNumber() + "&shac=1";
    setMyLocation(request,location);

    // figure out what command we want to perform
    String cmd = getStandardCommandParam(request);

    if (cmd.equals("HY") || cmd.equals("HN"))
    { // we want to hide or show the message
      try
      { // attempt to hide or show the message
	msg.setHidden(cmd.equals("HY"));

	// go back and display stuff
	throw new RedirectResult(location);

      } // end if
      catch (DataException de)
      { // there was a database error
	return new ErrorBox("Database Error","Database error setting hidden 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

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

    if (cmd.equals("SCR"))
    { // we want to scribble the message
      try
      { // attempt to scribble the message
	msg.scribble();

	// go back and display stuff
	throw new RedirectResult(location);

      } // end try
      catch (DataException de)
      { // there was a database error
	return new ErrorBox("Database Error","Database error scribbling message: " + 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

    } // end if ("scribble")

    if (cmd.equals("BY") || cmd.equals("BN"))
    { // we want to add or remove the bozo filter from the user here
      try
      { // attempt to set the bozo filter status
	topic.setBozo(msg.getCreatorUID(),cmd.equals("BY"));

	// go back and display stuff
	throw new RedirectResult(location);

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

      } // end catch

    } // end if

    if (cmd.equals("NUKE"))
    { // nuking requires confirmation
      try
      { // we need confirmation on this operation
	if (ConfirmBox.isConfirmed(request,NUKE_CONFIRM_ATTR,NUKE_CONFIRM_PARAM))
	{ // OK, go ahead, nuke the message!
	  msg.nuke();

	  // after which, redirect to topic view
	  throw new RedirectResult("confdisp?" + locator);

	} // end if (confirmed)
	else
	{ // not a proper confirmation - better display one
	  List aliases = conf.getAliases();
	  String message = "You are about to nuke message <" + (String)(aliases.get(0)) + "."
                         + topic.getTopicNumber() + "." + msg.getPostNumber() + ">, originally composed by <"
                         + msg.getCreatorName() + ">!  Are you sure you want to do this?";
	  String confirm_url = "postops?" + locator + "&msg=" + msg.getPostNumber() + "&cmd=NUKE";
	  return new ConfirmBox(request,NUKE_CONFIRM_ATTR,NUKE_CONFIRM_PARAM,"Nuke Message",
				message,confirm_url,location);

	} // end else (not yet confirmed)
	
      } // end try
      catch (DataException de)
      { // there was a database error
	return new ErrorBox("Database Error","Database error nuking message: " + 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

    } // end if ("nuke")

    if (cmd.equals("PU"))
    { // we want to publish the message to the front page
      try
      { // attempt to publish the message
	msg.publish();

	// go back and display stuff
	throw new RedirectResult(location);

      } // end try
      catch (DataException de)
      { // there was a database error
	return new ErrorBox("Database Error","Database error publishing message: " + 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

    } // end if

    // unrecognized command!
    logger.error("invalid command to PostOperations.doGet: " + cmd);
    return new ErrorBox("Internal Error","Invalid command to PostOperations.doGet",location);

  } // end doVeniceGet

} // end class PostOperations
