head	1.23;
access;
symbols
	TIGRIS_1_1_0RC2:1.7.2.1
	TIGRIS_1_1_0RC1:1.7.2.1
	TIGRIS_1_1:1.7.2.1.0.2
	TIGRIS_1_0_8:1.7.2.1
	TIGRIS_1_0_8RC3:1.7.2.1
	TIGRIS_1_0_8RC2:1.7.2.1
	TIGRIS_1_0_8RC1:1.7.2.1
	TIGRIS_1_0_7:1.7.2.1
	TIGRIS_1_0_7RC3:1.7.2.1
	TIGRIS_1_0_7RC2:1.7.2.1
	TIGRIS_1_0_7RC1:1.7.2.1
	TIGRIS_1_0_6:1.7.2.1
	TIGRIS_1_0_6RC5:1.7.2.1
	TIGRIS_1_0:1.7.0.2;
locks; strict;
comment	@# @;


1.23
date	2001.08.28.00.56.13;	author leonardr;	state Exp;
branches;
next	1.22;

1.22
date	2001.08.11.09.08.08;	author dlr;	state Exp;
branches;
next	1.21;

1.21
date	2001.08.06.18.15.56;	author dlr;	state Exp;
branches;
next	1.20;

1.20
date	2001.08.06.17.14.05;	author ms;	state Exp;
branches;
next	1.19;

1.19
date	2001.07.24.18.37.53;	author leonardr;	state Exp;
branches;
next	1.18;

1.18
date	2001.07.19.19.18.11;	author dlr;	state Exp;
branches;
next	1.17;

1.17
date	2001.07.19.18.42.23;	author dlr;	state Exp;
branches;
next	1.16;

1.16
date	2001.07.18.21.42.08;	author leonardr;	state Exp;
branches;
next	1.15;

1.15
date	2001.07.18.16.04.11;	author leonardr;	state Exp;
branches;
next	1.14;

1.14
date	2001.07.18.08.00.36;	author dlr;	state Exp;
branches;
next	1.13;

1.13
date	2001.07.17.23.20.23;	author leonardr;	state Exp;
branches;
next	1.12;

1.12
date	2001.07.17.20.54.07;	author leonardr;	state Exp;
branches;
next	1.11;

1.11
date	2001.07.17.17.55.46;	author leonardr;	state Exp;
branches;
next	1.10;

1.10
date	2001.07.16.23.22.09;	author leonardr;	state Exp;
branches;
next	1.9;

1.9
date	2001.07.16.22.36.27;	author leonardr;	state Exp;
branches;
next	1.8;

1.8
date	2001.07.05.23.07.52;	author ms;	state Exp;
branches;
next	1.7;

1.7
date	2001.06.13.01.46.33;	author leonardr;	state Exp;
branches
	1.7.2.1;
next	1.6;

1.6
date	2001.05.23.02.40.05;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2001.05.22.00.11.43;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2001.05.22.00.08.51;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2001.05.15.02.58.14;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.05.15.02.37.29;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2001.05.15.02.37.12;	author dlr;	state Exp;
branches;
next	;

1.7.2.1
date	2001.06.13.22.48.23;	author leonardr;	state Exp;
branches;
next	;


desc
@@


1.23
log
@Moved code out of BaseHelmServlet and into other classes which suit it better.
@
text
@/* ================================================================
 * Copyright (c) 2000-2001 CollabNet.  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 CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * 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 COLLABNET 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 CollabNet.
 */

package org.tigris.helm.tool;

import java.util.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.util.exception.NestableException;
import org.apache.commons.util.StringUtils;

import org.apache.turbine.RunData;
import org.apache.turbine.util.Log;

import org.apache.velocity.context.Context;

import org.tigris.helm.tool.HelmProperties;

/**
 * ContextTool is a utility class for use with templating-system contexts.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 */
public class ContextAPI
{
    /**
     * A constant for the empty {@@link java.lang.String} object sometimes
     * supplied to the context by <code>processField</code>.
     */
    private static final String EMPTY_STRING = "";

    /**
     * The context key for the {@@link org.apache.tubine.RunData}
     * instance for each request.
     */
    protected static final String RUN_DATA_KEY = "data";

    /**
     * The context key for the {@@link org.tigris.helm.tool.FormCache}
     * instance from a saved form post.
     */
    protected static final String FORM_CACHE_KEY = "Form";

    /**
     * The context key containing the current page on a paginated list.
     */
    private static final String REDIRECT_CONTEXT_KEY = "HelmRedirect";

    /**
     * Context key indicating that user input has been checked for
     * errors and that consequently errors should be shown.
     */
    private static String USER_INPUT_CHECKED = "userInputChecked";

    /**
     * Context key for the list of fatal errors.
     */
    private static String FATAL_ERRORS_KEY = "fatalErrors";

    /**
     * Context key for the list of errors.
     */
    private static String ERRORS_KEY = "errors";

    /**
     * Context key for the list of missing fields.
     */
    private static String MISSING_FIELDS_KEY = "missingFields";

    /**
     * Context key for the list of validation errors.
     */
    private static String VALIDATION_ERRORS_KEY = "validationErrors";

    /**
     * Context key for the list of messages.
     */
    private static String MESSAGES_KEY = "messages";

    /**
     * Adds the provided {@@link org.apache.turbine.RunData} and a new
     * {@@link org.tigris.helm.tool.FormCache} to the context.
     *
     * @@param ctx The context to retrieve the HTTP request used to create a
     *            cache from, and to add the cache reference to.  */
    public static final void putData(Context ctx, RunData data)
    {
        ctx.put(RUN_DATA_KEY, data);
        ctx.put(FORM_CACHE_KEY, new FormCache(getRequest(ctx)));
    }

    /**
     * Gets the any run data associated with the provided context.
     *
     * @@param ctx The context to extract an associated HTTP request from.
     */
    public static final RunData getRunData(Context ctx)
    {
        return (RunData) ctx.get(RUN_DATA_KEY);
    }

    /**
     * Gets the servlet response associated with the provided context.
     *
     * @@param ctx The context to extract an associated HTTP response from.
     */
    public static final HttpServletResponse getResponse(Context ctx)
    {
        RunData runData = getRunData(ctx);
        return (runData == null ? null : runData.getResponse());
    }

    /**
     * Gets the servlet request associated with the provided context.
     *
     * @@param ctx The context to extract an associated HTTP request from.
     */
    public static final HttpServletRequest getRequest(Context ctx)
    {
        RunData runData = getRunData(ctx);
        return (runData == null ? null : runData.getRequest());
    }

    /**
     * Retrieves the value of the named CGI the parameter for an associated
     * servlet request.
     */
    public static String getParameter(Context ctx, String name)
    {
        String value = null;
        if (ctx.containsKey(name))
        {
            value = (String) ctx.get(name);
        }
        else
        {
            HttpServletRequest req = getRequest(ctx);
            if (req != null)
            {
                value = req.getParameter(name);
            }
        }
        return value;
    }

    /**
     * Retrieves values of the named CGI the parameter for an associated
     * servlet request.
     */
    public static String[] getParameterValues(Context ctx, String name)
    {
        String[] values = null;
        HttpServletRequest req = getRequest(ctx);
        if (req != null)
        {
            values = req.getParameterValues(name);
        }
        return values;
    }

    /**
     * Retrieves the {@@link org.tigris.helm.tool.FormCache} from the context.
     *
     * @@param ctx The context to interrogate for data.
     * @@return    The cached data.
     */
    public static final FormCache getFormCache(Context ctx)
    {
        return (FormCache) ctx.get(FORM_CACHE_KEY);
    }

    /**
     * Sets up a client-side redirect.
     */
    public static void setClientRedirect(Context context, String newLocation)
    {
        context.put(REDIRECT_CONTEXT_KEY, newLocation);
    }

    /**
     * Sets up a client-side redirect.
     */
    public static String getClientRedirect(Context context)
    {
        return (String)context.get(REDIRECT_CONTEXT_KEY);
    }

    // Methods for field processing

    /**
     * @@see #processField
     */
    public static final String processField (Context context, String key)
    {
        return processField(context, key, (String)null);
    }

    /**
     * @@see #processField
     */
    public static final String processField (Context context, 
                                         String key, String defaultValue)
    {
        return processField(context, key, defaultValue, null, true);
    }

    /**
     * @@see #processField
     */
    public static final String processField (Context context, String key,
                                             HttpSession session)
    {
        return processField(context, key, (String)session.getValue(key));
    }

    /**
     * Attempts to get <code>defaultValue</code> from the 
     * <code>HttpSession</code> object, keyed by <code>key</code>.
     *
     * @@see #processField
     */
    public static final String processRequiredField 
        (Context context, String key, HttpSession session)
    {
        return processField(context, key, (String)session.getValue(key), 
                            null, true);
    }    


    /**
     * Attempts to get <code>defaultValue</code> from the 
     * <code>HttpSession</code> object, keyed by <code>key</code>.
     *
     * @@see #processField
     */
    public static final String processRequiredField 
        (Context context, String key, HttpSession session, String description)
    {
        return processField(context, key, (String)session.getValue(key), 
                            description, true);
    }    

    /**
     * @@see #processField
     */
    public static final String processRequiredField 
        (Context context, String key, String description)
    {
        return processField(context, key, null, description, true);
    }

    /**
     * Retrieves HTML form and/or CGI query string data, both adding the data 
     * to the context and returning it.  If the form field contains no data, 
     * the provided <code>description</code> is noted in the 
     * <code>missingFields</code> list (if provided).
     */
    public static String processField (Context context, String key, 
                                       String defaultValue, String description)
    {
        return processField(context, key, defaultValue, description, true);
    }

    /**
     * Retrieves HTML form and/or CGI query string data, both adding the data 
     * to the context and returning it.  If the form field contains no data, 
     * the provided <code>description</code> is noted in the 
     * <code>missingFields</code> list (if provided). Will also trim
     * leading and trailing spaces on demand.
     * <p>
     * The name of this method is, unfortunately, ambiguous (because it does 
     * so many things).  It may not be a cohesive, but it sure is useful!
     *
     * @@param context       The Context to add the param to.
     * @@param key           The name of the form field and context key.
     * @@param defaultValue  The default value to use if there is nothing in 
     *                      the form (if <code>null</code>, defaults to the
     *                      empty string).
     * @@param description   The description to put into the 
     *                      <code>missingFields</code> list if the the form 
     *                      field has no value (or <code>null</code>).
     * @@param missingFields The list of missing fields (or <code>null</code> 
     *                      if you do not care if the field is missing).
     * @@param cleanup       Whether or not to clean up the data before 
     *                      processing it.
     * @@return The form value retrieved.
     */
    protected static String processField (Context context, String key, 
                                          String defaultValue, 
                                          String description, boolean cleanup)
    {
        String value = getParameter(context, key);
        if (!StringUtils.isValid(value))
        {
            if (StringUtils.isValid(defaultValue))
            {
                value = defaultValue;
            }
            else
            {
                value = EMPTY_STRING;
                if (StringUtils.isValid(description))
                {
                    addMissingField(context, description);
                }
            }
        }

        if (cleanup)
        {
            value = value.trim();
        }
        context.put(key, value);
        return value;
    }

    /**
     * Process a field without modifying its data in any way.
     */
    public static final String processFieldRaw
        (Context context, String key)
    {
        return processFieldRaw(context, key, null);
    }
    
    /**
     * Process a field without modifying its data in any way.
     */
    public static final String processFieldRaw
        (Context context, String key, String defaultValue)
    {
        return processField(context, key, defaultValue, null, false);
    }
    
    /**
     * Process a field without modifying its data in any way.
     */
    public static final String processRequiredFieldRaw
        (Context context, String key, String description)
    {
        return processField(context, key, null, description, false);
    } 

    /**
     * Retrieves HTML form checkbox data, both adding the data to the
     * context as an object and returning it as a primitive.
     *
     * @@param ctx The context of user interaction.
     * @@param key The key to the desired field.
     * @@return    The whether the checkbox was checked.
     */
    public static final boolean processCheckbox (Context ctx, String key)
    {
        String value = getParameter(ctx, key);
        boolean bool = StringUtils.isValid(value);
        addBooleanToContext(ctx, key, bool);
        return bool;
    }

    /**
     * Search the form parameters for parameters that begin with a certain
     * prefix. Return the list of parameter values provided for that prefix.
     *
     * For instance, calling searchParamsForPrefix(context, "Add")
     * when "Add 4" and "Add 7" are form parameters will return a List
     * containing the Strings "4" and "7".
     */
    public static List searchParamsForPrefix(Context context, String prefix)
        throws NestableException
    {
        return searchParamsForPrefixAndTokenize(context, prefix, 1, false);
    }

    /**
     * Search the form parameters for parameters that begin with a certain
     * prefix. Return the list of parameter values provided for that prefix.
     *
     * For instance, calling searchParamsForPrefix(context, "Add"),
     * when "Add 4" and "Add 7" are form parameters mapping to "a" and
     * "b", will return a list containing the String arrays {"4" "a"}
     * and {"7" "b"}.  
     */
    public static List searchParamsForPrefixIncludeValue
        (Context context, String prefix)
        throws NestableException
    {
        return searchParamsForPrefixAndTokenize(context, prefix, 1, true);
    }

    /**
     * Search the form parameters for parameters that begin with a
     * certain prefix. Return the list of string arrays containing the
     * tokenized parameter values provided for that prefix.
     *
     * <p>Examples<br>
     * Assume the following parameters:
     * <li>"Add 4 7" => "a"
     * <li>"Add 10 4" => "b"
     *
     * <p>sPFPAT(context, "Add", 1, false) returns a List containing
     * the Strings "4 7" and "10 4".
     *
     * <p>sPFPAT(context, "Add", 2, false) returns a List containing
     * the String arrays {"4" "7"} and {"10" "4"}.
     *
     * <p>sPFPAT(context, "Add", 3, false) throws an exception
     * because there are not enough tokens in "Add 4 7".
     *
     * <p>sPFPAT(context, "Add", 2, true) returns a List containing
     * the String arrays {"4" "7" "a"} and {"10" "4" "b"}
     *
     * <p>sPFPAT(context, "Add", 1, true) returns a List containing
     * the String arrays {"4 7" "a"} and {"10 4" "b"}
     *
     * <p>sPFPAT(context, "Remove", 3, false) returns an empty List,
     * since there are no parameter values beginning with "Remove".
     */
    public static List searchParamsForPrefixAndTokenize
        (Context context, String prefix, int tokens, boolean appendValue)
        throws NestableException
    {
        List values = new ArrayList();
        Enumeration paramNames = getRequest(context).getParameterNames();
        while (paramNames.hasMoreElements())
        {
            String paramName = (String) paramNames.nextElement();
            if (paramName.startsWith(prefix))
            {
                StringTokenizer tok = new StringTokenizer (paramName);
                tok.nextToken();
                int size = tokens;
                if (appendValue)
                {
                    size++;
                }
                String[] theseValues = new String[size];
                for (int i = 0; i < tokens; i++)
                {
                    try
                    {                    
                        theseValues[i] = tok.nextToken();
                    }
                    catch (NoSuchElementException e)
                    {
                        throw new NestableException
                            ("Attempt to tokenize \"" + paramName + "\" " 
                             + (tokens+1) 
                             + " times failed: not enough tokens.");
                    }
                    if (appendValue)
                    {
                        String value = processField(context, paramName, 
                                                    (String)null);
                        theseValues[theseValues.length-1] = value;
                    }
                }
                if (theseValues.length == 1) 
                {
                    values.add(theseValues[0]);
                }
                else
                {
                    values.add(theseValues);
                }
            }
        }    
        return values;
    }   

    /**
     * Adds a boolean value to the context.
     *
     * @@param ctx  The context to add to.
     * @@param key  The key name of the value in the context.
     * @@param bool The boolean primitive whose object equivalent to add.
     */
    public static final void addBooleanToContext (Context ctx, String key, 
                                                  boolean bool)
    {
        ctx.put(key, (bool ? Boolean.TRUE : Boolean.FALSE));
    }

    // Methods for manipulating error and message lists.

    /**
     * Checks whether the user inputs have resulted in any errors.
     *
     * @@return                 The validity of the user input.
     */
    public static final boolean isValidUserInput(Context context)
    {
        return isValidUserInput(context, Boolean.TRUE);
    }

    /**
     * Checks whether the user inputs have resulted in any errors.
     *
     * @@param   context      The context to check
     * @@param   setAsChecked Whether or not to count this as a user input
     *                       check. If counted, missing fields and validation
     *                       errors will be shown in Errors.vm.
     * @@return                 The validity of the user input.
     */
    public static final boolean isValidUserInput(Context context, 
                                                 Boolean setAsChecked)
    {
        boolean bad = false;

        if (setAsChecked.booleanValue())
        {
            setUserInputChecked(context, setAsChecked);
        }

        if (!getFatalErrors(context).isEmpty() 
            || !getErrors(context).isEmpty())
        {
            bad = true;
        }

        if (getUserInputChecked(context).booleanValue()
            && (!getMissingFields(context).isEmpty() 
                || !getValidationErrors(context).isEmpty()))
        {
            bad = true;
        }

        return !bad;
    }

    /**
     * Returns a flag indicating whether missing fields are desired to
     * be required.
     */
    public static final Boolean getUserInputChecked(Context context)
    {
        Boolean userInputChecked = (Boolean)context.get
            (USER_INPUT_CHECKED);
        if (userInputChecked == null)
        {
            userInputChecked = Boolean.FALSE;
        }
        return userInputChecked;
    }


    /**
     * Sets a flag indicating whether missing fields are desired to be
     * required.
     */
    public static final void setUserInputChecked(Context context,
                                                 Boolean checked)
    {
        context.put(USER_INPUT_CHECKED, checked);
    }

    /**
     * Gets the current list of fatal errors.
     */
    public static final List getFatalErrors(Context context)
    {
        return getListFromContext(context, FATAL_ERRORS_KEY);
    }

    /**
     * Adds to the list of fatal errors.
     */
    public static final void addFatalError(Context context, String error)
    {
        addToContextList(context, FATAL_ERRORS_KEY, error);
    }

    /**
     * Gets the current list of errors.
     */
    public static final List getErrors(Context context)
    {
        return getListFromContext(context, ERRORS_KEY);
    }

    /**
     * Adds to the list of fatal errors.
     */
    public static final void addError(Context context, String error)
    {
        addToContextList(context, ERRORS_KEY, error);
    }

    /**
     * Gets the current list of missing fields.
     */
    public static final List getMissingFields(Context context)
    {
        return getListFromContext(context, MISSING_FIELDS_KEY);
    }

    /**
     * Adds to the list of missing fields.
     */
    public static final void addMissingField(Context context, 
                                             String description)
    {
        addToContextList(context, MISSING_FIELDS_KEY, description);
    }

    /**
     * Gets the current list of validation errors.
     */
    public static final List getValidationErrors(Context context)
    {
        return getListFromContext(context, VALIDATION_ERRORS_KEY);
    }

    /**
     * Adds to the list of validation errors.
     */
    public static final void addValidationError(Context context, String error)
    {
        addToContextList(context, VALIDATION_ERRORS_KEY, error);
    }

    /**
     * Gets the current list of messages.
     */
    public static final List getMessages(Context context)
    {
        return getListFromContext(context, MESSAGES_KEY);
    }

    /**
     * Adds to the list of messages.
     */
    public static final void addMessage(Context context, String message)
    {
        addToContextList(context, MESSAGES_KEY, message);
    }

    /**
     * Gets a list from the context.
     */
    private static final List getListFromContext(Context context, String key)
    {
        List l = (List)context.get(key);
        if (l == null)
        {
            l = new ArrayList(0);
            context.put(key, l);
        }        
        return l;
    }

    /**
     * Adds an element to a list in the context.
     */
    private static final void addToContextList
        (Context context, String key, Object element)
    {
        List l =  getListFromContext(context, key);
        l.add(element);
    }

    /**
     * Requires that a value is provided, adding it to the list of missing 
     * fields if it is not there.  This method is convenient to call in 
     * conjunction with the processField() variants which do not take a list 
     * of missing fields.
     *
     * @@param context The context keeping track of missing fields.
     * @@param value         The value to check.
     * @@param description   The description to use to describe a missing value.
     */
    public static void requireValue (Context context, String value, 
                                     String description)
    {
        if (!StringUtils.isValid(value))
        {
            addMissingField(context, description);
        }
    }

}
@


1.22
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
text
@d55 1
d405 110
@


1.21
log
@Rolled back last commit--it should not result in a NPE, and there's no
reason to perform those extra operations.
@
text
@d55 3
a57 2
import org.apache.turbine.util.RunData;
import org.apache.turbine.util.StringUtils;
d78 2
a79 2
     * The context key for the {@@link org.apache.tubine.util.RunData} instance
     * for each request.
d126 12
a214 11
    }

    /**
     * Adds the {@@link org.tigris.helm.tool.FormCache} to the context.
     *
     * @@param ctx The context to retrieve the HTTP request used to create a
     *            cache from, and to add the cache reference to.
     */
    public static final void putFormCache(Context ctx)
    {
        ctx.put(FORM_CACHE_KEY, new FormCache(getRequest(ctx)));
@


1.20
log
@return null rather then exploding with an NPE
@
text
@d165 1
a165 5
            Object o = ctx.get(name);
            if (o != null)
            {
                value = (String) o;
            }
@


1.19
log
@Got rid of extraneous debug.
@
text
@d165 5
a169 1
            value = (String) ctx.get(name);
@


1.18
log
@Removed Joist references.
@
text
@a534 1
        Log.debug("Adding missing field: " + description);
@


1.17
log
@Updated in light of the port of Joist JoistProperties class to Helm (as class HelmProperties).
@
text
@d83 2
a84 2
     * The context key for the {@@link org.joist.util.FormCache} instance from
     * a saved form post.
d194 1
a194 1
     * Retrieves the {@@link org.joist.util.FormCache} from the context.
d205 1
a205 1
     * Adds the {@@link org.joist.util.FormCache} to the context.
@


1.16
log
@I recommend not calling a method from itself.
@
text
@d61 1
a61 1
import org.joist.util.JoistProperties;
@


1.15
log
@Tweaked the error handling system to make it work like it did before. Bizarrely, there appears to be a built-in Velocimacro called errorList which does exactly the same thing as the errorList I wrote.
@
text
@d254 1
a254 1
                                         HttpSession session)
d256 1
a256 1
        return processField(context, key, session);
@


1.14
log
@* In isValidUserInput(), commented out try/catch to produce stack
trace.  Switched return value flag from object type to primitive
type.  Added comment.

* Added JavaDoc for get/setMissingFieldsRequired() methods.
@
text
@d94 2
a95 2
     * Context key indicating that user input contains missing fields
     * or validation errors.
d97 1
a97 1
    private static String MISSING_FIELDS_REQUIRED_KEY = "missingFieldsRequired";
d431 4
d438 1
a438 1
                                                 Boolean missingFieldsRequired)
a439 11
        /*
        try
        {
            throw new Exception("ContextAPI.isValidUserInput() called");
        }
        catch (Exception e)
        {
            Log.debug(e);
        }
        */

a440 6
        List[] check = 
        {
            getFatalErrors(context),
            getErrors(context),
            getValidationErrors(context)
        };
d442 1
a442 1
        for (int i = 0; i < check.length; i++)
d444 1
a444 4
            if (!check[i].isEmpty())
            {
                bad = true;
            }
d447 2
a448 3
        // The fields listed in the missingFields list may not always
        // be mandatory.
        if (missingFieldsRequired.booleanValue())
d450 1
a450 1
            setMissingFieldsRequired(context, missingFieldsRequired);
d453 3
a455 2
        if (getMissingFieldsRequired(context).booleanValue()
            && !getMissingFields(context).isEmpty())
d467 1
a467 1
    public static final Boolean getMissingFieldsRequired(Context context)
d469 3
a471 3
        Boolean missingFieldsRequired = (Boolean)context.get
            (MISSING_FIELDS_REQUIRED_KEY);
        if (missingFieldsRequired == null)
d473 1
a473 1
            missingFieldsRequired = Boolean.FALSE;
d475 1
a475 1
        return missingFieldsRequired;
d483 2
a484 2
    public static final void setMissingFieldsRequired(Context context,
                                                         Boolean required)
d486 1
a486 1
        context.put(MISSING_FIELDS_REQUIRED_KEY, required);
@


1.13
log
@Added flag so that you can signal when missing fields are important errors and when they're not.
@
text
@d436 1
d439 1
a439 1
            throw new Exception("");
d443 1
a443 1
            Log.error(e);
d445 3
a447 1
        Boolean bad = Boolean.FALSE;
d459 1
a459 1
                bad = Boolean.TRUE;
d463 2
d473 1
a473 1
            bad = Boolean.TRUE;
d476 1
a476 1
        return !bad.booleanValue();
d480 2
a481 1
     * 
d496 2
a497 1
     * 
@


1.12
log
@Cleanup for port to new error handling system.
@
text
@d57 1
d94 6
d425 20
a444 1
        boolean ok = true;
a448 1
            getMissingFields(context),
d451 1
d456 1
a456 1
                ok = false;
d459 37
a495 1
        return ok;
d544 1
@


1.11
log
@Porting of domain package to new error handling API.
@
text
@d403 2
a404 2
    protected static final void addBooleanToContext (Context ctx, String key, 
                                                     boolean bool)
@


1.10
log
@More changes to and first use of new error-handling system.
@
text
@d265 14
d380 29
d479 2
a480 1
    public static final void addMissingField(Context context, String error)
d482 1
a482 1
        addToContextList(context, MISSING_FIELDS_KEY, error);
d540 20
@


1.9
log
@First stab at error handling architecture.
@
text
@d229 1
a229 1
    protected final String processField (Context context, String key)
d237 1
a237 1
    protected final String processField (Context context, 
d246 1
a246 1
    protected final String processField (Context context, String key,
d258 1
a258 1
    protected final String processRequiredField 
d268 1
a268 1
    protected final String processRequiredField 
d280 2
a281 2
    protected String processField (Context context, String key, 
                                   String defaultValue, String description)
d310 3
a312 3
    protected String processField (Context context, String key, 
                                   String defaultValue, String description, 
                                   boolean cleanup)
a320 4
            else if (StringUtils.isValid(description))
            {
                addMissingField(context, description);
            }
d324 4
d342 1
a342 1
    protected final String processFieldRaw
d351 1
a351 1
    protected final String processFieldRaw
d360 1
a360 1
    protected final String processRequiredFieldRaw
d369 25
d396 1
a396 1
    public final List getFatalErrors(Context context)
d404 1
a404 1
    public final void addFatalError(Context context, String error)
d412 1
a412 1
    public final List getErrors(Context context)
d420 1
a420 1
    public final void addError(Context context, String error)
d428 1
a428 1
    public final List getMissingFields(Context context)
d436 1
a436 1
    public final void addMissingField(Context context, String error)
d444 1
a444 1
    public final List getValidationErrors(Context context)
d452 1
a452 1
    public final void addValidationError(Context context, String error)
d460 1
a460 1
    public final List getMessages(Context context)
d468 1
a468 1
    public final void addMessage(Context context, String message)
d476 1
a476 1
    private final List getListFromContext(Context context, String key)
d490 1
a490 1
    private final void addToContextList
@


1.8
log
@check context for value also
@
text
@d49 2
d53 1
d56 1
d70 6
d93 25
d222 248
@


1.7
log
@Do a client-side redirect to the defaulr project when a nonexistant project is accessed, rather than throwing an error.
@
text
@d121 1
a121 2
        HttpServletRequest req = getRequest(ctx);
        if (req != null)
d123 9
a131 1
            value = req.getParameter(name);
@


1.7.2.1
log
@Ported over client-side redirects to DEFAULT_PROJECT when an invalid project name is specified.
@
text
@d54 1
a54 1
import org.webmacro.servlet.*;
d66 12
d83 84
d169 1
a169 1
    public static void setClientRedirect(WebContext context, String newLocation)
d177 1
a177 1
    public static String getClientRedirect(WebContext context)
@


1.6
log
@Added putFormCache(Context) method to encapsulate the add (basically
to hide the hash key).
@
text
@d78 5
d164 16
@


1.5
log
@Added FORM_CACHE_KEY JavaDoc.
@
text
@a55 1
import org.joist.util.FormCache;
d148 11
@


1.4
log
@Added getFormCache() to retrieve a saved form post.
@
text
@d72 4
@


1.3
log
@Added a getResponse(Context ctx) class method.
@
text
@d56 1
d72 2
d134 11
@


1.2
log
@Removed unnecessary import of File.
@
text
@d49 1
d79 11
@


1.1
log
@This class holds the API for data which is stuffed into the
template-system contexts and used througout the life of a processing
request.
@
text
@a48 1
import java.io.File;
@

