/*
 * 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@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2004 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.CommunityContext;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.ui.servlet.RequestImpl;

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

  private int m_menu_sel = MENU_SELECTOR_NOCHANGE;   // menu selector output by this code
  private String m_title;                            // page title
  private String m_qid = null;                       // page Quick ID
  private String m_jsp_name;                         // name of JSP page to forward to
  private RequestInput m_rinput = null;              // input request used by inner code
  private HTMLRendering m_html = null;               // HTML rendering helper
  private HashMap m_metadata = null;                 // collection of metadata to output
  private HashMap m_http_metadata = null;            // collection of HTTP metadata to output

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

  public JSPView(String title, String jsp_name)
  {
    m_title = title;
    m_jsp_name = jsp_name;

  } // end constructor

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

  public boolean needFrame()
  {
    return true;

  } // end needFrame

  public int getMenuSelector()
  {
    return m_menu_sel;

  } // end getMenuSelector

  public String getPageTitle(RequestOutput ro)
  {
    return m_title;

  } // end getPageTitle

  public String getPageQID()
  {
    return m_qid;

  } // end getPageQID

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

  public String getJSPName()
  {
    return m_jsp_name;

  } // end getJSPName

  public void initialize(RequestInput req)
  {
    m_rinput = req;
    m_html = (HTMLRendering)(req.queryService(HTMLRendering.class));

  } // end initialize

  public void terminate(RequestInput req)
  {
    m_rinput = null;
    m_html = null;

  } // end terminate

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentMetadata
   *--------------------------------------------------------------------------------
   */

  public Map getMetadata()
  {
    if (m_metadata==null)
      return Collections.EMPTY_MAP;
    else
      return Collections.unmodifiableMap(m_metadata);

  } // end getMetadata

  public Map getHTTPMetadata()
  {
    if (m_http_metadata==null)
      return Collections.EMPTY_MAP;
    else
      return Collections.unmodifiableMap(m_http_metadata);

  } // end getHTTPMetadata

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

  public final void setMenuSelector(int sel)
  {
    if (m_rinput==null)
      m_menu_sel = sel;

  } // end setMenuSelector

  public final void setPageQID(String s)
  {
    if (m_rinput==null)
      m_qid = s;

  } // end setPageQID

  public final void addMetadata(String name, String value)
  {
    if (m_rinput!=null)
      return;
    if (m_metadata==null)
      m_metadata = new HashMap();
    m_metadata.put(name,value);

  } // end addMetadata

  public final void addHTTPMetadata(String name, String value)
  {
    if (m_rinput!=null)
      return;
    if (m_http_metadata==null)
      m_http_metadata = new HashMap();
    m_http_metadata.put(name,value);

  } // end addHTTPMetadata

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

  } // end getRequestAttribute

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

  } // end formatDate

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

  } // end getCommunity

  public final String getActivityString(Date date)
  {
    return ((m_html==null) ? null : m_html.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
