// 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(java.util);
importClass(Packages.com.silverwrist.dynamo.Namespaces);
importPackage(Packages.com.silverwrist.dynamo.except);
importPackage(Packages.com.silverwrist.dynamo.iface);
importPackage(Packages.com.silverwrist.dynamo.module);
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.iface);

req = bsf.lookupBean("request");
req_help = bsf.lookupBean("request_help");
user = vlib.getUser(req);

// 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.SYSTEM_PERMS_NAMESPACE,"uninstall.modules")))
  dynamo.scriptReturn(vlib.stdErrorBox(req,"Security Error","You are not permitted to uninstall modules."));

modops = cast.queryModuleOperations(req);

the_module = rhelp.getParameterString("mod");
if (the_module!=null)
{ // we want to uninstall this module
  rc = null;
  if (vlib.confirmed(req,"/sysadmin/module_uninstall.js.vs","confirmation","confnum"))
  { // OK, they have confirmed they want to uninstall the module...do it!
    mod = modops.findModuleByFilename(n);
    except = null;
    log = new SimpleMemoryLog();
    try
    { // uninstall the module
      mod.uninstall(user,log);
      modops.freeUnusedModules();  // GC the module list, as it were

    } // end try
    catch (e)
    { // save the exception
      except = e;

    } // end catch

    // Return the results.
    rc = new VelocityView("Module Uninstallation","sysadmin/module_uninstall_result.vm");
    rc.setParameter("module",the_module);
    rc.setParameter("log",log.getLog());
    if (except!=null)
      rc.setParameter("except",except);

  } // end if
  else
  { // generate a confirmation number box
    msg = "You are about to uninstall module '" + the_module + "'!  This will cause any and all data associated with "
        + "this module (such as database tables and their contents) to be <b>DESTROYED AND IRREVOCABLY LOST!</b> "
        + "Are you <em>really</em> sure you want to do this?";
    ok_url = "sysadmin/module_uninstall.js.vs?mod=" + stringutils.encodeURL(the_module);
    rc = new ConfirmBox(req,"/sysadmin/module_uninstall.js.vs","confirmation","confnum",
                        "Module Uninstall Confirmation",msg,"SERVLET",ok_url,"SERVLET",
                        "sysadmin/module_uninstall.js.vs");

  } // end else

  dynamo.scriptReturn(rc);

} // end if

// Get the set of all installed module files.
instmod = modops.listInstalledModuleNames();

// Turn it into a list of name/description pairs.
namelist = new ArrayList();
it = instmod.iterator();
while (it.hasNext())
{ // get each module name in turn, and get the module reference
  n = it.next() + "";
  mod = modops.findModuleByFilename(n);
  if (mod!=null)
  { // get the module's description and add it
    tmp = new HashMap();
    tmp.put("name",n);
    tmp.put("descr",mod.getDescription());
    namelist.add(tmp);

  } // end if

} // end while

// Return the Velocity view.
rc = new VelocityView("Uninstall Modules","sysadmin/module_uninstall.vm");
rc.setParameter("modlist",namelist);
dynamo.scriptOutput(rc);
