/*
 * 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) 2002-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 NestedJSPView implements ContentJSP, ContentMetadata
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Content m_outer;               // outer content being rendered
  private String m_jspname;              // JSP template to be rendered
  private RequestInput m_rinput = null;  // RequestInput object for use by internal code
  private HTMLRendering m_html = null;   // HTML rendering helper

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

  public NestedJSPView(Content outer, String jspname)
  {
    m_outer = outer;
    m_jspname = jspname;

  } // end constructor

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

  public boolean needFrame()
  {
    return m_outer.needFrame();

  } // end needFrame

  public int getMenuSelector()
  {
    return m_outer.getMenuSelector();

  } // end getMenuSelector

  public String getPageTitle(RequestOutput ro)
  {
    return m_outer.getPageTitle(ro);

  } // end getPageTitle

  public String getPageQID()
  {
    return m_outer.getPageQID();

  } // end getPageQID

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

  public String getJSPName()
  {
    return m_jspname;

  } // 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_outer instanceof ContentMetadata)
      return ((ContentMetadata)m_outer).getMetadata();
    else
      return Collections.EMPTY_MAP;

  } // end getMetadata

  public Map getHTTPMetadata()
  {
    if (m_outer instanceof ContentMetadata)
      return ((ContentMetadata)m_outer).getHTTPMetadata();
    else
      return Collections.EMPTY_MAP;

  } // end getHTTPMetadata
  
  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final Content getOuterView()
  {
    return m_outer;

  } // end getOuterView

  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 NestedJSPView get(ServletRequest sreq) throws ServletException
  {
    Object obj = sreq.getAttribute(RequestImpl.REQUEST_CONTENT);
    if (obj==null)
      throw new ServletException("NestedJSPView.get: unable to get request content");
    if (obj instanceof NestedJSPView)
      return (NestedJSPView)obj;
    throw new ServletException("NestedJSPView.get: request content is not a NestedJSPView 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("NestedJSPView.getRequestOutput: unable to get request object");
    if (obj instanceof RequestOutput)
      return (RequestOutput)obj;
    throw new ServletException("NestedJSPView.getRequestOutput: request object is not a RequestOutput object");

  } // end getRequestOutput

} // end class NestedJSPView
