001: package org.andromda.metafacades.emf.uml2;
002:
003: import java.util.Collection;
004:
005: import org.andromda.metafacades.uml.AttributeFacade;
006: import org.andromda.metafacades.uml.ClassifierFacade;
007: import org.andromda.metafacades.uml.Entity;
008: import org.andromda.metafacades.uml.EntityAttribute;
009: import org.andromda.metafacades.uml.EntityMetafacadeUtils;
010: import org.andromda.metafacades.uml.NameMasker;
011: import org.andromda.metafacades.uml.TypeMappings;
012: import org.andromda.metafacades.uml.UMLMetafacadeProperties;
013: import org.andromda.metafacades.uml.UMLProfile;
014: import org.apache.commons.lang.ObjectUtils;
015: import org.apache.commons.lang.StringUtils;
016:
017: /**
018: * MetafacadeLogic implementation for
019: * org.andromda.metafacades.uml.EntityAssociationEnd.
020: *
021: * @see org.andromda.metafacades.uml.EntityAssociationEnd
022: */
023: public class EntityAssociationEndLogicImpl extends
024: EntityAssociationEndLogic {
025: public EntityAssociationEndLogicImpl(final Object metaObject,
026: final String context) {
027: super (metaObject, context);
028: }
029:
030: /**
031: * Overridden to provide name masking.
032: *
033: * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
034: */
035: protected String handleGetName() {
036: final String nameMask = String
037: .valueOf(this
038: .getConfiguredProperty(UMLMetafacadeProperties.ENTITY_PROPERTY_NAME_MASK));
039: return NameMasker.mask(super .handleGetName(), nameMask);
040: }
041:
042: /**
043: * @see org.andromda.metafacades.uml.EntityAssociationEnd#getColumnName()
044: */
045: protected java.lang.String handleGetColumnName() {
046: String columnName = null;
047:
048: // prevent ClassCastException if the association isn't an Entity
049: if (this .getType() instanceof Entity) {
050: final String columnNamePrefix = this
051: .isConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX) ? ObjectUtils
052: .toString(this
053: .getConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX))
054: : null;
055: columnName = EntityMetafacadeUtils
056: .getSqlNameFromTaggedValue(
057: columnNamePrefix,
058: this ,
059: UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN,
060: ((Entity) this .getType())
061: .getMaxSqlNameLength(),
062: this .getForeignKeySuffix(),
063: this
064: .getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR));
065: }
066: return columnName;
067: }
068:
069: /**
070: * @see org.andromda.metafacades.uml.EntityAssociationEnd#getForeignKeySuffix()
071: */
072: protected java.lang.String handleGetForeignKeySuffix() {
073: return (String) this
074: .getConfiguredProperty(UMLMetafacadeProperties.FOREIGN_KEY_SUFFIX);
075: }
076:
077: /**
078: * @see org.andromda.metafacades.uml.EntityAssociationEnd#isForeignIdentifier()
079: */
080: protected boolean handleIsForeignIdentifier() {
081: final Object value = this
082: .findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_FOREIGN_IDENTIFIER);
083: return value != null
084: && Boolean.valueOf(String.valueOf(value))
085: .booleanValue();
086: }
087:
088: /**
089: * @see org.andromda.metafacades.uml.EntityAssociationEnd#getForeignKeyConstraintName()
090: */
091: protected java.lang.String handleGetForeignKeyConstraintName() {
092: String constraintName;
093:
094: final Object taggedValueObject = findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_FOREIGN_KEY_CONSTRAINT_NAME);
095: if (taggedValueObject == null) {
096: // we construct our own foreign key constraint name here
097: StringBuffer buffer = new StringBuffer();
098:
099: final ClassifierFacade type = getOtherEnd().getType();
100: if (type instanceof Entity) {
101: Entity entity = (Entity) type;
102: buffer.append(entity.getTableName());
103: } else {
104: // should not happen
105: buffer.append(type.getName().toUpperCase());
106: }
107:
108: buffer
109: .append(this
110: .getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR));
111: buffer.append(this .getColumnName());
112: constraintName = buffer.toString();
113:
114: final String suffix = ObjectUtils
115: .toString(
116: this
117: .getConfiguredProperty(UMLMetafacadeProperties.CONSTRAINT_SUFFIX))
118: .trim();
119: // we take into consideration the maximum length allowed
120: final String maxLengthString = (String) getConfiguredProperty(UMLMetafacadeProperties.MAX_SQL_NAME_LENGTH);
121: final short maxLength = (short) (Short.valueOf(
122: maxLengthString).shortValue() - suffix.length());
123: buffer = new StringBuffer(EntityMetafacadeUtils
124: .ensureMaximumNameLength(constraintName, new Short(
125: maxLength)));
126: buffer.append(suffix);
127: constraintName = buffer.toString();
128: } else {
129: // use the tagged value
130: constraintName = taggedValueObject.toString();
131: }
132: return constraintName;
133: }
134:
135: /**
136: * @see org.andromda.metafacades.uml.EntityAssociationEnd#getColumnIndex()
137: */
138: protected java.lang.String handleGetColumnIndex() {
139: final String index = (String) this
140: .findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_INDEX);
141: return index != null ? StringUtils.trimToEmpty(index) : null;
142: }
143:
144: /**
145: * @see org.andromda.metafacades.uml.EntityAssociationEnd#getSqlType()
146: */
147: protected java.lang.String handleGetSqlType() {
148: String value = null;
149: if (this .getSqlMappings() != null) {
150: EntityAttribute identifier = null;
151:
152: // we retrieve the column length from the first identifier of the
153: // primary key
154: // on the other side (since that should correspond to the foreign
155: // key).
156: if (this .getType() instanceof Entity) {
157: final Entity type = (Entity) this .getType();
158: final Collection identifiers = type.getIdentifiers();
159: if (identifiers != null && !identifiers.isEmpty()) {
160: AttributeFacade attribute = (AttributeFacade) identifiers
161: .iterator().next();
162: if (attribute instanceof EntityAttribute) {
163: identifier = (EntityAttribute) attribute;
164: }
165: }
166: }
167: if (identifier != null && identifier.getType() != null) {
168: String typeName = identifier.getType()
169: .getFullyQualifiedName(true);
170: value = this .getSqlMappings().getTo(typeName);
171: final String columnLength = identifier
172: .getColumnLength();
173: if (StringUtils.isNotEmpty(columnLength)) {
174: value = EntityMetafacadeUtils.constructSqlTypeName(
175: value, columnLength);
176: }
177: }
178: }
179: return value;
180: }
181:
182: /**
183: * Gets the SQL mappings that have been set for this entity attribute.
184: *
185: * @return the SQL Mappings instance.
186: */
187: public TypeMappings getSqlMappings() {
188: final String propertyName = UMLMetafacadeProperties.SQL_MAPPINGS_URI;
189: final Object property = this
190: .getConfiguredProperty(propertyName);
191: TypeMappings mappings = null;
192: String uri;
193: if (property instanceof String) {
194: uri = (String) property;
195: try {
196: mappings = TypeMappings.getInstance(uri);
197: this .setProperty(propertyName, mappings);
198: } catch (Throwable throwable) {
199: String errMsg = "Error getting '" + propertyName
200: + "' --> '" + uri + "'";
201: this .logger.error(errMsg, throwable);
202:
203: // don't throw the exception
204: }
205: } else {
206: mappings = (TypeMappings) property;
207: }
208: return mappings;
209: }
210:
211: protected boolean handleIsTransient() {
212: return this .hasStereotype(UMLProfile.STEREOTYPE_TRANSIENT);
213: }
214:
215: /**
216: * @see org.andromda.metafacades.uml.EntityAssociationEnd#isIdentifiersPresent()
217: */
218: protected boolean handleIsIdentifiersPresent() {
219: return this.hasStereotype(UMLProfile.STEREOTYPE_IDENTIFIER);
220: }
221: }
|