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

rinput = bsf.lookupBean("request");

if (!(rinput.user.hasAdminAccess()))
{ // no admin access - we can't do this
  vlib.output(new ErrorBox("Access Error","You do not have permission to administer the system.",null));
  vlib.done();

} // end if

if ("GET"==rinput.verb)
{ // build a JSPView to hold the upload form
  vlib.output(new JSPView("Import User Accounts","sysadmin/import_form.jsp"));
  vlib.done();

} // end if

// the following assumes a POST
if (rinput.isImageButtonClicked("cancel"))
{ // redirect back to the admin menu
  vlib.output(new Redirect("sysadmin/menu.js.vs",LinkTypes.SERVLET));
  vlib.done();

} // end if

if (!(rinput.isImageButtonClicked("upload")))
{ // the buttons must be wrong!
  logger.error("no known button click on POST, to sysadmin/import.js");
  vlib.output(new ErrorBox("Internal Error","Unknown command button pressed","sysadmin/menu.js.vs"));
  vlib.done();

} // end if

if (!(rinput.isFileParam("idata")))
{ // the idata parameter MUST be a file parameter!
  vlib.output(new ErrorBox("Internal Error","Invalid input file parameter.","sysadmin/menu.js.vs"));
  vlib.done();

} // end if

rc = null;
try
{ // prepare and load the XML file
  ihelper = new ImportHelper(rinput.user.adminInterface,rinput.getParameterDataStream("idata"));

  // set up the return value
  rinput.setRequestAttribute("import.helper",ihelper);
  rc = new JSPView("Import User Accounts","sysadmin/import_results.jsp");

} // end try
catch (e)
{ // translate the various exception types here
  etype = vlib.exceptionType(e) + "";
  if (etype.match("ServletMultipartException"))
    rc = new ErrorBox("Internal Error","Error loading post data: " + e.message,"sysadmin/menu.js.vs");
  else 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);  // all done!
