View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   import org.andromda.core.metafacade.MetafacadeImplsException;
6   import org.andromda.metafacades.uml.AssociationEndFacade;
7   import org.andromda.metafacades.uml.ClassifierFacade;
8   import org.andromda.metafacades.uml.Entity;
9   import org.andromda.metafacades.uml.EntityMetafacadeUtils;
10  import org.andromda.metafacades.uml.UMLMetafacadeProperties;
11  import org.andromda.metafacades.uml.UMLProfile;
12  import org.apache.commons.lang.ObjectUtils;
13  import org.apache.commons.lang.StringUtils;
14  import org.eclipse.uml2.uml.Association;
15  import org.eclipse.uml2.uml.Property;
16  import org.eclipse.uml2.uml.Type;
17  
18  /**
19   * MetafacadeLogic implementation for
20   * org.andromda.metafacades.uml.EntityAssociation.
21   *
22   * @see org.andromda.metafacades.uml.EntityAssociation
23   */
24  public class EntityAssociationLogicImpl
25      extends EntityAssociationLogic
26  {
27      private static final long serialVersionUID = -5313785900247499346L;
28  
29      /**
30       * @param metaObject
31       * @param context
32       */
33      public EntityAssociationLogicImpl(
34          final Object metaObject,
35          final String context)
36      {
37          super(metaObject, context);
38      }
39  
40      /**
41       * @see org.andromda.metafacades.uml.EntityAssociation#getTableName()
42       */
43      @Override
44      protected String handleGetTableName()
45      {
46          String tableName = null;
47          final Collection<AssociationEndFacade> ends = this.getAssociationEnds();
48          if (ends != null && !ends.isEmpty())
49          {
50              final AssociationEndFacade end = ends.iterator().next();
51              final ClassifierFacade type = end.getType();
52              // - prevent ClassCastException if the association isn't an Entity
53              if (type != null && type instanceof Entity)
54              {
55                  final String prefixProperty = UMLMetafacadeProperties.TABLE_NAME_PREFIX;
56                  final String tableNamePrefix =
57                      this.isConfiguredProperty(prefixProperty)
58                      ? ObjectUtils.toString(this.getConfiguredProperty(prefixProperty)) : null;
59                  tableName =
60                      EntityMetafacadeUtils.getSqlNameFromTaggedValue(
61                          tableNamePrefix,
62                          this,
63                          UMLProfile.TAGGEDVALUE_PERSISTENCE_TABLE,
64                          Short.valueOf(((Entity)type).getMaxSqlNameLength()),
65                          this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR),
66                          this.getConfiguredProperty(UMLMetafacadeProperties.SHORTEN_SQL_NAMES_METHOD));
67              }
68          }
69          return tableName;
70      }
71  
72      /**
73       * @see org.andromda.metafacades.uml.EntityAssociation#getSchema()
74       */
75      // TODO: Duplicated method from ancestor
76      @Override
77      protected String handleGetSchema()
78      {
79          String schemaName = ObjectUtils.toString(this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_SCHEMA));
80          if (StringUtils.isBlank(schemaName))
81          {
82              schemaName = ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.SCHEMA_NAME));
83          }
84          return schemaName;
85      }
86  
87      /**
88       * It is an entity association if both ends are entities (have the entity stereotype)
89       * @return boolean isEntityAssociation
90       */
91      @Override
92      protected boolean handleIsEntityAssociation()
93      {
94          // TODO: There may be a better implementation
95          // But it has to be tested (it may cause a stack overflow.
96          // return (getAssociationEndA().getType() instanceof Entity) &&
97          // (getAssociationEndB().getType() instanceof Entity);
98          if (this.metaObject == null || !(this.metaObject instanceof Association))
99          {
100             throw new MetafacadeImplsException("Incorrect metafacade mapping for " + this.toString());
101         }
102         boolean isEntityAssociation = true;
103         for (final Iterator<Property> ends = ((Association)this.metaObject).getMemberEnds().iterator(); ends.hasNext();)
104         {
105             final Property prop = ends.next();
106             final Type propertyType = prop.getType();
107             if (propertyType == null || !UmlUtilities.containsStereotype(
108                     propertyType,
109                     UMLProfile.STEREOTYPE_ENTITY))
110             {
111                 isEntityAssociation = false;
112             }
113         }
114         return isEntityAssociation;
115     }
116 }