View Javadoc
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   * MetafacadeLogic implementation for org.andromda.cartridges.ejb3.metafacades.EJB3EntityAttributeFacade.
12   *
13   * @see EJB3EntityAttributeFacade
14   */
15  public class EJB3EntityAttributeFacadeLogicImpl
16      extends EJB3EntityAttributeFacadeLogic
17  {
18      private static final long serialVersionUID = 34L;
19      /**
20       * The property that stores the default entity ID generator type
21       */
22      public static final String ENTITY_DEFAULT_GENERATOR_TYPE = "entityDefaultGeneratorType";
23  
24      /**
25       * The property that stores the default generator initial value
26       */
27      public static final String ENTITY_DEFAULT_GENERATOR_INITIAL_VALUE = "entityDefaultGeneratorInitialValue";
28  
29      /**
30       * The property that stores the default generator allocation size for incrementing ids
31       */
32      public static final String ENTITY_DEFAULT_GENERATOR_ALLOCATION_SIZE = "entityDefaultGeneratorAllocationSize";
33  
34      /**
35       * The property that stores the default enumeration string literal column length.
36       */
37      public static final String DEFAULT_ENUM_LITERAL_COLUMN_LENGTH = "entityDefaultEnumLiteralColumnLength";
38  
39      /**
40       * The property that stores the default temporal type for date based attributes
41       */
42      public static final String ENTITY_DEFAULT_TEMPORAL_TYPE = "entityDefaultTemporalType";
43  
44      // ---------------- constructor -------------------------------
45  
46      /**
47       * @param metaObject UML object used to create the EJB3EntityAttributeFacade
48       * @param context Context for creation of the EJB3EntityAttributeFacade
49       */
50      public EJB3EntityAttributeFacadeLogicImpl(final Object metaObject, final String context)
51      {
52          super (metaObject, context);
53      }
54  
55      // --------------- methods ---------------------
56  
57      /**
58       * Overridden to provide handling of inheritance.
59       *
60       * @see org.andromda.metafacades.uml.AttributeFacade#isRequired()
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               * Exclude ONLY if single table inheritance exists
72               */
73              if (entity.isRequiresGeneralizationMapping() && entity.isInheritanceSingleTable()
74                      && !entity.isEmbeddableSuperclassGeneralizationExists())
75              {
76                  required = false;
77              }
78          }
79          return required;
80      }
81  
82      /*
83       * Override to provide java specific handling of the default value.
84       *
85       * @see org.andromda.metafacades.uml.AttributeFacade#getDefaultValue()
86      @Override
87      public String getDefaultValue()
88      {
89          String defaultValue = super.getDefaultValue();
90          final ClassifierFacade type = this.getType();
91          if (type != null)
92          {
93              final String fullyQualifiedName = StringUtils.trimToEmpty(type.getFullyQualifiedName());
94              if (type.isStringType())
95              {
96                  defaultValue = '\"' + defaultValue + '\"';
97              }
98              else if (fullyQualifiedName.startsWith("java.lang"))
99              {
100                 defaultValue = fullyQualifiedName + ".valueOf(" + defaultValue + ')';
101             }
102         }
103         return defaultValue;
104     }
105      */
106 
107     /**
108      * @see EJB3EntityAttributeFacade#getFetchType()
109      */
110     @Override
111     protected String handleGetFetchType()
112     {
113         return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_FETCH_TYPE);
114     }
115 
116     /**
117      * @see EJB3EntityAttributeFacadeLogic#handleIsEager()
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      * @see EJB3EntityAttributeFacadeLogic#handleIsLazy()
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      * @see EJB3EntityAttributeFacade#isVersion()
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      * @see EJB3EntityAttributeFacade#isLob()
166      */
167     @Override
168     protected boolean handleIsLob()
169     {
170         return this.getType().isBlobType() || this.getType().isClobType();
171     }
172 
173     /**
174      * @see EJB3EntityAttributeFacadeLogic#handleGetLobType()
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      * @see EJB3EntityAttributeFacade#getGeneratorType()
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      * @see EJB3EntityAttributeFacadeLogic#handleIsGeneratorTypeSequence()
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      * @see EJB3EntityAttributeFacadeLogic#handleIsGeneratorTypeTable()
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      * @see EJB3EntityAttributeFacadeLogic#handleIsGeneratorTypeAuto()
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      * @see EJB3EntityAttributeFacadeLogic#handleIsGeneratorTypeGeneric()
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      * @see EJB3EntityAttributeFacadeLogic#handleIsGeneratorTypeNone()
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      * @see EJB3EntityAttributeFacadeLogic#handleIsGeneratorTypeIdentity()
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     /** Generator */
310     public static final String GENERATOR = "Generator";
311     /** _ */
312     public static final char UNDERSCORE = '_';
313     /**
314      * @see EJB3EntityAttributeFacadeLogic#handleGetGeneratorName()
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      * @see EJB3EntityAttributeFacadeLogic#handleGetGeneratorSourceName()
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      * @see EJB3EntityAttributeFacadeLogic#getGeneratorGenericStrategy()
351      */
352     @Override
353     protected String handleGetGeneratorGenericStrategy()
354     {
355         return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_GENERATOR_GENERIC_STRATEGY);
356     }
357 
358     /**
359      * @see EJB3EntityAttributeFacadeLogic#handleGetGeneratorPkColumnValue()
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      * @see EJB3EntityAttributeFacadeLogic#handleGetGeneratorInitialValue()
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      * @see EJB3EntityAttributeFacadeLogic#handleGetGeneratorAllocationSize()
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      * Override the super method to first look at the tagged value if one exists.
428      * If not, then return the default column length.
429      *
430      * @see org.andromda.metafacades.uml.EntityAttribute#getColumnLength()
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      * Override the super method to first look at the tagged value if one exists.
445      * If not, then return the default column name.
446      *
447      * @see org.andromda.metafacades.uml.EntityAttribute#getColumnName()
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      * @see EJB3EntityAttributeFacadeLogic#handleGetColumnDefinition()
462      *
463      * If the column definition has not manually been set and the attribute
464      * type is an enumeration, work out the schema from the length and type
465      * of the enumeration literals.  The definition is only set for if the
466      * literal types are String.
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      * @see EJB3EntityAttributeFacadeLogic#handleGetColumnPrecision()
499      */
500     @Override
501     protected String handleGetColumnPrecision()
502     {
503         return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_COLUMN_PRECISION);
504     }
505 
506     /**
507      * @see EJB3EntityAttributeFacadeLogic#handleGetColumnScale()
508      */
509     @Override
510     protected String handleGetColumnScale()
511     {
512         return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_COLUMN_SCALE);
513     }
514 
515     /**
516      * @see EJB3EntityAttributeFacadeLogic#handleIsColumnNullable()
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      * @see EJB3EntityAttributeFacadeLogic#handleGetTemporalType()
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      * @see EJB3EntityAttributeFacadeLogic#handleGetEnumerationType()
555      */
556     @Override
557     protected String handleGetEnumerationType()
558     {
559         return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_ENUMERATION_TYPE);
560     }
561 
562     /**
563      * @see EJB3EntityAttributeFacadeLogic#handleIsInsertEnabled()
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      * @see EJB3EntityAttributeFacadeLogic#handleIsUpdateEnabled()
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      * @see EJB3EntityAttributeFacadeLogic#handleIsContainsEmbeddedObject()
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      * @return findTaggedValue 'andromda_hibernate_type'
598      */
599     @Override
600     protected String handleGetOverrideType()
601     {
602         return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_PERSISTENCE_OVERRIDE_TYPE);
603     }
604 }