View Javadoc
1   package org.andromda.metafacades.uml14;
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   * Metaclass facade implementation.
22   * @author Bob Fields
23   */
24  public class ServiceLogicImpl
25          extends ServiceLogic
26  {
27      // ---------------- constructor -------------------------------
28  
29      /**
30       * @param metaObject
31       * @param context
32       */
33      public ServiceLogicImpl(Object metaObject, String context)
34      {
35          super(metaObject, context);
36      }
37  
38      /**
39       * @see org.andromda.metafacades.uml.Service#getEntityReferences()
40       */
41      @Override
42      public Collection<DependencyFacade> handleGetEntityReferences()
43      {
44          return new FilteredCollection(this.getSourceDependencies())
45          {
46              private static final long serialVersionUID = 34L;
47              public boolean evaluate(Object object)
48              {
49                  ModelElementFacade targetElement = ((DependencyFacade)object).getTargetElement();
50                  return targetElement != null && Entity.class.isAssignableFrom(targetElement.getClass());
51              }
52          };
53      }
54  
55      /**
56       * @see org.andromda.metafacades.uml.Service#getServiceReferences()
57       */
58      @Override
59      public Collection<DependencyFacade> handleGetServiceReferences()
60      {
61          return new FilteredCollection(this.getSourceDependencies())
62          {
63              private static final long serialVersionUID = 34L;
64              public boolean evaluate(Object object)
65              {
66                  ModelElementFacade targetElement = ((DependencyFacade)object).getTargetElement();
67                  return targetElement != null && Service.class.isAssignableFrom(targetElement.getClass());
68              }
69          };
70      }
71  
72      /**
73       * @see org.andromda.metafacades.uml.Service#getAllServiceReferences()
74       */
75      @Override
76      public Collection<DependencyFacade> handleGetAllServiceReferences()
77      {
78          final Collection<DependencyFacade> result = new LinkedHashSet<DependencyFacade>();
79  
80          // get references of the service itself
81          result.addAll(getServiceReferences());
82  
83          // get references of all super classes
84          CollectionUtils.forAllDo(this.getAllGeneralizations(), new Closure()
85          {
86              public void execute(Object object)
87              {
88                  if (object instanceof Service)
89                  {
90                      final Service service = (Service)object;
91                      result.addAll(service.getServiceReferences());
92                  }
93              }
94          });
95          return result;
96      }
97  
98      /**
99       * @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 }