1 package org.andromda.cartridges.ejb3.metafacades;
2
3 import org.andromda.cartridges.ejb3.EJB3Globals;
4 import org.andromda.cartridges.ejb3.EJB3Profile;
5 import org.andromda.metafacades.uml.AttributeFacade;
6 import org.andromda.metafacades.uml.UMLProfile;
7 import org.apache.commons.lang.StringUtils;
8 import org.apache.commons.lang.math.NumberUtils;
9
10
11
12
13
14
15 public class EJB3EntityAttributeFacadeLogicImpl
16 extends EJB3EntityAttributeFacadeLogic
17 {
18 private static final long serialVersionUID = 34L;
19
20
21
22 public static final String ENTITY_DEFAULT_GENERATOR_TYPE = "entityDefaultGeneratorType";
23
24
25
26
27 public static final String ENTITY_DEFAULT_GENERATOR_INITIAL_VALUE = "entityDefaultGeneratorInitialValue";
28
29
30
31
32 public static final String ENTITY_DEFAULT_GENERATOR_ALLOCATION_SIZE = "entityDefaultGeneratorAllocationSize";
33
34
35
36
37 public static final String DEFAULT_ENUM_LITERAL_COLUMN_LENGTH = "entityDefaultEnumLiteralColumnLength";
38
39
40
41
42 public static final String ENTITY_DEFAULT_TEMPORAL_TYPE = "entityDefaultTemporalType";
43
44
45
46
47
48
49
50 public EJB3EntityAttributeFacadeLogicImpl(final Object metaObject, final String context)
51 {
52 super (metaObject, context);
53 }
54
55
56
57
58
59
60
61
62 @Override
63 public boolean isRequired()
64 {
65 boolean required = super.isRequired();
66 if (this.getOwner() instanceof EJB3EntityFacade)
67 {
68 EJB3EntityFacade entity = (EJB3EntityFacade)this.getOwner();
69
70
71
72
73 if (entity.isRequiresGeneralizationMapping() && entity.isInheritanceSingleTable()
74 && !entity.isEmbeddableSuperclassGeneralizationExists())
75 {
76 required = false;
77 }
78 }
79 return required;
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 @Override
111 protected String handleGetFetchType()
112 {
113 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_FETCH_TYPE);
114 }
115
116
117
118
119 @Override
120 protected boolean handleIsEager()
121 {
122 boolean isEager = false;
123 if (StringUtils.isNotBlank(this.getFetchType()))
124 {
125 if (EJB3Globals.FETCH_TYPE_EAGER.equalsIgnoreCase(this.getFetchType()))
126 {
127 isEager = true;
128 }
129 }
130 return isEager;
131 }
132
133
134
135
136 @Override
137 protected boolean handleIsLazy()
138 {
139 boolean isLazy = false;
140 if (StringUtils.isNotBlank(this.getFetchType()))
141 {
142 if (EJB3Globals.FETCH_TYPE_LAZY.equalsIgnoreCase(this.getFetchType()))
143 {
144 isLazy = true;
145 }
146 }
147 return isLazy;
148 }
149
150
151
152
153 @Override
154 protected boolean handleIsVersion()
155 {
156 boolean isVersion = false;
157 if (this.hasStereotype(EJB3Profile.STEREOTYPE_VERSION))
158 {
159 isVersion = true;
160 }
161 return isVersion;
162 }
163
164
165
166
167 @Override
168 protected boolean handleIsLob()
169 {
170 return this.getType().isBlobType() || this.getType().isClobType();
171 }
172
173
174
175
176 @Override
177 protected String handleGetLobType()
178 {
179 return StringUtils.trimToEmpty((String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_PERSISTENCE_LOB_TYPE));
180 }
181
182
183
184
185 @Override
186 protected String handleGetGeneratorType()
187 {
188 String genType = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_TYPE);
189 if (StringUtils.isBlank(genType))
190 {
191 if (this.getType().isStringType() || this.getType().isDateType() || this.getType().isTimeType())
192 {
193 genType = EJB3Globals.GENERATOR_TYPE_NONE;
194 }
195 else
196 {
197 genType = String.valueOf(this.getConfiguredProperty(ENTITY_DEFAULT_GENERATOR_TYPE));
198 if (StringUtils.isBlank(genType))
199 {
200 genType = EJB3Globals.GENERATOR_TYPE_AUTO;
201 }
202 }
203 }
204 return genType;
205 }
206
207
208
209
210 @Override
211 protected boolean handleIsGeneratorTypeSequence()
212 {
213 boolean isSequence = false;
214 if (StringUtils.isNotBlank(this.getGeneratorType()))
215 {
216 if (EJB3Globals.GENERATOR_TYPE_SEQUENCE.equalsIgnoreCase(this.getGeneratorType()))
217 {
218 isSequence = true;
219 }
220 }
221 return isSequence;
222 }
223
224
225
226
227 @Override
228 protected boolean handleIsGeneratorTypeTable()
229 {
230 boolean isTable = false;
231 if (StringUtils.isNotBlank(this.getGeneratorType()))
232 {
233 if (EJB3Globals.GENERATOR_TYPE_TABLE.equalsIgnoreCase(this.getGeneratorType()))
234 {
235 isTable = true;
236 }
237 }
238 return isTable;
239 }
240
241
242
243
244 @Override
245 protected boolean handleIsGeneratorTypeAuto()
246 {
247 boolean isAuto = false;
248 if (StringUtils.isNotBlank(this.getGeneratorType()))
249 {
250 if (EJB3Globals.GENERATOR_TYPE_AUTO.equalsIgnoreCase(this.getGeneratorType()))
251 {
252 isAuto = true;
253 }
254 }
255 return isAuto;
256 }
257
258
259
260
261 @Override
262 protected boolean handleIsGeneratorTypeGeneric()
263 {
264 boolean isGeneric = false;
265 if (StringUtils.isNotBlank(this.getGeneratorType()))
266 {
267 if (EJB3Globals.GENERATOR_TYPE_GENERIC.equalsIgnoreCase(this.getGeneratorType()))
268 {
269 isGeneric = true;
270 }
271 }
272 return isGeneric;
273 }
274
275
276
277
278 @Override
279 protected boolean handleIsGeneratorTypeNone()
280 {
281 boolean isNone = false;
282 if (StringUtils.isNotBlank(this.getGeneratorType()))
283 {
284 if (EJB3Globals.GENERATOR_TYPE_NONE.equalsIgnoreCase(this.getGeneratorType()))
285 {
286 isNone = true;
287 }
288 }
289 return isNone;
290 }
291
292
293
294
295 @Override
296 protected boolean handleIsGeneratorTypeIdentity()
297 {
298 boolean isIdentity = false;
299 if (StringUtils.isNotBlank(this.getGeneratorType()))
300 {
301 if (EJB3Globals.GENERATOR_TYPE_IDENTITY.equalsIgnoreCase(this.getGeneratorType()))
302 {
303 isIdentity = true;
304 }
305 }
306 return isIdentity;
307 }
308
309
310 public static final String GENERATOR = "Generator";
311
312 public static final char UNDERSCORE = '_';
313
314
315
316 @Override
317 protected String handleGetGeneratorName()
318 {
319 String generatorName = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_NAME);
320 if (StringUtils.isBlank(generatorName))
321 {
322 generatorName = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_SOURCE_NAME);
323 if (StringUtils.isBlank(generatorName))
324 {
325 generatorName = this.getOwner().getName() + GENERATOR;
326 }
327 else
328 {
329 generatorName = this.getOwner().getName() + UNDERSCORE + generatorName;
330 }
331 }
332 return generatorName;
333 }
334
335
336
337
338 @Override
339 protected String handleGetGeneratorSourceName()
340 {
341 String sourceName = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_SOURCE_NAME);
342 if (StringUtils.isBlank(sourceName))
343 {
344 sourceName = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_NAME);
345 }
346 return sourceName;
347 }
348
349
350
351
352 @Override
353 protected String handleGetGeneratorGenericStrategy()
354 {
355 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_GENERIC_STRATEGY);
356 }
357
358
359
360
361 @Override
362 protected String handleGetGeneratorPkColumnValue()
363 {
364 String pkColumnValue = (String)this.findTaggedValue(
365 EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_PKCOLUMN_VALUE);
366
367 if (StringUtils.isBlank(pkColumnValue))
368 {
369 pkColumnValue = this.getOwner().getName() + UNDERSCORE + this.getColumnName();
370 }
371 return pkColumnValue;
372 }
373
374
375
376
377 @Override
378 protected int handleGetGeneratorInitialValue()
379 {
380 int initialValue = 1;
381 String initialValueStr =
382 (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_INITIAL_VALUE);
383 if (StringUtils.isNotBlank(initialValueStr))
384 {
385 initialValue = NumberUtils.toInt(initialValueStr);
386 }
387 else
388 {
389 initialValueStr =
390 String.valueOf(this.getConfiguredProperty(ENTITY_DEFAULT_GENERATOR_INITIAL_VALUE));
391 if (StringUtils.isNotBlank(initialValueStr))
392 {
393 initialValue = NumberUtils.toInt(initialValueStr);
394 }
395 }
396
397 return initialValue;
398 }
399
400
401
402
403 @Override
404 protected int handleGetGeneratorAllocationSize()
405 {
406 int allocationSize = 1;
407 String allocationSizeStr =
408 (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_ALLOCATION_SIZE);
409 if (StringUtils.isNotBlank(allocationSizeStr))
410 {
411 allocationSize = NumberUtils.toInt(allocationSizeStr);
412 }
413 else
414 {
415 allocationSizeStr =
416 String.valueOf(this.getConfiguredProperty(ENTITY_DEFAULT_GENERATOR_ALLOCATION_SIZE));
417 if (StringUtils.isNotBlank(allocationSizeStr))
418 {
419 allocationSize = NumberUtils.toInt(allocationSizeStr);
420 }
421 }
422
423 return allocationSize;
424 }
425
426
427
428
429
430
431
432 @Override
433 public String getColumnLength()
434 {
435 String columnLength = (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_LENGTH);
436 if (StringUtils.isBlank(columnLength))
437 {
438 columnLength = super.getColumnLength();
439 }
440 return columnLength;
441 }
442
443
444
445
446
447
448
449 @Override
450 public String getColumnName()
451 {
452 String columnName = (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN);
453 if (StringUtils.isBlank(columnName))
454 {
455 columnName = super.getColumnName();
456 }
457 return columnName;
458 }
459
460
461
462
463
464
465
466
467
468 @Override
469 protected String handleGetColumnDefinition()
470 {
471 String definition = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_COLUMN_DEFINITION);
472 if (StringUtils.isBlank(definition) && this.getType().isEnumeration())
473 {
474 boolean isOrdinal = false;
475 int length = NumberUtils.toInt(
476 String.valueOf(this.getConfiguredProperty(DEFAULT_ENUM_LITERAL_COLUMN_LENGTH)));
477 for (AttributeFacade attribute : this.getType().getAttributes())
478 {
479 if (!attribute.getType().isStringType())
480 {
481 isOrdinal = true;
482 break;
483 }
484 if (attribute.getName().length() > length)
485 {
486 length = attribute.getName().length();
487 }
488 }
489 if (!isOrdinal)
490 {
491 definition = "VARCHAR(" + length + ')';
492 }
493 }
494 return definition;
495 }
496
497
498
499
500 @Override
501 protected String handleGetColumnPrecision()
502 {
503 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_COLUMN_PRECISION);
504 }
505
506
507
508
509 @Override
510 protected String handleGetColumnScale()
511 {
512 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_COLUMN_SCALE);
513 }
514
515
516
517
518 @Override
519 protected boolean handleIsColumnNullable()
520 {
521 boolean nullable = true;
522 String nullableString = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_COLUMN_NULLABLE);
523
524 if (StringUtils.isBlank(nullableString))
525 {
526 nullable = (this.isIdentifier() || this.isUnique()) ? false : !this.isRequired();
527 }
528 else
529 {
530 nullable = Boolean.valueOf(nullableString).booleanValue();
531 }
532 return nullable;
533 }
534
535
536
537
538 @Override
539 protected String handleGetTemporalType()
540 {
541 String temporalType = null;
542 if (this.getType().isDateType())
543 {
544 temporalType = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_TEMPORAL_TYPE);
545 if (StringUtils.isBlank(temporalType))
546 {
547 temporalType = String.valueOf(this.getConfiguredProperty(ENTITY_DEFAULT_TEMPORAL_TYPE));
548 }
549 }
550 return temporalType;
551 }
552
553
554
555
556 @Override
557 protected String handleGetEnumerationType()
558 {
559 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_ENUMERATION_TYPE);
560 }
561
562
563
564
565 @Override
566 protected boolean handleIsInsertEnabled()
567 {
568 final String value = (String)super.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_COLUMN_INSERT);
569 return StringUtils.isNotBlank(value) ? Boolean.valueOf(value).booleanValue() : true;
570 }
571
572
573
574
575 @Override
576 protected boolean handleIsUpdateEnabled()
577 {
578 final String value = (String)super.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_COLUMN_UPDATE);
579 return StringUtils.isNotBlank(value) ? Boolean.valueOf(value).booleanValue() : true;
580 }
581
582
583
584
585 @Override
586 protected boolean handleIsContainsEmbeddedObject()
587 {
588 boolean returnValue = false;
589 if (this.getType() instanceof EJB3EmbeddedValueFacade)
590 {
591 returnValue = true;
592 }
593 return returnValue;
594 }
595
596
597
598
599 @Override
600 protected String handleGetOverrideType()
601 {
602 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_OVERRIDE_TYPE);
603 }
604 }