RegisterCarController.java

// license-header java merge-point
// Generated by andromda-jsf cartridge (controllers\Controller.java.vsl) DO NOT EDIT!
package org.andromda.samples.carrental.inventory.web.registerCar;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import org.andromda.samples.carrental.ControllerBase;
import org.andromda.samples.carrental.JsfUtils;
import org.andromda.samples.carrental.ServiceLocator;
import org.andromda.samples.carrental.admins.web.main.AdministrationController;
import org.andromda.samples.carrental.inventory.InventoryService;

/**
 * <p>
 * This controller allows to register a new Car. It calls the InventoryService on the server side
 * </p>
 * <p>
 * @author Carlos Cuenca
 * </p>
 */
public abstract class RegisterCarController
    extends ControllerBase
    implements Serializable
{
    /**
     * The serial version UID of this class. Needed for serialization.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Returns an instance from the JSF context
     *
     */
    public static RegisterCarController instance()
    {
        return (RegisterCarController)JsfUtils.resolveVariable("registerCarController");
    }
    
    /**
     * Populate action form and page variables
     *
     * @param currentView the current view
     * @param forward the forward view
     * @param sourceForm the source form
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    private void populateActionFormsAndPageVariables(final String currentView, String forward, final Object sourceForm) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
    {
        if(forward == null)
        {
            forward=currentView;
        }
        
        final Map<String,Object> pageVariables=this.getPageVariables(forward);
        if("/org/andromda/samples/carrental/inventory/web/registerCar/register-new-car.jsf".equals(forward))
        {
            RegisterCarViewPopulator.populateForm(sourceForm,this.getRegisterNewCarCreateForm());
            RegisterCarViewPopulator.populateRegisterNewCarPageVariables(sourceForm,pageVariables);
        }
        ControllerBase.getUseCaseScope().put(CURRENT_PAGE_VARIABLES_KEY, pageVariables);
    }
    
    /**
     * <p>
     * TODO: Model Documentation for
     * org.andromda.samples.carrental.inventory.web.registerCar.createCar
     * </p>
     *
     * @param form the associated form
     * @throws Throwable
     */
    public abstract String createCar(CreateCarForm form)
        throws Throwable;

    /**
     * <p>
     * TODO: Model Documentation for
     * org.andromda.samples.carrental.inventory.web.registerCar.searchAllCars
     * </p>
     *
     * @param form the associated form
     * @throws Throwable
     */
    public abstract void searchAllCars(SearchAllCarsForm form)
        throws Throwable;


    /**
     * Retrieves the {@link RegisterNewCarCreateFormImpl} form instance 
     *
     * @return RegisterNewCarCreateFormImpl
     */
    protected RegisterNewCarCreateFormImpl getRegisterNewCarCreateForm()
    {
        return (RegisterNewCarCreateFormImpl)JsfUtils.resolveVariable("registerCarRegisterNewCarCreateForm");
    }

    /**
     * <p>
     * TODO: Model Documentation for create
     * </p>
     * This method is called when 'create' is triggered in the view 'Register new Car'.
     * It can be safely overridden in descendant classes.
     *
     * @param form the associated form
     */
    protected void _registerNewCar_create(RegisterNewCarCreateFormImpl form)
    {
        //this method can be overridden
    }

    /**
     * Retrieves registerNewCarCreate()
     *    
     * @return registerNewCarCreate
     * @throws Throwable
     */
    public String registerNewCarCreate()
        throws Throwable
    {
        this.setLastPostedFormClientId("registerCarRegisterNewCarCreateForm");
        
        String forward=null;
        final RegisterNewCarCreateFormImpl form =
            this.getRegisterNewCarCreateForm();
            
        //trigger method execution
        _registerNewCar_create(form);

        forward = _saveCar(form);
        populateActionFormsAndPageVariables("/org/andromda/samples/carrental/inventory/web/registerCar/register-new-car.jsf",forward,form);
        if(forward != null)
        {
            if("/org/andromda/samples/carrental/inventory/web/registerCar/register-new-car.jsf".equals(forward))
            {
                forward = null; //the destination form is the same, stay on the current view
            }
        }
        
        return forward;
    }

    /**
     * <p>
     * TODO: Model Documentation for save car
     * </p>
     *
     * @param form the associated form
     * @return the forward view
     * @throws Throwable
     */
    protected String _saveCar(final RegisterNewCarCreateFormImpl form)
        throws Throwable
    {
        String forward = null;
    
        createCar(form);
        forward = ((AdministrationController)JsfUtils.resolveVariable("administrationController")).startUseCase();
        return forward;
    }

    /**
     * Retrieves the {@link RegisterCarFormImpl} form instance 
     *
     * @return RegisterCarFormImpl
     */
    protected RegisterCarFormImpl getRegisterCarForm()
    {
        return (RegisterCarFormImpl)JsfUtils.resolveVariable("registerCarRegisterCarForm");
    }

    /**
     * This method is called when the use case 'Register Car' starts.
     * It can be safely overridden in descendant classes.
     *
     * @param form the associated form
     */
    protected void _registerCar_started(RegisterCarFormImpl form)
    {
        //this method can be overridden
    }

    /**
     * Retrieves the internal start use case
     *    
     * @return registerCar
     * @throws Throwable
     */
    protected String internalStartUseCase(Map<String,Object> useCaseParameters)
        throws Throwable
    {
        this.setLastPostedFormClientId("registerCarRegisterCarForm");
        
        String forward=null;
        final RegisterCarFormImpl form =
            this.getRegisterCarForm();
            
        //copy parameters form caller use case
        form.copyFrom(useCaseParameters);
        
        //trigger method execution
        _registerCar_started(form);

        forward = _searchExistingCars(form);
        populateActionFormsAndPageVariables(null,forward,form);
        return forward;
    }

    /**
     * <p>
     * TODO: Model Documentation for search existing cars
     * </p>
     *
     * @param form the associated form
     * @return the forward view
     * @throws Throwable
     */
    protected String _searchExistingCars(final RegisterCarFormImpl form)
        throws Throwable
    {
        String forward = null;
    
        searchAllCars(form);
        forward = "/org/andromda/samples/carrental/inventory/web/registerCar/register-new-car.jsf";
        return forward;
    }

    /**
     * Returns a reference to the InventoryService back-end service.
     *
     * @return ServiceLocator.instance().getInventoryService()
     */
    protected final InventoryService getInventoryService()
    {
        try
        {
            return ServiceLocator.instance().getInventoryService();
        }
        catch (final Throwable throwable)
        {
            throw new RuntimeException(throwable);
        }
    }


}