CustomerLogInController.java

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

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.contracts.web.reserveCar.CustomerReservesCarController;
import org.andromda.samples.carrental.customers.CustomerService;

/**
 * <p>
 * This controller allows the user to log in. It calls the CustomerService on the server side
 * </p>
 * <p>
 * @author Carlos Cuenca
 * </p>
 */
public abstract class CustomerLogInController
    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 CustomerLogInController instance()
    {
        return (CustomerLogInController)JsfUtils.resolveVariable("customerLogInController");
    }
    
    /**
     * 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/customers/web/login/customer-login.jsf".equals(forward))
        {
            CustomerLogInViewPopulator.populateForm(sourceForm,this.getCustomerLoginAuthenticateForm());
        }
        ControllerBase.getUseCaseScope().put(CURRENT_PAGE_VARIABLES_KEY, pageVariables);
    }
    
    /**
     * <p>
     * TODO: Model Documentation for
     * org.andromda.samples.carrental.customers.web.login.authenticateAsCustomer
     * </p>
     *
     * @param form the associated form
     * @throws Throwable
     */
    public abstract String authenticateAsCustomer(AuthenticateAsCustomerForm form)
        throws Throwable;


    /**
     * Retrieves the {@link CustomerLoginAuthenticateFormImpl} form instance 
     *
     * @return CustomerLoginAuthenticateFormImpl
     */
    protected CustomerLoginAuthenticateFormImpl getCustomerLoginAuthenticateForm()
    {
        return (CustomerLoginAuthenticateFormImpl)JsfUtils.resolveVariable("customerLogInCustomerLoginAuthenticateForm");
    }

    /**
     * <p>
     * TODO: Model Documentation for authenticate
     * </p>
     * This method is called when 'authenticate' is triggered in the view 'Customer Login'.
     * It can be safely overridden in descendant classes.
     *
     * @param form the associated form
     */
    protected void _customerLogin_authenticate(CustomerLoginAuthenticateFormImpl form)
    {
        //this method can be overridden
    }

    /**
     * Retrieves customerLoginAuthenticate()
     *    
     * @return customerLoginAuthenticate
     * @throws Throwable
     */
    public String customerLoginAuthenticate()
        throws Throwable
    {
        this.setLastPostedFormClientId("customerLogInCustomerLoginAuthenticateForm");
        
        String forward=null;
        final CustomerLoginAuthenticateFormImpl form =
            this.getCustomerLoginAuthenticateForm();
            
        //trigger method execution
        _customerLogin_authenticate(form);

        forward = _checkLogin(form);
        populateActionFormsAndPageVariables("/org/andromda/samples/carrental/customers/web/login/customer-login.jsf",forward,form);
        if(forward != null)
        {
            if("/org/andromda/samples/carrental/customers/web/login/customer-login.jsf".equals(forward))
            {
                forward = null; //the destination form is the same, stay on the current view
            }
        }
        
        return forward;
    }

    /**
     * <p>
     * TODO: Model Documentation for Check Login
     * </p>
     *
     * @param form the associated form
     * @return the forward view
     * @throws Throwable
     */
    protected String _checkLogin(final CustomerLoginAuthenticateFormImpl form)
        throws Throwable
    {
        String forward = null;
    
        authenticateAsCustomer(form);
        forward = __authenticateAsCustomer(form);
        return forward;
    }

    /**
     * <p>
     * TODO: Model Documentation for Start Login
     * </p>
     *
     * @param form the associated form
     * @return the forward view
     * @throws Throwable
     */
    protected String _startLogin(final CustomerLoginAuthenticateFormImpl form)
        throws Throwable
    {
        String forward = null;
        forward = "/org/andromda/samples/carrental/customers/web/login/customer-login.jsf";
        return forward;
    }

    /**
     * <p>
     * TODO: Model Documentation for org.andromda.samples.carrental.customers.web.login.authenticate
     * </p>
     *
     * @return a transition
     */
    protected String __authenticateAsCustomer(final CustomerLoginAuthenticateFormImpl form)
        throws Throwable
    {
        final String value = String.valueOf(authenticateAsCustomer(form));
        String forward = null;
        if (value.equals("success"))
        {
            forward = ((CustomerReservesCarController)JsfUtils.resolveVariable("customerReservesCarController")).startUseCase();
        }
        if (value.equals("failure"))
        {
            forward = _startLogin(form);
        }
        if (forward == null)
        {
            // throw exception in case we have an invalid return value from the controller
            throw new RuntimeException("Runtime model error: no valid path selected. Selected path="+value);
        }
        else
        {
            return forward;
        }
    }

    /**
     * Retrieves the {@link CustomerLogInFormImpl} form instance 
     *
     * @return CustomerLogInFormImpl
     */
    protected CustomerLogInFormImpl getCustomerLogInForm()
    {
        return (CustomerLogInFormImpl)JsfUtils.resolveVariable("customerLogInCustomerLogInForm");
    }

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

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

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

    /**
     * <p>
     * TODO: Model Documentation for Start Login
     * </p>
     *
     * @param form the associated form
     * @return the forward view
     * @throws Throwable
     */
    protected String _startLogin(final CustomerLogInFormImpl form)
        throws Throwable
    {
        String forward = null;
        forward = "/org/andromda/samples/carrental/customers/web/login/customer-login.jsf";
        return forward;
    }

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


}