/*
 * 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.format;

import java.util.*;
import javax.servlet.*;
import com.silverwrist.venice.htmlcheck.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;

public class NewTopicForm implements JSPRender
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  // Attribute name for request attribute
  protected static final String ATTR_NAME = "com.silverwrist.venice.content.NewTopicForm";

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

  private CommunityContext comm;        // the community we're in
  private ConferenceContext conf;       // the conference being created in
  private String topic_name;            // the initial topic name
  private String pseud;                 // the pseud to display
  private boolean attach;               // is there an attachment?
  private String post_box;              // stuff from the post box
  private String preview = null;        // the preview & spellchuck data
  private int num_errors = 0;           // the number of spelling errors we get

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

  public NewTopicForm(CommunityContext comm, ConferenceContext conf)
  {
    this.comm = comm;
    this.conf = conf;

  } // end constrcutor

  /*--------------------------------------------------------------------------------
   * External static functions
   *--------------------------------------------------------------------------------
   */

  public static NewTopicForm retrieve(ServletRequest request)
  {
    return (NewTopicForm)(request.getAttribute(ATTR_NAME));

  } // end retrieve

  /*--------------------------------------------------------------------------------
   * Implementations from interface VeniceContent
   *--------------------------------------------------------------------------------
   */

  public String getPageTitle(RenderData rdat)
  {
    return "Create New Topic";

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface JSPRender
   *--------------------------------------------------------------------------------
   */

  public void store(ServletRequest request)
  {
    request.setAttribute(ATTR_NAME,this);

  } // end store

  public String getTargetJSPName()
  {
    return "newtopic.jsp";

  } // end getTargetJSPName

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public void setupNewRequest()
  {
    topic_name = "";
    pseud = conf.getDefaultPseud();
    attach = false;
    post_box = "";

  } // end setupNewRequest

  public void generatePreview(VeniceEngine engine, ConferenceContext conf, ServletRequest request)
      throws ValidationException
  {
    HTMLChecker check;

    try
    { // run the data through the HTML Checker
      check = engine.getEscapingChecker();

      // sanitize the "title" parameter
      String foo = request.getParameter("title");
      if (foo==null)
	throw new ValidationException("Title parameter was not specified.");
      check.append(foo);
      check.finish();
      topic_name = check.getValue();
      check.reset();

      // sanitize the "pseud" parameter
      foo = request.getParameter("pseud");
      if (foo==null)
	throw new ValidationException("Pseud parameter was not specified.");
      check.append(foo);
      check.finish();
      pseud = check.getValue();
      check.reset();

      // sanitize the body text itself
      foo = request.getParameter("pb");
      if (foo==null)
	throw new ValidationException("Body text was not specified.");
      check.append(foo);
      check.finish();
      post_box = check.getValue();

      // generate the body text preview
      check = conf.getNewTopicPreviewChecker();
      check.append(foo);
      check.finish();
      preview = check.getValue();
      num_errors = check.getCounter("spelling");

    } // end try
    catch (HTMLCheckerException e)
    { // make sure we catch the HTML Checker exceptions
      throw new InternalStateError("spurious HTMLCheckerException in generatePreview",e);

    } // end catch

    // set the "attach" flag to save the checkbox state
    final String yes = "Y";
    attach = yes.equals(request.getParameter("attach"));

  } // end generatePreview

  public int getCommunityID()
  {
    return comm.getCommunityID();

  } // end getCommunityID

  public int getConfID()
  {
    return conf.getConfID();

  } // end getConfID

  public String getConfName()
  {
    return conf.getName();

  } // end getConfName

  public String getTopicName()
  {
    return topic_name;

  } // end getTopicName

  public String getPseud()
  {
    return pseud;

  } // end getPseud

  public boolean getAttachCheck()
  {
    return attach;

  } // end getAttachCheck

  public String getPostBoxData()
  {
    return post_box;

  } // end getPostBoxData

  public boolean isPreview()
  {
    return (preview!=null);

  } // end isPreview

  public String getPreviewData()
  {
    return preview;

  } // end getPreviewData

  public int getNumSpellingErrors()
  {
    return num_errors;

  } // end getNumSpellingErrors

  public boolean isNullRequest()
  {
    return ((topic_name.length()==0) || (post_box.length()==0));

  } // end isNullRequest

} // end class NewTopicForm
