1 package org.andromda.cartridges.spring.metafacades;
2
3 import java.text.MessageFormat;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Iterator;
7
8 import org.andromda.cartridges.spring.SpringHibernateUtils;
9 import org.andromda.cartridges.spring.SpringProfile;
10 import org.andromda.metafacades.uml.AttributeFacade;
11 import org.andromda.metafacades.uml.ClassifierFacade;
12 import org.andromda.metafacades.uml.DependencyFacade;
13 import org.andromda.metafacades.uml.EnumerationFacade;
14 import org.andromda.metafacades.uml.FilteredCollection;
15 import org.andromda.metafacades.uml.GeneralizableElementFacade;
16 import org.andromda.metafacades.uml.OperationFacade;
17 import org.andromda.metafacades.uml.ValueObject;
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20
21
22
23
24
25
26
27 public class SpringEntityLogicImpl
28 extends SpringEntityLogic
29 {
30 public SpringEntityLogicImpl(
31 Object metaObject,
32 String context)
33 {
34 super(metaObject, context);
35 }
36
37
38
39
40 private static final String INHERITANCE_STRATEGY_CLASS = "class";
41
42
43
44
45 private static final String INHERITANCE_STRATEGY_SUBCLASS = "subclass";
46
47
48
49
50 private static final String INHERITANCE_STRATEGY_CONCRETE = "concrete";
51
52
53
54
55 private static final String INHERITANCE_STRATEGY_INTERFACE = "interface";
56
57
58
59
60 private static final Collection inheritanceStrategies = new ArrayList();
61
62 static
63 {
64 inheritanceStrategies.add(INHERITANCE_STRATEGY_CLASS);
65 inheritanceStrategies.add(INHERITANCE_STRATEGY_SUBCLASS);
66 inheritanceStrategies.add(INHERITANCE_STRATEGY_CONCRETE);
67 inheritanceStrategies.add(INHERITANCE_STRATEGY_INTERFACE);
68 }
69
70
71
72
73 protected java.lang.String handleGetDaoName()
74 {
75 return this.getDaoNamePattern().replaceAll(
76 "\\{0\\}",
77 this.getName());
78 }
79
80
81
82
83
84
85 private String getDaoNamePattern()
86 {
87 return String.valueOf(this.getConfiguredProperty(SpringGlobals.DAO_PATTERN));
88 }
89
90
91
92
93 protected java.lang.String handleGetFullyQualifiedDaoName()
94 {
95 return SpringMetafacadeUtils.getFullyQualifiedName(
96 this.getPackageName(),
97 this.getDaoName());
98 }
99
100
101
102
103 protected java.lang.String handleGetDaoImplementationName()
104 {
105 return this.getDaoImplementationNamePattern().replaceAll(
106 "\\{0\\}",
107 this.getName());
108 }
109
110
111
112
113
114
115 private String getDaoImplementationNamePattern()
116 {
117 return String.valueOf(this.getConfiguredProperty(SpringGlobals.DAO_IMPLEMENTATION_PATTERN));
118 }
119
120
121
122
123 protected java.lang.String handleGetFullyQualifiedDaoImplementationName()
124 {
125 return SpringMetafacadeUtils.getFullyQualifiedName(
126 this.getPackageName(),
127 this.getDaoImplementationName());
128 }
129
130
131
132
133 protected java.lang.String handleGetDaoBaseName()
134 {
135 return this.getDaoBaseNamePattern().replaceAll(
136 "\\{0\\}",
137 this.getName());
138 }
139
140
141
142
143
144
145 private String getDaoBaseNamePattern()
146 {
147 return String.valueOf(this.getConfiguredProperty(SpringGlobals.DAO_BASE_PATTERN));
148 }
149
150
151
152
153 protected java.lang.String handleGetFullyQualifiedDaoBaseName()
154 {
155 return SpringMetafacadeUtils.getFullyQualifiedName(
156 this.getPackageName(),
157 this.getDaoBaseName());
158 }
159
160
161
162
163 protected java.lang.String handleGetEntityImplementationName()
164 {
165 return this.getEntityName() + SpringGlobals.IMPLEMENTATION_SUFFIX;
166 }
167
168
169
170
171 protected java.lang.String handleGetFullyQualifiedEntityImplementationName()
172 {
173 return SpringMetafacadeUtils.getFullyQualifiedName(
174 this.getPackageName(),
175 this.getEntityName(),
176 SpringGlobals.IMPLEMENTATION_SUFFIX);
177 }
178
179
180
181
182 protected java.lang.String handleGetBeanName(boolean targetSuffix)
183 {
184 final String beanName = StringUtils.uncapitalize(StringUtils.trimToEmpty(this.getName()));
185 StringBuffer beanNameBuffer = new StringBuffer(String.valueOf(this.getConfiguredProperty(SpringGlobals.BEAN_NAME_PREFIX)));
186 beanNameBuffer.append(this.getDaoNamePattern().replaceAll("\\{0\\}", beanName));
187 if (targetSuffix)
188 {
189 beanNameBuffer.append(SpringGlobals.BEAN_NAME_TARGET_SUFFIX);
190 }
191 return beanNameBuffer.toString();
192 }
193
194
195
196
197 protected String handleGetEntityName()
198 {
199 final String entityNamePattern = (String)this.getConfiguredProperty("entityNamePattern");
200 return MessageFormat.format(
201 entityNamePattern,
202 new Object[] {StringUtils.trimToEmpty(this.getName())});
203 }
204
205
206
207
208 protected String handleGetFullyQualifiedEntityName()
209 {
210 return SpringMetafacadeUtils.getFullyQualifiedName(
211 this.getPackageName(),
212 this.getEntityName(),
213 null);
214 }
215
216
217
218
219 protected Object handleGetRoot()
220 {
221 GeneralizableElementFacade generalization = this;
222 for (
223 ; generalization.getGeneralization() != null && generalization instanceof SpringEntity;
224 generalization = generalization.getGeneralization())
225 ;
226 return generalization;
227 }
228
229
230
231
232 protected boolean handleIsDaoBusinessOperationsPresent()
233 {
234 return this.getDaoBusinessOperations() != null && !this.getDaoBusinessOperations().isEmpty();
235 }
236
237
238
239
240 protected Collection handleGetDaoBusinessOperations()
241 {
242
243 Collection finders = this.getQueryOperations();
244 Collection operations = this.getOperations();
245
246 Collection nonFinders = CollectionUtils.subtract(operations, finders);
247 return new FilteredCollection(nonFinders)
248 {
249 public boolean evaluate(Object object)
250 {
251 return ((OperationFacade)object).isStatic();
252 }
253 };
254 }
255
256
257
258
259 protected Collection handleGetValueObjectReferences()
260 {
261 return this.getValueObjectReferences(false);
262 }
263
264
265
266
267
268
269 protected Collection getValueObjectReferences(boolean follow)
270 {
271 final Collection sourceDependencies = new ArrayList(this.getSourceDependencies());
272 if (follow)
273 {
274 for (
275 GeneralizableElementFacade entity = this.getGeneralization(); entity != null;
276 entity = entity.getGeneralization())
277 {
278 sourceDependencies.addAll(entity.getSourceDependencies());
279 }
280 }
281 return new FilteredCollection(sourceDependencies)
282 {
283 public boolean evaluate(Object object)
284 {
285 boolean valid = false;
286 Object targetElement = ((DependencyFacade)object).getTargetElement();
287 if (targetElement instanceof ClassifierFacade)
288 {
289 ClassifierFacade element = (ClassifierFacade)targetElement;
290 valid = element.isDataType() || element instanceof ValueObject || element instanceof EnumerationFacade;
291 }
292 return valid;
293 }
294 };
295 }
296
297
298
299
300 protected Collection handleGetAllValueObjectReferences()
301 {
302 return this.getValueObjectReferences(true);
303 }
304
305
306
307
308 protected boolean handleIsDaoImplementationRequired()
309 {
310 return !this.getValueObjectReferences().isEmpty() || !this.getDaoBusinessOperations().isEmpty() ||
311 !this.getQueryOperations(true).isEmpty();
312 }
313
314
315
316
317 private static final String NO_TRANSFORMATION_CONSTANT_SUFFIX = "NONE";
318
319
320
321
322 protected String handleGetDaoNoTransformationConstantName()
323 {
324 return SpringGlobals.TRANSFORMATION_CONSTANT_PREFIX + NO_TRANSFORMATION_CONSTANT_SUFFIX;
325 }
326
327
328
329
330 protected boolean checkHibInheritance(String inheritance)
331 {
332 return inheritance.equals(getHibernateInheritanceStrategy());
333 }
334
335
336
337
338 protected boolean handleIsHibernateInheritanceClass()
339 {
340 return checkHibInheritance(INHERITANCE_STRATEGY_CLASS);
341 }
342
343
344
345
346 protected boolean handleIsHibernateInheritanceInterface()
347 {
348 return checkHibInheritance(INHERITANCE_STRATEGY_INTERFACE);
349 }
350
351
352
353
354 protected boolean handleIsHibernateInheritanceSubclass()
355 {
356 return checkHibInheritance(INHERITANCE_STRATEGY_SUBCLASS);
357 }
358
359
360
361
362 protected boolean handleIsHibernateInheritanceConcrete()
363 {
364 return checkHibInheritance(INHERITANCE_STRATEGY_CONCRETE);
365 }
366
367
368
369
370 private static final String INHERITANCE_STRATEGY = "hibernateInheritanceStrategy";
371
372
373
374
375 protected String handleGetHibernateInheritanceStrategy()
376 {
377 String inheritance = this.getInheritance(this);
378 for (SpringEntity superEntity = this.getSuperEntity(); superEntity != null && StringUtils.isBlank(inheritance);)
379 {
380 inheritance = superEntity.getHibernateInheritanceStrategy();
381 }
382 inheritance = inheritance != null ? inheritance.toLowerCase() : null;
383 if (StringUtils.isBlank(inheritance) || !inheritanceStrategies.contains(inheritance))
384 {
385 inheritance = this.getDefaultInheritanceStrategy();
386 }
387 return inheritance;
388 }
389
390
391
392
393
394
395 private String getDefaultInheritanceStrategy()
396 {
397 return String.valueOf(this.getConfiguredProperty(INHERITANCE_STRATEGY));
398 }
399
400
401
402
403
404
405
406 private String getInheritance(SpringEntity entity)
407 {
408 String inheritance = null;
409 if (entity != null)
410 {
411 Object value = entity.findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_INHERITANCE);
412 if (value != null)
413 {
414 inheritance = String.valueOf(value);
415 }
416 }
417 return inheritance;
418 }
419
420
421
422
423 protected boolean handleIsRequiresHibernateMapping()
424 {
425 final SpringEntity superEntity = this.getSuperEntity();
426 return
427 SpringHibernateUtils.mapSubclassesInSeparateFile(
428 (String)this.getConfiguredProperty(SpringGlobals.HIBERNATE_MAPPING_STRATEGY)) ||
429 this.isRoot() &&
430 (
431 !this.isHibernateInheritanceInterface() || this.getSpecializations().isEmpty() ||
432 (superEntity != null && superEntity.isHibernateInheritanceInterface())
433 );
434 }
435
436
437
438
439 private boolean isRoot()
440 {
441 final SpringEntity superEntity = this.getSuperEntity();
442 boolean abstractConcreteEntity =
443 (this.isHibernateInheritanceConcrete() || this.isHibernateInheritanceInterface()) && this.isAbstract();
444 return (
445 this.getSuperEntity() == null ||
446 (superEntity.isHibernateInheritanceInterface() || superEntity.isHibernateInheritanceConcrete())
447 ) && !abstractConcreteEntity;
448 }
449
450
451
452
453
454
455
456 private SpringEntity getSuperEntity()
457 {
458 SpringEntity superEntity = null;
459 if (this.getGeneralization() != null && this.getGeneralization() instanceof SpringEntity)
460 {
461 superEntity = (SpringEntity)this.getGeneralization();
462 }
463 return superEntity;
464 }
465
466
467
468
469 protected String handleGetAttributeEmbeddedValueList()
470 {
471 final StringBuffer buffer = new StringBuffer();
472 for (final Iterator iterator = this.getEmbeddedValues().iterator(); iterator.hasNext();)
473 {
474 final AttributeFacade attribute = (AttributeFacade)iterator.next();
475 final String name = attribute.getName();
476 if (StringUtils.isNotBlank(name))
477 {
478 buffer.append('\"' + name + '\"');
479 if (iterator.hasNext())
480 {
481 buffer.append(", ");
482 }
483 }
484 }
485 return buffer.toString();
486 }
487
488
489
490
491 protected boolean handleIsRichClient()
492 {
493 String richClient =
494 StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty("richClient")));
495
496 return richClient.equalsIgnoreCase("true");
497 }
498
499 }