001: package org.andromda.metafacades.uml14;
002:
003: import org.andromda.metafacades.uml.ClassifierFacade;
004: import org.andromda.metafacades.uml.EntityMetafacadeUtils;
005: import org.andromda.metafacades.uml.EnumerationFacade;
006: import org.andromda.metafacades.uml.NameMasker;
007: import org.andromda.metafacades.uml.TypeMappings;
008: import org.andromda.metafacades.uml.UMLMetafacadeProperties;
009: import org.andromda.metafacades.uml.UMLProfile;
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.commons.lang.ObjectUtils;
012:
013: /**
014: * Metaclass facade implementation.
015: */
016: public class EntityAttributeLogicImpl extends EntityAttributeLogic {
017:
018: public EntityAttributeLogicImpl(java.lang.Object metaObject,
019: String context) {
020: super (metaObject, context);
021: }
022:
023: /**
024: * Overridden to provide name masking.
025: *
026: * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
027: */
028: protected String handleGetName() {
029: final String nameMask = String
030: .valueOf(this
031: .getConfiguredProperty(UMLMetafacadeProperties.ENTITY_PROPERTY_NAME_MASK));
032: return NameMasker.mask(super .handleGetName(), nameMask);
033: }
034:
035: /**
036: * @see org.andromda.metafacades.uml.EntityAttribute#getColumnName()
037: */
038: protected String handleGetColumnName() {
039: final Short maxSqlNameLength = Short
040: .valueOf((String) this
041: .getConfiguredProperty(UMLMetafacadeProperties.MAX_SQL_NAME_LENGTH));
042: final String columnNamePrefix = this
043: .isConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX) ? ObjectUtils
044: .toString(this
045: .getConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX))
046: : null;
047: return EntityMetafacadeUtils
048: .getSqlNameFromTaggedValue(
049: columnNamePrefix,
050: this ,
051: UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN,
052: maxSqlNameLength,
053: this
054: .getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR));
055: }
056:
057: /**
058: * @see org.andromda.metafacades.uml.EntityAttribute#getColumnLength()
059: */
060: protected String handleGetColumnLength() {
061: return (String) this
062: .findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_LENGTH);
063: }
064:
065: /**
066: * @see org.andromda.metafacades.uml.EntityAttribute#isIdentifier()
067: */
068: protected boolean handleIsIdentifier() {
069: return this .hasStereotype(UMLProfile.STEREOTYPE_IDENTIFIER);
070: }
071:
072: /**
073: * @see org.andromda.metafacades.uml.EntityAttribute#isUnique()
074: */
075: protected boolean handleIsUnique() {
076: return this .hasStereotype(UMLProfile.STEREOTYPE_UNIQUE);
077: }
078:
079: /**
080: * @see org.andromda.metafacades.uml.EntityAttribute#getColumnIndex()
081: */
082: protected java.lang.String handleGetColumnIndex() {
083: final String index = (String) this
084: .findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_INDEX);
085: return index != null ? StringUtils.trimToEmpty(index) : null;
086: }
087:
088: /**
089: * @see org.andromda.metafacades.uml.EntityAttribute#getSqlType()
090: */
091: protected java.lang.String handleGetSqlType() {
092: String value = null;
093: if (this .getSqlMappings() != null) {
094: final ClassifierFacade type = this .getType();
095: if (type != null) {
096: String typeName = type.getFullyQualifiedName(true);
097: // if its an enumeration, the sql type is the literal type
098: if (type.isEnumeration()) {
099: ClassifierFacade literalType = ((EnumerationFacade) type)
100: .getLiteralType();
101: if (literalType != null) {
102: typeName = literalType
103: .getFullyQualifiedName(true);
104: }
105: }
106: value = this .getSqlMappings().getTo(typeName);
107: final String columnLength = this .getColumnLength();
108: if (StringUtils.isNotEmpty(columnLength)) {
109: value = EntityMetafacadeUtils.constructSqlTypeName(
110: value, columnLength);
111: }
112: }
113: }
114: return value;
115: }
116:
117: /**
118: * @see org.andromda.metafacades.uml.EntityAttribute#getJdbcType()
119: */
120: protected java.lang.String handleGetJdbcType() {
121: String value = null;
122: if (this .getJdbcMappings() != null) {
123: final ClassifierFacade type = this .getType();
124: if (type != null) {
125: final String typeName = type
126: .getFullyQualifiedName(true);
127: value = this .getJdbcMappings().getTo(typeName);
128: }
129: }
130:
131: return value;
132: }
133:
134: /**
135: * Gets the SQL mappings that have been set for this entity attribute.
136: *
137: * @return the SQL Mappings instance.
138: */
139: protected TypeMappings handleGetSqlMappings() {
140: return this
141: .getMappingsProperty(UMLMetafacadeProperties.SQL_MAPPINGS_URI);
142: }
143:
144: /**
145: * Gets the JDBC mappings.
146: */
147: protected TypeMappings handleGetJdbcMappings() {
148: return this
149: .getMappingsProperty(UMLMetafacadeProperties.JDBC_MAPPINGS_URI);
150: }
151:
152: /**
153: * Gets a Mappings instance from a property registered under the given <code>propertyName</code>.
154: *
155: * @param propertyName the property name to register under.
156: * @return the Mappings instance.
157: */
158: private TypeMappings getMappingsProperty(final String propertyName) {
159: final Object property = this
160: .getConfiguredProperty(propertyName);
161: TypeMappings mappings = null;
162: String uri;
163: if (property instanceof String) {
164: uri = (String) property;
165: try {
166: mappings = TypeMappings.getInstance(uri);
167: this .setProperty(propertyName, mappings);
168: } catch (Throwable th) {
169: String errMsg = "Error getting '" + propertyName
170: + "' --> '" + uri + "'";
171: logger.error(errMsg, th);
172: // don't throw the exception
173: }
174: } else {
175: mappings = (TypeMappings) property;
176: }
177: return mappings;
178: }
179:
180: protected boolean handleIsTransient() {
181: return this .hasStereotype(UMLProfile.STEREOTYPE_TRANSIENT);
182: }
183:
184: /**
185: * @see org.andromda.metafacades.uml.EntityAttribute#getUniqueGroup()
186: */
187: protected String handleGetUniqueGroup() {
188: final String group = (String) this
189: .findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_UNIQUE_GROUP);
190: return group != null ? StringUtils.trimToEmpty(group) : null;
191: }
192: }
|