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