// 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.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

// Create the new view.
view = new AdminFindUserView(rinput.engine.getStdNumSearchResults());

if ("GET"==rinput.verb)
{ // initialize the field, mode, and term and do the search
  view.field = SearchMode.FIELD_USER_NAME;
  view.mode = SearchMode.SEARCH_PREFIX;
  view.term = "";
  vlib.output(view);
  vlib.done();

} // end if

// everything here on is for a POST
rc = null;
try
{ // load the search field parameter
  x = rinput.getParameterInt("field",SearchMode.FIELD_USER_NAME);
  if (   (x!=SearchMode.FIELD_USER_NAME) && (x!=SearchMode.FIELD_USER_DESCRIPTION)
      && (x!=SearchMode.FIELD_USER_GIVEN_NAME) && (x!=SearchMode.FIELD_USER_FAMILY_NAME))
    throw new ValidationException("The search field parameter is not valid.");
  view.field = x;

  // load the search mode parameter    
  x = rinput.getParameterInt("mode",SearchMode.SEARCH_PREFIX);
  if ((x!=SearchMode.SEARCH_PREFIX) && (x!=SearchMode.SEARCH_SUBSTRING) && (x!=SearchMode.SEARCH_REGEXP))
    throw new ValidationException("The search mode parameter is not valid.");
  view.mode = x;

  // load the search term, offset, and find count
  view.term = rinput.getParameter("term");
  view.offset = rinput.getParameterInt("ofs",0);
  view.findCount = rinput.getParameterInt("fcount",-1);

  // adjust the offset based on the command button click
  if (rinput.isImageButtonClicked("search"))
    view.offset = 0;
  else if (rinput.isImageButtonClicked("previous"))
    view.offset = Math.max(view_offset - view.maxResults,0);
  else if (rinput.isImageButtonClicked("next"))
    view.offset += view.maxResults;
  else
    throw new ValidationException("Unable to determine what action triggered the form.");

  // get the user results
  view.results = rinput.engine.searchForUsers(view.field,view.mode,view.term,view.offset,view.maxResults);
  if (view.findCount<0)
    view.findCount = rinput.engine.getSearchUserCount(view.field,view.mode,view.term);

  rc = view;

} // end try
catch (e)
{ // get the exception type
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error on find: " + e.message,"sysadmin/find_user.js.vs");
  else if (etype.match("ValidationException"))
    rc = new ErrorBox("Find Error",e.message,"sysadmin/find_user.js.vs");
  else
    rc = e;

} // end catch

vlib.output(rc);
