/*
 * 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 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.view;

import java.util.Date;
import java.util.HashMap;
import javax.servlet.*;
import com.silverwrist.venice.core.CommunityContext;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.servlet.RequestImpl;

public class JSPView implements ContentJSP
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int menu_sel = MENU_SELECTOR_NOCHANGE;
  private String title;
  private String qid = null;
  private String jsp_name;
  private RequestInput rinput = null;

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

  public JSPView(String title, String jsp_name)
  {
    this.title = title;
    this.jsp_name = jsp_name;

  } // end constructor

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

  public boolean needFrame()
  {
    return true;

  } // end needFrame

  public int getMenuSelector()
  {
    return menu_sel;

  } // end getMenuSelector

  public String getPageTitle(RequestOutput ro)
  {
    return title;

  } // end getPageTitle

  public String getPageQID()
  {
    return qid;

  } // end getPageQID

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

  public String getJSPName()
  {
    return jsp_name;

  } // end getJSPName

  public void initialize(RequestInput req)
  {
    rinput = req;

  } // end initialize

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

  } // end terminate

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

  public final void setMenuSelector(int sel)
  {
    if (rinput==null)
      menu_sel = sel;

  } // end setMenuSelector

  public final void setPageQID(String s)
  {
    if (rinput==null)
      qid = s;

  } // end setPageQID

  public final Object getRequestAttribute(String s)
  {
    return ((rinput==null) ? null : rinput.getRequestAttribute(s));

  } // end getRequestAttribute

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

  } // end formatDate

  public final CommunityContext getCommunity()
  {
    return ((rinput==null) ? null : rinput.getCommunity());

  } // end getCommunity

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

  } // end getActivityString

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

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

  } // end get

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

  } // end getRequestOutput

} // end class JSPView
