// 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(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.conf);
importPackage(Packages.com.silverwrist.venice.ui.conf.view);
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");
on_error = "conf/conferences.js.vs?cc=" + comm.communityID;

// get the current conference
currc = new CurrentConference(rinput);
conf = currc.getConference(false,"conf/conferences.js.vs?cc=" + comm.communityID);

// get the topic
topic = null;
if (conf!=null)
{ // get the topic
  on_error = "conf/topics.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID;
  topic = currc.getTopic(false,"conf/topics.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID);
  if (topic!=null)
    on_error = "conf/posts.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID + "&top="
             + topic.topicNumber;

} // end if

view = new FindPostView(comm,conf,topic);
view.maxResults = rinput.engine.getStdNumSearchResults();

if ("GET"==rinput.verb)
{ // display the find dialog
  vlib.output(view);
  vlib.done();

} // end if

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

// Retrieve the offset and find count parameters.
view.offset = rinput.getParameterInt("ofs",0);
view.findCount = rinput.getParameterInt("fcount",-1);

// Adjust the search return offset based on the command button click.
// EJB 1/18/2002 - N.B.: some browsers can POST the "Search" form without triggering the
// "Search" button (by just pressing Enter), so we include the hidden "newsearch" parameter
// to distinguish between the two
if (rinput.isImageButtonClicked("search") || (rinput.getParameterInt("newsearch",0)==1))
  view.offset = 0;
else if (rinput.isImageButtonClicked("previous"))
  view.offset = Math.max(view.offset - view.maxResults,0);  // go backwards
else if (rinput.isImageButtonClicked("next"))
  view.offset += view.maxResults;  // go forwards instead
else
  throw new InternalStateError("Unable to determine what action triggered the form.");

rc = null;
try
{ // do the search
  if (topic!=null)
  { // search the topic
    view.results = topic.searchPosts(view.term,view.offset,view.maxResults);
    if (view.findCount<0)
      view.findCount = topic.getSearchPostCount(view.term);

  } // end if
  else if (conf!=null)
  { // search the conference
    view.results = conf.searchPosts(view.term,view.offset,view.maxResults);
    if (view.findCount<0)
      view.findCount = conf.getSearchPostCount(view.term);

  } // end else if
  else
  { // search the community
    view.results = comm.searchPosts(view.term,view.offset,view.maxResults);
    if (view.findCount<0)
      view.findCount = comm.getSearchPostCount(view.term);

  } // end else

  rc = view;

} // end try
catch (e)
{ // error doing the find
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error on find: " + e.message,on_error);
  else if (etype.match("AccessError"))
    rc = new ErrorBox("Access Error",e.message,on_error);
  else
    rc = e;

} // end catch

vlib.output(rc);
