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
17
18
19
20 public class SpringDependencyLogicImpl
21 extends SpringDependencyLogic
22 {
23 private static final long serialVersionUID = 34L;
24
25
26
27
28
29
30 public SpringDependencyLogicImpl(
31 Object metaObject,
32 String context)
33 {
34 super(metaObject, context);
35 }
36
37
38
39
40
41 protected String handleGetTransformationConstantName()
42 {
43 return SpringGlobals.TRANSFORMATION_CONSTANT_PREFIX + this.getName().toUpperCase();
44 }
45
46
47
48
49
50 protected String handleGetTransformationMethodName()
51 {
52 return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName());
53 }
54
55
56
57
58 private static final String TRANSFORMATION_ANONYMOUS_NAME_SUFFIX = "_TRANSFORMER";
59
60
61
62
63
64 protected String handleGetTransformationAnonymousName()
65 {
66 return this.getName().toUpperCase() + TRANSFORMATION_ANONYMOUS_NAME_SUFFIX;
67 }
68
69
70
71
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
105
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
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
152
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
162
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
172
173
174 protected String handleGetDaoName()
175 {
176 return this.getDaoNamePattern().replaceAll(
177 "\\{0\\}",
178 this.getName());
179 }
180
181
182
183
184
185
186 private String getDaoNamePattern()
187 {
188 return String.valueOf(this.getConfiguredProperty(SpringGlobals.DAO_PATTERN));
189 }
190
191
192
193
194
195 protected String handleGetDaoGetterName()
196 {
197 return "get" + StringUtils.capitalize(this.getDaoName());
198 }
199
200
201
202
203
204 protected String handleGetDaoSetterName()
205 {
206 return "set" + StringUtils.capitalize(this.getDaoName());
207 }
208
209
210
211
212
213 protected String handleGetTransformationToEntityCollectionMethodName()
214 {
215 return this.getTransformationToEntityMethodName() + SpringGlobals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX;
216 }
217
218
219
220
221 private static final String TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX = "ToEntity";
222
223
224
225
226
227 protected String handleGetTransformationToEntityMethodName()
228 {
229 return this.getName() + TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX;
230 }
231
232
233
234
235 private static final String VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX = "Transformer";
236
237
238
239
240
241 protected String handleGetValueObjectToEntityTransformerName()
242 {
243 return StringUtils.capitalize(this.getTransformationToEntityMethodName()) +
244 VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX;
245 }
246 }