001: package org.andromda.metafacades.emf.uml2;
002:
003: import org.andromda.core.metafacade.MetafacadeConstants;
004: import org.andromda.metafacades.uml.AttributeFacade;
005: import org.andromda.metafacades.uml.ClassifierFacade;
006: import org.andromda.metafacades.uml.ModelElementFacade;
007: import org.andromda.metafacades.uml.NameMasker;
008: import org.andromda.metafacades.uml.UMLMetafacadeProperties;
009: import org.apache.commons.collections.CollectionUtils;
010: import org.apache.commons.collections.Predicate;
011: import org.apache.commons.lang.BooleanUtils;
012: import org.apache.commons.lang.StringUtils;
013: import org.eclipse.uml2.Enumeration;
014: import org.eclipse.uml2.NamedElement;
015: import org.eclipse.uml2.EnumerationLiteral;
016:
017: import java.util.Collection;
018:
019: /**
020: * MetafacadeLogic implementation for
021: * org.andromda.metafacades.uml.EnumerationFacade.
022: *
023: * @see org.andromda.metafacades.uml.EnumerationFacade
024: */
025: public class EnumerationFacadeLogicImpl extends EnumerationFacadeLogic {
026: public EnumerationFacadeLogicImpl(final Object metaObject,
027: final String context) {
028: super (metaObject, context);
029: }
030:
031: /**
032: * Overridden to provide name masking.
033: *
034: * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
035: */
036: protected String handleGetName() {
037: final String nameMask = String
038: .valueOf(this
039: .getConfiguredProperty(UMLMetafacadeProperties.ENUMERATION_NAME_MASK));
040: return NameMasker.mask(super .handleGetName(), nameMask);
041: }
042:
043: /**
044: * @see org.andromda.metafacades.uml.EnumerationFacade#getLiterals()
045: */
046: protected java.util.Collection handleGetLiterals() {
047: // To Check: could be sufficient to return the collection of literals only
048: // without filtering
049: final Collection literals = (this .metaObject instanceof Enumeration ? ((Enumeration) this .metaObject)
050: .getOwnedLiterals()
051: : CollectionUtils.collect(this .getAttributes(),
052: UmlUtilities.ELEMENT_TRANSFORMER));
053:
054: CollectionUtils.filter(literals, new Predicate() {
055: public boolean evaluate(Object object) {
056: // evaluates to true in case it's a native literal
057: // or an attribute which is not considered a regular member
058: return (object instanceof EnumerationLiteral)
059: || (object instanceof AttributeFacade && !((AttributeFacade) object)
060: .isEnumerationMember());
061: }
062: });
063: return literals;
064: }
065:
066: /**
067: * @see org.andromda.metafacades.uml.EnumerationFacade#getMemberVariables()
068: */
069: protected Collection handleGetMemberVariables() {
070: // To Check: could be sufficient to return the collection of attributes only
071: // without filtering
072: final Collection variables = (this .metaObject instanceof Enumeration ? ((Enumeration) this .metaObject)
073: .getOwnedLiterals()
074: : CollectionUtils.collect(this .getAttributes(),
075: UmlUtilities.ELEMENT_TRANSFORMER));
076:
077: CollectionUtils.filter(variables, new Predicate() {
078: public boolean evaluate(Object object) {
079: // evaluates to true in case it's NOT a native literal
080: // but an attribute which considered as a regular member
081: return object instanceof AttributeFacade
082: && ((AttributeFacade) object)
083: .isEnumerationMember();
084: }
085: });
086: return variables;
087: }
088:
089: /**
090: * @see org.andromda.metafacades.uml.EnumerationFacade#getFromOperationSignature()
091: */
092: protected String handleGetFromOperationSignature() {
093: final StringBuffer signature = new StringBuffer(this
094: .getFromOperationName());
095: final ClassifierFacade type = this .getLiteralType();
096: if (type != null) {
097: signature.append('(');
098: signature.append(type.getFullyQualifiedName());
099: signature.append(" value)");
100: }
101: return signature.toString();
102: }
103:
104: /**
105: * @see org.andromda.metafacades.uml.EnumerationFacade#isTypeSafe()
106: */
107: protected boolean handleIsTypeSafe() {
108: return BooleanUtils
109: .toBoolean(String
110: .valueOf(this
111: .getConfiguredProperty(UMLMetafacadeProperties.TYPE_SAFE_ENUMS_ENABLED)));
112: }
113:
114: /**
115: * @see org.andromda.metafacades.uml.EnumerationFacade#getFromOperationName()
116: */
117: protected String handleGetFromOperationName() {
118: final StringBuffer name = new StringBuffer("from");
119: final ClassifierFacade type = this .getLiteralType();
120: if (type != null) {
121: name.append(StringUtils.capitalize(type.getName()));
122: }
123: return name.toString();
124: }
125:
126: /**
127: * @see org.andromda.metafacades.uml.EnumerationFacade#getLiteralType()
128: */
129: protected Object handleGetLiteralType() {
130: Object type = null;
131: final Collection literals = this .getLiterals();
132: if (literals != null && !literals.isEmpty()) {
133: ModelElementFacade literal = (ModelElementFacade) literals
134: .iterator().next();
135: if (literal instanceof AttributeFacade) {
136: type = ((AttributeFacade) literals.iterator().next())
137: .getType();
138: } else {
139: type = UmlUtilities.findByFullyQualifiedName(
140: ((NamedElement) this .metaObject).eResource()
141: .getResourceSet(),
142: "datatype::String", // todo: use this (doesn't work for some reason): UMLMetafacadeProperties.DEFAULT_ENUMERATION_LITERAL_TYPE,
143: MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR,
144: true);
145: }
146: }
147: return type;
148: }
149: }
|