/*
 * 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): 
 */
package com.silverwrist.venice.ui.view;

import java.util.*;
import javax.servlet.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.ui.servlet.RequestImpl;

public class FindView implements ContentJSP
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  // The attribute that the current display category gets stored as.
  public static final String DISPLAY_PARAM_ATTRIBUTE = "Find.display";

  // The various display categories
  public static final int FD_COMMUNITIES = 0;
  public static final int FD_USERS = 1;
  public static final int FD_CATEGORIES = 2;
  public static final int FD_POSTS = 3;

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int display;                          // which display are we doing?
  private int max_results;                      // the maximum number of results displayed
  private RequestInput rinput = null;           // the RequestInput (during execution)
  private HTMLRendering html = null;            // HTML Rendering module (during execution)
  private int field = -1;                       // search field indicator
  private int mode = -1;                        // search mode indicator
  private String term = null;                   // current search term
  private CategoryDescriptor cat = null;        // current category descriptor
  private List subcats = null;                  // list of subcategories
  private List results = null;                  // the search results
  private int offset = 0;                       // offset in list of results
  private int find_count = 0;                   // total count of things found

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public FindView(int display, int max_results)
  {
    this.display = display;
    this.max_results = max_results;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Content
   *--------------------------------------------------------------------------------
   */

  public boolean needFrame()
  {
    return true;

  } // end needFrame

  public int getMenuSelector()
  {
    return MENU_SELECTOR_TOP;

  } // end getMenuSelector

  public String getPageTitle(RequestOutput ro)
  {
    return "Find";

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentJSP
   *--------------------------------------------------------------------------------
   */

  public String getJSPName()
  {
    return "find.jsp";

  } // end getJSPName

  public void initialize(RequestInput req)
  {
    rinput = req;
    html = (HTMLRendering)(req.queryService(HTMLRendering.class));

  } // end initialize

  public void terminate(RequestInput req)
  {
    rinput = null;
    html = null;

  } // end terminate

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final String formatDate(Date date)
  {
    return ((html==null) ? null : html.formatDate(date));

  } // end formatDate

  public final String getActivityString(Date date)
  {
    return (html==null) ? null : html.getActivityString(date);

  } // end getActivityString

  public final int getDisplayMode()
  {
    return display;

  } // end getDisplayMode

  public final boolean testDisplayMode(int val)
  {
    return (display==val);

  } // end testDisplayMode

  public final int getMaxResults()
  {
    return max_results;

  } // end getMaxResults

  public final int getField()
  {
    return field;

  } // end getField

  public final void setField(int f)
  {
    if (rinput==null)
      this.field = f;

  } // end setField

  public final boolean testField(int val)
  {
    return (field==val);

  } // end testField

  public final int getMode()
  {
    return mode;

  } // end getMode

  public final void setMode(int m)
  {
    if (rinput==null)
      this.mode = m;

  } // end setMode

  public final boolean testMode(int val)
  {
    return (mode==val);

  } // end testMode

  public final String getTerm()
  {
    return term;

  } // end getTerm

  public final void setTerm(String s)
  {
    if (rinput==null)
      this.term = s;

  } // end setTerm

  public final CategoryDescriptor getCategory()
  {
    return cat;

  } // end getCategory

  public final void setCategory(CategoryDescriptor cat) throws DataException
  {
    if (rinput!=null)
      return;

    this.cat = cat;
    this.subcats = cat.getSubCategories();

  } // end setCategory

  public final List getSubCategories()
  {
    return subcats;

  } // end getSubCategories

  public final List getResults()
  {
    return results;

  } // end getResults

  public final void setResults(List l)
  {
    if (rinput==null)
      this.results = l;

  } // end setResults

  public final int getOffset()
  {
    return offset;

  } // end getOffset

  public final void setOffset(int v)
  {
    if (rinput==null)
      this.offset = v;

  } // end setOffset

  public final int getFindCount()
  {
    return find_count;

  } // end getFindCount

  public final void setFindCount(int c)
  {
    if (rinput==null)
      this.find_count = c;

  } // end setFindCount

  /*--------------------------------------------------------------------------------
   * External static operations
   *--------------------------------------------------------------------------------
   */

  public static final FindView get(ServletRequest sreq) throws ServletException
  {
    Object obj = sreq.getAttribute(RequestImpl.REQUEST_CONTENT);
    if (obj==null)
      throw new ServletException("FindView.get: unable to get request content");
    if (obj instanceof FindView)
      return (FindView)obj;
    throw new ServletException("FindView.get: request content is not a FindView object");

  } // end get

  public static String getCommunityHostName(CommunityContext comm)
  {
    try
    { // get the host name for the specified community
      UserProfile prof = comm.getHostProfile();
      return prof.getUserName();

    } // end try
    catch (DataException e)
    { // just return NULL if an exception strikes
      return null;

    } // end catch

  } // end getCommunityHostName

  public static int getCommunityMemberCount(CommunityContext comm)
  {
    try
    { // get the member count for the specified community
      return comm.getMemberCount();

    } // end try
    catch (DataException e)
    { // just return -1 if an exception strikes
      return -1;

    } // end catch

  } // end getCommunityMemberCount

} // end class FindView
