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