// 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-02 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.dlg);
importPackage(Packages.com.silverwrist.venice.ui.helpers);

// small function to setup the global properties dialog
function setupDialog(dlg,engine)
{
  sinf = engine.securityInfo;
  dlg.sendMessage("create_lvl","setRoleList",sinf.getRoleList("Global.CreateCommunity"));

} // end setupDialog

// get the request object
rinput = bsf.lookupBean("request");

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

  try
  { // get the administrative interface
    adm = user.adminInterface;
    props = adm.properties;
    dlg.setValue("search_items",props.searchItemsPerPage);
    dlg.setValue("fp_posts",props.postsOnFrontPage);
    dlg.setValue("audit_recs",props.auditRecordsPerPage);
    dlg.setValue("create_lvl",props.communityCreateLevel);
    dlg.setValue("comm_mbrs",props.communityMembersPerPage);
    dlg.setValue("posts_page",props.postsPerPage);
    dlg.setValue("old_posts",props.oldPostsAtTop);
    dlg.setValue("conf_mbrs",props.conferenceMembersPerPage);
    if (props.displayPostPictures)
      dlg.setValue("pic_in_post",1);
    if (props.disableCategories)
      dlg.setValue("no_cats",1);

    rc = dlg;

  } // end try
  catch (e)
  { // exception setting up dialog
    etype = vlib.exceptionType(e) + "";
    if (etype.match("AccessError"))
      rc = new ErrorBox("Access Error","You do not have permission to administer the system.",null);
    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 menu
  vlib.output(new Redirect("sysadmin/menu.js.vs",LinkTypes.SERVLET));
  vlib.done();

} // end if

if (op=="update")
{ // load the values from the dialog
  dlg.load(rinput);

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

    // set all the properties correctly
    adm = user.adminInterface;
    props = adm.properties;
    props.searchItemsPerPage = dlg.getValue("search_items").intValue();
    props.postsOnFrontPage = dlg.getValue("fp_posts").intValue();
    props.auditRecordsPerPage = dlg.getValue("audit_recs").intValue();
    props.communityCreateLevel = dlg.getValue("create_lvl").intValue();
    props.communityMembersPerPage = dlg.getValue("comm_mbrs").intValue();
    props.postsPerPage = dlg.getValue("posts_page").intValue();
    props.oldPostsAtTop = dlg.getValue("old_posts").intValue();
    props.conferenceMembersPerPage = dlg.getValue("conf_mbrs").intValue();
    props.displayPostPictures = dlg.getValue("pic_in_post").booleanValue();
    props.disableCategories = dlg.getValue("no_cats").booleanValue();
    adm.properties = props;

    // done - bounce back to the menu
    rc = new Redirect("sysadmin/menu.js.vs",LinkTypes.SERVLET);

  } // end try
  catch (e)
  { // caught an exception - figure out what to do
    etype = vlib.exceptionType(e) + "";
    if (etype.match("ValidationException"))
    { // validation error - reset the dialog and try again
      dlg.setErrorMessage(e.message + " Please try again.");
      setupDialog(dlg,rinput.engine);
      rc = dlg;

    } // end if
    else if (etype.match("AccessError"))
      rc = new ErrorBox("Access Error","You do not have permission to administer the system.",null);
    else if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Unable to update global properties: " + e.message,
                        "sysadmin/menu.js.vs");
    else
      rc = e;

  } // end catch

} // end if
else
{ // what the hell got clicked?
  logger.error("no known button click on POST to sysadmin/global.js");
  rc = new ErrorBox("Internal Error","Unknown command button pressed","sysadmin/menu.js.vs");

} // end else

vlib.output(rc);