View Javadoc

1   package org.andromda.metafacades.uml14;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   
6   import org.andromda.metafacades.uml.ClassifierFacade;
7   import org.andromda.metafacades.uml.EnumerationFacade;
8   import org.andromda.metafacades.uml.NameMasker;
9   import org.andromda.metafacades.uml.TypeMappings;
10  import org.andromda.metafacades.uml.UMLMetafacadeProperties;
11  import org.andromda.metafacades.uml.UMLMetafacadeUtils;
12  import org.andromda.metafacades.uml.UMLProfile;
13  import org.andromda.utils.StringUtilsHelper;
14  import org.apache.commons.lang.BooleanUtils;
15  import org.apache.commons.lang.ObjectUtils;
16  import org.apache.commons.lang.StringUtils;
17  import org.omg.uml.foundation.datatypes.ChangeableKindEnum;
18  import org.omg.uml.foundation.datatypes.Multiplicity;
19  import org.omg.uml.foundation.datatypes.MultiplicityRange;
20  import org.omg.uml.foundation.datatypes.OrderingKind;
21  import org.omg.uml.foundation.datatypes.OrderingKindEnum;
22  import org.omg.uml.foundation.datatypes.ScopeKindEnum;
23  
24  
25  /***
26   * Metaclass facade implementation.
27   */
28  public class AttributeFacadeLogicImpl
29      extends AttributeFacadeLogic
30  {
31      public AttributeFacadeLogicImpl(
32          org.omg.uml.foundation.core.Attribute metaObject,
33          String context)
34      {
35          super(metaObject, context);
36      }
37  
38      /***
39       * @see org.andromda.core.metafacade.MetafacadeBase#getValidationOwner()
40       */
41      public Object getValidationOwner()
42      {
43          return this.getOwner();
44      }
45  
46      /***
47       * @see org.andromda.metafacades.uml.AttributeFacade#getGetterName()
48       */
49      public java.lang.String handleGetGetterName()
50      {
51          return UMLMetafacadeUtils.getGetterPrefix(this.getType()) + StringUtilsHelper.capitalize(this.getName());
52      }
53  
54      /***
55       * @see org.andromda.metafacades.uml.AttributeFacade#getSetterName()
56       */
57      public java.lang.String handleGetSetterName()
58      {
59          return "set" + StringUtils.capitalize(this.getName());
60      }
61  
62      /***
63       * @see org.andromda.metafacades.uml.AttributeFacade#getDefaultValue()
64       */
65      public String handleGetDefaultValue()
66      {
67          String defaultValue = null;
68          if (this.metaObject.getInitialValue() != null)
69          {
70              defaultValue = this.metaObject.getInitialValue().getBody();
71          }
72          return defaultValue;
73      }
74  
75      /***
76       * @see org.andromda.metafacades.uml.AttributeFacade#isChangeable()
77       */
78      public boolean handleIsChangeable()
79      {
80          return ChangeableKindEnum.CK_CHANGEABLE.equals(metaObject.getChangeability());
81      }
82  
83      /***
84       * @see org.andromda.metafacades.uml.AttributeFacade#isAddOnly()
85       */
86      public boolean handleIsAddOnly()
87      {
88          return ChangeableKindEnum.CK_ADD_ONLY.equals(metaObject.getChangeability());
89      }
90  
91      /***
92       * @see org.andromda.metafacades.uml.AttributeFacade#getType()
93       */
94      protected Object handleGetType()
95      {
96          return metaObject.getType();
97      }
98  
99      /***
100      * @see org.andromda.metafacades.uml.AttributeFacade#getOwner()
101      */
102     public Object handleGetOwner()
103     {
104         return this.metaObject.getOwner();
105     }
106 
107     /***
108      * @see org.andromda.metafacades.uml.AttributeFacade#isReadOnly()
109      */
110     public boolean handleIsReadOnly()
111     {
112         return ChangeableKindEnum.CK_FROZEN.equals(metaObject.getChangeability());
113     }
114 
115     /***
116      * @see org.andromda.metafacades.uml.AttributeFacade#isStatic()
117      */
118     public boolean handleIsStatic()
119     {
120         return ScopeKindEnum.SK_CLASSIFIER.equals(this.metaObject.getOwnerScope());
121     }
122 
123     /***
124      * @see org.andromda.metafacades.uml.AttributeFacade#findTaggedValue(java.lang.String, boolean)
125      */
126     public Object handleFindTaggedValue(
127         String name,
128         boolean follow)
129     {
130         name = StringUtils.trimToEmpty(name);
131         Object value = findTaggedValue(name);
132         if (follow)
133         {
134             ClassifierFacade type = this.getType();
135             while (value == null && type != null)
136             {
137                 value = type.findTaggedValue(name);
138                 type = (ClassifierFacade)type.getGeneralization();
139             }
140         }
141         return value;
142     }
143 
144     /***
145      * @see org.andromda.metafacades.uml.AttributeFacade#isRequired()
146      */
147     public boolean handleIsRequired()
148     {
149         int lower = this.getMultiplicityRangeLower();
150         return lower >= 1;
151     }
152 
153     /***
154      * @see org.andromda.metafacades.uml.AttributeFacade#isMany()
155      */
156     public boolean handleIsMany()
157     {
158         boolean isMany = false;
159         final Multiplicity multiplicity = this.metaObject.getMultiplicity();
160 
161         // assume no multiplicity is 1
162         if (multiplicity != null)
163         {
164             final Collection ranges = multiplicity.getRange();
165             if (ranges != null && !ranges.isEmpty())
166             {
167                 final Iterator rangeIt = ranges.iterator();
168                 while (rangeIt.hasNext())
169                 {
170                     final MultiplicityRange multiplicityRange = (MultiplicityRange)rangeIt.next();
171                     final int upper = multiplicityRange.getUpper();
172                     isMany = upper > 1 || upper < 0;
173                 }
174             }
175         }
176         return isMany;
177     }
178 
179     /***
180      * Returns the lower range of the multiplicty for the passed in associationEnd
181      *
182      * @return int the lower range of the multiplicty or 1 if it isn't defined.
183      */
184     private int getMultiplicityRangeLower()
185     {
186         Integer lower = null;
187         final Multiplicity multiplicity = metaObject.getMultiplicity();
188         if (multiplicity != null)
189         {
190             final Collection ranges = multiplicity.getRange();
191             if (ranges != null && !ranges.isEmpty())
192             {
193                 final Iterator rangeIt = ranges.iterator();
194                 while (rangeIt.hasNext())
195                 {
196                     final MultiplicityRange multiplicityRange = (MultiplicityRange)rangeIt.next();
197                     lower = new Integer(multiplicityRange.getLower());
198                 }
199             }
200         }
201         if (lower == null)
202         {
203             final String defaultMultiplicity = this.getDefaultMultiplicity();
204             if (defaultMultiplicity.startsWith("0"))
205             {
206                 lower = new Integer(0);
207             }
208             else
209             {
210                 lower = new Integer(1);
211             }
212         }
213         return lower.intValue();
214     }
215 
216     /***
217      * Gets the default multiplicity for this attribute (the
218      * multiplicity if none is defined).
219      *
220      * @return the defautl multiplicity as a String.
221      */
222     private String getDefaultMultiplicity()
223     {
224         return ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.DEFAULT_MULTIPLICITY));
225     }
226 
227     /***
228      * @see org.andromda.metafacades.uml.AttributeFacade#getEnumeration()
229      */
230     protected Object handleGetEnumeration()
231     {
232         return this.isEnumerationLiteral() ? this.getOwner() : null;
233     }
234 
235     /***
236      * @see org.andromda.metafacades.uml.AttributeFacade#isEnumerationLiteral()
237      */
238     protected boolean handleIsEnumerationLiteral()
239     {
240         final ClassifierFacade owner = this.getOwner();
241         return (owner != null) && owner.isEnumeration();
242     }
243 
244     /***
245      * @see org.andromda.metafacades.uml.AttributeFacade#getEnumerationValue()
246      */
247     protected String handleGetEnumerationValue()
248     {
249         String value = null;
250         if (this.isEnumerationLiteral())
251         {
252             value = this.getDefaultValue();
253             value = (value == null) ? getName() : String.valueOf(value);
254         }
255         if (this.getType().isStringType())
256         {
257             value = "\"" + value + "\"";
258         }
259         return value;
260     }
261 
262     /***
263      * @see org.andromda.metafacades.uml.AttributeFacade#handleIsEnumerationMember()
264      */
265     protected boolean handleIsEnumerationMember()
266     {
267         boolean isMemberVariable = false;
268         final String isMemberVariableAsString = (String)this.findTaggedValue(
269                 UMLProfile.TAGGEDVALUE_PERSISTENCE_ENUMERATION_MEMBER_VARIABLE);
270         if (StringUtils.isNotEmpty(isMemberVariableAsString) && BooleanUtils.toBoolean(isMemberVariableAsString))
271         {
272             isMemberVariable = true;
273         }
274         return isMemberVariable;
275     }
276 
277     /***
278      * @see org.andromda.metafacades.uml.AttributeFacade#handleGetEnumerationLiteralParameters()
279      */
280     protected String handleGetEnumerationLiteralParameters()
281     {
282         return (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_ENUMERATION_LITERAL_PARAMETERS);
283     }
284 
285     /***
286      * @see org.andromda.metafacades.uml.AttributeFacade#handleIsEnumerationLiteralParametersExist()
287      */
288     protected boolean handleIsEnumerationLiteralParametersExist()
289     {
290         boolean parametersExist = false;
291         if (StringUtils.isNotBlank(this.getEnumerationLiteralParameters()))
292         {
293             parametersExist = true;
294         }
295         return parametersExist;
296     }
297     
298     /***
299      * @see org.andromda.metafacades.uml.AttributeFacade#isDefaultValuePresent()
300      */
301     public boolean handleIsDefaultValuePresent()
302     {
303         return StringUtils.isNotBlank(this.getDefaultValue());
304     }
305 
306     /***
307      * Overridden to provide different handling of the name if this attribute represents a literal.
308      *
309      * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
310      */
311     protected String handleGetName()
312     {
313         String name = null;
314         if (this.isEnumerationMember())
315         {
316             name = super.handleGetName();
317         }
318         else
319         {
320             final String mask = String.valueOf(this.getConfiguredProperty(
321                 this.getOwner() instanceof EnumerationFacade
322                     ? UMLMetafacadeProperties.ENUMERATION_LITERAL_NAME_MASK
323                     : UMLMetafacadeProperties.CLASSIFIER_PROPERTY_NAME_MASK ));
324     
325             name = NameMasker.mask(super.handleGetName(), mask);
326         }
327         return name;
328     }
329 
330     /***
331      * @see org.andromda.metafacades.uml.AttributeFacade#isOrdered()
332      */
333     public boolean handleIsOrdered()
334     {
335         boolean ordered = false;
336 
337         final OrderingKind ordering = metaObject.getOrdering();
338 
339         // no ordering is 'unordered'
340         if (ordering != null)
341         {
342             ordered = ordering.equals(OrderingKindEnum.OK_ORDERED);
343         }
344 
345         return ordered;
346     }
347 
348     /***
349      * @see org.andromda.metafacades.uml.AttributeFacade#getGetterSetterTypeName()
350      */
351     public String handleGetGetterSetterTypeName()
352     {
353         String name = null;
354         if (this.isMany())
355         {
356             final TypeMappings mappings = this.getLanguageMappings();
357             name =
358                 isOrdered() ? mappings.getTo(UMLProfile.LIST_TYPE_NAME) : mappings.getTo(
359                     UMLProfile.COLLECTION_TYPE_NAME);
360 
361             // set this attribute's type as a template parameter if required
362             if (BooleanUtils.toBoolean(
363                     ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.ENABLE_TEMPLATING))))
364             {
365                 name = name + "<" + this.getType().getFullyQualifiedName() + ">";
366             }
367         }
368         if (name == null && this.getType() != null)
369         {
370             name = this.getType().getFullyQualifiedName();
371         }
372         return name;
373     }
374 
375     /***
376      * Get the UML upper multiplicity
377      * Not implemented for UML1.4
378      */
379     protected int handleGetUpper()
380     {
381         throw new java.lang.UnsupportedOperationException("'upper' is not a UML1.4 feature");
382      }
383 
384     /***
385      * Get the UML lower multiplicity
386      * Not implemented for UML1.4
387      */
388     protected int handleGetLower()
389     {
390         throw new java.lang.UnsupportedOperationException("'lower' is not a UML1.4 feature");
391     }
392 }