// 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-02 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");

// Determine which Find display needs to be brought up.
disp = 0;
if (rinput.hasParameter("disp"))
{ // get the display parameter from the input
  disp = rinput.getParameterInt("disp",-1);
  if (   (disp!=FindView.FD_COMMUNITIES) && (disp!=FindView.FD_USERS) && (disp!=FindView.FD_CATEGORIES)
      && (disp!=FindView.FD_POSTS))
  { // the display parameter is not correct
    vlib.output(new ErrorBox(null,"Invalid display parameter.","top.js.vs"));
    vlib.done();

  } // end if

  if ((disp==FindView.FD_CATEGORIES) && !(rinput.engine.useCategories()))
  { // the display parameter is not correct
    vlib.output(new ErrorBox(null,"Invalid display parameter.","top.js.vs"));
    vlib.done();

  } // end if

  // save off the session attribute
  rinput.setSessionAttribute(FindView.DISPLAY_PARAM_ATTRIBUTE,new java.lang.Integer(disp));

} // end if
else
{ // get the display parameter from the session attributes
  tmp_disp = rinput.getSessionAttribute(FindView.DISPLAY_PARAM_ATTRIBUTE);
  if (tmp_disp==null)
  { // make a new entry and set it
    tmp_disp = new java.lang.Integer(FindView.FD_COMMUNITIES);
    rinput.setSessionAttribute(FindView.DISPLAY_PARAM_ATTRIBUTE,tmp_disp);

  } // end if

  disp = tmp_disp.intValue();

} // end else

max_results = rinput.engine.getStdNumSearchResults();
view = new FindView(disp,max_results);   // create the view object

rc = null;
if ("GET"==rinput.verb)
{ // set up the category parameter
  catid = -1;
  if (disp==FindView.FD_COMMUNITIES)
    catid = rinput.getParameterInt("cat",-1);
  
  try
  { // prep the view fields
    if (disp==FindView.FD_COMMUNITIES)
    { // set the category (which also sets up the subcategory list and contained communities)
      if (rinput.engine.useCategories())
      { // only do this if we're using categories
        view.category = rinput.user.getCategoryDescriptor(catid);
        if (view.category.categoryID>=0)
        { // fill in the communities that are in this category
          view.results = rinput.user.getCommunitiesInCategory(view.category,view.offset,max_results);
          view.findCount = rinput.user.getNumCommunitiesInCategory(view.category);

        } // end if

      } // end if

      view.field = SearchMode.FIELD_COMMUNITY_NAME;

    } // end if
    else if (disp==FindView.FD_USERS)
      view.field = SearchMode.FIELD_USER_NAME;
    view.mode = SearchMode.SEARCH_PREFIX;
    view.term = "";
    rc = view;

  } // end try
  catch (e)
  { // exception in initializing the view...
    etype = vlib.exceptionType(e) + "";
    if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error accessing category data: " + e.message,"top.js.vs");
    else
      rc = e;

  } // end catch

  vlib.output(rc);
  vlib.done();

} // end if ("GET" operation)

// Set up the form for a POST.
try
{ // load some of the parameters
  catid = -1;

  if (disp==FindView.FD_COMMUNITIES)
  { // load the category ID and field parameters
    if (rinput.engine.useCategories())
    { // don't try to get the category unless we support such a thing
      catid = rinput.getParameterInt("cat",-1);
      if (!(rinput.engine.isValidCategoryID(catid)))
        throw new ValidationException("The category ID parameter is not valid.");

    } // end if

    x = rinput.getParameterInt("field",SearchMode.FIELD_COMMUNITY_NAME);
    if ((x!=SearchMode.FIELD_COMMUNITY_NAME) && (x!=SearchMode.FIELD_COMMUNITY_SYNOPSIS))
      throw new ValidationException("The search field parameter is not valid.");
    view.field = x;

  } // end if
  else if (disp==FindView.FD_USERS)
  { // load the 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;
    
  } // end else if

  if (disp!=FindView.FD_POSTS)
  { // 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;

  } // end if

  // 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 - max_results,0);
  else if (rinput.isImageButtonClicked("next"))
    view.offset = view.offset + max_results;
  else
    throw new ValidationException("Unable to determine what action triggered the form.");

  // run the actual search!
  if (disp==FindView.FD_COMMUNITIES)
  { // need to work it differently if there's a category ID
    if (rinput.engine.useCategories() && (catid>=0))
    { // set up the category
      view.category = rinput.user.getCategoryDescriptor(catid);
      if (view.category.categoryID>=0)
      { // fill in the communities that are in this category
        view.results = rinput.user.getCommunitiesInCategory(view.category,view.offset,max_results);
        view.findCount = rinput.user.getNumCommunitiesInCategory(view.category);

      } // end if

    } // end if
    else
    { // set the results as the community search data
      view.results = rinput.user.searchForCommunities(view.field,view.mode,view.term,view.offset,max_results);
      if (view.findCount<0)
        view.findCount = rinput.user.getSearchCommunityCount(view.field,view.mode,view.term);

    } // end else

  } // end if
  else if (disp==FindView.FD_USERS)
  { // search for users
    view.results = rinput.engine.searchForUsers(view.field,view.mode,view.term,view.offset,max_results);
    if (view.findCount<0)
      view.findCount = rinput.engine.getSearchUserCount(view.field,view.mode,view.term);

  } // end else if
  else if (disp==FindView.FD_CATEGORIES)
  { // search for categories
    view.results = rinput.user.searchForCategories(view.mode,view.term,view.offset,max_results);
    if (view.findCount<0)
      view.findCount = rinput.user.getSearchCategoryCount(view.mode,view.term);

  } // end else if
  else if (disp==FindView.FD_POSTS)
  { // search for posts
    view.results = rinput.user.searchPosts(view.term,view.offset,max_results);
    if (view.findCount<0)
      view.findCount = rinput.user.getSearchPostCount(view.term);

  } // end else if

  rinput.location = "find.js.vs?disp=" + disp;  // reset location for the POST
  rc = view;

} // end try
catch (e)
{ // exception thrown somewhere in the POST operation
  etype = vlib.exceptionType(e) + "";
  if (etype.match("ValidationException"))
    rc = new ErrorBox("Find Error",e.message,"top.js.vs");
  else if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error on find: " + e.message,"top.js.vs");
  else
    rc = e;

} // end catch

vlib.output(rc);  // all done!
