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