View Javadoc
1   package org.andromda.cartridges.spring.metafacades;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import java.util.List;
7   import org.andromda.cartridges.spring.SpringHibernateUtils;
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  /**
16   * MetafacadeLogic implementation for org.andromda.cartridges.spring.metafacades.SpringDependency.
17   *
18   * @see org.andromda.cartridges.spring.metafacades.SpringDependency
19   */
20  public class SpringDependencyLogicImpl
21      extends SpringDependencyLogic
22  {
23      private static final long serialVersionUID = 34L;
24      /**
25       * Public constructor for SpringDependencyLogicImpl
26       * @param metaObject
27       * @param context
28       * @see org.andromda.cartridges.spring.metafacades.SpringDependency
29       */
30      public SpringDependencyLogicImpl(
31          Object metaObject,
32          String context)
33      {
34          super(metaObject, context);
35      }
36  
37      /**
38       * @return SpringGlobals.TRANSFORMATION_CONSTANT_PREFIX + this.getName().toUpperCase()
39       * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationConstantName()
40       */
41      protected String handleGetTransformationConstantName()
42      {
43          return SpringGlobals.TRANSFORMATION_CONSTANT_PREFIX + this.getName().toUpperCase();
44      }
45  
46      /**
47       * @return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName())
48       * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationMethodName()
49       */
50      protected String handleGetTransformationMethodName()
51      {
52          return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName());
53      }
54  
55      /**
56       * The suffix for the transformation annonymous name.
57       */
58      private static final String TRANSFORMATION_ANONYMOUS_NAME_SUFFIX = "_TRANSFORMER";
59  
60      /**
61       * @return getName().toUpperCase() + TRANSFORMATION_ANONYMOUS_NAME_SUFFIX
62       * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationAnonymousName()
63       */
64      protected String handleGetTransformationAnonymousName()
65      {
66          return this.getName().toUpperCase() + TRANSFORMATION_ANONYMOUS_NAME_SUFFIX;
67      }
68  
69      /**
70       * @return dependency.getTargetElement().equals(sourceElement)
71       * @see org.andromda.cartridges.spring.metafacades.SpringDependency#isCircularReference()
72       */
73      protected boolean handleIsCircularReference()
74      {
75          boolean circularReference = false;
76          final ModelElementFacade sourceElement = this.getSourceElement();
77          final ModelElementFacade targetElement = this.getTargetElement();
78          final Collection<DependencyFacade> sourceDependencies = targetElement.getSourceDependencies();
79          if (sourceDependencies != null && !sourceDependencies.isEmpty())
80          {
81              circularReference =
82                  CollectionUtils.find(
83                      sourceDependencies,
84                      new Predicate()
85                      {
86                          public boolean evaluate(Object object)
87                          {
88                              DependencyFacade dependency = (DependencyFacade)object;
89                              return dependency != null && dependency.getTargetElement().equals(sourceElement);
90                          }
91                      }) != null;
92          }
93          return circularReference;
94      }
95  
96      private boolean isXmlPersistenceActive()
97      {
98          return SpringHibernateUtils.isXmlPersistenceActive(
99              (String)this.getConfiguredProperty(SpringGlobals.HIBERNATE_VERSION),
100             (String)this.getConfiguredProperty(SpringGlobals.HIBERNATE_XML_PERSISTENCE));
101     }
102 
103     /**
104      * @return incoming ValueObject references
105      * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationConstantValue()
106      */
107     protected int handleGetTransformationConstantValue()
108     {
109         int value = 0;
110         ModelElementFacade element = this.getSourceElement();
111         if (element instanceof SpringEntity)
112         {
113             final List<SpringEntity> hierarchy = new ArrayList<SpringEntity>();
114             for (SpringEntity entity = (SpringEntity)element; entity != null;
115                 entity = (SpringEntity)entity.getGeneralization())
116             {
117                 hierarchy.add(entity);
118             }
119             boolean breakOut = false;
120             for (int ctr = hierarchy.size() - 1; ctr >= 0; ctr--)
121             {
122                 final SpringEntity generalization = hierarchy.get(ctr);
123                 //TODO Bug? generalization.getValueObjectReferences() can contain not only ClassifierFacades
124                 for (final Iterator referenceIterator = generalization.getValueObjectReferences().iterator();
125                     referenceIterator.hasNext();)
126                 {
127                     final Object reference = referenceIterator.next();
128                     value++;
129                     if (reference.equals(this))
130                     {
131                         breakOut = true;
132                         break;
133                     }
134                 }
135                 if (breakOut)
136                 {
137                     break;
138                 }
139             }
140         }
141 
142         if (isXmlPersistenceActive())
143         {
144             value++;
145         }
146 
147         return value;
148     }
149 
150     /**
151      * @return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + getName() + SpringGlobals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX
152      * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationToCollectionMethodName()
153      */
154     protected String handleGetTransformationToCollectionMethodName()
155     {
156         return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName()) +
157         SpringGlobals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX;
158     }
159 
160     /**
161      * @return TRANSFORMATION_METHOD_PREFIX + capitalize(getName()) + TRANSFORMATION_TO_ARRAY_METHOD_SUFFIX
162      * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationToArrayMethodName()
163      */
164     protected String handleGetTransformationToArrayMethodName()
165     {
166         return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName()) +
167             SpringGlobals.TRANSFORMATION_TO_ARRAY_METHOD_SUFFIX;
168     }
169 
170     /**
171      * @return getDaoNamePattern().replaceAll("\\{0\\}", getName())
172      * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getDaoName()
173      */
174     protected String handleGetDaoName()
175     {
176         return this.getDaoNamePattern().replaceAll(
177             "\\{0\\}",
178             this.getName());
179     }
180 
181     /**
182      * Gets the value of the {@link SpringGlobals#DAO_PATTERN}.
183      *
184      * @return the DAO name pattern.
185      */
186     private String getDaoNamePattern()
187     {
188         return String.valueOf(this.getConfiguredProperty(SpringGlobals.DAO_PATTERN));
189     }
190 
191     /**
192      * @return "get" + StringUtils.capitalize(this.getDaoName())
193      * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getDaoGetterName()
194      */
195     protected String handleGetDaoGetterName()
196     {
197         return "get" + StringUtils.capitalize(this.getDaoName());
198     }
199 
200     /**
201      * @return "set" + StringUtils.capitalize(this.getDaoName())
202      * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getDaoSetterName()
203      */
204     protected String handleGetDaoSetterName()
205     {
206         return "set" + StringUtils.capitalize(this.getDaoName());
207     }
208 
209     /**
210      * @return getTransformationToEntityMethodName() + TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX
211      * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationToEntityCollectionMethodName()
212      */
213     protected String handleGetTransformationToEntityCollectionMethodName()
214     {
215         return this.getTransformationToEntityMethodName() + SpringGlobals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX;
216     }
217 
218     /**
219      * The suffix for the transformation to entity method name.
220      */
221     private static final String TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX = "ToEntity";
222 
223     /**
224      * @return getName() + TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX
225      * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationToEntityMethodName()
226      */
227     protected String handleGetTransformationToEntityMethodName()
228     {
229         return this.getName() + TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX;
230     }
231 
232     /**
233      * The suffix for the value object to entity transformer. "Transformer"
234      */
235     private static final String VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX = "Transformer";
236 
237     /**
238      * @return capitalize(this.getTransformationToEntityMethodName()) + VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX
239      * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getValueObjectToEntityTransformerName()
240      */
241     protected String handleGetValueObjectToEntityTransformerName()
242     {
243         return StringUtils.capitalize(this.getTransformationToEntityMethodName()) +
244         VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX;
245     }
246 }