001package org.andromda.cartridges.hibernate.metafacades;
002
003import org.andromda.cartridges.hibernate.HibernateProfile;
004import org.andromda.cartridges.hibernate.HibernateUtils;
005import org.andromda.metafacades.uml.ClassifierFacade;
006import org.andromda.metafacades.uml.EntityMetafacadeUtils;
007import org.andromda.metafacades.uml.NameMasker;
008import org.andromda.metafacades.uml.TypeMappings;
009import org.andromda.metafacades.uml.UMLMetafacadeProperties;
010import org.andromda.utils.JavaTypeConverter;
011import org.apache.commons.lang.StringUtils;
012
013
014/**
015 * MetafacadeLogic implementation for
016 * org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute.
017 *
018 * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute
019 * @author Bob Fields
020 */
021public class HibernateEntityAttributeLogicImpl
022    extends HibernateEntityAttributeLogic
023{
024    private static final long serialVersionUID = 34L;
025    /**
026     * @param metaObject
027     * @param context
028     */
029    // ---------------- constructor -------------------------------
030    public HibernateEntityAttributeLogicImpl(
031        Object metaObject,
032        String context)
033    {
034        super(metaObject, context);
035    }
036
037    /**
038     * Overridden to provide handling of inheritance.
039     *
040     * @see org.andromda.metafacades.uml.AttributeFacade#isRequired()
041     */
042    @Override
043    public boolean isRequired()
044    {
045        boolean required = super.isRequired();
046        if (this.getOwner() instanceof HibernateEntity)
047        {
048            HibernateEntity entity = (HibernateEntity)this.getOwner();
049            if (entity.isHibernateInheritanceClass() && entity.getGeneralization() != null)
050            {
051                required = false;
052            }
053        }
054        return required;
055    }
056
057    /**
058     * Override to provide java specific handling of the default value.
059     *
060     * @see org.andromda.metafacades.uml.AttributeFacade#getDefaultValue()
061     */
062    @Override
063    public String getDefaultValue()
064    {
065        String defaultValue = super.getDefaultValue();
066        final ClassifierFacade type = this.getType();
067        if (type != null)
068        {
069            final String fullyQualifiedName = StringUtils.trimToEmpty(type.getFullyQualifiedName());
070            if (("java.lang.String".equals(fullyQualifiedName) || "String".equals(fullyQualifiedName)) && fullyQualifiedName.indexOf('"')<0)
071            {
072                defaultValue = '\"' + defaultValue.replaceAll("\"", "") + '\"';
073            }
074            else if (fullyQualifiedName.startsWith("java.lang"))
075            {
076                defaultValue = fullyQualifiedName + ".valueOf(" + defaultValue + ')';
077            }
078            else if (type.isEnumeration())
079            {
080                final String mask = (String)this.getConfiguredProperty(UMLMetafacadeProperties.ENUMERATION_LITERAL_NAME_MASK);
081                defaultValue = type.getFullyQualifiedName() + '.' + NameMasker.mask(defaultValue, mask);
082            }
083        }
084        return defaultValue;
085    }
086
087    /**
088     * @return returnValue = true if this.getType() instanceof HibernateEmbeddedValue
089     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isContainsEmbeddedObject()
090     */
091    @Override
092    protected boolean handleIsContainsEmbeddedObject()
093    {
094        boolean returnValue = false;
095        if (this.getType() instanceof HibernateEmbeddedValue)
096        {
097            returnValue = true;
098        }
099        return returnValue;
100    }
101
102    /**
103     * @param prefix
104     * @param name
105     * @return returnValue prefix + this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR) + name
106     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#concatColumnName(String, String)
107     */
108    @Override
109    protected String handleConcatColumnName(
110        String prefix,
111        String name)
112    {
113        String returnValue = name;
114        if (StringUtils.isNotBlank(prefix)) 
115        {
116            returnValue = prefix + this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR) + name;
117
118            // handle maxSqlNameLength
119            Short maxSqlNameLength =
120                Short.valueOf((String)this.getConfiguredProperty(UMLMetafacadeProperties.MAX_SQL_NAME_LENGTH));
121            final String method = (String)super.getConfiguredProperty(UMLMetafacadeProperties.SHORTEN_SQL_NAMES_METHOD);
122            returnValue = EntityMetafacadeUtils.ensureMaximumNameLength(returnValue, maxSqlNameLength,method);
123        }
124        return returnValue;
125    }
126
127    /**
128     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isLazy()
129     */
130    @Override
131    protected boolean handleIsLazy()
132    {
133        final String value = (String)findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_LAZY);
134        return StringUtils.isNotBlank(value) ? Boolean.valueOf(value).booleanValue() : false;
135    }
136
137    /**
138     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#getFormula()
139     */
140    @Override
141    protected String handleGetFormula()
142    {
143        return (String)findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_FORMULA);
144    }
145
146    /**
147     * @return HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_INSERT
148     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isInsertEnabled()
149     */
150    @Override
151    protected boolean handleIsInsertEnabled()
152    {
153        final String value = (String)findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_INSERT);
154        return StringUtils.isNotBlank(value) ? Boolean.valueOf(value).booleanValue() : true;
155    }
156
157    /**
158     * @return HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_UPDATE
159     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isUpdateEnabled()
160     */
161    @Override
162    protected boolean handleIsUpdateEnabled()
163    {
164        final String value = (String)findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_UPDATE);
165        return StringUtils.isNotBlank(value) ? Boolean.valueOf(value).booleanValue() : true;
166    }
167
168    private boolean isXmlPersistenceActive()
169    {
170       return HibernateUtils.isXmlPersistenceActive((String)this.getConfiguredProperty(HibernateGlobals.HIBERNATE_VERSION),
171                                                    (String)this.getConfiguredProperty(HibernateGlobals.HIBERNATE_XML_PERSISTENCE));
172    }
173
174    private boolean persistIDAsAttribute()
175    {
176        boolean persistAsAttribute = true;
177        String prop = (String)this.getConfiguredProperty(HibernateGlobals.HIBERNATE_XML_PERSISTENCE_ID_AS_ATTRIBUTE);
178        if (prop != null && "false".equalsIgnoreCase(prop))
179            persistAsAttribute = false;
180
181        return persistAsAttribute;
182    }
183
184    /**
185     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttributeLogic#handleGetXmlTagName()
186     */
187    @Override
188    protected String handleGetXmlTagName()
189    {
190        String tagName = null;
191
192        if (isXmlPersistenceActive())
193        {
194            tagName = (String)this.findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_XML_TAG_NAME);
195
196            if (StringUtils.isBlank(tagName))
197            {
198                if (this.isIdentifier() && this.persistIDAsAttribute())
199                    tagName = '@' + this.getName();
200                else
201                    tagName = this.getName();
202            }
203
204        }
205        return (StringUtils.isBlank(tagName)) ? null : tagName;
206    }
207
208    /**
209     * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttributeLogic#handleGetFullyQualifiedHibernateType()
210     */
211    @Override
212    protected String handleGetFullyQualifiedHibernateType()
213    {
214        String fullyQualifiedName;
215
216        if (this.getType() != null && this.getType().isEnumeration())
217        {
218            fullyQualifiedName = "org.andromda.persistence.hibernate.usertypes.HibernateEnumType";
219        }
220        else
221        {
222            final String hibernateTypeMappingsUri = (String)this.getConfiguredProperty("hibernateTypeMappingsUri");
223            final TypeMappings mappings = TypeMappings.getInstance(hibernateTypeMappingsUri);
224
225            if (this.getType() != null && mappings == null)
226            {
227                fullyQualifiedName = this.getType().getFullyQualifiedName();
228            }
229            else
230            {
231                final String fullyQualifiedModelName = this.getType().getFullyQualifiedName(true);
232                if (mappings.getMappings().containsFrom(fullyQualifiedModelName))
233                {
234                    fullyQualifiedName = mappings.getTo(fullyQualifiedModelName);
235                }
236                else
237                {
238                    fullyQualifiedName = this.getType().getFullyQualifiedName();
239                }
240            }
241            fullyQualifiedName = JavaTypeConverter.getJavaLangTypeName(fullyQualifiedName);
242        }
243
244        return fullyQualifiedName;
245    }
246}