View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.LinkedHashSet;
6   import java.util.Set;
7   import org.andromda.metafacades.uml.DependencyFacade;
8   import org.andromda.metafacades.uml.Destination;
9   import org.andromda.metafacades.uml.Entity;
10  import org.andromda.metafacades.uml.FilteredCollection;
11  import org.andromda.metafacades.uml.ModelElementFacade;
12  import org.andromda.metafacades.uml.Role;
13  import org.andromda.metafacades.uml.Service;
14  import org.andromda.metafacades.uml.ServiceOperation;
15  import org.apache.commons.collections.Closure;
16  import org.apache.commons.collections.CollectionUtils;
17  import org.apache.commons.collections.Predicate;
18  import org.apache.commons.collections.Transformer;
19  
20  /**
21   * MetafacadeLogic implementation for org.andromda.metafacades.uml.Service.
22   *
23   * @see org.andromda.metafacades.uml.Service
24   */
25  public class ServiceLogicImpl extends ServiceLogic
26  {
27      private static final long serialVersionUID = 34L;
28      /**
29       * @param metaObject
30       * @param context
31       */
32      public ServiceLogicImpl(final Object metaObject, final String context)
33      {
34          super(metaObject, context);
35      }
36  
37      /**
38       * @see org.andromda.metafacades.uml.Service#getServiceReferences()
39       */
40      @Override
41      protected Collection<DependencyFacade> handleGetServiceReferences()
42      {
43          return new FilteredCollection(this.getSourceDependencies())
44          {
45              private static final long serialVersionUID = 34L;
46              @Override
47              public boolean evaluate(final Object object)
48              {
49                  final ModelElementFacade targetElement = ((DependencyFacade)object).getTargetElement();
50                  return targetElement != null && Service.class.isAssignableFrom(targetElement.getClass());
51              }
52          };
53      }
54  
55      /**
56       * @see org.andromda.metafacades.uml.Service#getEntityReferences()
57       */
58      @Override
59      protected Collection<DependencyFacade> handleGetEntityReferences()
60      {
61          return new FilteredCollection(this.getSourceDependencies())
62          {
63              private static final long serialVersionUID = 34L;
64              @Override
65              public boolean evaluate(final Object object)
66              {
67                  final ModelElementFacade targetElement = ((DependencyFacade)object).getTargetElement();
68                  return targetElement != null && Entity.class.isAssignableFrom(targetElement.getClass());
69              }
70          };
71      }
72  
73      /**
74       * @see org.andromda.metafacades.uml.Service#getAllServiceReferences()
75       */
76      @Override
77      public Collection<DependencyFacade> handleGetAllServiceReferences()
78      {
79          final Collection<DependencyFacade> result = new ArrayList<DependencyFacade>();
80  
81          // get references of the service itself
82          result.addAll(this.getServiceReferences());
83  
84          // get references of all super classes
85          CollectionUtils.forAllDo(
86              this.getAllGeneralizations(),
87              new Closure()
88              {
89                  public void execute(final Object object)
90                  {
91                      final Service service = (Service)object;
92                      result.addAll(service.getServiceReferences());
93                  }
94              });
95          return result;
96      }
97  
98      /**
99       * @see org.andromda.metafacades.uml.Service#getRoles()
100      */
101     @Override
102     protected Collection<Role> handleGetRoles()
103     {
104         final Collection<DependencyFacade> roles = new ArrayList<DependencyFacade>(this.getTargetDependencies());
105         CollectionUtils.filter(
106             roles,
107             new Predicate()
108             {
109                 public boolean evaluate(final Object object)
110                 {
111                     final DependencyFacade dependency = (DependencyFacade)object;
112                     return dependency != null && dependency.getSourceElement() instanceof Role;
113                 }
114             });
115         CollectionUtils.transform(
116             roles,
117             new Transformer()
118             {
119                 public Object transform(final Object object)
120                 {
121                     return ((DependencyFacade)object).getSourceElement();
122                 }
123             });
124 
125         final Collection allRoles = new LinkedHashSet(roles);
126 
127         // add all roles which are generalizations of this one
128         CollectionUtils.forAllDo(
129             roles,
130             new Closure()
131             {
132                 public void execute(final Object object)
133                 {
134                     allRoles.addAll(((Role)object).getAllSpecializations());
135                 }
136             });
137         return allRoles;
138     }
139 
140     /**
141      * @see org.andromda.metafacades.uml.Service#getAllRoles()
142      */
143     @Override
144     protected Collection<Role> handleGetAllRoles()
145     {
146         final Collection<Role> roles = new LinkedHashSet<Role>(this.getRoles());
147         CollectionUtils.forAllDo(
148             this.getOperations(),
149             new Closure()
150             {
151                 public void execute(final Object object)
152                 {
153                     if (object instanceof ServiceOperation)
154                     {
155                         roles.addAll(((ServiceOperation)object).getRoles());
156                     }
157                 }
158             });
159         return roles;
160     }
161 
162     /**
163      * @see org.andromda.metafacades.uml.Service#getMessagingDestinations()
164      */
165     @Override
166     protected Collection<Destination> handleGetMessagingDestinations()
167     {
168         final Set<Destination> destinations = new LinkedHashSet<Destination>();
169         CollectionUtils.forAllDo(this.getOperations(), new Closure()
170         {
171             public void execute(final Object object)
172             {
173                 if (object instanceof ServiceOperation)
174                 {
175                     final ServiceOperation operation = (ServiceOperation)object;
176                     if (operation.isIncomingMessageOperation())
177                     {
178                         destinations.add(operation.getIncomingDestination());
179                     }
180                     else if (operation.isOutgoingMessageOperation())
181                     {
182                         destinations.add(operation.getOutgoingDestination());
183                     }
184                 }
185             }
186         });
187         return destinations;
188     }
189 
190     /**
191      * @see org.andromda.metafacades.uml.Service#getAllEntityReferences()
192      */
193     @Override
194     protected Collection<DependencyFacade> handleGetAllEntityReferences()
195     {
196         final Collection<DependencyFacade> result = new LinkedHashSet<DependencyFacade>();
197 
198         // get references of the service itself
199         result.addAll(this.getEntityReferences());
200 
201         // get references of all super classes
202         CollectionUtils.forAllDo(this.getAllGeneralizations(), new Closure()
203         {
204             public void execute(final Object object)
205             {
206                 if (object instanceof Service)
207                 {
208                     final Service service = (Service)object;
209                     result.addAll(service.getEntityReferences());
210                 }
211             }
212         });
213         return result;
214     }
215 
216     /**
217      * @see org.andromda.metafacades.uml.Service#getAllMessagingDestinations()
218      */
219     @Override
220     protected Collection<Destination> handleGetAllMessagingDestinations()
221     {
222         final Collection<Destination> result = new LinkedHashSet<Destination>();
223 
224         // get references of the service itself
225         result.addAll(this.getMessagingDestinations());
226 
227         // get references of all super classes
228         CollectionUtils.forAllDo(this.getAllGeneralizations(), new Closure()
229         {
230             public void execute(final Object object)
231             {
232                 if (object instanceof Service)
233                 {
234                     final Service service = (Service)object;
235                     result.addAll(service.getMessagingDestinations());
236                 }
237             }
238 
239         });
240         return result;
241     }
242 }