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
20
21
22
23
24
25 public class EnumerationFacadeLogicImpl
26 extends EnumerationFacadeLogic
27 {
28 private static final long serialVersionUID = 3610615632915162579L;
29
30
31
32
33
34 public EnumerationFacadeLogicImpl(
35 final Object metaObject,
36 final String context)
37 {
38 super(metaObject, context);
39 }
40
41
42
43
44
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
58
59
60 @Override
61 protected Collection<? extends ModelElementFacade> handleGetLiterals()
62 {
63
64
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
77
78 return
79 (object instanceof EnumerationLiteral) ||
80 (object instanceof AttributeFacade && !((AttributeFacade)object).isEnumerationMember());
81 }
82 }
83 );
84 return literals;
85 }
86
87
88
89
90 @Override
91 protected Collection<AttributeFacade> handleGetMemberVariables()
92 {
93
94
95
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
107
108 return object instanceof AttributeFacade && ((AttributeFacade)object).isEnumerationMember();
109 }
110 }
111 );
112 return variables;
113 }
114
115
116
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
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
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
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
175
176 type = UmlUtilities.findByFullyQualifiedName(
177 ((NamedElement)this.metaObject).eResource().getResourceSet(),
178 "datatype::String",
179 MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
180 true);
181 if (type==null)
182 {
183
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
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
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
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
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 }