// 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.VeniceNamespaces);
importPackage(Packages.com.silverwrist.venice.community);
importPackage(Packages.com.silverwrist.venice.content);

req = bsf.lookupBean("request");           // get request
rhelp = bsf.lookupBean("request_help");    // get request helper
session = rhelp.getSession();              // get the session
user = vlib.getUser(session);              // get user

// Tell the session we visited this page last.
session.setObject("/find.js.vs","last.visited","find_categories.js.vs");

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

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

if (rhelp.isVerb("POST"))
{ // Read the form parameters
  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;

  // Get the category service object.
  catsvc = vcast.queryCategoryService(rhelp.getRequestObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"categories"));

  try
  { // search for categories!
    if (fcount<0)
      fcount = catsvc.getSearchCategoryCount(mode,term);
    results = catsvc.searchForCategories(mode,term,ofs,pagesize);

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

  } // end catch

} // end else

if (rc==null)
{ // create the VelocityView for the output
  rc = new VelocityView("Find Categories","find_categories.vm");
  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);

  // Load the profile menu.
  mprov = vcast.queryMenuProvider(rhelp.getRequestObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"venice-menus"));
  menu = mprov.getInlineMenu(user,VeniceNamespaces.CONTENT_LAF_NAMESPACE,"find.menu",cast.newIntArray(0));
  ndx = menu.getItemContainingLinkText("find_categories.js.vs");
  menu.setSelectedIndex(ndx);
  globals = vcast.getGlobalPropertiesStore(req);
  opts = cast.toOptionSet(globals.getObject(VeniceNamespaces.COMMUNITY_GLOBALS_NAMESPACE,"options"));
  if (opts.get(0))
    menu.setVariable("use_categories",Boolean.TRUE);
  rc.setParameter("menu",menu);

} // end if

dynamo.scriptOutput(rc);
