// 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) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importPackage(Packages.com.silverwrist.util);
importClass(Packages.com.silverwrist.dynamo.Namespaces);
importPackage(Packages.com.silverwrist.dynamo.except);
importPackage(Packages.com.silverwrist.dynamo.iface);
importPackage(Packages.com.silverwrist.dynamo.security);
importPackage(Packages.com.silverwrist.dynamo.util);
importClass(Packages.com.silverwrist.venice.VeniceNamespaces);
importPackage(Packages.com.silverwrist.venice.content);
importPackage(Packages.com.silverwrist.venice.frame);

req = bsf.lookupBean("request");
req_help = bsf.lookupBean("request_help");
user = vlib.getUser(req);
vlib.setOnError(req,"sysadmin/main.js.vs");
vlib.setOnErrorType(req,"SERVLET");

// Make sure we are permitted to be here.
srm = cast.querySecurityReferenceMonitor(req_help.getRequestObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"srm"));
acl = srm.getGlobalAcl();
if (!(acl.testPermission(user,VeniceNamespaces.SESSION_CONTROL_NAMESPACE,"set.property")))
  dynamo.scriptReturn(vlib.stdErrorBox(req,"Security Error","You are not permitted to modify E-mail settings."));

// Get the global properties store.
globals = vcast.getGlobalPropertiesStore(req);
blocks = vcast.getGlobalBlocksStore(req);

// Create the dialog.
loader = cast.queryDialogLoader(req);
dlg = loader.loadDialogResource("sysadmin/session.dlg.xml");

my_ns = VeniceNamespaces.SESSION_CONTROL_NAMESPACE;  // shortcut

rc = null;
if (req_help.isVerb("GET"))
{ // load the default values into the dialog
  dlg.setValue("init",globals.getObject(my_ns,"session.init.script"));
  dlg.setValue("cookie",globals.getObject(my_ns,"login.cookie"));
  dlg.setValue("cookieage",globals.getObject(my_ns,"login.cookie.maxage"));
  dlg.setValue("recovery",globals.getObject(my_ns,"password.recovery.time"));

} // end if
else
{ // Load information from the dialog
  op = dlg.getClickedButton(req) + "";
  if (op=="cancel")  // user cancelled - bounce back to the previous menu
    rc = new Redirect("SERVLET","sysadmin/main.js.vs");
  else
  { // load and validate the dialog
    dlg.load(req);
    try
    { // validate the dialog
      dlg.validate(req);

      // set the appropriate values into the system properties
      globals.setObject(user,my_ns,"session.init.script",dlg.getValue("init"));
      globals.setObject(user,my_ns,"login.cookie",dlg.getValue("cookie"));
      globals.setObject(user,my_ns,"login.cookie.maxage",dlg.getValue("cookieage"));
      globals.setObject(user,my_ns,"password.recovery.time",dlg.getValue("recovery"));

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

    } // end try
    catch (e)
    { // thrown an exception from the above process - what sort?
      etype = dynamo.exceptionType(e) + "";
      logger.error("Caught exception of type " + etype);
      if (etype.match(/ValidationException/))
        dlg.setErrorMessage(dynamo.exceptionMessage(e) + " Please try again.");
      else if (etype.match(/DatabaseException/))
        rc = new ErrorBox("Database Error",e,"SERVLET","sysadmin/main.js.vs");
      else if (etype.match(/DynamoSecurityException/))
        rc = new ErrorBox("Security Error",e,"SERVLET","sysadmin/main.js.vs");
      else
        rc = new ErrorBox("Unknown Exception",e,"SERVLET","sysadmin/main.js.vs");

    } // end catch

  } // end else

} // end else

if (rc==null)
  rc = new FrameDialog(dlg);  // output dialog if we don't have another value
dynamo.scriptOutput(rc);
