View Javadoc
1   package org.andromda.cartridges.bpm4struts.metafacades;
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.LinkedHashMap;
8   import java.util.LinkedHashSet;
9   import java.util.List;
10  import java.util.Map;
11  import java.util.Set;
12  import org.andromda.cartridges.bpm4struts.Bpm4StrutsProfile;
13  import org.andromda.metafacades.uml.EventFacade;
14  import org.andromda.metafacades.uml.GuardFacade;
15  import org.andromda.metafacades.uml.PseudostateFacade;
16  import org.andromda.metafacades.uml.StateVertexFacade;
17  import org.andromda.utils.StringUtilsHelper;
18  import org.apache.commons.lang.StringUtils;
19  /**
20   * MetafacadeLogic implementation.
21   *
22   * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForward
23   * @author Bob Fields
24   */
25  public class StrutsForwardLogicImpl
26      extends StrutsForwardLogic
27  {
28      private static final long serialVersionUID = 34L;
29      /**
30       * @param metaObject
31       * @param context
32       */
33      public StrutsForwardLogicImpl(
34          Object metaObject,
35          String context)
36      {
37          super(metaObject, context);
38      }
39  
40      /**
41       * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleGetGuardName()
42       */
43      protected String handleGetGuardName()
44      {
45          final GuardFacade guard = this.getGuard();
46          return (guard == null) ? null : guard.getName();
47      }
48  
49      /**
50       * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleIsEnteringPage()
51       */
52      protected boolean handleIsEnteringPage()
53      {
54          return this.isEnteringView();
55      }
56  
57      /**
58       * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleGetForwardName()
59       */
60      protected String handleGetForwardName()
61      {
62          return StringUtilsHelper.toResourceMessageKey(this.resolveName());
63      }
64  
65      /**
66       * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleGetForwardPath()
67       */
68      protected String handleGetForwardPath()
69      {
70          String forwardPath = null;
71  
72          final StateVertexFacade target = this.getTarget();
73          if (isEnteringPage())
74          {
75              forwardPath = ((StrutsJsp)target).getFullPath() + ".jsp";
76          }
77          else if (isEnteringFinalState())
78          {
79              forwardPath = ((StrutsFinalState)target).getFullPath();
80          }
81  
82          return forwardPath;
83      }
84  
85      /**
86       * @return lowerCamelCaseName(resolveName())
87       */
88      protected String handleGetActionMethodName()
89      {
90          return StringUtilsHelper.lowerCamelCaseName(this.resolveName());
91      }
92  
93      /**
94       * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleGetTargetNameKey()
95       */
96      protected String handleGetTargetNameKey()
97      {
98          if (this.isEnteringPage())
99          {
100             return ((StrutsJsp)this.getTarget()).getTitleKey();
101         }
102         else if (this.isEnteringFinalState())
103         {
104             return ((StrutsUseCase)((StrutsFinalState)this.getTarget()).getTargetUseCase()).getTitleKey();
105         }
106         return null;
107     }
108 
109     /**
110      * If this forward has a trigger this method returns that trigger's name, otherwise if this forward
111      * has a name this method returns that name, otherwise if this forward's target has a name this
112      * method returns that name, otherwise simply returns <code>"unknown"</code>
113      */
114     private String resolveName()
115     {
116         String forwardName = null;
117         //trigger
118         final EventFacade trigger = this.getTrigger();
119         if (trigger != null) forwardName = trigger.getName();
120         //name
121         if (StringUtils.isEmpty(forwardName)) forwardName = this.getName();
122         //target
123         if (StringUtils.isEmpty(forwardName)) forwardName = this.getTarget().getName();
124         // else
125         if (StringUtils.isEmpty(forwardName)) forwardName = "unknown";
126         // return
127         return forwardName;
128     }
129 
130     /**
131      * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleIsExitingPage()
132      */
133     protected boolean handleIsExitingPage()
134     {
135         return this.isExitingView();
136     }
137 
138     /**
139      * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleIsSuccessMessagesPresent()
140      */
141     protected boolean handleIsSuccessMessagesPresent()
142     {
143         return !this.getSuccessMessages().isEmpty();
144     }
145 
146     /**
147      * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleIsWarningMessagesPresent()
148      */
149     protected boolean handleIsWarningMessagesPresent()
150     {
151         return !this.getWarningMessages().isEmpty();
152     }
153 
154     /**
155      * Collects specific messages in a map.
156      *
157      * @param taggedValue the tagged value from which to read the message
158      * @return maps message keys to message values, but only those that match the arguments
159      *         will have been recorded
160      */
161     private Map getMessages(String taggedValue)
162     {
163         Map messages;
164 
165         final Collection taggedValues = this.findTaggedValues(taggedValue);
166         if (taggedValues.isEmpty())
167         {
168             messages = Collections.emptyMap();
169         }
170         else
171         {
172             messages = new LinkedHashMap(); // we want to keep the order
173 
174             for (final Iterator iterator = taggedValues.iterator(); iterator.hasNext();)
175             {
176                 final String value = (String)iterator.next();
177                 messages.put(StringUtilsHelper.toResourceMessageKey(value), value);
178             }
179         }
180 
181         return messages;
182     }
183 
184     /**
185      * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleGetSuccessMessages()
186      */
187     protected Map handleGetSuccessMessages()
188     {
189         return this.getMessages(Bpm4StrutsProfile.TAGGEDVALUE_ACTION_SUCCESS_MESSAGE);
190     }
191 
192     /**
193      * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleGetWarningMessages()
194      */
195     protected Map handleGetWarningMessages()
196     {
197         return this.getMessages(Bpm4StrutsProfile.TAGGEDVALUE_ACTION_WARNING_MESSAGE);
198     }
199 
200     /**
201      * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForwardLogic#handleGetStrutsActivityGraph()
202      */
203     protected Object handleGetStrutsActivityGraph()
204     {
205         return this.getFrontEndActivityGraph();
206     }
207 
208     /**
209      * Overridden since StrutsAction doesn't extend FrontEndAction.
210      *
211      * @see org.andromda.metafacades.uml.FrontEndForward#getActions()
212      */
213     public List getActions()
214     {
215         final Set actions = new LinkedHashSet();
216         this.findActions(actions, new LinkedHashSet());
217         return new ArrayList(actions);
218     }
219 
220     /**
221      * Recursively finds all actions for this forward, what this means depends on the context in which
222      * this forward is used: if the source is a page action state it will collect all actions going out
223      * of this page, if the source is a regular action state it will collect all actions that might traverse
224      * this action state, if the source is the initial state it will collect all actions forwarding to this
225      * forward's use-case (please not that those actions most likely are defined in other use-cases).
226      *
227      * @param actions         the default set of actions, duplicates will not be recorded
228      * @param handledForwards the forwards already processed
229      */
230     private final void findActions(
231         final Set actions,
232         final Set handledForwards)
233     {
234         if (!handledForwards.contains(this))
235         {
236             handledForwards.add(this);
237 
238             if (this instanceof StrutsAction) // @todo this is not so nice because StrutsAction extends StrutsForward, solution would be to override in StrutsAction
239             {
240                 actions.add(this);
241             }
242             else
243             {
244                 final StateVertexFacade vertex = getSource();
245                 if (vertex instanceof StrutsJsp)
246                 {
247                     final StrutsJsp jsp = (StrutsJsp)vertex;
248                     actions.addAll(jsp.getActions());
249                 }
250                 else if (vertex instanceof StrutsActionState)
251                 {
252                     final StrutsActionState actionState = (StrutsActionState)vertex;
253                     actions.addAll(actionState.getContainerActions());
254                 }
255                 else if (vertex instanceof PseudostateFacade)
256                 {
257                     final PseudostateFacade pseudostate = (PseudostateFacade)vertex;
258                     if (!pseudostate.isInitialState())
259                     {
260                         final Collection incomingForwards = pseudostate.getIncomings();
261                         for (final Iterator forwardIterator = incomingForwards.iterator(); forwardIterator.hasNext();)
262                         {
263                             final StrutsForward forward = (StrutsForward)forwardIterator.next();
264                             actions.addAll(forward.getActions());
265                         }
266                     }
267                 }
268             }
269         }
270     }
271 }