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