View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import java.util.Collection;
4   import org.andromda.core.metafacade.MetafacadeConstants;
5   import org.andromda.metafacades.uml.AttributeFacade;
6   import org.andromda.metafacades.uml.ClassifierFacade;
7   import org.andromda.metafacades.uml.ModelElementFacade;
8   import org.andromda.metafacades.uml.NameMasker;
9   import org.andromda.metafacades.uml.UMLMetafacadeProperties;
10  import org.apache.commons.collections.CollectionUtils;
11  import org.apache.commons.collections.Predicate;
12  import org.apache.commons.lang.BooleanUtils;
13  import org.apache.commons.lang.StringUtils;
14  import org.eclipse.uml2.uml.Enumeration;
15  import org.eclipse.uml2.uml.EnumerationLiteral;
16  import org.eclipse.uml2.uml.NamedElement;
17  
18  /**
19   * MetafacadeLogic implementation for
20   * org.andromda.metafacades.uml.EnumerationFacade.
21   *
22   * @see org.andromda.metafacades.uml.EnumerationFacade
23   * @author Bob Fields
24   */
25  public class EnumerationFacadeLogicImpl
26      extends EnumerationFacadeLogic
27  {
28      private static final long serialVersionUID = 3610615632915162579L;
29  
30      /**
31       * @param metaObject
32       * @param context
33       */
34      public EnumerationFacadeLogicImpl(
35          final Object metaObject,
36          final String context)
37      {
38          super(metaObject, context);
39      }
40  
41      /**
42       * Overridden to provide name masking.
43       *
44       * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
45       */
46      @Override
47      protected String handleGetName()
48      {
49          final String nameMask =
50              String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.ENUMERATION_NAME_MASK));
51          return NameMasker.mask(
52              super.handleGetName(),
53              nameMask);
54      }
55  
56      /**
57       * Return type can be either EnumerationLiteral or AttributeFacade
58       * @see org.andromda.metafacades.uml.EnumerationFacade#getLiterals()
59       */
60      @Override
61      protected Collection<? extends ModelElementFacade> handleGetLiterals()
62      {
63          // To Check: could be sufficient to return the collection of literals only
64          //           without filtering
65          final Collection<? extends ModelElementFacade> literals =
66              this.metaObject instanceof Enumeration
67              ? ((Enumeration)this.metaObject).getOwnedLiterals()
68              : CollectionUtils.collect(this.getAttributes(), UmlUtilities.ELEMENT_TRANSFORMER);
69  
70          CollectionUtils.filter(
71              literals,
72              new Predicate()
73              {
74                  public boolean evaluate(final Object object)
75                  {
76                      // evaluates to true in case it's a native literal
77                      // or an attribute which is not considered a regular member
78                      return
79                          (object instanceof EnumerationLiteral) ||
80                            (object instanceof AttributeFacade && !((AttributeFacade)object).isEnumerationMember());
81                  }
82              }
83          );
84          return literals;
85      }
86  
87      /**
88       * @see org.andromda.metafacades.uml.EnumerationFacade#getMemberVariables()
89       */
90      @Override
91      protected Collection<AttributeFacade> handleGetMemberVariables()
92      {
93          // To Check: could be sufficient to return the collection of attributes only
94          //           without filtering
95          // http://andromda.sourceforge.net/jira/browse/UMLMETA-87
96          final Collection variables = this.metaObject instanceof Enumeration
97                  ? ((Enumeration)this.metaObject).getOwnedAttributes()
98                  : CollectionUtils.collect(this.getAttributes(), UmlUtilities.ELEMENT_TRANSFORMER);
99  
100         CollectionUtils.filter(
101             variables,
102             new Predicate()
103             {
104                 public boolean evaluate(final Object object)
105                 {
106                     // evaluates to true in case it's NOT a native literal
107                     // but an attribute which considered as a regular member
108                     return object instanceof AttributeFacade && ((AttributeFacade)object).isEnumerationMember();
109                 }
110             }
111         );
112         return variables;
113     }
114 
115     /**
116      * @see org.andromda.metafacades.uml.EnumerationFacade#getFromOperationSignature()
117      */
118     @Override
119     protected String handleGetFromOperationSignature()
120     {
121         final StringBuilder signature = new StringBuilder(this.getFromOperationName());
122         final ClassifierFacade type = this.getLiteralType();
123         if (type != null)
124         {
125             signature.append('(');
126             signature.append(type.getFullyQualifiedName());
127             signature.append(" value)");
128         }
129         return signature.toString();
130     }
131 
132     /**
133      * @see org.andromda.metafacades.uml.EnumerationFacade#isTypeSafe()
134      */
135     @Override
136     protected boolean handleIsTypeSafe()
137     {
138         return BooleanUtils.toBoolean(
139                 String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.TYPE_SAFE_ENUMS_ENABLED)));
140     }
141 
142     /**
143      * @see org.andromda.metafacades.uml.EnumerationFacade#getFromOperationName()
144      */
145     @Override
146     protected String handleGetFromOperationName()
147     {
148         final StringBuilder name = new StringBuilder("from");
149         final ClassifierFacade type = this.getLiteralType();
150         if (type != null)
151         {
152             name.append(StringUtils.capitalize(type.getName()));
153         }
154         return name.toString();
155     }
156 
157     /**
158      * @see org.andromda.metafacades.uml.EnumerationFacade#getLiteralType()
159      */
160     @Override
161     protected Object handleGetLiteralType()
162     {
163         Object type = null;
164         final Collection<AttributeFacade> literals = this.getLiterals();
165         if (literals != null && !literals.isEmpty())
166         {
167             final ModelElementFacade literal = (ModelElementFacade)literals.iterator().next();
168             if (literal instanceof AttributeFacade)
169             {
170                 type = ((AttributeFacade)literal).getType();
171             }
172             else
173             {
174                 //String defaultType = String.valueOf(this.getConfiguredProperty(
175                 //    UMLMetafacadeProperties.DEFAULT_ENUMERATION_LITERAL_TYPE));
176                 type = UmlUtilities.findByFullyQualifiedName(
177                     ((NamedElement)this.metaObject).eResource().getResourceSet(),
178                     "datatype::String", // TODO: use this (doesn't work for some reason): UMLMetafacadeProperties.DEFAULT_ENUMERATION_LITERAL_TYPE,
179                     MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
180                     true);
181                 if (type==null)
182                 {
183                     // Requires customized mapping in etc/*Mappings.xml files
184                     type = UmlUtilities.findByFullyQualifiedName(
185                         ((NamedElement)this.metaObject).eResource().getResourceSet(),
186                         "UML2::String",
187                         MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
188                         true);
189                 }
190                 if (type==null)
191                 {
192                     // Requires customized mapping in etc/*Mappings.xml files
193                     type = UmlUtilities.findByFullyQualifiedName(
194                         ((NamedElement)this.metaObject).eResource().getResourceSet(),
195                         "PrimitiveTypes::String",
196                         MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
197                         true);
198                 }
199                 if (type==null)
200                 {
201                     // Requires customized mapping in etc/*Mappings.xml files
202                     type = UmlUtilities.findByFullyQualifiedName(
203                         ((NamedElement)this.metaObject).eResource().getResourceSet(),
204                         "UMLPrimitiveTypes::String",
205                         MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
206                         true);
207                 }
208                 if (type==null)
209                 {
210                     // Requires customized mapping in etc/*Mappings.xml files
211                     type = UmlUtilities.findByFullyQualifiedName(
212                         ((NamedElement)this.metaObject).eResource().getResourceSet(),
213                         "UMLTypes::String",
214                         MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
215                         true);
216                 }
217                 if (type==null)
218                 {
219                     // Requires customized mapping in etc/*Mappings.xml files
220                     type = UmlUtilities.findByFullyQualifiedName(
221                         ((NamedElement)this.metaObject).eResource().getResourceSet(),
222                         "String",
223                         MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
224                         true);
225                 }
226             }
227         }
228         return type;
229     }
230 }