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

// Implements the session API

importPackage(java.util);
importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.ui.helpers);
importPackage(Packages.com.silverwrist.venice.ui.rpc);

rinput = bsf.lookupBean("request");
xreq = bsf.lookupBean("xmlrpc");
env = bsf.lookupBean("environment");
call_name = env.get("call");
//logger.debug("call selector = " + call_name);

if ("destroy"==call_name)
{ // venice:session.destroy <session-id> - destroys a session
  if (xreq.paramCount!=1)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else
  { // end the session
    sctl = vlib.querySessionControl(rinput);
    sctl.endSession();
    vlib.output(vlib.booleanObject(true));

  } // end else

  vlib.done();
  
} // end if

if ("login"==call_name)
{ // venice:session.login <session-id> <username> <password> - logs in the session
  if (xreq.paramCount!=3)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else
  { // get the user name and password and log in
    username = xreq.getParamString(1);
    password = xreq.getParamString(2);

    if (rinput.user.isLoggedIn())
      vlib.output(new XmlRpcFault(XmlRpcFault.DEFAULT_ERROR,"user already logged in"));
    else
      rinput.user.authenticate(username,password);
    vlib.output(vlib.booleanObject(true));

  } // end else

  vlib.done();

} // end if

// just in case there's a method name that wasn't listed
vlib.output(new XmlRpcFault(XmlRpcFault.METHOD_NOT_FOUND,"invalid method name: " + xreq.method));
