001package org.andromda.metafacades.uml14;
002
003import org.andromda.metafacades.uml.ClassifierFacade;
004import org.andromda.metafacades.uml.EntityMetafacadeUtils;
005import org.andromda.metafacades.uml.EnumerationFacade;
006import org.andromda.metafacades.uml.NameMasker;
007import org.andromda.metafacades.uml.TypeMappings;
008import org.andromda.metafacades.uml.UMLMetafacadeProperties;
009import org.andromda.metafacades.uml.UMLProfile;
010import org.apache.commons.lang.ObjectUtils;
011import org.apache.commons.lang.StringUtils;
012import org.apache.log4j.Logger;
013
014/**
015 * Metaclass facade implementation.
016 * @author Bob Fields
017 */
018public class EntityAttributeLogicImpl
019        extends EntityAttributeLogic
020{
021    private static final long serialVersionUID = 34L;
022    /**
023     * @param metaObject
024     * @param context
025     */
026    public EntityAttributeLogicImpl(Object metaObject, String context)
027    {
028        super(metaObject, context);
029    }
030
031    /**
032     * The logger instance.
033     */
034    private static final Logger logger = Logger.getLogger(EntityAttributeLogicImpl.class);
035
036    /**
037     * Overridden to provide name masking.
038     *
039     * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
040     */
041    @Override
042    protected String handleGetName()
043    {
044        final String nameMask = String.valueOf(this.getConfiguredProperty(
045                UMLMetafacadeProperties.ENTITY_PROPERTY_NAME_MASK));
046        return NameMasker.mask(super.handleGetName(), nameMask);
047    }
048
049    /**
050     * @see org.andromda.metafacades.uml.EntityAttribute#getColumnName()
051     */
052    @Override
053    protected String handleGetColumnName()
054    {
055        final Short maxSqlNameLength =
056            Short.valueOf((String)this.getConfiguredProperty(UMLMetafacadeProperties.MAX_SQL_NAME_LENGTH));
057        final String columnNamePrefix =
058            this.isConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX)
059            ? ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX)) : null;
060        final String columnNameSuffix =
061            this.isConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_SUFFIX)
062            ? ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_SUFFIX)) : null;
063        return EntityMetafacadeUtils.getSqlNameFromTaggedValue(
064            columnNamePrefix, this, UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN,
065            maxSqlNameLength, columnNameSuffix,
066            this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR),
067            this.getConfiguredProperty(UMLMetafacadeProperties.SHORTEN_SQL_NAMES_METHOD));
068    }
069
070    /**
071     * @see org.andromda.metafacades.uml.EntityAttribute#getColumnLength()
072     */
073    @Override
074    protected String handleGetColumnLength()
075    {
076        return (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_LENGTH);
077    }
078
079    /**
080     * @see org.andromda.metafacades.uml.EntityAttribute#isIdentifier()
081     */
082    @Override
083    protected boolean handleIsIdentifier()
084    {
085        return this.hasStereotype(UMLProfile.STEREOTYPE_IDENTIFIER);
086    }
087
088    /*
089     * @see org.andromda.metafacades.uml.EntityAttribute#isUnique()
090    @Override
091    protected boolean handleIsUnique()
092    {
093        return this.hasStereotype(UMLProfile.STEREOTYPE_UNIQUE);
094    }
095     */
096
097    /**
098     * @see org.andromda.metafacades.uml.EntityAttribute#getColumnIndex()
099     */
100    @Override
101    protected String handleGetColumnIndex()
102    {
103        final String index = (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_INDEX);
104        return index != null ? StringUtils.trimToEmpty(index) : null;
105    }
106
107    /**
108     * @see org.andromda.metafacades.uml.EntityAttribute#getSqlType()
109     */
110    @Override
111    protected String handleGetSqlType()
112    {
113        String value = null;
114        if (this.getSqlMappings() != null)
115        {
116            final ClassifierFacade type = this.getType();
117            if (type != null)
118            {
119                String typeName = type.getFullyQualifiedName(true);
120                // if its an enumeration, the sql type is the literal type
121                if (type.isEnumeration())
122                {
123                    ClassifierFacade literalType = ((EnumerationFacade)type).getLiteralType();
124                    if (literalType != null)
125                    {
126                        typeName = literalType.getFullyQualifiedName(true);
127                    }
128                }
129                value = this.getSqlMappings().getTo(typeName);
130                final String columnLength = this.getColumnLength();
131                if (StringUtils.isNotBlank(columnLength))
132                {
133                    value = EntityMetafacadeUtils.constructSqlTypeName(value, columnLength);
134                }
135            }
136        }
137        return value;
138    }
139
140
141    /**
142     * @see org.andromda.metafacades.uml.EntityAttribute#getJdbcType()
143     */
144    @Override
145    protected String handleGetJdbcType()
146    {
147        String value = null;
148        if (this.getJdbcMappings() != null)
149        {
150            final ClassifierFacade type = this.getType();
151            if (type != null)
152            {
153                final String typeName = type.getFullyQualifiedName(true);
154                value = this.getJdbcMappings().getTo(typeName);
155            }
156        }
157
158        return value;
159    }
160
161    /**
162     * Gets the SQL mappings that have been set for this entity attribute.
163     *
164     * @return the SQL Mappings instance.
165     */
166    protected TypeMappings handleGetSqlMappings()
167    {
168        return this.getMappingsProperty(UMLMetafacadeProperties.SQL_MAPPINGS_URI);
169    }
170
171    /**
172     * Gets the JDBC mappings.
173     */
174    @Override
175    protected TypeMappings handleGetJdbcMappings()
176    {
177        return this.getMappingsProperty(UMLMetafacadeProperties.JDBC_MAPPINGS_URI);
178    }
179
180    /**
181     * Gets a Mappings instance from a property registered under the given <code>propertyName</code>.
182     *
183     * @param propertyName the property name to register under.
184     * @return the Mappings instance.
185     */
186    private TypeMappings getMappingsProperty(final String propertyName)
187    {
188        final Object property = this.getConfiguredProperty(propertyName);
189        TypeMappings mappings = null;
190        String uri;
191        if (property instanceof String)
192        {
193            uri = (String)property;
194            try
195            {
196                mappings = TypeMappings.getInstance(uri);
197                this.setProperty(propertyName, mappings);
198            }
199            catch (Throwable th)
200            {
201                String errMsg = "Error getting '" + propertyName + "' --> '" + uri + '\'';
202                logger.error(errMsg, th);
203                // don't throw the exception
204            }
205        }
206        else
207        {
208            mappings = (TypeMappings)property;
209        }
210        return mappings;
211    }
212
213    /**
214     * @see org.andromda.metafacades.uml14.EntityAttributeLogic#handleIsTransient()
215     */
216    protected boolean handleIsTransient()
217    {
218        return this.hasStereotype(UMLProfile.STEREOTYPE_TRANSIENT);
219    }
220
221    /**
222     * @return unique group
223     * @see org.andromda.metafacades.uml.EntityAttribute#getUniqueGroup()
224     */
225    protected String handleGetUniqueGroup() {
226        final String group = (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_UNIQUE_GROUP);
227        return group != null ? StringUtils.trimToEmpty(group) : null;
228    }
229
230    /**
231     * @see org.andromda.metafacades.uml.EntityAttribute#getKeywords()
232    public Collection<String> handleGetKeywords()
233    {
234        //return super.getKeywords();
235        return this.metaObject.getKeywords();
236    }
237     */
238
239    /*public String handleGetLabel()
240    {
241        return this.metaObject.getLabel();
242    }
243
244    public ModelElementFacade handleGetModelNamespace()
245    {
246        return this.metaObject.getNamespace();
247    }
248
249    public String handleGetQualifiedName()
250    {
251        return this.metaObject.getQualifiedName();
252    }*/
253
254    /**
255     * @see org.andromda.metafacades.uml.ModelElementFacade#hasKeyword(String)
256    public boolean handleHasKeyword(String keywordName)
257    {
258        return super.hasKeyword(keywordName);
259    }
260     */
261}