DecisionServiceWSDelegator.java

// license-header java merge-point
// Generated by SpringWebServiceDelegator.vsl in andromda-spring-cartridge. Do not modify by hand!.
package org.andromda.samples.animalquiz.decisiontree;

import org.andromda.samples.animalquiz.ServiceLocator;
import org.apache.commons.beanutils.PropertyUtils;

/**
 * Web service delegator for {@link DecisionService}.
 *
 * @see DecisionService
 */
public class DecisionServiceWSDelegator
{
    /**
     * Gets an instance of {@link DecisionService}
     */
    private DecisionService getDecisionService()
    {
        return ServiceLocator.instance().getDecisionService();
    }

    /**
     * @see DecisionService#getFirstQuestion()
     */
    public VODecisionItem getFirstQuestion()
        throws DecisionException
    {
        try
        {
            return getDecisionService().getFirstQuestion();
        }
        catch (Exception exception)
        {
            if (exception instanceof DecisionException)
            {
                throw (DecisionException)exception;
            }
            final Throwable cause = getRootCause(exception);
            if (cause instanceof DecisionException)
            {
                throw (DecisionException)cause;
            }
            throw new RuntimeException(cause);
        }
    }

    /**
     * @see DecisionService#getNextQuestion(Long)
     */
    public VODecisionItem getNextQuestion(Long itemId)
        throws DecisionException
    {
        try
        {
            return getDecisionService().getNextQuestion(itemId);
        }
        catch (Exception exception)
        {
            if (exception instanceof DecisionException)
            {
                throw (DecisionException)exception;
            }
            final Throwable cause = getRootCause(exception);
            if (cause instanceof DecisionException)
            {
                throw (DecisionException)cause;
            }
            throw new RuntimeException(cause);
        }
    }

    /**
     * @see DecisionService#addNewAnimalWithQuestion(String, String, Long)
     */
    public void addNewAnimalWithQuestion(String animalName, String promptForYes, Long idOfLastNoDecision)
        throws DecisionException
    {
        try
        {
            getDecisionService().addNewAnimalWithQuestion(animalName, promptForYes, idOfLastNoDecision);
        }
        catch (Exception exception)
        {
            if (exception instanceof DecisionException)
            {
                throw (DecisionException)exception;
            }
            final Throwable cause = getRootCause(exception);
            if (cause instanceof DecisionException)
            {
                throw (DecisionException)cause;
            }
            throw new RuntimeException(cause);
        }
    }

    /**
     * Finds the root cause of the parent exception
     * by traveling up the exception tree.
     */
    private static Throwable getRootCause(Throwable throwable)
    {
        if (throwable != null)
        {
            // Reflectively get any exception causes.
            try
            {
                Throwable targetException = null;

                // java.lang.reflect.InvocationTargetException
                String exceptionProperty = "targetException";
                if (PropertyUtils.isReadable(throwable, exceptionProperty))
                {
                    targetException = (Throwable)PropertyUtils.getProperty(throwable, exceptionProperty);
                }
                else
                {
                    exceptionProperty = "causedByException";
                    //javax.ejb.EJBException
                    if (PropertyUtils.isReadable(throwable, exceptionProperty))
                    {
                        targetException = (Throwable)PropertyUtils.getProperty(throwable, exceptionProperty);
                    }
                }
                if (targetException != null)
                {
                    throwable = targetException;
                }
            }
            catch (Exception exception)
            {
                // just print the exception and continue
                exception.printStackTrace();
            }
            if (throwable.getCause() != null)
            {
                throwable = throwable.getCause();
                throwable = getRootCause(throwable);
            }
        }
        return throwable;
    }
}