// 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.lang);
importClass(Packages.com.silverwrist.dynamo.Namespaces);
importPackage(Packages.com.silverwrist.dynamo.iface);
importPackage(Packages.com.silverwrist.dynamo.util);
importClass(Packages.com.silverwrist.venice.SearchMode);
importClass(Packages.com.silverwrist.venice.UserSearchField);
importClass(Packages.com.silverwrist.venice.VeniceNamespaces);
importClass(Packages.com.silverwrist.venice.app.AdvancedUserService);
importPackage(Packages.com.silverwrist.venice.content);

req = bsf.lookupBean("request");           // get request
rhelp = bsf.lookupBean("request_help");    // get request helper
user = vlib.getUser(req);                  // get user
comm = vlib.getCommunity(req);             // get community

membergroup = comm.getMemberGroup();

// Get the user's configured page size.
pagesize = cast.toInteger(user.getObject(VeniceNamespaces.USER_SETTINGS_NAMESPACE,"search.result.count"));

stdlist = 1;
field = UserSearchField.USERNAME;
mode = SearchMode.PREFIX;
term = "";
ofs = 0;
fcount = -1;
results = null;
rc = null;

if (rhelp.isVerb("POST"))
{ // Read the form parameters
  stdlist = rhelp.getParameterInt("sl",1);
  field = UserSearchField.getEnum(rhelp.getParameter("field"));
  mode = SearchMode.getEnum(rhelp.getParameter("mode"));
  term = rhelp.getParameter("term");
  ofs = rhelp.getParameterInt("ofs",0);
  fcount = rhelp.getParameterInt("fcount",-1);

  // Adjust offset based on the button that was clicked.
  if (rhelp.isImageButtonClicked("search"))
  { // beginning of a new search
    ofs = 0;
    fcount = -1;

  } // end if
  else if (rhelp.isImageButtonClicked("previous"))
    ofs = Math.max(ofs - pagesize,0);
  else if (rhelp.isImageButtonClicked("next"))
    ofs += pagesize;

} // end else

try
{ // fill in the results and find count
  if (stdlist==1)
  { // get the usual member list
    if (fcount<0)
      fcount = membergroup.getMemberCount();
    results = membergroup.getMembers(ofs,pagesize);

  } // end if
  else
  { // we're searching
    usersvc = vcast.queryAdvancedUserService(rhelp.getRequestObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"adv-users"));
    if (fcount<0)
      fcount = usersvc.getSearchMemberCount(user,membergroup,field,mode,term);
    results = usersvc.searchForMembers(user,membergroup,field,mode,term,ofs,pagesize);

  } // end else

} // end try
catch (e)
{ // exception thrown
  rc = new ErrorBox("Database Error",e,"SERVLET","comm/members.js.vs");

} // end catch

if (rc==null)
{ // create the VelocityView for the output
  rc = new VelocityView(comm.name + ": Members","comm/members.vm");
  rc.menuSelector = "community";
  rc.setParameter("comm",comm);
  rc.setParameter("stdlist",cast.toIntegerObject(stdlist));
  rc.setParameter("field",field.getName());
  rc.setParameter("mode",mode.getName());
  rc.setParameter("term",term.toString());
  rc.setParameter("ofs",cast.toIntegerObject(ofs));
  rc.setParameter("fcount",cast.toIntegerObject(fcount));
  rc.setParameter("pagesize",cast.toIntegerObject(pagesize));
  if (results!=null)
    rc.setParameter("results",results);

} // end if

dynamo.scriptOutput(rc);
