001package org.andromda.cartridges.hibernate.metafacades;
002
003import org.andromda.utils.JavaTypeConverter;
004import org.apache.commons.lang.StringUtils;
005
006
007/**
008 * MetafacadeLogic implementation for
009 * org.andromda.cartridges.hibernate.metafacades.HibernateEnumeration.
010 *
011 * @see org.andromda.cartridges.hibernate.metafacades.HibernateEnumeration
012 */
013public class HibernateEnumerationLogicImpl
014    extends HibernateEnumerationLogic
015{
016    private static final long serialVersionUID = 34L;
017    // ---------------- constructor -------------------------------
018    /**
019     * @param metaObject
020     * @param context
021     */
022    public HibernateEnumerationLogicImpl(
023        Object metaObject,
024        String context)
025    {
026        super(metaObject, context);
027    }
028
029    /**
030     * The pattern to use when constructing the enumeration name.
031     */
032    private static final String ENUMERATION_NAME_PATTERN = "enumerationNamePattern";
033
034    /**
035     * Returns the value of the enumeration name pattern.
036     *
037     * @return the enumeration name pattern.
038     */
039    private String getEnumerationNamePattern()
040    {
041        return String.valueOf(this.getConfiguredProperty(ENUMERATION_NAME_PATTERN));
042    }
043
044    /**
045     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEnumeration#getFullyQualifiedHibernateType()
046     */
047    @Override
048    protected String handleGetFullyQualifiedHibernateType()
049    {
050        return JavaTypeConverter.getJavaLangTypeName(super.getFullyQualifiedName());
051    }
052
053    /**
054     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEnumeration#getEnumerationName()
055     */
056    @Override
057    protected String handleGetEnumerationName()
058    {
059        return StringUtils.trimToEmpty(this.getEnumerationNamePattern()).replaceAll(
060            "\\{0\\}",
061            super.getName());
062    }
063
064    /**
065     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEnumeration#getFullyQualifiedHibernateEnumerationType()
066     */
067    @Override
068    protected String handleGetFullyQualifiedHibernateEnumerationType()
069    {
070        return HibernateMetafacadeUtils.getFullyQualifiedName(
071            this.getHibernateEnumerationPackageName(),
072            this.getEnumerationName(),
073            null);
074    }
075
076    /**
077     * Check for optional defined enumTypesPackage (overrides modeled package name)
078     */
079    private String getHibernateEnumerationPackageName() {
080        String enumPackageName = (String) this.getConfiguredProperty(HibernateGlobals.ENUM_TYPES_PACKAGE);
081        if (enumPackageName != null) {
082            return enumPackageName;
083        } else {
084            return super.getPackageName();
085        }
086    }
087
088    /**
089     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEnumeration#getVersion()
090     */
091    @Override
092    protected int handleGetVersion()
093    {
094        return Integer.parseInt(((String)this.getConfiguredProperty(HibernateGlobals.HIBERNATE_VERSION)).substring(0, 1));
095    }
096}