CrudControllerBase.java

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

import java.util.Collection;
import java.util.Map;
import javax.faces.model.SelectItem;
import org.apache.commons.lang.ObjectUtils;

/**
 * Base class for all crud controllers
 */
public abstract class CrudControllerBase extends ControllerBase {

    /**
     * Initializes the controller, calling the doInit method
     *
     * @return the controller view path.
     * @throws Throwable
     */
    public abstract String init() throws Throwable;
    /**
     * The cancel edit action
     *
     * @return null
     * @throws Throwable
     */
    public abstract String cancel() throws Throwable;
    /**
     * The new instance action.
     *
     * @return null
     * @throws Throwable
     */
    public abstract String startNew() throws Throwable;
    /**
     * The save and select instance action.
     *
     * @return null
     * @throws Throwable
     */
    public abstract String saveAndSelect() throws Throwable;    
    /**
     * The save instance action.
     *
     * @return null
     * @throws Throwable
     */
    public abstract String save() throws Throwable;    
    /**
     * The save and new instance action.
     *
     * @return null
     * @throws Throwable
     */
    public abstract String saveAndNew() throws Throwable;
    /**
     * The search action.
     *
     * @return null
     * @throws Throwable
     */
    public abstract String search() throws Throwable;
    /**
     * Helper method to fill the select component list.
     *
     * @return a collection with the filtered list.
     * @throws Throwable
     */
    public abstract Collection<SelectItem> getAsSelectItems() throws Throwable;
    /**
     * Save and keep editing.
     *
     * @return null
     * @throws Throwable
     */
    public abstract String saveAndKeepEditing() throws Throwable;
    
    protected abstract void fillFormFromParameters(final Map<String,Object> useCaseParameters);
    protected abstract boolean isIdSet();
    protected abstract void internalLoad(final Object idParam) throws Throwable;
    
    final static String CRUD_OPERATION_KEY = "_crudAction";
    final static String CRUD_EDIT_KEY = "edit";
    final static String CRUD_CREATE_KEY = "create";
    final static String CRUD_SELECT_ONE_KEY = "selectOne";
    final static String CRUD_FILTERED_KEY = "filtered";
    final static String CRUD_FILTER_ATTRIBUTE_KEY = "filterAttribute";
    final static String CRUD_FILTER_VALUE_KEY = "filterValue";
    final static String CRUD_EDIT_ID_KEY = "editId";
    final static String CRUD_SELECTED_ID_KEY = "selectedId";
    final static String CRUD_SELECTED_LABEL_KEY = "selectedLabel";
    final static String CRUD_SAVED_KEY = "_saved";

    @Override
    protected String internalStartUseCase(final Map<String,Object> useCaseParameters) throws Throwable
    {
        final String forward=init();

        Object operation=useCaseParameters.get(CRUD_OPERATION_KEY);
        
        if(useCaseParameters.containsKey(CRUD_FILTER_ATTRIBUTE_KEY) && useCaseParameters.containsKey(CRUD_FILTER_VALUE_KEY)){
            ControllerBase.getUseCaseScope().put(CRUD_FILTER_ATTRIBUTE_KEY, useCaseParameters.get(CRUD_FILTER_ATTRIBUTE_KEY));
            ControllerBase.getUseCaseScope().put(CRUD_FILTER_VALUE_KEY, useCaseParameters.get(CRUD_FILTER_VALUE_KEY));
            useCaseParameters.remove(CRUD_FILTER_ATTRIBUTE_KEY);
            useCaseParameters.remove(CRUD_FILTER_VALUE_KEY);
        }

        if(CRUD_EDIT_KEY.equals(operation))
        {
            ControllerBase.getUseCaseScope().put(CRUD_OPERATION_KEY,CRUD_EDIT_KEY);
            this.internalLoad(useCaseParameters.get(CRUD_EDIT_ID_KEY));
        }
        else if(CRUD_CREATE_KEY.equals(operation))
        {
            ControllerBase.getUseCaseScope().put(CRUD_OPERATION_KEY,CRUD_CREATE_KEY);
            //copy parameters form caller use case
            this.fillFormFromParameters(useCaseParameters);
            
            startNew();
        }
        else if(CRUD_SELECT_ONE_KEY.equals(operation))
        {
            ControllerBase.getUseCaseScope().put(CRUD_OPERATION_KEY,CRUD_SELECT_ONE_KEY);
            //copy parameters form caller use case
            this.fillFormFromParameters(useCaseParameters);
        }
        else if(CRUD_FILTERED_KEY.equals(operation))
        {
            ControllerBase.getUseCaseScope().put(CRUD_OPERATION_KEY,CRUD_FILTERED_KEY);

            //copy parameters form caller use case
            this.fillFormFromParameters(useCaseParameters);
            
            this.search();
        }
        
        return forward;
    }

    protected void clearSaved()
    {
        ControllerBase.getUseCaseScope().remove(CRUD_SAVED_KEY);
    }
    
    protected void setAsSaved()
    {
        ControllerBase.getUseCaseScope().put(CRUD_SAVED_KEY,Boolean.TRUE);
    }
    
    public boolean isSaved()
    {
        return ControllerBase.getUseCaseScope().containsKey(CRUD_SAVED_KEY);
    }
    
    /**
     * Indicates if it is running filtered
     *
     * @return flag that indicates it is running filtered
     */
    public boolean isFiltered()
    {
        return CRUD_FILTERED_KEY.equals(ControllerBase.getUseCaseScope().get(CRUD_OPERATION_KEY));
    }
    
    /**
     * Returns the current filtered attribute
     *
     * @return the current filtered attribute
     */
    public String getFilterAttribute()
    {
        return ObjectUtils.toString(ControllerBase.getUseCaseScope().get(CRUD_FILTER_ATTRIBUTE_KEY));
    }
    
    /**
     * Returns the current filter value
     *
     * @return the current filter value
     */
    public Object getFilterValue()
    {
        return ControllerBase.getUseCaseScope().get(CRUD_FILTER_VALUE_KEY);
    }
    
    /**
     * Indicates if should show select action
     *
     * @return flag that indicates if should show select action
     */
    public boolean isShowSelectAction()
    {
        return this.isUseCaseInDialog() && CRUD_SELECT_ONE_KEY.equals(ControllerBase.getUseCaseScope().get(CRUD_OPERATION_KEY));
    }
    
    /**
     * Indicates if should show new action
     *
     * @return flag that indicates if should show new action
     */    
    public boolean isShowNewAction()
    {
        return true;
    }
    
    /**
     * Indicates if should show search action
     *
     * @return flag that indicates if should show search action
     */    
    public boolean isShowSearchAction()
    {
        return true;
    }
    
    /**
     * Indicates if should show save and select action
     *
     * @return flag that indicates if should show save and select action
     */    
    public boolean isShowSaveAndSelectAction()
    {
        return this.isUseCaseInDialog() &&
               (CRUD_SELECT_ONE_KEY.equals(ControllerBase.getUseCaseScope().get(CRUD_OPERATION_KEY)) ||
               CRUD_CREATE_KEY.equals(ControllerBase.getUseCaseScope().get(CRUD_OPERATION_KEY)) ||
               CRUD_EDIT_KEY.equals(ControllerBase.getUseCaseScope().get(CRUD_OPERATION_KEY)));
    }
    
    /**
     * Indicates if should show save action
     *
     * @return flag that indicates if should show save action
     */    
    public boolean isShowSaveAction()
    {
        return !this.isUseCaseInDialog() || this.isFiltered() || CRUD_SELECT_ONE_KEY.equals(ControllerBase.getUseCaseScope().get(CRUD_OPERATION_KEY));
    }

    /**
     * Indicates if should show save action
     *
     * @return flag that indicates if should show save action
     */    
    public boolean isShowCancelAction()
    {
        return !this.isSaved();
    }

    /**
     * Indicates if should show delete action
     *
     * @return flag that indicates if should show save action
     */    
    public boolean isShowDeleteAction()
    {
        return this.isIdSet() && (this.isFiltered() || !this.isUseCaseInDialog());
    }

    /**
     * Shows a message warning the user can exists more records available.
     */
    protected void saveMaxResultsWarning()
    {
        JsfUtils.addWarningMessage(Messages.get("maximum.results.fetched.warning", new Object[]{String.valueOf("250")}));
    }

    /**
     * The instance select action.
     *
     * @param id the identifier
     * @throws Throwable
     */
    public String select(final Object value, final Object label)
        throws Throwable
    {
        return setReturnValues(
            new String[]{CRUD_SELECTED_ID_KEY,CRUD_SELECTED_LABEL_KEY},
            new Object[]{value,label});
    }

    public String nullAction()
    {
        return null;
    }
    
    // crud-controller-base merge-point
}