View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.Iterator;
7   import java.util.List;
8   import org.andromda.core.metafacade.MetafacadeBase;
9   import org.andromda.metafacades.uml.ActionStateFacade;
10  import org.andromda.metafacades.uml.ActivityGraphFacade;
11  import org.andromda.metafacades.uml.ClassifierFacade;
12  import org.andromda.metafacades.uml.FinalStateFacade;
13  import org.andromda.metafacades.uml.UMLProfile;
14  import org.andromda.metafacades.uml.UseCaseFacade;
15  import org.apache.commons.collections.CollectionUtils;
16  import org.apache.commons.collections.Predicate;
17  import org.apache.commons.lang.StringUtils;
18  import org.apache.log4j.Logger;
19  import org.eclipse.uml2.uml.Actor;
20  import org.eclipse.uml2.uml.Class;
21  import org.eclipse.uml2.uml.FinalState;
22  import org.eclipse.uml2.uml.Package;
23  import org.eclipse.uml2.uml.State;
24  import org.eclipse.uml2.uml.StateMachine;
25  import org.eclipse.uml2.uml.Transition;
26  import org.eclipse.uml2.uml.UseCase;
27  import org.eclipse.uml2.uml.resource.UMLResource;
28  
29  /**
30   * MetafacadeLogic implementation for org.andromda.metafacades.uml.ModelFacade.
31   *
32   * @see org.andromda.metafacades.uml.ModelFacade
33   */
34  public class ModelFacadeLogicImpl
35      extends ModelFacadeLogic
36  {
37      private static final long serialVersionUID = -7114972076481221838L;
38  
39      /**
40       * @param metaObject
41       * @param context
42       */
43      public ModelFacadeLogicImpl(
44          final UMLResource metaObject,
45          final String context)
46      {
47          super(metaObject, context);
48      }
49  
50      /**
51       * The logger instance.
52       */
53      private static final Logger LOGGER = Logger.getLogger(ModelFacadeLogicImpl.class);
54  
55      /**
56       * @see org.andromda.metafacades.uml.ModelFacade#findUseCaseWithTaggedValueOrHyperlink(String,
57       *      String)
58       */
59      @Override
60      protected UseCaseFacade handleFindUseCaseWithTaggedValueOrHyperlink(
61          final String tag,
62          final String value)
63      {
64          UseCaseFacade useCaseWithTaggedValue = null;
65  
66          final Collection<UseCaseFacade> useCases = this.getAllUseCases();
67          for (final Iterator<UseCaseFacade> useCaseIterator = useCases.iterator(); useCaseIterator.hasNext()
68              && useCaseWithTaggedValue == null;)
69          {
70              final UseCaseFacade useCase = useCaseIterator.next();
71              // UML2: taggedValue must belong to a defined stereotype
72              if (useCase.findTaggedValue(tag, true) == null)
73              {
74                  final Object taggedValue = useCase.findTaggedValue(UMLProfile.TAGGEDVALUE_MODEL_HYPERLINK);
75                  if (taggedValue != null)
76                  {
77                      useCaseWithTaggedValue = useCase;
78                  }
79              }
80              else
81              {
82                  useCaseWithTaggedValue = useCase;
83              }
84          }
85  
86          //throw new UnsupportedOperationException();
87          return useCaseWithTaggedValue;
88      }
89  
90      /**
91       *
92       * @see org.andromda.metafacades.uml.ModelFacade#findClassWithTaggedValueOrHyperlink(String,
93       *      String)
94       * @throws UnsupportedOperationException always
95       */
96      @Override
97      protected ClassifierFacade handleFindClassWithTaggedValueOrHyperlink(
98          final String tag,
99          final String value)
100     {
101         ClassifierFacade classWithTaggedValue = null;
102 
103         final Collection<ClassifierFacade> classes = this.getAllClasses();
104         for (final Iterator<ClassifierFacade> classIterator = classes.iterator(); classIterator.hasNext()
105             && classWithTaggedValue == null;)
106         {
107             final ClassifierFacade clazz = classIterator.next();
108             if (clazz.findTaggedValue(tag, true) == null)
109             {
110                 final Object taggedValue = clazz.findTaggedValue(UMLProfile.TAGGEDVALUE_MODEL_HYPERLINK);
111                 if (taggedValue != null)
112                 {
113                     classWithTaggedValue = clazz;
114                 }
115             }
116             else
117             {
118                 classWithTaggedValue = clazz;
119             }
120         }
121 
122         return classWithTaggedValue;
123     }
124 
125     /**
126      * @see org.andromda.metafacades.uml.ModelFacade#findActivityGraphByName(String)
127      */
128     @Override
129     protected ActivityGraphFacade handleFindActivityGraphByName(
130         final String name)
131     {
132         return this.findActivityGraphByNameAndStereotype(
133             name,
134             null);
135     }
136 
137     /**
138      * @see org.andromda.metafacades.uml.ModelFacade#findActivityGraphByNameAndStereotype(String,
139      *      String)
140      */
141     @Override
142     protected ActivityGraphFacade handleFindActivityGraphByNameAndStereotype(
143         final String name,
144         final String stereotypeName)
145     {
146         ActivityGraphFacade agfFound = null;
147 
148         final List<StateMachine> agfCollection =
149             (List<StateMachine>) UmlUtilities.getAllMetaObjectsInstanceOf(
150             StateMachine.class,
151             UmlUtilities.getModels());
152 
153         for (final Iterator<StateMachine> it = agfCollection.iterator(); it.hasNext() && agfFound == null;)
154         {
155             final ActivityGraphFacade agf = (ActivityGraphFacade)this.shieldedElement(it.next());
156             if (agf.getName().equals(name)
157                 && (stereotypeName == null || agf.hasStereotype(stereotypeName)))
158             {
159                 agfFound = agf;
160             }
161         }
162         return agfFound;
163     }
164 
165     /**
166      * @see org.andromda.metafacades.uml.ModelFacade#findUseCaseByName(String)
167      */
168     @Override
169     protected UseCaseFacade handleFindUseCaseByName(final String name)
170     {
171         return this.findUseCaseWithNameAndStereotype(
172             name,
173             null);
174     }
175 
176     /**
177      * @see org.andromda.metafacades.uml.ModelFacade#findUseCaseWithNameAndStereotype(String,
178      *      String)
179      */
180     @Override
181     protected UseCaseFacade handleFindUseCaseWithNameAndStereotype(
182         final String name,
183         final String stereotypeName)
184     {
185         UseCaseFacade ucfFound = null;
186         final Collection<UseCaseFacade> ucCollections = this.getAllUseCases();
187         for (final Iterator<UseCaseFacade> it = ucCollections.iterator(); it.hasNext() && ucfFound == null;)
188         {
189             final UseCaseFacade ucf = it.next();
190             if (ucf.getName().equals(name) &&
191                 (stereotypeName == null || ucf.hasStereotype(stereotypeName)))
192             {
193                 ucfFound = ucf;
194             }
195         }
196         return ucfFound;
197     }
198 
199     /**
200      * @see org.andromda.metafacades.uml.ModelFacade#findFinalStatesWithNameOrHyperlink(org.andromda.metafacades.uml.UseCaseFacade)
201      */
202     @Override
203     protected Collection<FinalStateFacade> handleFindFinalStatesWithNameOrHyperlink(
204         final UseCaseFacade useCase)
205     {
206         final List<FinalState> fsCollection =
207             (List<FinalState>) UmlUtilities.getAllMetaObjectsInstanceOf(
208             FinalState.class,
209             UmlUtilities.getModels());
210         CollectionUtils.filter(
211             fsCollection,
212             new Predicate()
213             {
214                 public boolean evaluate(final Object candidate)
215                 {
216                     final FinalState finalState = (FinalState)candidate;
217                     return (finalState != null && StringUtils.isNotBlank(finalState.getName()) && finalState.getName().equals(useCase.getName()));
218                 }
219             });
220 
221         return this.shieldedElements(fsCollection);
222     }
223 
224     /**
225      * @see org.andromda.metafacades.uml.ModelFacade#getAllActionStatesWithStereotype(org.andromda.metafacades.uml.ActivityGraphFacade,
226      *      String)
227      */
228     @Override
229     protected Collection<ActionStateFacade> handleGetAllActionStatesWithStereotype(
230         final ActivityGraphFacade activityGraph,
231         final String stereotypeName)
232     {
233         final Collection<ActionStateFacade> asCollection = this.getAllActionStates();
234         CollectionUtils.filter(
235             asCollection,
236             new Predicate()
237             {
238                 public boolean evaluate(final Object candidate)
239                 {
240                     final ActionStateFacade asf = (ActionStateFacade)candidate;
241                     return asf.hasStereotype(stereotypeName) &&
242                     asf.getPartition().getActivityGraph().equals(activityGraph);
243                 }
244             });
245 
246         return asCollection;
247     }
248 
249     /**
250      * @see org.andromda.metafacades.uml.ModelFacade#getRootPackage()
251      */
252     @Override
253     protected Package handleGetRootPackage()
254     {
255         final Package model = UmlUtilities.findModel(this.metaObject);
256         if (ModelFacadeLogicImpl.LOGGER.isDebugEnabled())
257         {
258             ModelFacadeLogicImpl.LOGGER.debug("Root package " + model);
259         }
260         return model;
261     }
262 
263     /**
264      * @see org.andromda.metafacades.uml.ModelFacade#getAllActors()
265      */
266     @Override
267     protected List<Actor> handleGetAllActors()
268     {
269         return (List<Actor>) UmlUtilities.getAllMetaObjectsInstanceOf(
270             Actor.class,
271             UmlUtilities.getModels());
272     }
273 
274     /**
275      * @see org.andromda.metafacades.uml.ModelFacade#getAllUseCases()
276      */
277     @Override
278     protected List<UseCase> handleGetAllUseCases()
279     {
280         return (List<UseCase>) UmlUtilities.getAllMetaObjectsInstanceOf(
281             UseCase.class,
282             UmlUtilities.getModels());
283     }
284 
285     /**
286      * @see org.andromda.metafacades.uml.ModelFacade#getAllActionStates()
287      */
288     @Override
289     protected Collection<ActionStateFacade> handleGetAllActionStates()
290     {
291         // cf documentation, action states are mapped to uml2 normal state
292         final Collection allActionStates =
293             UmlUtilities.getAllMetaObjectsInstanceOf(
294                 State.class,
295                 UmlUtilities.getModels());
296         CollectionUtils.filter(
297             allActionStates,
298             new Predicate()
299             {
300                 public boolean evaluate(final Object candidate)
301                 {
302                     return (!(candidate instanceof FinalState));
303                 }
304             });
305         return allActionStates;
306     }
307 
308     /** NOT IMPLEMENTED
309      * @see org.andromda.metafacades.uml.ModelFacade#getAllObjectFlowStates()
310      */
311     @Override
312     protected Collection handleGetAllObjectFlowStates()
313     {
314         // TODO: Implement handleGetAllObjectFlowStates
315         return Collections.emptyList();
316     }
317 
318     /**
319      * @see org.andromda.metafacades.uml.ModelFacade#getAllClasses()
320      */
321     @Override
322     protected List<ClassifierFacade> handleGetAllClasses()
323     {
324         List<ClassifierFacade> facades = new ArrayList<ClassifierFacade>();
325         List<Class> classes = (List<Class>) UmlUtilities.getAllMetaObjectsInstanceOf(
326             Class.class,
327             UmlUtilities.getModels());
328         // Some examples where UML2 Class does not map to andromda ClassifierFacade:
329         // ActivityImpl -> EventFacadeLogicImpl
330         // Must filter out classes that don't map correctly, we just want Classes and Datatypes and Interfaces
331         for (Class metaClass : classes)
332         {
333             MetafacadeBase metafacade = this.shieldedElement(metaClass);
334             if (metafacade instanceof ClassifierFacade)
335             {
336                 facades.add((ClassifierFacade) metafacade);
337             }
338         }
339         return facades;
340     }
341 
342     /**
343      * @see org.andromda.metafacades.emf.uml22.ModelFacadeLogic#handleGetAllTransitions()
344      */
345     @Override
346     protected List<Transition> handleGetAllTransitions()
347     {
348         return (List<Transition>) UmlUtilities.getAllMetaObjectsInstanceOf(
349             Transition.class,
350             UmlUtilities.getModels());
351     }
352 }