001package org.andromda.metafacades.uml14;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.LinkedHashSet;
006import java.util.Set;
007import org.andromda.metafacades.uml.DependencyFacade;
008import org.andromda.metafacades.uml.Destination;
009import org.andromda.metafacades.uml.Entity;
010import org.andromda.metafacades.uml.FilteredCollection;
011import org.andromda.metafacades.uml.ModelElementFacade;
012import org.andromda.metafacades.uml.Role;
013import org.andromda.metafacades.uml.Service;
014import org.andromda.metafacades.uml.ServiceOperation;
015import org.apache.commons.collections.Closure;
016import org.apache.commons.collections.CollectionUtils;
017import org.apache.commons.collections.Predicate;
018import org.apache.commons.collections.Transformer;
019
020/**
021 * Metaclass facade implementation.
022 * @author Bob Fields
023 */
024public class ServiceLogicImpl
025        extends ServiceLogic
026{
027    // ---------------- constructor -------------------------------
028
029    /**
030     * @param metaObject
031     * @param context
032     */
033    public ServiceLogicImpl(Object metaObject, String context)
034    {
035        super(metaObject, context);
036    }
037
038    /**
039     * @see org.andromda.metafacades.uml.Service#getEntityReferences()
040     */
041    @Override
042    public Collection<DependencyFacade> handleGetEntityReferences()
043    {
044        return new FilteredCollection(this.getSourceDependencies())
045        {
046            private static final long serialVersionUID = 34L;
047            public boolean evaluate(Object object)
048            {
049                ModelElementFacade targetElement = ((DependencyFacade)object).getTargetElement();
050                return targetElement != null && Entity.class.isAssignableFrom(targetElement.getClass());
051            }
052        };
053    }
054
055    /**
056     * @see org.andromda.metafacades.uml.Service#getServiceReferences()
057     */
058    @Override
059    public Collection<DependencyFacade> handleGetServiceReferences()
060    {
061        return new FilteredCollection(this.getSourceDependencies())
062        {
063            private static final long serialVersionUID = 34L;
064            public boolean evaluate(Object object)
065            {
066                ModelElementFacade targetElement = ((DependencyFacade)object).getTargetElement();
067                return targetElement != null && Service.class.isAssignableFrom(targetElement.getClass());
068            }
069        };
070    }
071
072    /**
073     * @see org.andromda.metafacades.uml.Service#getAllServiceReferences()
074     */
075    @Override
076    public Collection<DependencyFacade> handleGetAllServiceReferences()
077    {
078        final Collection<DependencyFacade> result = new LinkedHashSet<DependencyFacade>();
079
080        // get references of the service itself
081        result.addAll(getServiceReferences());
082
083        // get references of all super classes
084        CollectionUtils.forAllDo(this.getAllGeneralizations(), new Closure()
085        {
086            public void execute(Object object)
087            {
088                if (object instanceof Service)
089                {
090                    final Service service = (Service)object;
091                    result.addAll(service.getServiceReferences());
092                }
093            }
094        });
095        return result;
096    }
097
098    /**
099     * @see org.andromda.metafacades.uml.Service#getRoles()
100     */
101    @Override
102    protected Collection handleGetRoles()
103    {
104        final Collection roles = new ArrayList(this.getTargetDependencies());
105        CollectionUtils.filter(roles, new Predicate()
106        {
107            public boolean evaluate(final Object object)
108            {
109                DependencyFacade dependency = (DependencyFacade)object;
110                return dependency != null && dependency.getSourceElement() instanceof Role;
111            }
112        });
113        CollectionUtils.transform(roles, new Transformer()
114        {
115            public Object transform(final Object object)
116            {
117                return ((DependencyFacade)object).getSourceElement();
118            }
119        });
120        final Collection allRoles = new LinkedHashSet(roles);
121        // add all roles which are generalizations of this one
122        CollectionUtils.forAllDo(roles, new Closure()
123        {
124            public void execute(final Object object)
125            {
126                allRoles.addAll(((Role)object).getAllSpecializations());
127            }
128        });
129        return allRoles;
130    }
131
132    /**
133     * @see org.andromda.metafacades.uml.Service#getAllRoles()
134     */
135    @Override
136    protected Collection<Role> handleGetAllRoles()
137    {
138        final Collection<Role> roles = new LinkedHashSet<Role>(this.getRoles());
139        CollectionUtils.forAllDo(this.getOperations(), new Closure()
140        {
141            public void execute(Object object)
142            {
143                if (object instanceof ServiceOperation)
144                {
145                    roles.addAll(((ServiceOperation)object).getRoles());
146                }
147            }
148        });
149        return roles;
150    }
151
152    /**
153     * @see org.andromda.metafacades.uml.Service#getMessagingDestinations()
154     */
155    @Override
156    protected Collection<Destination> handleGetMessagingDestinations()
157    {
158        final Set<Destination> destinations = new LinkedHashSet<Destination>();
159        CollectionUtils.forAllDo(this.getOperations(), new Closure()
160        {
161            public void execute(Object object)
162            {
163                if (object instanceof ServiceOperation)
164                {
165                    final ServiceOperation operation = (ServiceOperation)object;
166                    if (operation.isIncomingMessageOperation())
167                    {
168                        destinations.add(operation.getIncomingDestination());
169                    }
170                    else if (operation.isOutgoingMessageOperation())
171                    {
172                        destinations.add(operation.getOutgoingDestination());
173                    }
174                }
175            }
176        });
177        return destinations;
178    }
179
180    /**
181     * @see org.andromda.metafacades.uml.Service#getAllEntityReferences()
182     */
183    @Override
184    protected Collection<DependencyFacade> handleGetAllEntityReferences()
185    {
186        final Collection<DependencyFacade> result = new LinkedHashSet<DependencyFacade>();
187
188        // get references of the service itself
189        result.addAll(this.getEntityReferences());
190
191        // get references of all super classes
192        CollectionUtils.forAllDo(this.getAllGeneralizations(), new Closure()
193        {
194            public void execute(Object object)
195            {
196                if (object instanceof Service)
197                {
198                    final Service service = (Service)object;
199                    result.addAll(service.getEntityReferences());
200                }
201            }
202        });
203        return result;
204    }
205
206    /**
207     * @see org.andromda.metafacades.uml.Service#getAllMessagingDestinations()
208     */
209    @Override
210    protected Collection<Destination> handleGetAllMessagingDestinations()
211    {
212        final Collection<Destination> result = new LinkedHashSet<Destination>();
213
214        // get references of the service itself
215        result.addAll(this.getMessagingDestinations());
216
217        // get references of all super classes
218        CollectionUtils.forAllDo(this.getAllGeneralizations(), new Closure()
219        {
220            public void execute(Object object)
221            {
222                if (object instanceof Service)
223                {
224                    final Service service = (Service)object;
225                    result.addAll(service.getMessagingDestinations());
226                }
227            }
228        });
229        return result;
230    }
231}