View Javadoc
1   package org.andromda.cartridges.hibernate.metafacades;
2   
3   import org.andromda.cartridges.hibernate.HibernateProfile;
4   import org.andromda.cartridges.hibernate.HibernateUtils;
5   import org.andromda.metafacades.uml.ClassifierFacade;
6   import org.andromda.metafacades.uml.EntityMetafacadeUtils;
7   import org.andromda.metafacades.uml.NameMasker;
8   import org.andromda.metafacades.uml.TypeMappings;
9   import org.andromda.metafacades.uml.UMLMetafacadeProperties;
10  import org.andromda.utils.JavaTypeConverter;
11  import org.apache.commons.lang.StringUtils;
12  
13  
14  /**
15   * MetafacadeLogic implementation for
16   * org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute.
17   *
18   * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute
19   * @author Bob Fields
20   */
21  public class HibernateEntityAttributeLogicImpl
22      extends HibernateEntityAttributeLogic
23  {
24      private static final long serialVersionUID = 34L;
25      /**
26       * @param metaObject
27       * @param context
28       */
29      // ---------------- constructor -------------------------------
30      public HibernateEntityAttributeLogicImpl(
31          Object metaObject,
32          String context)
33      {
34          super(metaObject, context);
35      }
36  
37      /**
38       * Overridden to provide handling of inheritance.
39       *
40       * @see org.andromda.metafacades.uml.AttributeFacade#isRequired()
41       */
42      @Override
43      public boolean isRequired()
44      {
45          boolean required = super.isRequired();
46          if (this.getOwner() instanceof HibernateEntity)
47          {
48              HibernateEntity entity = (HibernateEntity)this.getOwner();
49              if (entity.isHibernateInheritanceClass() && entity.getGeneralization() != null)
50              {
51                  required = false;
52              }
53          }
54          return required;
55      }
56  
57      /**
58       * Override to provide java specific handling of the default value.
59       *
60       * @see org.andromda.metafacades.uml.AttributeFacade#getDefaultValue()
61       */
62      @Override
63      public String getDefaultValue()
64      {
65          String defaultValue = super.getDefaultValue();
66          final ClassifierFacade type = this.getType();
67          if (type != null)
68          {
69              final String fullyQualifiedName = StringUtils.trimToEmpty(type.getFullyQualifiedName());
70              if (("java.lang.String".equals(fullyQualifiedName) || "String".equals(fullyQualifiedName)) && fullyQualifiedName.indexOf('"')<0)
71              {
72                  defaultValue = '\"' + defaultValue.replaceAll("\"", "") + '\"';
73              }
74              else if (fullyQualifiedName.startsWith("java.lang"))
75              {
76                  defaultValue = fullyQualifiedName + ".valueOf(" + defaultValue + ')';
77              }
78              else if (type.isEnumeration())
79              {
80                  final String mask = (String)this.getConfiguredProperty(UMLMetafacadeProperties.ENUMERATION_LITERAL_NAME_MASK);
81                  defaultValue = type.getFullyQualifiedName() + '.' + NameMasker.mask(defaultValue, mask);
82              }
83          }
84          return defaultValue;
85      }
86  
87      /**
88       * @return returnValue = true if this.getType() instanceof HibernateEmbeddedValue
89       * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isContainsEmbeddedObject()
90       */
91      @Override
92      protected boolean handleIsContainsEmbeddedObject()
93      {
94          boolean returnValue = false;
95          if (this.getType() instanceof HibernateEmbeddedValue)
96          {
97              returnValue = true;
98          }
99          return returnValue;
100     }
101 
102     /**
103      * @param prefix
104      * @param name
105      * @return returnValue prefix + this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR) + name
106      * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#concatColumnName(String, String)
107      */
108     @Override
109     protected String handleConcatColumnName(
110         String prefix,
111         String name)
112     {
113         String returnValue = name;
114         if (StringUtils.isNotBlank(prefix)) 
115         {
116             returnValue = prefix + this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR) + name;
117 
118             // handle maxSqlNameLength
119             Short maxSqlNameLength =
120                 Short.valueOf((String)this.getConfiguredProperty(UMLMetafacadeProperties.MAX_SQL_NAME_LENGTH));
121             final String method = (String)super.getConfiguredProperty(UMLMetafacadeProperties.SHORTEN_SQL_NAMES_METHOD);
122             returnValue = EntityMetafacadeUtils.ensureMaximumNameLength(returnValue, maxSqlNameLength,method);
123         }
124         return returnValue;
125     }
126 
127     /**
128      * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isLazy()
129      */
130     @Override
131     protected boolean handleIsLazy()
132     {
133         final String value = (String)findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_LAZY);
134         return StringUtils.isNotBlank(value) ? Boolean.valueOf(value).booleanValue() : false;
135     }
136 
137     /**
138      * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#getFormula()
139      */
140     @Override
141     protected String handleGetFormula()
142     {
143         return (String)findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_FORMULA);
144     }
145 
146     /**
147      * @return HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_INSERT
148      * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isInsertEnabled()
149      */
150     @Override
151     protected boolean handleIsInsertEnabled()
152     {
153         final String value = (String)findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_INSERT);
154         return StringUtils.isNotBlank(value) ? Boolean.valueOf(value).booleanValue() : true;
155     }
156 
157     /**
158      * @return HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_UPDATE
159      * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttribute#isUpdateEnabled()
160      */
161     @Override
162     protected boolean handleIsUpdateEnabled()
163     {
164         final String value = (String)findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_PROPERTY_UPDATE);
165         return StringUtils.isNotBlank(value) ? Boolean.valueOf(value).booleanValue() : true;
166     }
167 
168     private boolean isXmlPersistenceActive()
169     {
170        return HibernateUtils.isXmlPersistenceActive((String)this.getConfiguredProperty(HibernateGlobals.HIBERNATE_VERSION),
171                                                     (String)this.getConfiguredProperty(HibernateGlobals.HIBERNATE_XML_PERSISTENCE));
172     }
173 
174     private boolean persistIDAsAttribute()
175     {
176         boolean persistAsAttribute = true;
177         String prop = (String)this.getConfiguredProperty(HibernateGlobals.HIBERNATE_XML_PERSISTENCE_ID_AS_ATTRIBUTE);
178         if (prop != null && "false".equalsIgnoreCase(prop))
179             persistAsAttribute = false;
180 
181         return persistAsAttribute;
182     }
183 
184     /**
185      * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttributeLogic#handleGetXmlTagName()
186      */
187     @Override
188     protected String handleGetXmlTagName()
189     {
190         String tagName = null;
191 
192         if (isXmlPersistenceActive())
193         {
194             tagName = (String)this.findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_XML_TAG_NAME);
195 
196             if (StringUtils.isBlank(tagName))
197             {
198                 if (this.isIdentifier() && this.persistIDAsAttribute())
199                     tagName = '@' + this.getName();
200                 else
201                     tagName = this.getName();
202             }
203 
204         }
205         return (StringUtils.isBlank(tagName)) ? null : tagName;
206     }
207 
208     /**
209      * @see org.andromda.cartridges.hibernate.metafacades.HibernateEntityAttributeLogic#handleGetFullyQualifiedHibernateType()
210      */
211     @Override
212     protected String handleGetFullyQualifiedHibernateType()
213     {
214         String fullyQualifiedName;
215 
216         if (this.getType() != null && this.getType().isEnumeration())
217         {
218             fullyQualifiedName = "org.andromda.persistence.hibernate.usertypes.HibernateEnumType";
219         }
220         else
221         {
222             final String hibernateTypeMappingsUri = (String)this.getConfiguredProperty("hibernateTypeMappingsUri");
223             final TypeMappings mappings = TypeMappings.getInstance(hibernateTypeMappingsUri);
224 
225             if (this.getType() != null && mappings == null)
226             {
227                 fullyQualifiedName = this.getType().getFullyQualifiedName();
228             }
229             else
230             {
231                 final String fullyQualifiedModelName = this.getType().getFullyQualifiedName(true);
232                 if (mappings.getMappings().containsFrom(fullyQualifiedModelName))
233                 {
234                     fullyQualifiedName = mappings.getTo(fullyQualifiedModelName);
235                 }
236                 else
237                 {
238                     fullyQualifiedName = this.getType().getFullyQualifiedName();
239                 }
240             }
241             fullyQualifiedName = JavaTypeConverter.getJavaLangTypeName(fullyQualifiedName);
242         }
243 
244         return fullyQualifiedName;
245     }
246 }