View Javadoc
1   package org.andromda.metafacades.uml14;
2   
3   import java.util.Collection;
4   import org.andromda.metafacades.uml.Entity;
5   import org.andromda.metafacades.uml.EntityAssociationEnd;
6   import org.andromda.metafacades.uml.EntityAttribute;
7   import org.andromda.metafacades.uml.EntityMetafacadeUtils;
8   import org.andromda.metafacades.uml.ModelElementFacade;
9   import org.andromda.metafacades.uml.NameMasker;
10  import org.andromda.metafacades.uml.TypeMappings;
11  import org.andromda.metafacades.uml.UMLMetafacadeProperties;
12  import org.andromda.metafacades.uml.UMLProfile;
13  import org.apache.commons.lang.ObjectUtils;
14  import org.apache.commons.lang.StringUtils;
15  import org.apache.log4j.Logger;
16  
17  /**
18   * Represents an association end of an entity.
19   * Metaclass facade implementation.
20   * @author Bob Fields
21   */
22  public class EntityAssociationEndLogicImpl
23          extends EntityAssociationEndLogic
24  {
25      private static final long serialVersionUID = 34L;
26      /**
27       * @param metaObject
28       * @param context
29       */
30      public EntityAssociationEndLogicImpl(Object metaObject, String context)
31      {
32          super(metaObject, context);
33      }
34  
35      /**
36       * Overridden to provide name masking.
37       *
38       * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
39       */
40      @Override
41      protected String handleGetName()
42      {
43          final String nameMask = String.valueOf(
44              this.getConfiguredProperty(UMLMetafacadeProperties.ENTITY_PROPERTY_NAME_MASK));
45          return NameMasker.mask(super.handleGetName(), nameMask);
46      }
47  
48      /**
49       * The logger instance.
50       */
51      private static final Logger logger = Logger.getLogger(EntityAssociationEndLogicImpl.class);
52  
53      /**
54       * @see org.andromda.metafacades.uml.EntityAssociationEnd#getColumnName()
55       */
56      @Override
57      public String handleGetColumnName()
58      {
59          String columnName = null;
60          // prevent ClassCastException if the association isn't an Entity
61          if (this.getType() instanceof Entity)
62          {
63              final String columnNamePrefix =
64                  this.isConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX)
65                  ? ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX)) : null;
66              final String columnNameSuffix =
67                  this.isConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_SUFFIX)
68                  ? ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_SUFFIX)) : "";
69              columnName =
70                  EntityMetafacadeUtils.getSqlNameFromTaggedValue(
71                      columnNamePrefix,
72                      this,
73                      UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN,
74                      ((Entity)this.getType()).getMaxSqlNameLength(),
75                      columnNameSuffix + this.getForeignKeySuffix(),
76                      this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR),
77                      this.getConfiguredProperty(UMLMetafacadeProperties.SHORTEN_SQL_NAMES_METHOD));
78          }
79          return columnName;
80      }
81  
82      /**
83       * @see org.andromda.metafacades.uml.EntityAssociationEnd#getForeignKeySuffix()
84       */
85      @Override
86      public String handleGetForeignKeySuffix()
87      {
88          return (String)this.getConfiguredProperty(UMLMetafacadeProperties.FOREIGN_KEY_SUFFIX);
89      }
90  
91      /**
92       * @see org.andromda.metafacades.uml.EntityAssociationEnd#isForeignIdentifier()
93       */
94      @Override
95      protected boolean handleIsForeignIdentifier()
96      {
97          final Object value = this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_FOREIGN_IDENTIFIER);
98          return value != null && Boolean.valueOf(String.valueOf(value));
99      }
100 
101     /**
102      * @see org.andromda.metafacades.uml.EntityAssociationEnd#getForeignKeyConstraintName()
103      */
104     @Override
105     protected String handleGetForeignKeyConstraintName()
106     {
107         return EntityMetafacadeUtils.getForeignKeyConstraintName(
108             (EntityAssociationEnd)THIS(),
109             ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.CONSTRAINT_SUFFIX)).trim(),
110             ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR)).trim(),
111             ObjectUtils.toString(getConfiguredProperty(UMLMetafacadeProperties.MAX_SQL_NAME_LENGTH)).trim(),
112             ObjectUtils.toString(getConfiguredProperty(UMLMetafacadeProperties.SHORTEN_SQL_NAMES_METHOD)).trim());
113     }
114 
115     /**
116      * @see org.andromda.metafacades.uml.EntityAssociationEnd#getColumnIndex()
117      */
118     @Override
119     public String handleGetColumnIndex()
120     {
121         final String index = (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_INDEX);
122         return index != null ? StringUtils.trimToEmpty(index) : null;
123     }
124 
125     /**
126      * @see org.andromda.metafacades.uml14.EntityAssociationEndLogic#handleGetSqlType()
127      */
128     protected String handleGetSqlType()
129     {
130         String value = null;
131         if (this.getSqlMappings() != null)
132         {
133             EntityAttribute identifier = null;
134             // we retrieve the column length from the first identifier of the primary key
135             // on the other side (since that should correspond to the foreign key).
136             if (this.getType() instanceof Entity)
137             {
138                 final Entity type = (Entity)this.getType();
139                 final Collection<ModelElementFacade> identifiers = type.getIdentifiers();
140                 if (identifiers != null && !identifiers.isEmpty())
141                 {
142                     ModelElementFacade attribute = identifiers.iterator().next();
143                     if (attribute instanceof EntityAttribute)
144                     {
145                         identifier = (EntityAttribute)attribute;
146                     }
147                 }
148             }
149             if (identifier != null && identifier.getType() != null)
150             {
151                 String typeName = identifier.getType().getFullyQualifiedName(true);
152                 value = this.getSqlMappings().getTo(typeName);
153                 final String columnLength = identifier.getColumnLength();
154                 if (StringUtils.isNotBlank(columnLength))
155                 {
156                     value = EntityMetafacadeUtils.constructSqlTypeName(value, columnLength);
157                 }
158             }
159         }
160         return value;
161     }
162 
163     /**
164      * Gets the SQL mappings that have been set for this entity attribute.
165      *
166      * @return the SQL Mappings instance.
167      */
168     public TypeMappings getSqlMappings()
169     {
170         final String propertyName = UMLMetafacadeProperties.SQL_MAPPINGS_URI;
171         final Object property = this.getConfiguredProperty(propertyName);
172         TypeMappings mappings = null;
173         String uri;
174         if (property instanceof String)
175         {
176             uri = (String)property;
177             try
178             {
179                 mappings = TypeMappings.getInstance(uri);
180                 this.setProperty(propertyName, mappings);
181             }
182             catch (final Throwable throwable)
183             {
184                 logger.error("Error getting '" + propertyName + "' --> '" + uri + '\'', throwable);
185                 // don't throw the exception
186             }
187         }
188         else
189         {
190             mappings = (TypeMappings)property;
191         }
192         return mappings;
193     }
194 
195     /**
196      * @see org.andromda.metafacades.uml14.EntityAssociationEndLogic#handleIsTransient()
197      */
198     protected boolean handleIsTransient()
199     {
200         return this.hasStereotype(UMLProfile.STEREOTYPE_TRANSIENT);
201     }
202 
203     /**
204      * @see org.andromda.metafacades.uml.EntityAssociationEnd#isIdentifiersPresent()
205      */
206     @Override
207     protected boolean handleIsIdentifiersPresent() {
208         return this.hasStereotype(UMLProfile.STEREOTYPE_IDENTIFIER);
209     }
210 
211     /**
212      * @return findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_ASSOCIATION_END_UNIQUE_GROUP)
213      * @see org.andromda.metafacades.uml.EntityAssociationEnd#getUniqueGroup()
214      */
215     //@Override
216     protected String handleGetUniqueGroup() {
217         final String group = (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_ASSOCIATION_END_UNIQUE_GROUP);
218         return group != null ? StringUtils.trimToEmpty(group) : null;
219     }
220 
221     /**
222      * @see org.andromda.metafacades.uml.EntityAssociationEnd#isIdentifier()
223      */
224     @Override
225     protected boolean handleIsIdentifier()
226     {
227         return this.hasStereotype(UMLProfile.STEREOTYPE_IDENTIFIER);
228     }
229 }