1 package org.andromda.metafacades.emf.uml22;
2
3 import org.andromda.metafacades.uml.ClassifierFacade;
4 import org.andromda.metafacades.uml.EntityAttribute;
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.UMLMetafacadeUtils;
10 import org.andromda.metafacades.uml.UMLProfile;
11 import org.andromda.utils.StringUtilsHelper;
12 import org.apache.commons.lang.BooleanUtils;
13 import org.apache.commons.lang.ObjectUtils;
14 import org.apache.commons.lang.StringUtils;
15 import org.eclipse.uml2.uml.Class;
16 import org.eclipse.uml2.uml.LiteralUnlimitedNatural;
17 import org.eclipse.uml2.uml.Type;
18
19
20
21
22
23
24
25
26 public class AttributeFacadeLogicImpl
27 extends AttributeFacadeLogic
28 {
29 private static final long serialVersionUID = 34L;
30
31
32
33
34 public AttributeFacadeLogicImpl(
35 final Attribute metaObjectIn,
36 final String context)
37 {
38 super(metaObjectIn, context);
39 }
40
41
42
43
44 @Override
45 protected String handleGetGetterName()
46 {
47 return UMLMetafacadeUtils.getGetterPrefix(this.getType(), this.getLower()) + StringUtils.capitalize(this.handleGetName());
48 }
49
50
51
52
53 @Override
54 protected String handleGetSetterName()
55 {
56 return "set" + StringUtils.capitalize(this.handleGetName());
57 }
58
59
60
61
62 @Override
63 protected boolean handleIsReadOnly()
64 {
65 return this.metaObject.isReadOnly();
66 }
67
68
69
70
71 @Override
72 protected String handleGetDefaultValue()
73 {
74 String defaultValue = this.metaObject.getDefault();
75
76
77 if (StringUtils.isNotBlank(defaultValue) && !this.isMany())
78 {
79 final String typeName = this.metaObject.getType().getName();
80 final String fullyQualifiedName = this.handleGetFullyQualifiedName();
81 if ("String".equals(typeName) && defaultValue.indexOf('"')<0)
82 {
83 defaultValue = '"' + defaultValue + '"';
84 }
85 else if (("char".equals(typeName) || "Character".equals(typeName))
86 && defaultValue.indexOf('\'')<0)
87 {
88 defaultValue = "'" + defaultValue.charAt(0) + '\'';
89 }
90 else if (fullyQualifiedName.startsWith("java.lang") && defaultValue.indexOf(".valueOf(")<0)
91 {
92 defaultValue = fullyQualifiedName + ".valueOf(" + defaultValue + ')';
93 }
94 }
95 if (defaultValue==null) {defaultValue="";}
96 return defaultValue;
97 }
98
99
100
101
102 @Override
103 protected boolean handleIsStatic()
104 {
105 return this.metaObject.isStatic();
106 }
107
108
109
110
111
112 @Override
113 protected boolean handleIsLeaf()
114 {
115 return this.metaObject.isLeaf();
116 }
117
118
119
120
121 @Override
122 protected boolean handleIsMany()
123 {
124
125
126 return this.getUpper() > 1 || this.getUpper() == LiteralUnlimitedNatural.UNLIMITED
127 || (null!=this.getType() && (this.getType().isArrayType() || this.getType().isCollectionType()));
128 }
129
130
131
132
133 @Override
134 protected boolean handleIsRequired()
135 {
136 return this.getLower() > 0;
137 }
138
139
140
141
142 @Override
143 protected boolean handleIsChangeable()
144 {
145 return !this.metaObject.isReadOnly();
146 }
147
148
149
150
151 @Override
152 protected boolean handleIsAddOnly()
153 {
154
155 return false;
156 }
157
158
159
160
161 @Override
162 protected boolean handleIsEnumerationLiteral()
163 {
164 final ClassifierFacade owner = this.getOwner();
165 return (owner != null) && owner.isEnumeration();
166 }
167
168
169
170
171 @Override
172 protected String handleGetEnumerationValue()
173 {
174 String value = null;
175 if (this.isEnumerationLiteral())
176 {
177 value = this.getDefaultValue();
178 value = (StringUtils.isBlank(value)) ? this.handleGetName() : String.valueOf(value);
179 }
180 if (this.getType().isStringType() && value!=null && value.indexOf('"')<0)
181 {
182 value = '\"' + value + '\"';
183 }
184 return value;
185 }
186
187
188
189
190 @Override
191 protected boolean handleIsEnumerationMember()
192 {
193 boolean isMemberVariable = false;
194 final String isMemberVariableAsString = (String)this.findTaggedValue(
195 UMLProfile.TAGGEDVALUE_PERSISTENCE_ENUMERATION_MEMBER_VARIABLE);
196 if (StringUtils.isNotBlank(isMemberVariableAsString) && BooleanUtils.toBoolean(isMemberVariableAsString))
197 {
198 isMemberVariable = true;
199 }
200 return isMemberVariable;
201 }
202
203
204
205
206 @Override
207 protected String handleGetEnumerationLiteralParameters()
208 {
209 return (String)this.findTaggedValue(UMLProfile.TAGGEDVALUE_PERSISTENCE_ENUMERATION_LITERAL_PARAMETERS);
210 }
211
212
213
214
215 @Override
216 protected boolean handleIsEnumerationLiteralParametersExist()
217 {
218 boolean parametersExist = false;
219 if (StringUtils.isNotBlank(this.getEnumerationLiteralParameters()))
220 {
221 parametersExist = true;
222 }
223 return parametersExist;
224 }
225
226
227
228
229 @Override
230 protected String handleGetGetterSetterTypeName()
231 {
232 String name = null;
233 if (this.getUpper() > 1 || this.getUpper() == LiteralUnlimitedNatural.UNLIMITED)
234 {
235 final TypeMappings mappings = this.getLanguageMappings();
236
237
238
239
240
241
242
243
244
245
246 name =
247 this.isOrdered() ? mappings.getTo(UMLProfile.LIST_TYPE_NAME)
248 : mappings.getTo(UMLProfile.COLLECTION_TYPE_NAME);
249
250
251
252 if (BooleanUtils.toBoolean(
253 ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.ENABLE_TEMPLATING))))
254 {
255 String type = this.getType().getFullyQualifiedName();
256 if (this.getType().isPrimitive() || this.getLower() > 0)
257 {
258
259 type = this.getType().getWrapperName();
260 if (type == null)
261 {
262
263 type = this.getType().getFullyQualifiedName();
264 }
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279 name += '<' + type + '>';
280
281 }
282 }
283 if (name == null && this.handleGetType() != null)
284 {
285 name = this.getType().getFullyQualifiedName();
286
287
288 if (this.getType().isBooleanType())
289 {
290
291 if (this.getType().isPrimitive() && this.getLower() < 1 &&
292 (!(this instanceof EntityAttribute) || !((EntityAttribute) this).isIdentifier()))
293 {
294
295 name = this.getType().getWrapperName();
296 if (name == null)
297 {
298
299 name = this.getType().getFullyQualifiedName();
300 }
301 }
302
303
304
305
306 }
307 }
308 return name;
309 }
310
311
312
313
314 @Override
315 protected boolean handleIsOrdered()
316 {
317 return this.metaObject.isOrdered();
318 }
319
320
321
322
323 @Override
324 protected boolean handleIsUnique()
325 {
326 return this.metaObject.isUnique() || this.hasStereotype(UMLProfile.STEREOTYPE_UNIQUE);
327 }
328
329
330
331
332 @Override
333 protected Class handleGetOwner()
334 {
335
336 return this.metaObject.getClass_();
337 }
338
339
340
341
342 @Override
343 public Object getValidationOwner()
344 {
345 return this.getOwner();
346 }
347
348
349
350
351 @Override
352 protected Type handleGetType()
353 {
354 return this.metaObject.getType();
355 }
356
357
358
359
360 @Override
361 protected EnumerationFacade handleGetEnumeration()
362 {
363 return (EnumerationFacade)(this.isEnumerationLiteral() ? this.getOwner() : null);
364 }
365
366
367
368
369 @Override
370 protected boolean handleIsDefaultValuePresent()
371 {
372 return StringUtils.isNotBlank(this.getDefaultValue());
373 }
374
375
376
377
378
379
380 @Override
381 protected String handleGetName()
382 {
383 final String mask = String.valueOf(this.getConfiguredProperty(
384
385 this.getOwner() instanceof EnumerationFacade && !this.isEnumerationMember()
386 ? UMLMetafacadeProperties.ENUMERATION_LITERAL_NAME_MASK
387 : UMLMetafacadeProperties.CLASSIFIER_PROPERTY_NAME_MASK ));
388
389 String name = NameMasker.mask(super.handleGetName(), mask);
390 if (this.isMany() && this.isPluralizeAttributeNames() && !this.isEnumerationMember())
391 {
392 name = StringUtilsHelper.pluralize(name);
393 }
394
395 return name;
396 }
397
398
399
400
401
402
403 private boolean isPluralizeAttributeNames()
404 {
405 final Object value = this.getConfiguredProperty(UMLMetafacadeProperties.PLURALIZE_ATTRIBUTE_NAMES);
406 return value != null && Boolean.valueOf(String.valueOf(value));
407 }
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443 @Override
444 protected int handleGetUpper()
445 {
446
447 return UmlUtilities.parseMultiplicity(this.metaObject.getUpperValue(), 1);
448 }
449
450
451
452
453
454 @Override
455 protected int handleGetLower()
456 {
457
458 return UmlUtilities.parseLowerMultiplicity(this.metaObject.getLowerValue(),
459 this.getType(),
460 ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.DEFAULT_MULTIPLICITY)));
461 }
462
463
464
465
466
467
468 @Override
469 protected String handleGetVisibility()
470 {
471 String visibility = super.handleGetVisibility();
472 if (visibility==null || visibility.equals("private"))
473 {
474 visibility = "public";
475 }
476 return visibility;
477 }
478
479
480
481
482 @Override
483 protected Object handleFindTaggedValue(
484 String name,
485 final boolean follow)
486 {
487 name = StringUtils.trimToEmpty(name);
488 Object value = this.findTaggedValue(name);
489 if (follow)
490 {
491 ClassifierFacade type = this.getType();
492 while (value == null && type != null)
493 {
494 value = type.findTaggedValue(name);
495 type = (ClassifierFacade)type.getGeneralization();
496 }
497 }
498 return value;
499 }
500
501
502
503
504 @Override
505 protected boolean handleIsDerived()
506 {
507 return this.metaObject.isDerived();
508 }
509 }