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