001package org.andromda.metafacades.uml14;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.LinkedHashSet;
006import org.andromda.metafacades.uml.DependencyFacade;
007import org.andromda.metafacades.uml.Destination;
008import org.andromda.metafacades.uml.ModelElementFacade;
009import org.andromda.metafacades.uml.Role;
010import org.andromda.metafacades.uml.Service;
011import org.apache.commons.collections.Closure;
012import org.apache.commons.collections.CollectionUtils;
013import org.apache.commons.collections.Predicate;
014import org.apache.commons.collections.Transformer;
015
016
017/**
018 * MetafacadeLogic implementation for org.andromda.metafacades.uml.ServiceOperation.
019 *
020 * @see org.andromda.metafacades.uml.ServiceOperation
021 * @author Bob Fields
022 */
023public class ServiceOperationLogicImpl
024    extends ServiceOperationLogic
025{
026    private static final long serialVersionUID = -8122318748041405360L;
027    // ---------------- constructor -------------------------------
028    /**
029     * @param metaObject
030     * @param context
031     */
032    public ServiceOperationLogicImpl(
033        Object metaObject,
034        String context)
035    {
036        super(metaObject, context);
037    }
038
039    /**
040     * @see org.andromda.metafacades.uml.ServiceOperation#getRoles()
041     */
042    @Override
043    public Collection handleGetRoles()
044    {
045        final Collection roles = new LinkedHashSet();
046        if (this.getOwner() instanceof Service)
047        {
048            roles.addAll(((Service)this.getOwner()).getRoles());
049        }
050        final Collection operationRoles = new ArrayList(this.getTargetDependencies());
051        CollectionUtils.filter(
052            operationRoles,
053            new Predicate()
054            {
055                public boolean evaluate(Object object)
056                {
057                    DependencyFacade dependency = (DependencyFacade)object;
058                    return dependency != null && dependency.getSourceElement() != null &&
059                    Role.class.isAssignableFrom(dependency.getSourceElement().getClass());
060                }
061            });
062        CollectionUtils.transform(
063            operationRoles,
064            new Transformer()
065            {
066                public Object transform(Object object)
067                {
068                    return ((DependencyFacade)object).getSourceElement();
069                }
070            });
071        roles.addAll(operationRoles);
072        final Collection allRoles = new LinkedHashSet(roles);
073
074        // add all roles which are specializations of this one
075        CollectionUtils.forAllDo(
076            roles,
077            new Closure()
078            {
079                public void execute(Object object)
080                {
081                    if (object instanceof Role)
082                    {
083                        allRoles.addAll(((Role)object).getAllSpecializations());
084                    }
085                }
086            });
087        return allRoles;
088    }
089
090    /**
091     * @see org.andromda.metafacades.uml.ServiceOperation#getService()
092     */
093    @Override
094    protected Service handleGetService()
095    {
096        Service owner = null;
097        if (this.getOwner() instanceof Service)
098        {
099            owner = (Service)this.getOwner();
100        }
101        return owner;
102    }
103
104    /**
105     * @see org.andromda.metafacades.uml.ServiceOperation#isMessageOperation()
106     */
107    @Override
108    public boolean handleIsMessageOperation()
109    {
110        return this.isIncomingMessageOperation() || this.isOutgoingMessageOperation();
111    }
112
113    /**
114     * @see org.andromda.metafacades.uml.ServiceOperation#isIncomingMessageOperation()
115     */
116    @Override
117    public boolean handleIsIncomingMessageOperation()
118    {
119        return this.getIncomingDestination() != null;
120    }
121
122    /**
123     * @see org.andromda.metafacades.uml.ServiceOperation#isOutgoingMessageOperation()
124     */
125    @Override
126    public boolean handleIsOutgoingMessageOperation()
127    {
128        return this.getOutgoingDestination() != null;
129    }
130
131    /**
132     * @see org.andromda.metafacades.uml.ServiceOperation#getIncomingDestination()
133     */
134    @Override
135    public ModelElementFacade handleGetIncomingDestination()
136    {
137        final Collection<DependencyFacade> dependencies = this.getTargetDependencies();
138        final DependencyFacade dependency = (DependencyFacade)
139            CollectionUtils.find(dependencies,
140                new Predicate() {
141
142                    public boolean evaluate(Object object)
143                    {
144                        return ((DependencyFacade)object).getSourceElement() instanceof Destination;
145                    }});
146        return dependency != null ? dependency.getSourceElement() : null;
147    }
148
149    /**
150     * @see org.andromda.metafacades.uml.ServiceOperation#getOutgoingDestination()
151     */
152    @Override
153    public ModelElementFacade handleGetOutgoingDestination()
154    {
155        final Collection<DependencyFacade> dependencies = this.getSourceDependencies();
156        final DependencyFacade dependency = (DependencyFacade)
157        CollectionUtils.find(dependencies,
158            new Predicate() {
159
160                public boolean evaluate(Object object)
161                {
162                    return ((DependencyFacade)object).getTargetElement() instanceof Destination;
163                }});
164        return dependency != null ? dependency.getTargetElement() : null;
165    }
166}