CriteriaSearchProperties.java

// license-header java merge-point
//
// Attention: Generated code! Do not modify by hand!
// Generated by hibernate/HibernateSearchProperties.vsl in andromda-spring-cartridge on 08/18/2014 15:29:44-0400. Do not modify by hand!.
//
package org.andromda.samples.carrental;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.andromda.samples.carrental.admins.AdministratorImpl;
import org.andromda.samples.carrental.contracts.AccidentDocImpl;
import org.andromda.samples.carrental.contracts.ContractImpl;
import org.andromda.samples.carrental.contracts.HandoutDocImpl;
import org.andromda.samples.carrental.contracts.PartnerInAccidentImpl;
import org.andromda.samples.carrental.contracts.ReservationImpl;
import org.andromda.samples.carrental.contracts.ReturnDocImpl;
import org.andromda.samples.carrental.customers.CustomerImpl;
import org.andromda.samples.carrental.customers.DriverImpl;
import org.andromda.samples.carrental.inventory.CarAccessoryImpl;
import org.andromda.samples.carrental.inventory.CarAccessoryTypeImpl;
import org.andromda.samples.carrental.inventory.CarImpl;
import org.andromda.samples.carrental.inventory.CarTypeImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.StringUtils;

/**
 * Stores the embedded values and associations of all entities in the system by type.
 * Is used to determine the appropriate parameter name when an embedded value's property
 * is referenced as the attribute to search by (as opposed to an association).
 *
 * @author Chad Brandon
 */
public class CriteriaSearchProperties
{
    private static final Map<Class<?>,Collection<PropertyType>> embeddedValuesByType = new HashMap<Class<?>,Collection<PropertyType>>();
    private static final Map<Class<?>,Collection<String>> embeddedValueNamesByType = new HashMap<Class<?>,Collection<String>>();
    private static final Map<Class<?>,Collection<PropertyType>> navigableAssociationEndsByType = new HashMap<Class<?>,Collection<PropertyType>>();

    static
    {
        initializeAdministrator1();
        initializeAccidentDoc2();
        initializeContract3();
        initializeHandoutDoc4();
        initializePartnerInAccident5();
        initializeReservation6();
        initializeReturnDoc7();
        initializeCustomer8();
        initializeDriver9();
        initializeCar10();
        initializeCarAccessory11();
        initializeCarAccessoryType12();
        initializeCarType13();
    }

    private static final void initializeAdministrator1()
    {
        embeddedValueNamesByType.put(
            AdministratorImpl.class,
            null);
        embeddedValuesByType.put(
            AdministratorImpl.class,
            null);
        navigableAssociationEndsByType.put(
            AdministratorImpl.class,
            null);
    }

    private static final void initializeAccidentDoc2()
    {
        embeddedValueNamesByType.put(
            AccidentDocImpl.class,
            null);
        embeddedValuesByType.put(
            AccidentDocImpl.class,
            null);
        navigableAssociationEndsByType.put(
            AccidentDocImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("contract", ContractImpl.class),
                    new PropertyType("partnerInAccident", PartnerInAccidentImpl.class)
                }
            )
        );
    }

    private static final void initializeContract3()
    {
        embeddedValueNamesByType.put(
            ContractImpl.class,
            null);
        embeddedValuesByType.put(
            ContractImpl.class,
            null);
        navigableAssociationEndsByType.put(
            ContractImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("handoutDoc", HandoutDocImpl.class),
                    new PropertyType("returnDoc", ReturnDocImpl.class),
                    new PropertyType("accidentDoc", AccidentDocImpl.class),
                    new PropertyType("cars", CarImpl.class),
                    new PropertyType("customer", CustomerImpl.class),
                    new PropertyType("driver", DriverImpl.class)
                }
            )
        );
    }

    private static final void initializeHandoutDoc4()
    {
        embeddedValueNamesByType.put(
            HandoutDocImpl.class,
            null);
        embeddedValuesByType.put(
            HandoutDocImpl.class,
            null);
        navigableAssociationEndsByType.put(
            HandoutDocImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("contract", ContractImpl.class)
                }
            )
        );
    }

    private static final void initializePartnerInAccident5()
    {
        embeddedValueNamesByType.put(
            PartnerInAccidentImpl.class,
            null);
        embeddedValuesByType.put(
            PartnerInAccidentImpl.class,
            null);
        navigableAssociationEndsByType.put(
            PartnerInAccidentImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("accidentDoc", AccidentDocImpl.class)
                }
            )
        );
    }

    private static final void initializeReservation6()
    {
        embeddedValueNamesByType.put(
            ReservationImpl.class,
            null);
        embeddedValuesByType.put(
            ReservationImpl.class,
            null);
        navigableAssociationEndsByType.put(
            ReservationImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("customer", CustomerImpl.class)
                }
            )
        );
    }

    private static final void initializeReturnDoc7()
    {
        embeddedValueNamesByType.put(
            ReturnDocImpl.class,
            null);
        embeddedValuesByType.put(
            ReturnDocImpl.class,
            null);
        navigableAssociationEndsByType.put(
            ReturnDocImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("contract", ContractImpl.class)
                }
            )
        );
    }

    private static final void initializeCustomer8()
    {
        embeddedValueNamesByType.put(
            CustomerImpl.class,
            null);
        embeddedValuesByType.put(
            CustomerImpl.class,
            null);
        navigableAssociationEndsByType.put(
            CustomerImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("reservations", ReservationImpl.class),
                    new PropertyType("drivers", DriverImpl.class),
                    new PropertyType("contracts", ContractImpl.class)
                }
            )
        );
    }

    private static final void initializeDriver9()
    {
        embeddedValueNamesByType.put(
            DriverImpl.class,
            null);
        embeddedValuesByType.put(
            DriverImpl.class,
            null);
        navigableAssociationEndsByType.put(
            DriverImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("customer", CustomerImpl.class),
                    new PropertyType("contracts", ContractImpl.class)
                }
            )
        );
    }

    private static final void initializeCar10()
    {
        embeddedValueNamesByType.put(
            CarImpl.class,
            null);
        embeddedValuesByType.put(
            CarImpl.class,
            null);
        navigableAssociationEndsByType.put(
            CarImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("contracts", ContractImpl.class),
                    new PropertyType("carAccessories", CarAccessoryImpl.class),
                    new PropertyType("carType", CarTypeImpl.class)
                }
            )
        );
    }

    private static final void initializeCarAccessory11()
    {
        embeddedValueNamesByType.put(
            CarAccessoryImpl.class,
            null);
        embeddedValuesByType.put(
            CarAccessoryImpl.class,
            null);
        navigableAssociationEndsByType.put(
            CarAccessoryImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("car", CarImpl.class),
                    new PropertyType("carAccessoryType", CarAccessoryTypeImpl.class)
                }
            )
        );
    }

    private static final void initializeCarAccessoryType12()
    {
        embeddedValueNamesByType.put(
            CarAccessoryTypeImpl.class,
            null);
        embeddedValuesByType.put(
            CarAccessoryTypeImpl.class,
            null);
        navigableAssociationEndsByType.put(
            CarAccessoryTypeImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("carAccessories", CarAccessoryImpl.class)
                }
            )
        );
    }

    private static final void initializeCarType13()
    {
        embeddedValueNamesByType.put(
            CarTypeImpl.class,
            null);
        embeddedValuesByType.put(
            CarTypeImpl.class,
            null);
        navigableAssociationEndsByType.put(
            CarTypeImpl.class,
            Arrays.asList(
                new PropertyType[]
                {
                    new PropertyType("cars", CarImpl.class)
                }
            )
        );
    }

    /**
     * Attempts to get the embedded value list for the given type (or returns null
     * if one doesn't exist).
     *
     * @param type the type of which to retrieve the value.
     * @return the collection of embedded value names.
     */
    public static Collection<String> getEmbeddedValues(final Class<?> type)
    {
        return embeddedValueNamesByType.get(type);
    }

    /**
     * Gets the type of the navigable association end given the <code>ownerType</code>
     * and <code>name</code>
     *
     * @param ownerType the owner of the association.
     * @param name the name of the association end to find.
     * @return the type of the association end.
     */
    public static Class<?> getNavigableAssociationEndType(final Class<?> ownerType, final String name)
    {
        return getPropertyType(navigableAssociationEndsByType, ownerType, name);
    }

    /**
     * Gets the type of the embedded value given the <code>ownerType</code>
     * and <code>name</code>
     *
     * @param ownerType the owner of the association.
     * @param name the name of the association end to find.
     * @return the type of the association end.
     */
    public static Class<?> getEmbeddedValueType(final Class<?> ownerType, final String name)
    {
        return getPropertyType(embeddedValuesByType, ownerType, name);
    }

    /**
     * Gets the type of the embedded value given the <code>ownerType</code>
     * and <code>name</code>
     *
     * @param properties Map contains the Collection of properties given the ownerType Class
     * @param ownerType the owner of the association.
     * @param name the name of the association end to find.
     * @return the type of the association end.
     */
    public static Class<?> getPropertyType(final Map<Class<?>,Collection<PropertyType>> properties, final Class<?> ownerType, final String name)
    {
        final Collection<PropertyType> ends = properties.get(ownerType);
        final PropertyType type = (PropertyType)CollectionUtils.find(
            ends,
            new Predicate()
            {
                public boolean evaluate(final Object object)
                {
                    return ((PropertyType)object).name.equals(name);
                }
            });
        return type != null ? type.type : null;
    }

    /**
     * Gets the type of the property given the <code>ownerType</code>
     * and <code>name</code>
     *
     * @param ownerType the owner of the association.
     * @param name the name of the association end to find.
     * @return the type of the association end.
     */
    public static Class<?> getPropertyType(final Class<?> ownerType, final String name)
    {
        Class<?> type = getNavigableAssociationEndType(ownerType, name);
        if (type == null)
        {
            type = getEmbeddedValueType(ownerType, name);
        }
        if (type == null)
        {
            try 
            {
                Method getter;
                try
                {
                    getter=ownerType.getMethod("get"+StringUtils.capitalize(name), (Class[])null);
                } catch (NoSuchMethodException e) {
                    getter=ownerType.getMethod("is"+StringUtils.capitalize(name), (Class[])null);
                }
                type = getter.getReturnType();
            } catch (SecurityException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException("No accessible property named '" + name + "', exists on: " + ownerType.getName()); 
            }
        }
        return type;
    }

    /**
     * A private class storing the name and type.
     */
    protected static final class PropertyType
    {
        /**
         * @param nameIn
         * @param typeIn
         */
        protected PropertyType(final String nameIn, final Class<?> typeIn)
        {
            this.name = nameIn;
            this.type = typeIn;
        }

        /** PropertyType name */
        protected String name;
        /** PropertyType type Class */
        protected Class<?> type;
    }
}