001package org.andromda.metafacades.uml14;
002
003import java.util.Collection;
004import java.util.Iterator;
005import java.util.LinkedHashSet;
006import org.andromda.metafacades.uml.AssociationEndFacade;
007import org.andromda.metafacades.uml.ClassifierFacade;
008import org.andromda.metafacades.uml.DependencyFacade;
009import org.andromda.metafacades.uml.FrontEndUseCase;
010import org.andromda.metafacades.uml.GeneralizableElementFacade;
011import org.andromda.metafacades.uml.NameMasker;
012import org.andromda.metafacades.uml.Service;
013import org.andromda.metafacades.uml.ServiceOperation;
014import org.andromda.metafacades.uml.UMLMetafacadeProperties;
015import org.andromda.metafacades.uml.UMLProfile;
016import org.apache.commons.collections.CollectionUtils;
017import org.apache.commons.collections.Predicate;
018import org.apache.commons.lang.StringUtils;
019
020/**
021 * MetafacadeLogic implementation for org.andromda.metafacades.uml.Role.
022 *
023 * @see org.andromda.metafacades.uml.Role
024 * @author Bob Fields
025 */
026public class RoleLogicImpl
027    extends RoleLogic
028{
029    private static final long serialVersionUID = -2715508135820019632L;
030    // ---------------- constructor -------------------------------
031    /**
032     * @param metaObject
033     * @param context
034     */
035    public RoleLogicImpl(
036        Object metaObject,
037        String context)
038    {
039        super(metaObject, context);
040    }
041
042    /**
043     * @see org.andromda.metafacades.uml14.ModelElementFacadeLogic#handleGetName()
044     */
045    public String handleGetName()
046    {
047        String name;
048        Object value = this.findTaggedValue(UMLProfile.TAGGEDVALUE_ROLE_NAME);
049        if (value != null)
050        {
051            name = StringUtils.trimToEmpty(String.valueOf(value));
052        }
053        else
054        {
055            name = super.handleGetName();
056            String mask =
057                StringUtils.trimToEmpty(
058                    String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.ROLE_NAME_MASK)));
059            name = NameMasker.mask(name, mask);
060        }
061        return name;
062    }
063
064    /**
065     * @see org.andromda.metafacades.uml.Role#isReferencesPresent()
066     */
067    @Override
068    protected boolean handleIsReferencesPresent()
069    {
070        final Collection<DependencyFacade> allSourceDependencies = new LinkedHashSet<DependencyFacade>(this.getSourceDependencies());
071        for (
072            GeneralizableElementFacade parent = this.getGeneralization(); parent != null;
073            parent = parent.getGeneralization())
074        {
075            allSourceDependencies.addAll(parent.getSourceDependencies());
076        }
077        boolean present =
078            CollectionUtils.find(
079                allSourceDependencies,
080                new Predicate()
081                {
082                    public boolean evaluate(Object object)
083                    {
084                        DependencyFacade dependency = (DependencyFacade)object;
085                        Object target = dependency.getTargetElement();
086                        return target instanceof Service || target instanceof ServiceOperation;
087                    }
088                }) != null;
089
090        // - if no references on any services, try the FrontEndUseCases
091        if (!present)
092        {
093            final Collection<AssociationEndFacade> associationEnds = this.getAssociationEnds();
094            for (final Iterator<AssociationEndFacade> iterator = associationEnds.iterator(); iterator.hasNext() && !present;)
095            {
096                final AssociationEndFacade associationEnd = iterator.next();
097                final ClassifierFacade classifier = associationEnd.getOtherEnd().getType();
098                present = classifier instanceof FrontEndUseCase;
099            }
100
101            // - a generalized role is still a role, and therefore is associated with the FrontEndUseCase
102            if (!present)
103            {
104                present = !this.getGeneralizedActors().isEmpty();
105            }
106        }
107
108        return present;
109    }
110}