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

// Define a simple function to set up the dialog stuff.
function setupDialog(dlg,conf)
{
  dlg.setSubtitle(conf.name);
  sinf = conf.securityInfo;
  dlg.sendMessage("read_lvl","setRoleList",sinf.getRoleList("Conference.Read"));
  dlg.sendMessage("post_lvl","setRoleList",sinf.getRoleList("Conference.Post"));
  dlg.sendMessage("create_lvl","setRoleList",sinf.getRoleList("Conference.Create"));
  dlg.sendMessage("hide_lvl","setRoleList",sinf.getRoleList("Conference.Hide"));
  dlg.sendMessage("nuke_lvl","setRoleList",sinf.getRoleList("Conference.Nuke"));
  dlg.sendMessage("change_lvl","setRoleList",sinf.getRoleList("Conference.Change"));
  dlg.sendMessage("delete_lvl","setRoleList",sinf.getRoleList("Conference.Delete"));
  dlg.setEnabled("hide",conf.canSetHideList());

} // end setupDialog

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

if (!(conf.canChangeConference()))
{ // you can't fiddle with this conference
  vlib.output(new ErrorBox("Access Error","You do not have permission to change this conference's attributes.",
                           on_error));
  vlib.done();

} // end if

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

rc = null;
if ("GET"==rinput.verb)
{ // set up the dialog
  setupDialog(dlg,conf);

  try
  { // fill the dialog with the conference information
    dlg.setValue("cc",comm.communityID + "");
    dlg.setValue("conf",conf.confID + "");
    dlg.setValue("name",conf.name);
    dlg.setValue("descr",conf.description);
    if (conf.canSetHideList() && conf.getHideList())
      dlg.setValue("hide",1);
    dlg.setValue("read_lvl",conf.getReadLevel() + "");
    dlg.setValue("post_lvl",conf.getPostLevel() + "");
    dlg.setValue("create_lvl",conf.getCreateLevel() + "");
    dlg.setValue("hide_lvl",conf.getHideLevel() + "");
    dlg.setValue("nuke_lvl",conf.getNukeLevel() + "");
    dlg.setValue("change_lvl",conf.getChangeLevel() + "");
    dlg.setValue("delete_lvl",conf.getDeleteLevel() + "");

    props = conf.properties;
    if (props.displayPostPictures)
      dlg.setValue("pic_in_post",1);

    rc = dlg;

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

// everything that follows is for a POST operation
op = dlg.whichButton(rinput) + "";
if (op=="cancel")
{ // user cancelled profile update - bounce back to the target
  vlib.output(new Redirect(on_error,LinkTypes.SERVLET));
  vlib.done();

} // end if

if (op!="update")
{ // what the hell was that button?
  logger.error("no known button click on POST to conf/edit.js");
  vlib.output(new ErrorBox("Internal Error","Unknown command button pressed",on_error));
  vlib.done();

} // end if

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

try
{ // validate the dialog
  dlg.validate();

  // get all the levels out of the dialog
  read_lvl = dlg.getValue("read_lvl").intValue();
  post_lvl = dlg.getValue("post_lvl").intValue();
  create_lvl = dlg.getValue("create_lvl").intValue();
  hide_lvl = dlg.getValue("hide_lvl").intValue();
  nuke_lvl = dlg.getValue("nuke_lvl").intValue();
  change_lvl = dlg.getValue("change_lvl").intValue();
  delete_lvl = dlg.getValue("delete_lvl").intValue();

  // set the various conference attributes
  conf.setSecurityLevels(read_lvl,post_lvl,create_lvl,hide_lvl,nuke_lvl,change_lvl,delete_lvl);
  conf.name = dlg.getValue("name");
  conf.description = dlg.getValue("descr");
  if (conf.canSetHideList())
    conf.setHideList(dlg.getValue("hide").booleanValue());

  // set the conference properties
  props = conf.properties;
  props.displayPostPictures = dlg.getValue("pic_in_post").booleanValue();
  conf.properties = props;

  // bounce back to the manage display
  rc = new Redirect(on_error,LinkTypes.SERVLET);

} // end try
catch (e)
{ // error editing the conference
  etype = vlib.exceptionType(e) + "";
  if (etype.match("ValidationException"))
  { // reset the dialog and try again
    dlg.setErrorMessage(e.message + " Please try again.");
    setupDialog(dlg,conf);
    rc = dlg;

  } // end if
  else if (etype.match("AccessError"))
    rc = new ErrorBox("Access Error",e.message,on_error);
  else if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error editing conference: " + e.message,on_error);
  else
    rc = e;

} // end catch

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