// 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(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.dlg);
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");

if (!(comm.canCreateConference()))
{ // can't create a conference
  vlib.output(new ErrorBox("Access Error","You are not permitted to create conferences in this community.",
			   "conf/conferences.js.vs?cc=" + comm.communityID));
  vlib.done();

} // end if

dlg = rinput.getDialog("conf.create");

if ("GET"==rinput.verb)
{ // return the dialog to be filled in
  dlg.setSubtitle("in Community: " + comm.name);
  dlg.setValue("cc",comm.communityID + "");
  vlib.output(dlg);
  vlib.done();

} // end if

op = dlg.whichButton(rinput) + "";
if (op=="cancel")
{ // they canceled - redirect back to the conferences list
  vlib.output(new Redirect("conf/conferences.js.vs?cc=" + comm.communityID,LinkTypes.SERVLET));
  vlib.done();

} // end if

dlg.load(rinput);  // load the dialog

rc = null;
if (op=="create")
{ // we actually want to create the conference...
  try
  { // first validate the dialog
    dlg.validate();

    // now validate that the alias doesn't exist yet
    if (rinput.engine.confAliasExists(dlg.getValue("alias")))
      throw new ValidationException("That alias is already used by another conference on the system.");

    // OK, create the conference!
    pvt = ("1"==dlg.getValue("ctype"));
    conf = comm.createConference(dlg.getValue("name"),dlg.getValue("alias"),dlg.getValue("descr"),pvt,
                                 dlg.getValue("hide").booleanValue());

    // now bounce to the new conference's topic list so we can start setting it up
    rc = new Redirect("conf/topics.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID,LinkTypes.SERVLET);

  } // end try
  catch (e)
  { // error creating the conference
    etype = vlib.exceptionType(e) + "";
    if (etype.match("ValidationException"))
    { // reset the dialog and try again
      dlg.setErrorMessage(e.message + " Please try again.");
      dlg.setSubtitle("in Community: " + comm.name);
      rc = dlg;

    } // end if
    else if (etype.match("AccessError"))
      rc = new ErrorBox("Access Error",e.message,"conf/conferences.js.vs?cc=" + comm.communityID);
    else if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error creating conference: " + e.message,
			"conf/conferences.js.vs?cc=" + comm.communityID);
    else
      rc = e;

  } // end catch

} // end if
else
{ // error - don't know what button was clicked
  logger.error("no known button click on POST to conf/create.js");
  rc = new ErrorBox("Internal Error","Unknown command button pressed",
                    "conf/conferences.js.vs?cc=" + comm.communityID);

} // end else

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