View Javadoc
1   package org.andromda.cartridges.ejb3.metafacades;
2   
3   import java.text.MessageFormat;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.List;
7   import org.andromda.cartridges.ejb3.EJB3Globals;
8   import org.andromda.metafacades.uml.DependencyFacade;
9   import org.andromda.metafacades.uml.ModelElementFacade;
10  import org.apache.commons.collections.CollectionUtils;
11  import org.apache.commons.collections.Predicate;
12  import org.apache.commons.lang.StringUtils;
13  
14  /**
15   * MetafacadeLogic implementation for org.andromda.cartridges.ejb3.metafacades.EJB3DependencyFacade.
16   *
17   * @see EJB3DependencyFacade
18   */
19  public class EJB3DependencyFacadeLogicImpl
20      extends EJB3DependencyFacadeLogic
21  {
22      private static final long serialVersionUID = 34L;
23      /**
24       * The suffix for the transformation anonymous name.
25       */
26      private static final String TRANSFORMATION_ANONYMOUS_NAME_SUFFIX = "_TRANSFORMER";
27  
28      /**
29       * The suffix for the transformation to entity method name.
30       */
31      private static final String TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX = "ToEntity";
32  
33      /**
34       * The suffix for the value object to entity transformer.
35       */
36      private static final String VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX = "Transformer";
37  
38      /**
39       * @param metaObject
40       * @param context
41       */
42      public EJB3DependencyFacadeLogicImpl(final Object metaObject, final String context)
43      {
44          super (metaObject, context);
45      }
46  
47      /**
48       * @return EJB3Globals.TRANSFORMATION_CONSTANT_PREFIX + this.getName().toUpperCase()
49       * @see EJB3DependencyFacade#getTransformationConstantName()
50       */
51      @Override
52      protected String handleGetTransformationConstantName()
53      {
54          return EJB3Globals.TRANSFORMATION_CONSTANT_PREFIX + this.getName().toUpperCase();
55      }
56  
57      /**
58       * @return EJB3Globals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(getName())
59       * @see EJB3DependencyFacade#getTransformationMethodName()
60       */
61      @Override
62      protected String handleGetTransformationMethodName()
63      {
64          return EJB3Globals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName());
65      }
66  
67      /**
68       * @see EJB3DependencyFacade#getTransformationAnonymousName()
69       */
70      @Override
71      protected String handleGetTransformationAnonymousName()
72      {
73          return this.getName().toUpperCase() + TRANSFORMATION_ANONYMOUS_NAME_SUFFIX;
74      }
75  
76      /**
77       * @return circularReference
78       * @see EJB3DependencyFacade#isCircularReference()
79       */
80      @Override
81      protected boolean handleIsCircularReference()
82      {
83          boolean circularReference = false;
84          final ModelElementFacade sourceElement = this.getSourceElement();
85          final ModelElementFacade targetElement = this.getTargetElement();
86          final Collection<DependencyFacade> sourceDependencies = targetElement.getSourceDependencies();
87          if (sourceDependencies != null && !sourceDependencies.isEmpty())
88          {
89              circularReference = CollectionUtils.find(sourceDependencies, new Predicate()
90              {
91                  public boolean evaluate(final Object object)
92                  {
93                      DependencyFacade dependency = (DependencyFacade)object;
94                      return (dependency != null) && dependency.getTargetElement().equals(sourceElement);
95                  }
96              }) != null;
97          }
98          return circularReference;
99      }
100 
101     /**
102      * @see EJB3DependencyFacade#getTransformationConstantValue()
103      */
104     @Override
105     protected int handleGetTransformationConstantValue()
106     {
107         int value = 0;
108         ModelElementFacade element = this.getSourceElement();
109         if (element instanceof EJB3EntityFacade)
110         {
111             final List<EJB3EntityFacade> hierarchyList = new ArrayList<EJB3EntityFacade>();
112             for (EJB3EntityFacade entity = (EJB3EntityFacade)element; entity != null;
113                 entity = (EJB3EntityFacade)entity.getGeneralization())
114             {
115                 hierarchyList.add(entity);
116             }
117             boolean breakOut = false;
118             for (int ctr = hierarchyList.size() - 1; ctr >= 0; ctr--)
119             {
120                 final EJB3EntityFacade generalization = hierarchyList.get(ctr);
121                 for (final Object reference : generalization.getValueObjectReferences())
122                 {
123                     value++;
124                     if (reference.equals(this))
125                     {
126                         breakOut = true;
127                         break;
128                     }
129                 }
130                 if (breakOut)
131                 {
132                     break;
133                 }
134             }
135         }
136         return value;
137     }
138 
139     /**
140      * @see EJB3DependencyFacade#getTransformationToCollectionMethodName()
141      */
142     @Override
143     protected String handleGetTransformationToCollectionMethodName()
144     {
145         return EJB3Globals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName()) +
146             EJB3Globals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX;
147     }
148 
149     /**
150      * @see EJB3DependencyFacade#getDaoName()
151      */
152     @Override
153     protected String handleGetDaoName()
154     {
155         return MessageFormat.format(
156                 this.getDaoNamePattern(),
157                 StringUtils.trimToEmpty(this.getName()));
158     }
159 
160     /**
161      * Gets the value of the {@link EJB3Globals#DAO_PATTERN}.
162      *
163      * @return the DAO name pattern.
164      */
165     private String getDaoNamePattern()
166     {
167         return String.valueOf(this.getConfiguredProperty(EJB3Globals.DAO_PATTERN));
168     }
169 
170     /**
171      * @see EJB3DependencyFacade#getDaoGetterName()
172      */
173     @Override
174     protected String handleGetDaoGetterName()
175     {
176         return "get" + StringUtils.capitalize(this.getDaoName());
177     }
178 
179     /**
180      * @see EJB3DependencyFacade#getDaoSetterName()
181      */
182     @Override
183     protected String handleGetDaoSetterName()
184     {
185         return "set" + StringUtils.capitalize(this.getDaoName());
186     }
187 
188     /**
189      * @see EJB3DependencyFacade#getTransformationToEntityCollectionMethodName()
190      */
191     @Override
192     protected String handleGetTransformationToEntityCollectionMethodName()
193     {
194         return this.getTransformationToEntityMethodName() + EJB3Globals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX;
195     }
196 
197     /**
198      * @see EJB3DependencyFacade#getTransformationToEntityMethodName()
199      */
200     @Override
201     protected String handleGetTransformationToEntityMethodName()
202     {
203         return this.getName() + TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX;
204     }
205 
206     /**
207      * @see EJB3DependencyFacade#getValueObjectToEntityTransformerName()
208      */
209     @Override
210     protected String handleGetValueObjectToEntityTransformerName()
211     {
212         return StringUtils.capitalize(this.getTransformationToEntityMethodName()) +
213             VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX;
214     }
215 }