View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import org.andromda.metafacades.uml.ClassifierFacade;
4   import org.andromda.metafacades.uml.EntityMetafacadeUtils;
5   import org.andromda.metafacades.uml.EnumerationFacade;
6   import org.andromda.metafacades.uml.NameMasker;
7   import org.andromda.metafacades.uml.TypeMappings;
8   import org.andromda.metafacades.uml.UMLMetafacadeProperties;
9   import org.andromda.metafacades.uml.UMLProfile;
10  import org.apache.commons.lang.ObjectUtils;
11  import org.apache.commons.lang.StringUtils;
12  import org.apache.log4j.Logger;
13  
14  /**
15   * MetafacadeLogic implementation for
16   * org.andromda.metafacades.uml.EntityAttribute.
17   *
18   * @see org.andromda.metafacades.uml.EntityAttribute
19   */
20  public class EntityAttributeLogicImpl
21      extends EntityAttributeLogic
22  {
23      private static final long serialVersionUID = 34L;
24      /**
25       * @param metaObject
26       * @param context
27       */
28      public EntityAttributeLogicImpl(
29          final Object metaObject,
30          final String context)
31      {
32          super(metaObject, context);
33      }
34  
35      /**
36       * The logger instance.
37       */
38      private static final Logger LOGGER = Logger.getLogger(EntityAttributeLogicImpl.class);
39  
40      /**
41       * Overridden to provide name masking.
42       *
43       * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
44       */
45      @Override
46      protected String handleGetName()
47      {
48          final String nameMask =
49              String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.ENTITY_PROPERTY_NAME_MASK));
50          return NameMasker.mask(
51              super.handleGetName(),
52              nameMask);
53      }
54  
55      /**
56       * @see org.andromda.metafacades.uml.EntityAttribute#getColumnLength()
57       */
58      @Override
59      protected String handleGetColumnLength()
60      {
61          return (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_LENGTH);
62      }
63  
64      /**
65       * @see org.andromda.metafacades.uml.EntityAttribute#getColumnName()
66       */
67      @Override
68      protected String handleGetColumnName()
69      {
70          final Short maxSqlNameLength =
71              Short.valueOf((String)this.getConfiguredProperty(UMLMetafacadeProperties.MAX_SQL_NAME_LENGTH));
72          final String columnNamePrefix =
73              this.isConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX)
74              ? ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.COLUMN_NAME_PREFIX)) : null;
75          return EntityMetafacadeUtils.getSqlNameFromTaggedValue(
76              columnNamePrefix,
77              this,
78              UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN,
79              maxSqlNameLength,
80              this.getConfiguredProperty(UMLMetafacadeProperties.SQL_NAME_SEPARATOR),
81              this.getConfiguredProperty(UMLMetafacadeProperties.SHORTEN_SQL_NAMES_METHOD));
82      }
83  
84      /**
85       * @see org.andromda.metafacades.uml.EntityAttribute#getJdbcMappings()
86       */
87      @Override
88      protected org.andromda.metafacades.uml.TypeMappings handleGetJdbcMappings()
89      {
90          return this.getMappingsProperty(UMLMetafacadeProperties.JDBC_MAPPINGS_URI);
91      }
92  
93      /**
94       * @see org.andromda.metafacades.uml.EntityAttribute#getJdbcType()
95       */
96      @Override
97      protected String handleGetJdbcType()
98      {
99          String value = null;
100         if (this.getJdbcMappings() != null)
101         {
102             final ClassifierFacade type = this.getType();
103             if (type != null)
104             {
105                 final String typeName = type.getFullyQualifiedName(true);
106                 value = this.getJdbcMappings().getTo(typeName);
107             }
108         }
109 
110         return value;
111     }
112 
113     /**
114      * @see org.andromda.metafacades.uml.EntityAttribute#getSqlMappings()
115      */
116     @Override
117     protected org.andromda.metafacades.uml.TypeMappings handleGetSqlMappings()
118     {
119         return this.getMappingsProperty(UMLMetafacadeProperties.SQL_MAPPINGS_URI);
120     }
121 
122     /**
123      * @see org.andromda.metafacades.uml.EntityAttribute#getSqlType()
124      */
125     @Override
126     protected String handleGetSqlType()
127     {
128         String value = null;
129         if (this.getSqlMappings() != null)
130         {
131             final ClassifierFacade type = this.getType();
132             if (type != null)
133             {
134                 String typeName = type.getFullyQualifiedName(true);
135 
136                 // if its an enumeration, the sql type is the literal type
137                 if (type.isEnumeration())
138                 {
139                     final ClassifierFacade literalType = ((EnumerationFacade)type).getLiteralType();
140                     if (literalType != null)
141                     {
142                         typeName = literalType.getFullyQualifiedName(true);
143                     }
144                 }
145                 value = this.getSqlMappings().getTo(typeName);
146                 final String columnLength = this.getColumnLength();
147                 if (StringUtils.isNotBlank(columnLength))
148                 {
149                     value = EntityMetafacadeUtils.constructSqlTypeName(
150                             value,
151                             columnLength);
152                 }
153             }
154         }
155         return value;
156     }
157 
158     /**
159      * @see org.andromda.metafacades.uml.EntityAttribute#isIdentifier()
160      */
161     @Override
162     protected boolean handleIsIdentifier()
163     {
164         return this.hasStereotype(UMLProfile.STEREOTYPE_IDENTIFIER);
165     }
166 
167     /* super.isUnique() causes StackOverflowError
168      * @see org.andromda.metafacades.uml.EntityAttribute#isUnique()
169     @Override
170     protected boolean handleIsUnique()
171     {
172         return super.isUnique() || this.hasStereotype(UMLProfile.STEREOTYPE_UNIQUE);
173     }
174     */
175 
176     /**
177      * @see org.andromda.metafacades.uml.EntityAttribute#getColumnIndex()
178      */
179     // TODO: Duplicated method warning from ancestor
180     @Override
181     protected String handleGetColumnIndex()
182     {
183         final String index = (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_INDEX);
184         return index == null ? null : StringUtils.trimToEmpty(index);
185     }
186 
187     /**
188      * Gets a Mappings instance from a property registered under the given
189      * <code>propertyName</code>.
190      *
191      * @param propertyName
192      *            the property name to register under.
193      * @return the Mappings instance.
194      */
195     private TypeMappings getMappingsProperty(final String propertyName)
196     {
197         final Object property = this.getConfiguredProperty(propertyName);
198         TypeMappings mappings = null;
199         if (property instanceof String)
200         {
201             final String uri = (String)property;
202             try
203             {
204                 mappings = TypeMappings.getInstance(uri);
205                 this.setProperty(
206                     propertyName,
207                     mappings);
208             }
209             catch (Throwable th)
210             {
211                 final String errMsg = "Error getting '" + propertyName + "' --> '" + uri + '\'';
212                 EntityAttributeLogicImpl.LOGGER.error(
213                     errMsg,
214                     th);
215 
216                 // don't throw the exception
217             }
218         }
219         else
220         {
221             mappings = (TypeMappings)property;
222         }
223         return mappings;
224     }
225 
226     /**
227      * @see org.andromda.metafacades.emf.uml22.EntityAttributeLogic#handleIsTransient()
228      */
229     @Override
230     protected boolean handleIsTransient()
231     {
232         return this.hasStereotype(UMLProfile.STEREOTYPE_TRANSIENT);
233     }
234 
235     /**
236      * @see org.andromda.metafacades.uml.EntityAttribute#getUniqueGroup()
237      */
238     @Override
239     protected String handleGetUniqueGroup() {
240         final String group = (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_COLUMN_UNIQUE_GROUP);
241         return group == null ? null : StringUtils.trimToEmpty(group);
242     }
243 }