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

import java.util.Date;
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
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Content outer;
  private String jspname;
  private RequestInput rinput = null;
  private HTMLRendering html = null;

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

  public NestedJSPView(Content outer, String jspname)
  {
    this.outer = outer;
    this.jspname = jspname;

  } // end constructor

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

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

  } // end needFrame

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

  } // end getMenuSelector

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

  } // end getPageTitle

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

  } // end getPageQID

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

  public String getJSPName()
  {
    return jspname;

  } // 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 Content getOuterView()
  {
    return outer;

  } // end getOuterView

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

  } // end getRequestAttribute

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

  } // end formatDate

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

  } // end getCommunity

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