/*
 * 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.servlets.format;

import java.io.Writer;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.servlets.Variables;
import com.silverwrist.venice.servlets.format.menus.LeftMenu;

public class BaseJSPData
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */
  protected static final String ATTR_NAME = "com.silverwrist.venice.BaseJSPData";

  private static Category logger = Category.getInstance(BaseJSPData.class.getName());

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

  private VeniceEngine engine;            // the Venice engine
  private UserContext user;               // user context
  private String location;                // location string to use for this page
  private VeniceContent content;          // the actual content
  private boolean login_links = true;     // show login links up top?

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

  public BaseJSPData(VeniceEngine engine, UserContext user, String location, VeniceContent content)
  {
    this.engine = engine;
    this.user = user;
    this.location = location;
    this.content = content;

  } // end constructor

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

  public static BaseJSPData retrieve(ServletRequest request)
  {
    return (BaseJSPData)(request.getAttribute(ATTR_NAME));

  } // end retrieve

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

  public void store(ServletRequest request)
  {
    request.setAttribute(ATTR_NAME,this);

  } // end store

  public String getTitle(RenderData rdat)
  {
    if (content==null)
      return "";
    else
      return content.getPageTitle(rdat);

  } // end getTitle

  public String getPageQID()
  {
    if (content==null)
      return null;
    else
      return content.getPageQID();

  } // end getPageQID

  public boolean locationSpecified()
  {
    return (location!=null);

  } // end if

  public String getLocation()
  {
    return location;

  } // end getLocation

  public boolean displayLoginLinks()
  {
    return login_links && (location!=null);

  } // end displayLoginLinks

  public void displayLoginLinks(boolean value)
  {
    login_links = value;

  } // end displayLoginLinks

  public void transfer(ServletContext ctxt, RenderData rdat) throws IOException, ServletException
  {
    rdat.storeBaseJSPData(this);
    RequestDispatcher dispatcher = ctxt.getRequestDispatcher(rdat.getFormatJSPPath("base.jsp"));
    rdat.forwardDispatch(dispatcher);

  } // end transfer

  public void renderBannerAd(Writer out, RenderData rdat) throws IOException
  {
    Advertisement advert;
    if (user.isLoggedIn())
      advert = user.selectAd();
    else
      advert = engine.selectAd();
    if (advert==null)
    { // no advert found - bail out now!
      out.write("&nbsp;");
      return;

    } // end if

    String full_image_path;
    if (advert.getImagePathStyle()==Advertisement.CONTEXT_RELATIVE)
    { // return the full image path
      full_image_path = rdat.getContextRelativePath(advert.getImagePath());

    } // end if
    else
    { // can't render the ad - just punt
      out.write("&nbsp;");
      return;

    } // end else

    // Write the banner ad.
    String url = advert.getLinkURL();
    String capt = advert.getCaption();
    if (url!=null)
      out.write("<A HREF=\"" + url + "\">");
    out.write("<IMG SRC=\"" + full_image_path + "\" ALT=\"");
    if (capt!=null)
      out.write(capt);
    out.write("\" ALIGN=RIGHT WIDTH=468 HEIGHT=60 HSPACE=2 VSPACE=2 BORDER=0>");
    if (url!=null)
      out.write("</A>");

  } // end renderBannerAd

  public void renderMenu(HttpSession session, Writer out, RenderData rdat) throws IOException
  {
    ComponentRender menu = Variables.getMenu(session);
    if (menu==null)
      menu = (ComponentRender)(rdat.getLeftMenu("top"));
    menu.renderHere(out,rdat);

  } // end renderMenu

  public void renderFixedMenu(Writer out, RenderData rdat) throws IOException
  {
    ComponentRender menu = (ComponentRender)(rdat.getLeftMenu("fixed"));
    menu.renderHere(out,rdat);

  } // end renderFixedMenu

  public void renderContent(ServletContext ctxt, Writer out, RenderData rdat)
      throws IOException, ServletException
  {
    if (content==null)
    { // there is no content!
      new ErrorBox(null,"Internal Error: There is no content available",null).renderHere(out,rdat);
      return;

    } // end if

    if (content instanceof ContentRender)
    { // we have an old-style ContentRender
      ContentRender cr = (ContentRender)content;
      cr.renderHere(out,rdat);
      return;

    } // end if
    else if (content instanceof JSPRender)
    { // we have a JSP-rendered page - include it
      JSPRender jr = (JSPRender)content;
      rdat.storeJSPRender(jr);
      RequestDispatcher dispatcher = ctxt.getRequestDispatcher(rdat.getFormatJSPPath(jr.getTargetJSPName()));
      out.flush();
      rdat.flushOutput();  // make sure the stuff to be output first is output
      rdat.includeDispatch(dispatcher);
      rdat.flushOutput();  // now make sure the included page is properly flushed
      return;
      
    } // end else if
    else // this is the fallback if we don't recognize the content
      new ErrorBox(null,"Internal Error: Content of invalid type",null).renderHere(out,rdat);

  } // end renderContent

} // end class BaseJSPData
