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