/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Struts", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */


package org.apache.velocity.tools.view.tools;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;
import org.apache.velocity.tools.view.context.ViewContext;
import org.apache.velocity.tools.view.tools.ContextTool;


/**
 * Abstract superclass for pull-style context tools
 * 
 * Subclasses should 
 * a. have a constructor that calls 
 *    <code>super(ViewContext context)</code>
 * b. implement <code>public void init(ViewContext)</code>
 *    to return instances of themselves using the
 *    above mentioned constructor
 * c. implement <code>public void reset()</code> to set
 *    to null any references the class is holding
 * 
 * The primary advantage to extending this class is 
 * the convenience of the parameter parsing methods.
 * If you have a context tool that couldn't care less about
 * parsing parameters, then don't have it extend this.
 *
 * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
 * @version $Revision: 1.2 $ $Date: 2002/02/11 16:55:47 $
 */

public abstract class PullTool implements ContextTool
{


    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;


    /**
     * default constructor
     */
    public PullTool()
    {
        //do nothing
    }


    /**
     * This constructor is only for use inside subclasses!
     * Use method {@link #init(ViewContext context)} on
     * subclasses to obtain instances of the tool.
     */
    public PullTool(ViewContext context)
    {
        this.request = context.getRequest();
        this.session = request.getSession(false);
        this.application = context.getServletContext();
        //TODO? get the velocity context too?
    }



    /**************************** ContextTool Interface ****************************/

    /**
     * A new tool object will be instantiated per-request by calling
     * this method. A ContextTool is effectively a factory used to
     * create objects for use in templates. Some tools may simply return
     * themselves from this method others may instantiate new objects
     * to hold the per-request state.
     *
     * Subclass implementations of this method should simply return
     * a new instance of themselves using the MyPullTool(ViewContext ctx) ctor.
     */
    public abstract Object init(ViewContext context);


    /**
     * At the end of processing this method will be called to
     * return the object generated by init(), in case it needs
     * to be recycled or otherwise cleaned up.
     */
    public void destroy(Object obj)
    {
        PullTool tool = (PullTool)obj;
        tool.request = null;
        tool.session = null;
        tool.application = null;
        tool.reset();
    }


    /*************************** PullTool methods *********************************/

    /**
     * Implementations should set all references in the object to null
     */
    public abstract void reset();


    /**
     * Return the session object
     */
    public HttpSession getSession()
    {
        return session;
    }


    /**
     * Return the request object
     */
    public HttpServletRequest getRequest()
    {
        return request;
    }


    /**
     * Return the servlet context
     */
    public ServletContext getServletContext()
    {
        return application;
    }


    //FIXME: get me some real logging!
    public void log(String msg)
    {
        System.out.println(msg);
        //this would write to the server log...
        //application.log(msg);
    }


    //FIXME: get me some real logging!
    public void log(String msg, Throwable t)
    {
        System.out.println(msg+t);
        //this would write to the server log...
        //application.log(msg, t);
    }


    /************************ parameter retrieval methods ************************/

    /**
     * Return param that matches the given key or
     * if none is found return null
     */
    public Object getParam(String key)
    {
        return request.getParameter(key);
    }


    /**
     * Return param that matches the given or
     * if none is found, return the
     * provided alternate Object
     */
    public Object getParam(String key, Object alternate)
    {
        Object obj = getParam(key);
        return (obj != null) ? obj : alternate;
    }


    /**
     * Return param that matches the given key as
     * a String if one is found.  otherwise, return null
     */
    public String getStringParam(String key)
    {
        return (String)getParam(key);
    }


    /**
     * Return param that matches the given key as
     * a String or if none is found then return the
     * provided alternate String
     */
    public String getStringParam(String key, String alternate)
    {
        String s = getStringParam(key);
        return (s != null) ? s : alternate;
    }


    /**
     * Return param that matches the given key as
     * a Number if one is found.  otherwise, return null
     */
    public Number getNumberParam(String key)
    {
        Object obj = getParam(key);
        if (obj != null && obj instanceof String)
        {
            String s = (String)obj;
            if (s.indexOf('.') >= 0)
            {
                return new Double(s);
            }
            return new Long(s);
        }
        return (Number)obj;
    }


    /**
     * Return param that matches the given key as
     * a Number or if none is found, return the
     * provided alternate Number
     */
    public Number getNumberParam(String key, Number alternate)
    {
        Number n = getNumberParam(key);
        return (n != null) ? n : alternate;
    }


    /**
     * Return param that matches the given key as
     * an int or if none is found, return the
     * provided alternate int
     */
    public int getIntParam(String key, int alternative)
    {
        Number n = getNumberParam(key);
        return (n != null) ? n.intValue() : alternative;
    }


    /**
     * Return param that matches the given key as
     * a double or if none is found, return the
     * provided alternate double
     */
    public double getDoubleParam(String key, double alternative)
    {
        Number n = getNumberParam(key);
        return (n != null) ? n.doubleValue() : alternative;
    }


}