View Javadoc
1   package org.andromda.cartridges.ejb3.metafacades;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   import java.util.List;
6   import org.andromda.metafacades.uml.AssociationEndFacade;
7   import org.andromda.metafacades.uml.Entity;
8   import org.andromda.metafacades.uml.MetafacadeUtils;
9   import org.andromda.metafacades.uml.UMLMetafacadeProperties;
10  import org.andromda.metafacades.uml.UMLProfile;
11  import org.apache.commons.lang.ObjectUtils;
12  import org.apache.commons.lang.StringUtils;
13  
14  /**
15   * MetafacadeLogic implementation for org.andromda.cartridges.ejb3.metafacades.EJB3AssociationFacade.
16   *
17   * @see EJB3AssociationFacade
18   */
19  public class EJB3AssociationFacadeLogicImpl
20      extends EJB3AssociationFacadeLogic
21  {
22      private static final long serialVersionUID = 34L;
23      // ---------------- constructor -------------------------------
24  
25      /**
26       * @param metaObject
27       * @param context
28       */
29      public EJB3AssociationFacadeLogicImpl(final Object metaObject, final String context)
30      {
31          super(metaObject, context);
32      }
33  
34      // --------------- methods ---------------------
35  
36      /**
37       * Override to provide support for One-2-Many unidirectional associations as well as Many-2-Many.
38       *
39       * Returns the EJB3 cartridge specific table name for the association
40       * @return table name
41       */
42      @Override
43      public String getTableName()
44      {
45          String tableName = null;
46          final List<AssociationEndFacade> ends = this.getAssociationEnds();
47          if (ends != null && !ends.isEmpty())
48          {
49              for (AssociationEndFacade facade : ends)
50              {
51                  final EJB3AssociationEndFacade end = (EJB3AssociationEndFacade)facade;
52                  if ((end.isMany2Many() && end.isOwning()) ||
53                      (end.isOne2Many() && !end.isNavigable() && end.getOtherEnd().isNavigable()))
54                  {
55                      // prevent ClassCastException if the association isn't an
56                      // Entity
57                      if (Entity.class.isAssignableFrom(end.getType().getClass()))
58                      {
59                          final String prefixProperty = UMLMetafacadeProperties.TABLE_NAME_PREFIX;
60                          final String tableNamePrefix =
61                              this.isConfiguredProperty(prefixProperty)
62                              ? ObjectUtils.toString(this.getConfiguredProperty(prefixProperty)) : null;
63                          tableName =
64                              EJB3MetafacadeUtils.getSqlNameFromTaggedValue(
65                                  tableNamePrefix,
66                                  this,
67                                  UMLProfile.TAGGEDVALUE_PERSISTENCE_TABLE,
68                                  ((Entity)end.getType()).getMaxSqlNameLength(),
69                                  null,
70                                  this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR),
71                                  this.getConfiguredProperty(UMLMetafacadeProperties.SHORTEN_SQL_NAMES_METHOD));
72                      }
73                      break;
74                  }
75              }
76          }
77  
78  //        if (StringUtils.isNotBlank(tableName) && getName().toLowerCase().startsWith(tableName.toLowerCase()))
79  //        {
80  //            tableName = getRelationName().replaceAll("-", "_").toUpperCase();
81  //        }
82  
83          return tableName;
84      }
85  
86      /**
87       * Override the default implementation to use the current getRelationName implementation
88       * @return name
89       */
90      @Override
91      public String getName()
92      {
93          String name = (super.getName().equalsIgnoreCase(super.getRelationName()) ? null : super.getName());
94  
95          // if the name isn't defined, use the this implementation of relation name
96          if (StringUtils.isBlank(name))
97          {
98              name = this.getRelationName();
99          }
100         return name;
101     }
102 
103     /**
104      * Override the default implementation to set the owning side name first followed by inverse side.
105      * If there is no owning side defined, then adopt the default logic of using alphabetical ordering.
106      * @return relation name
107      */
108     @Override
109     public String getRelationName()
110     {
111         final Collection<AssociationEndFacade> ends = this.getAssociationEnds();
112         final Iterator endIt = ends.iterator();
113         final EJB3AssociationEndFacade firstEnd = (EJB3AssociationEndFacade)endIt.next();
114         final EJB3AssociationEndFacade secondEnd = (EJB3AssociationEndFacade)endIt.next();
115         final String separator = String.valueOf(
116                 this.getConfiguredProperty(UMLMetafacadeProperties.RELATION_NAME_SEPARATOR));
117 
118         if (secondEnd.isOwning())
119         {
120             return secondEnd.getName() + separator + firstEnd.getName();
121         }
122         else if (firstEnd.isOwning())
123         {
124             return firstEnd.getName() + separator + secondEnd.getName();
125         }
126         else
127         {
128             return MetafacadeUtils.toRelationName(
129                     firstEnd.getName(),
130                     secondEnd.getName(),
131                     separator);
132         }
133     }
134 }