// 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@ricochet.com>,
// for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
// Copyright (C) 2002-2004 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);

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

// get the message under consideration
msg = currc.getTopicMessageParam("msg",true,"conf/posts.js.vs?cc=" + comm.communityID + "&conf="
                                           + conf.confID + "&top=" + topic.topicNumber);
on_error = "conf/posts.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID + "&top=" + topic.topicNumber
         + "&p1=" + msg.getPostNumber() + "&shac=1";

if ("GET"==rinput.verb)
{ // set up the prompt for which topic to move the post to
  rinput.setRequestAttribute("message.move.message",msg);
  poster = "";
  try
  { // get the poster name
    poster = msg.creatorName;

  } // end try
  catch (foo)
  { // we hit a DataException here
    poster = "(unknown)";

  } // end catch

  rinput.setRequestAttribute("message.move.postername",poster);

  rc = null;
  try
  { // get the list of topics in the conference
    in_list = vlib.castList(conf.getTopicList(ConferenceContext.GET_ALL,ConferenceContext.SORT_NAME,true));
    out_list = vlib.createList();
    it = in_list.iterator();
    while (it.hasNext())
    { // add all topic names to the list EXCEPT the current one
      xtopic = vlib.castTopicContext(it.next());
      if (xtopic.topicID!=topic.topicID)
        out_list.add(xtopic);

    } // end while

    rinput.setRequestAttribute("message.move.topics",out_list);

    // create the actual view and output it
    rc = new JSPView("Move Message","conf/move_message.jsp");
    rc.menuSelector = Content.MENU_SELECTOR_COMMUNITY;

  } // end try
  catch (e)
  { // handle the exception here
    etype = vlib.exceptionType(e) + "";
    if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error listing messages: " + 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 ("GET" request)

if (rinput.isImageButtonClicked("cancel"))
{ // Cancel button pressed - bounce back to the message
  vlib.output(new Redirect(on_error,LinkTypes.SERVLET));
  vlib.done();

} // end if

if (!(rinput.isImageButtonClicked("move")))
{ // invalid command button pressed
  logger.error("no known button click on POST to conf/move_message.js");
  vlib.output(new ErrorBox("Internal Error","Invalid command button pressed.",on_error));
  vlib.done();

} // end if

rc = null;
try
{ // move the message!
  topic_id = rinput.getParameterInt("target",-1);
  msg.moveToTopic(topic_id);

  // bounce to the topic we moved from
  rc = new Redirect("conf/posts.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID + "&top="
                    + topic.topicNumber,LinkTypes.SERVLET);

} // end try
catch (e)
{ // handle the exception
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error moving 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...
