// 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.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);
on_error = "conf/topics.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID;

if ("GET"==rinput.verb)
{ // create the blank "New Topic Form"
  rinput.setRequestAttribute("postbox.newtopic","");
  rinput.setRequestAttribute("postbox.pseud",conf.defaultPseud);
  rinput.setRequestAttribute("postbox.data","");
  view = new JSPView("Create New Topic","conf/new_topic.jsp");
  view.menuSelector = Content.MENU_SELECTOR_COMMUNITY;
  vlib.output(view);
  vlib.done();

} // end if

if (rinput.isImageButtonClicked("cancel"))
{ // they cancelled - bail out
  vlib.output(new Redirect(on_error,LinkTypes.SERVLET));
  vlib.done();

} // end if

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

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

    // escape the data
    postdata = rinput.getParameter("pb");
    checker.reset();
    checker.append(postdata);
    checker.finish();
    rinput.setRequestAttribute("postbox.data",checker.getValue());

    // run the preview
    checker = conf.getNewTopicPreviewChecker();
    checker.append(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 new_topic.js preview: " + e);

  } // end catch

  // save off any additional parameters we need
  tmp = rinput.getParameter("attach");
  if (!(vlib.emptyString(tmp)))
    rinput.setRequestAttribute("postbox.attach","Y");

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

} // end if

if (!(rinput.isImageButtonClicked("post1")))
{ // there were only the three buttons - bug out!
  logger.error("no known button click on POST to conf/new_topic.js");
  vlib.output(new ErrorBox("Internal Error","Unknown command button pressed",on_error));
  vlib.done();

} // end if

rc = null;
try
{ // add the new topic!
  topic = conf.addTopic(rinput.getParameter("title"),rinput.getParameter("pseud"),rinput.getParameter("pb"));

  // decide where we go from here
  if (vlib.emptyString(rinput.getParameter("attach")))
    rc = new Redirect(on_error,LinkTypes.SERVLET);  // bounce straight to our new view!
  else
  { // we need to display the attachment form
    msg = topic.getMessage(0);  // the message the attachment gets hooked to
    rinput.setRequestAttribute("attachment.postid",msg.postID + "");
    rinput.setRequestAttribute("attachment.target",on_error);
    rc = new JSPView("Upload Attachment","conf/attachment.jsp");
    rc.menuSelector = Content.MENU_SELECTOR_COMMUNITY;

  } // end else

} // end try
catch (e)
{ // new topic creation failed
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error creating topic: " + 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!
