View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import java.util.LinkedHashMap;
7   import java.util.LinkedHashSet;
8   import java.util.List;
9   import java.util.Map;
10  import org.andromda.metafacades.uml.ActionStateFacade;
11  import org.andromda.metafacades.uml.ActivityGraphFacade;
12  import org.andromda.metafacades.uml.ActorFacade;
13  import org.andromda.metafacades.uml.AssociationEndFacade;
14  import org.andromda.metafacades.uml.ClassifierFacade;
15  import org.andromda.metafacades.uml.FinalStateFacade;
16  import org.andromda.metafacades.uml.FrontEndAction;
17  import org.andromda.metafacades.uml.FrontEndActivityGraph;
18  import org.andromda.metafacades.uml.FrontEndController;
19  import org.andromda.metafacades.uml.FrontEndFinalState;
20  import org.andromda.metafacades.uml.FrontEndForward;
21  import org.andromda.metafacades.uml.FrontEndParameter;
22  import org.andromda.metafacades.uml.FrontEndUseCase;
23  import org.andromda.metafacades.uml.FrontEndView;
24  import org.andromda.metafacades.uml.Role;
25  import org.andromda.metafacades.uml.UMLProfile;
26  import org.andromda.metafacades.uml.UseCaseFacade;
27  import org.apache.commons.lang.StringUtils;
28  
29  /**
30   * MetafacadeLogic implementation for
31   * org.andromda.metafacades.uml.FrontEndUseCase.
32   *
33   * @see org.andromda.metafacades.uml.FrontEndUseCase
34   */
35  public class FrontEndUseCaseLogicImpl
36      extends FrontEndUseCaseLogic
37  {
38      private static final long serialVersionUID = 34L;
39      /**
40       * @param metaObject
41       * @param context
42       */
43      public FrontEndUseCaseLogicImpl(
44          final Object metaObject,
45          final String context)
46      {
47          super(metaObject, context);
48      }
49  
50      /**
51       * @see org.andromda.metafacades.uml.FrontEndUseCase#isEntryUseCase()
52       */
53      @Override
54      protected boolean handleIsEntryUseCase()
55      {
56          return this.hasStereotype(UMLProfile.STEREOTYPE_FRONT_END_APPLICATION);
57      }
58  
59      /**
60       * @see org.andromda.metafacades.uml.FrontEndUseCase#getController()
61       */
62      @Override
63      protected FrontEndController handleGetController()
64      {
65          final FrontEndActivityGraph graph = this.getActivityGraph();
66          return graph == null ? null : graph.getController();
67      }
68  
69      /**
70       * @see org.andromda.metafacades.uml.FrontEndUseCase#getActivityGraph()
71       */
72      @Override
73      protected ActivityGraphFacade handleGetActivityGraph()
74      {
75          // There is a method in use case Facade.
76          // We can use it because, for now, we don't support hyperlink neither
77          // tag value way to define
78          // which activity graph is modelized for this use case.
79          return this.getFirstActivityGraph();
80      }
81  
82      /**
83       * @see org.andromda.metafacades.uml.FrontEndUseCase#getReferencingFinalStates()
84       */
85      @Override
86      protected List<FinalStateFacade> handleGetReferencingFinalStates()
87      {
88          return new ArrayList<FinalStateFacade>(this.getModel().findFinalStatesWithNameOrHyperlink(this));
89      }
90  
91      /**
92       * @see org.andromda.metafacades.uml.FrontEndUseCase#getAllUseCases()
93       */
94      @Override
95      protected List<FrontEndUseCase> handleGetAllUseCases()
96      {
97          final List<FrontEndUseCase> useCases = new ArrayList<FrontEndUseCase>();
98          for (UseCaseFacade useCase : this.getModel().getAllUseCases())
99          {
100             if (useCase instanceof FrontEndUseCase)
101             {
102                 useCases.add((FrontEndUseCase)useCase);
103             }
104         }
105         return useCases;
106     }
107 
108     /**
109      * Gets those roles directly associated to this use-case.
110      */
111     private Collection<Role> getAssociatedRoles()
112     {
113         final Collection<Role> usersList = new ArrayList<Role>();
114         final Collection<AssociationEndFacade> associationEnds = this.getAssociationEnds();
115         for (final Iterator<AssociationEndFacade> iterator = associationEnds.iterator(); iterator.hasNext();)
116         {
117             final AssociationEndFacade associationEnd = iterator.next();
118             final ClassifierFacade classifier = associationEnd.getOtherEnd().getType();
119             if (classifier instanceof Role)
120             {
121                 usersList.add((Role)classifier);
122             }
123         }
124         return usersList;
125     }
126 
127     /**
128      * Recursively collects all roles generalizing the argument user, in the
129      * specified collection.
130      */
131     private void collectRoles(
132         final Role role,
133         final Collection<Role> roles)
134     {
135         if (!roles.contains(role))
136         {
137             roles.add(role);
138             final Collection<ActorFacade> childUsers = role.getGeneralizedByActors();
139             for (final Iterator<ActorFacade> iterator = childUsers.iterator(); iterator.hasNext();)
140             {
141                 final Role childUser = (Role)iterator.next();
142                 this.collectRoles(
143                     childUser,
144                     roles);
145             }
146         }
147     }
148 
149     /**
150      * @see org.andromda.metafacades.uml.FrontEndUseCase#getRoles()
151      */
152     @Override
153     protected List<Role> handleGetRoles()
154     {
155         final Collection<Role> allRoles = new LinkedHashSet<Role>();
156         final Collection<Role> associatedUsers = this.getAssociatedRoles();
157         for (Role user : associatedUsers)
158         {
159             this.collectRoles(
160                 user,
161                 allRoles);
162         }
163         return new ArrayList<Role>(allRoles);
164     }
165 
166     /**
167      * @see org.andromda.metafacades.uml.FrontEndUseCase#getAllRoles()
168      */
169     @Override
170     protected List<Role> handleGetAllRoles()
171     {
172         final Collection<Role> allRoles = new LinkedHashSet<Role>();
173         for (final FrontEndUseCase useCase : this.getAllUseCases())
174         {
175             allRoles.addAll(useCase.getRoles());
176         }
177         return new ArrayList<Role>(allRoles);
178     }
179 
180     /**
181      * @see org.andromda.metafacades.uml.FrontEndUseCase#isSecured()
182      */
183     @Override
184     protected boolean handleIsSecured()
185     {
186         return !this.getRoles().isEmpty();
187     }
188 
189     /**
190      * @see org.andromda.metafacades.uml.FrontEndUseCase#getViews()
191      */
192     @Override
193     protected List<ActionStateFacade> handleGetViews()
194     {
195         // TODO: Return type FrontEndView, instead of ActionStateFacade
196         final List views = new ArrayList<ActionStateFacade>();
197         final ActivityGraphFacade graph = this.getActivityGraph();
198         if (graph != null)
199         {
200             views.addAll(this.getModel().getAllActionStatesWithStereotype(
201                         graph,
202                         UMLProfile.STEREOTYPE_FRONT_END_VIEW));
203         }
204         return views;
205     }
206 
207     /**
208      * @see org.andromda.metafacades.uml.FrontEndUseCase#getViews()
209      */
210     @Override
211     protected List<FrontEndAction> handleGetActions()
212     {
213         final Collection<FrontEndAction> actions = new LinkedHashSet<FrontEndAction>();
214         final Collection<FrontEndView> pages = this.getViews();
215         for (final Iterator<FrontEndView> pageIterator = pages.iterator(); pageIterator.hasNext();)
216         {
217             final FrontEndView view = pageIterator.next();
218             actions.addAll(view.getActions());
219         }
220 
221         final FrontEndActivityGraph graph = this.getActivityGraph();
222         if (graph != null)
223         {
224             final FrontEndAction action = graph.getInitialAction();
225             if (action != null)
226             {
227                 actions.add(action);
228             }
229         }
230         return new ArrayList<FrontEndAction>(actions);
231     }
232 
233     /**
234      * @see org.andromda.metafacades.uml.FrontEndUseCase#getInitialView()
235      */
236     @Override
237     protected FrontEndView handleGetInitialView()
238     {
239         FrontEndView view = null;
240         final FrontEndActivityGraph graph = this.getActivityGraph();
241         final FrontEndAction action = graph == null ? null : this.getActivityGraph().getInitialAction();
242         final Collection<FrontEndForward> forwards = action == null ? null : action.getActionForwards();
243         if (forwards != null)
244         {
245             for (final Iterator<FrontEndForward> iterator = forwards.iterator(); iterator.hasNext();)
246             {
247                 final FrontEndForward forward = iterator.next();
248                 final Object target = forward.getTarget();
249                 if (target instanceof FrontEndView)
250                 {
251                     view = (FrontEndView)target;
252                 }
253                 else if (target instanceof FrontEndFinalState)
254                 {
255                     final FrontEndFinalState finalState = (FrontEndFinalState)target;
256                     final FrontEndUseCase targetUseCase = finalState.getTargetUseCase();
257                     if (targetUseCase != null && !targetUseCase.equals(this.THIS()))
258                     {
259                         view = targetUseCase.getInitialView();
260                     }
261                 }
262             }
263         }
264         return view;
265     }
266 
267     /**
268      * @see org.andromda.metafacades.uml.FrontEndUseCase#getViewVariables()
269      */
270     @Override
271     protected List<FrontEndParameter> handleGetViewVariables()
272     {
273         final Map<String, FrontEndParameter> pageVariableMap = new LinkedHashMap<String, FrontEndParameter>();
274 
275         // - page variables can occur twice or more in the usecase if their
276         // names are the same for different forms, storing them in a map
277         // solves this issue because those names do not have the action-name
278         // prefix
279         final Collection<FrontEndView> views = this.getViews();
280         for (final Iterator pageIterator = views.iterator(); pageIterator.hasNext();)
281         {
282             final FrontEndView view = (FrontEndView)pageIterator.next();
283             final Collection<FrontEndParameter> variables = view.getVariables();
284             for (FrontEndParameter variable : variables)
285             {
286                 final String name = variable.getName();
287                 if (StringUtils.isNotBlank(name))
288                 {
289                     final FrontEndParameter existingVariable = pageVariableMap.get(name);
290                     if (existingVariable != null && existingVariable.isTable())
291                     {
292                         variable = existingVariable;
293                     }
294                     pageVariableMap.put(
295                         name,
296                         variable);
297                 }
298             }
299         }
300         return new ArrayList<FrontEndParameter>(pageVariableMap.values());
301     }
302 }