// 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): 

importPackage(java.util);
importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.htmlcheck);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.conf);
importPackage(Packages.com.silverwrist.venice.ui.helpers);
importPackage(Packages.com.silverwrist.venice.ui.view);

// get the request object and the community
rinput = bsf.lookupBean("request");
comm = rinput.getCommunity(true,"top.js.vs");

// get the current conference
currc = new CurrentConference(rinput);
conf = currc.getConference(true,"conf/conferences.js.vs?cc=" + comm.communityID);

// get the current topic
topic = currc.getTopic(true,"conf/topics.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID);
on_error = "conf/posts.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID + "&top=" + topic.topicNumber;

if (rinput.isImageButtonClicked("cancel"))
{ // cancelling - go back where we came from
  vlib.output(new Redirect(on_error,LinkTypes.SERVLET));
  vlib.done();

} // end if

// get the current post number
post_num = rinput.getParameterInt("sd",-1);
if (post_num<0)
{ // this value is critical for slippage detection - we must have it
  vlib.output(new ErrorBox(null,"Invalid parameter.",on_error));
  vlib.done();

} // end if

// get the raw post data
raw_postdata = rinput.getParameter("pb");
if (raw_postdata==null)
  raw_postdata = "";

if (rinput.isImageButtonClicked("preview"))
{ // generate a preview!  start by passing all the data through the HTML checker as needed
  try
  { // escape the pseud
    checker = rinput.engine.getEscapingChecker();
    checker.append(rinput.getParameter("pseud"));
    checker.finish();
    rinput.setRequestAttribute("postbox.pseud",checker.getValue());

    // escape the data
    checker.reset();
    checker.append(raw_postdata);
    checker.finish();
    rinput.setRequestAttribute("postbox.data",checker.getValue());

    // run the preview
    checker = topic.getPreviewChecker();
    checker.append(raw_postdata);
    checker.finish();
    rinput.setRequestAttribute("preview.text",checker.getValue());
    rinput.setRequestAttribute("preview.num_errors",new java.lang.Integer(checker.getCounter("spelling")));

  } // end try
  catch (e)
  { // watch out for spurious HTMLCheckerExceptions
    etype = vlib.exceptionType(e) + "";
    if (etype.match("HTMLCheckerException"))
      throw new InternalStateError("spurious HTMLCheckerException thrown");
    else
      throw new InternalStateError("unknown exception thrown in post_message.js preview: " + e);

  } // end catch

  // save off any additional parameters we need
  tmp = rinput.getParameter("next");
  if (tmp!=null)
    rinput.setRequestAttribute("postbox.hidden.next",tmp);
  rinput.setRequestAttribute("postbox.hidden.sd",post_num + "");
  tmp = rinput.getParameter("attach");
  if (!(vlib.emptyString(tmp)))
    rinput.setRequestAttribute("postbox.attach","Y");
  tmp = rinput.getParameter("slip");
  if (!(vlib.emptyString(tmp)))
    rinput.setRequestAttribute("postbox.hidden.slip","Y");

  // create a JSPView for the preview and output it
  view = new JSPView("Previewing Message","conf/preview.jsp");
  view.menuSelector = Content.MENU_SELECTOR_COMMUNITY;
  vlib.output(view);
  vlib.done();

} // end if

if (!(rinput.isImageButtonClicked("post") || rinput.isImageButtonClicked("postnext")
    || rinput.isImageButtonClicked("posttopics")))
{ // one of the three POST buttons MUST have been checked!
  logger.error("no known button click on POST to conf/post_message.js");
  vlib.output(new ErrorBox("Internal Error","Unknown command button pressed",on_error));
  vlib.done();

} // end if

if (post_num!=topic.getTotalMessages())
{ // slippage detected!  generate parameters for the Slippage view
  rc = null;
  try
  { // get the list of messages associated with the slippage
    rinput.setRequestAttribute("slippage.messages",topic.getMessages(post_num,topic.totalMessages-1));

    // generate the topic stem
    aliases = conf.aliases;
    rinput.setRequestAttribute("slippage.topic_stem",
                               aliases.get(0).toString() + "." + topic.topicNumber + ".");

    // escape the pseud
    checker = rinput.engine.getEscapingChecker();
    checker.append(rinput.getParameter("pseud"));
    checker.finish();
    rinput.setRequestAttribute("postbox.pseud",checker.getValue());

    // escape the data
    checker.reset();
    checker.append(raw_postdata);
    checker.finish();
    rinput.setRequestAttribute("postbox.data",checker.getValue());

    // save off any additional parameters we need
    tmp = rinput.getParameter("next");
    if (tmp!=null)
      rinput.setRequestAttribute("postbox.hidden.next",tmp);
    rinput.setRequestAttribute("postbox.hidden.sd",topic.totalMessages + "");
    tmp = rinput.getParameter("attach");
    if (!(vlib.emptyString(tmp)))
      rinput.setRequestAttribute("postbox.attach","Y");

    // create the slippage view
    rc = new JSPView("Slippage or Double-Click Detected","conf/slippage.jsp");
    rc.menuSelector = Content.MENU_SELECTOR_COMMUNITY;

  } // end try
  catch (e)
  { // handle an exception generating the slippage view
    etype = vlib.exceptionType(e) + "";
    if (etype.match("HTMLCheckerException"))
      throw new InternalStateError("spurious HTMLCheckerException thrown");
    else if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error generating slippage view: " + e.message,on_error);
    else if (etype.match("AccessError"))
      rc = new ErrorBox("Access Error",e.message,on_error);
    else
      rc = e;

  } // end catch

  vlib.output(rc);
  vlib.done();

} // end if

rc = null;
try
{ // actually post the darn thing!
  msg = topic.postNewMessage(0,rinput.getParameter("pseud"),raw_postdata);

  // If there was no slippage, make sure we mark the new post as "read."
  if (!(rinput.hasParameter("slip")))
    topic.fixSeen();

  // Where do we want to go now?
  tmp = "";
  if (rinput.isImageButtonClicked("posttopics"))
    tmp = "conf/topics.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID;
  else
  { // find the topic number to go to next and compute the new URL
    my_topicnum = -1;
    if (rinput.isImageButtonClicked("postnext"))
      my_topicnum = rinput.getParameterShort("next",topic.topicNumber);
    else
      my_topicnum = topic.topicNumber;
    tmp = "conf/posts.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID + "&top=" + my_topicnum
        + "&rnm=1";

  } // end else

  if (vlib.emptyString(rinput.getParameter("attach")))
    rc = new Redirect(tmp,LinkTypes.SERVLET);  // bounce straight to our new view!
  else
  { // we need to display the attachment form
    rinput.setRequestAttribute("attachment.postid",msg.postID + "");
    rinput.setRequestAttribute("attachment.target",tmp);
    rc = new JSPView("Upload Attachment","conf/attachment.jsp");
    rc.menuSelector = Content.MENU_SELECTOR_COMMUNITY;

  } // end else

} // end try
catch (e)
{ // handle an error in doing the post
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error posting message: " + e.message,on_error);
  else if (etype.match("AccessError"))
    rc = new ErrorBox("Access Error",e.message,on_error);
  else
    rc = e;

} // end catch

vlib.output(rc);  // all done!
