// 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(java.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);

// get the request object and the community
rinput = bsf.lookupBean("request");
comm = rinput.getCommunity(true,"top.js.vs");

// create the view object
view = new MembersView(rinput.engine.getStdNumSearchResults(),comm.name);
rc = null;
try
{ // set up the member list
  if ("GET"==rinput.verb)
  { // initial GET of the member list - just get the start of the list
    mbr_list = comm.getMemberList();
    view.simple = true;
    view.results = mbr_list;
    view.findCount = mbr_list.size();

  } // end if
  else
  { // this is a more complicated operation...
    if ("1"==rinput.getParameter("sl"))
    { // Set up some of the easy initial parameters.
      view.simple = true;

      // Retrieve the offset and find count.
      view.offset = rinput.getParameterInt("ofs",0);
      view.findCount = rinput.getParameterInt("fcount",-1);
 
      // Adjust the offset based on the button clicked.
      if (rinput.isImageButtonClicked("search"))
        throw new ValidationException("Invalid button clicked."); // can't happen
      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 list and subset it if necessary.
      mbr_list = comm.getMemberList();
      if (view.findCount<0)
        view.findCount = mbr_list.size();
      if (view.offset==0)
        view.results = mbr_list;
      else
        view.results = mbr_list.subList(offset,mbr_list.size());

    } // end if
    else
    { // This is a search operation...
      view.simple = false;

      // Get 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;

      // Get 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;

      // Get the search term parameter.
      view.term = rinput.getParameter("term");
      if (view.term==null)
        view.term = "";

      // Retrieve the offset and find count.
      view.offset = rinput.getParameterInt("ofs",0);
      view.findCount = rinput.getParameterInt("fcount",-1);
 
      // Adjust the offset based on the button clicked.
      if (rinput.isImageButtonClicked("search"))
        view.offset = 0;  // begin a new search
      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.");

      // Perform the search!
      view.results = comm.searchForMembers(view.field,view.mode,view.term,view.offset,view.maxResults);
      if (view.findCount<0)
	view.findCount = comm.getSearchMemberCount(view.field,view.mode,view.term);

    } // end else (search operation)

  } // end else (POST)

  rc = view;

} // end try
catch (e)
{ // exception in the find operation
  etype = vlib.exceptionType(e) + "";
  if (etype.match("ValidationException"))
    rc = new ErrorBox("Find Error",e.message,"comm/members.js.vs?cc=" + comm.communityID);
  else if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error getting member list: " + e.message,
                      "community/" + comm.alias);
  else
    rc = e;

} // end catch

vlib.output(rc);