ControllerBase.java

// license-header java merge-point
// Generated by andromda-jsf cartridge (utils\ControllerBase.java.vsl) DO NOT EDIT!
package org.andromda.samples.carrental;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

/**
 * Base class for all controllers
 */
public abstract class ControllerBase
{
    public static final String USE_CASE_SCOPE_KEY="useCaseScope";
    public static final String CURRENT_PAGE_VARIABLES_KEY="viewVariables";
    public static final String PAGE_VARIABLES_SUFFIX="-pageVariables";
    
    public static final String USE_CASE_PARAMETERS_KEY="useCaseParameters";
    public static final String USE_CASE_RETURN_VALUES_KEY="useCaseReturnValues";
    public static final String USE_CASE_IN_DIALOG_KEY="_dialog";
    public static final String USE_CASE_PARAMETER_PREFIX="_useCaseParameter.";
    public static final String USE_CASE_JS_RETURN_VALUE_KEY="jsReturnValue";

    /**
     * Returns the page variables associated with the view.
     *
     * @param view the view
     */
    @SuppressWarnings("unchecked")
    public Map<String,Object> getPageVariables(String view)
    {
        final String key=view+"-"+PAGE_VARIABLES_SUFFIX;
        final Map<String,Object> useCaseScope=getUseCaseScope();
        final Map<String,Object> pageVariables;
        if(useCaseScope.containsKey(key))
        {
            pageVariables=(Map<String,Object>)useCaseScope.get(key);
        } 
        else
        {
            pageVariables=new HashMap<String,Object>();
            useCaseScope.put(key, pageVariables);
        }
        return pageVariables;
    }

    /**
     * Returns the use case parameters.
     *
     * @return Map<String,Object>
     */
    @SuppressWarnings("unchecked")
    public Map<String,Object> getUseCaseParameters()
    {
        return (Map<String,Object>)getUseCaseScope().get(USE_CASE_PARAMETERS_KEY);
    }
    
    /**
     * Returns the use case return values.
     *
     * @return Map<String,Object>
     */
    @SuppressWarnings("unchecked")
    public static Map<String,Object> getUseCaseReturnValues()
    {
        return (Map<String,Object>)getUseCaseScope().get(USE_CASE_RETURN_VALUES_KEY);
    }
    
    /**
     * Removes all the forms and page variables from the current flash scope.
     */
    protected Map<String,Object> resetUseCaseScope()
    {
        final Map<String,Object> useCaseScope=setUseCaseScope(Collections.synchronizedMap(new HashMap<String,Object>()));
        useCaseScope.put(USE_CASE_PARAMETERS_KEY,new HashMap<String,Object>());
        useCaseScope.put(USE_CASE_RETURN_VALUES_KEY,new HashMap<String,Object>());
        return useCaseScope;
    }

    /**
     * Returns the useCaseScope
     *
     * @return Map<String,Object>
     */
    @SuppressWarnings("unchecked")
    public static Map<String,Object> getUseCaseScope()
    {
        return (Map<String,Object>)JsfUtils.getValueInRequestMap(USE_CASE_SCOPE_KEY);
    }
    
    /**
     * Sets the current useCaseScope
     * 
     * @param useCaseScope the new value for useCaseScope
     */
    protected Map<String,Object> setUseCaseScope(final Map<String,Object> useCaseScope)
    {
        JsfUtils.getRequestMap().put(USE_CASE_SCOPE_KEY, useCaseScope);
        return useCaseScope;
    }
    
    //used in the ExceptionHandler
    public static final String LAST_POSTED_FORM_CLIENT_ID=ControllerBase.class.getName()+"__LAST_POSTED_FORM_CLIENT_ID__";

    /**
     * Sets the client identifier of the last posted form
     *
     * @param clientId the client identifier of the last posted form
     */
    protected void setLastPostedFormClientId(final String clientId)
    {
        JsfUtils.setRequestAttribute(LAST_POSTED_FORM_CLIENT_ID, clientId);
    }
    
    /**
     * Gets the client identifier of the last posted form
     *
     * @return the client identifier of the last posted form
     */    
    public String getLastPostedFormClientId()
    {
        return (String) JsfUtils.getRequestAttribute(LAST_POSTED_FORM_CLIENT_ID);
    }
 
    //start/finish use case
    
    protected abstract String internalStartUseCase(Map<String,Object> useCaseParameters) throws Throwable;

    /**
     * Starts a use case
     *
     * @return the start use case
     */
    public String startUseCase() throws Throwable
    {
        final Map<String,Object> useCaseParameters;
        
        if(ControllerBase.getUseCaseScope() == null)//can be null if executed from the url
        {
            useCaseParameters=new HashMap<String,Object>();
            final HttpServletRequest request=JsfUtils.getRequest();
            final Enumeration<String> parameterNames=request.getParameterNames();
            while(parameterNames.hasMoreElements())
            {
                final String parameterName=parameterNames.nextElement();
                if(parameterName.startsWith(USE_CASE_PARAMETER_PREFIX))
                {
                    useCaseParameters.put(StringUtils.substringAfter(parameterName, USE_CASE_PARAMETER_PREFIX),request.getParameter(parameterName));
                }
            }
        }
        else
        {
            useCaseParameters=this.getUseCaseParameters();
        }

        //reset the useCaseScope and put the parameters available.
        this.resetUseCaseScope().put(USE_CASE_PARAMETERS_KEY,useCaseParameters);
        
        setUseCaseInDialog(BooleanUtils.toBoolean(ObjectUtils.toString(useCaseParameters.get(USE_CASE_IN_DIALOG_KEY))));

        return internalStartUseCase(useCaseParameters);
    }

    /**
     * Checks if use case is in a dialog
     *
     * @return true if use case is in a dialog
     */    
    public boolean isUseCaseInDialog()
    {
        return Boolean.TRUE.equals(getUseCaseScope().get(USE_CASE_IN_DIALOG_KEY));
    }
    
    /**
     * Informs if the use case is in a dialog
     * 
     * @param value flag that indicates if the use case is in a dialog
     */    
    protected void setUseCaseInDialog(boolean value)
    {
        getUseCaseScope().put(USE_CASE_IN_DIALOG_KEY, value);
    }

    /**
     * Returns from the dialog
     * 
     */    
    protected String closeDialog()
    {
        return "/closeDialog.jsf";
    }
    
    /**
     * Sets the return values in useCaseReturnValues
     * 
     */    
    protected String setReturnValues(String[] names, Object[] values)
    {
        if(names != null)
        {
            final Map<String,Object> returnValues=getUseCaseReturnValues();
            final StringBuilder jsResult=new StringBuilder();
            jsResult.append("{");
            for(int i=0; i<names.length; i++)
            {
                returnValues.put(names[i], values[i]);
                jsResult.append(names[i]).append(':').append('\'').append(values[i]).append('\'');
                if(i<names.length)
                {
                    jsResult.append(',');
                }
            }
            jsResult.append('}');
            returnValues.put(USE_CASE_JS_RETURN_VALUE_KEY,jsResult.toString());
        }
        
        return closeDialog();
    }
    
    public String nullAction()
    {
        return null;
    }
    
    // controller-base merge-point
}