View Javadoc

1   package org.andromda.cartridges.bpm4struts.metafacades;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import java.util.LinkedHashSet;
7   import java.util.List;
8   
9   import org.andromda.cartridges.bpm4struts.Bpm4StrutsGlobals;
10  import org.andromda.cartridges.bpm4struts.Bpm4StrutsUtils;
11  import org.andromda.metafacades.uml.ActivityGraphFacade;
12  import org.andromda.metafacades.uml.StateMachineFacade;
13  import org.andromda.metafacades.uml.StateVertexFacade;
14  import org.andromda.metafacades.uml.TransitionFacade;
15  import org.andromda.metafacades.uml.UMLMetafacadeProperties;
16  import org.andromda.metafacades.uml.UseCaseFacade;
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.StrutsJsp
25   */
26  public class StrutsJspLogicImpl
27      extends StrutsJspLogic
28  {
29      public StrutsJspLogicImpl(
30          Object metaObject,
31          String context)
32      {
33          super(metaObject, context);
34      }
35  
36      public String getPackageName()
37      {
38          String packageName = null;
39  
40          final StateMachineFacade graphContext = getStateMachine();
41          if (graphContext instanceof ActivityGraphFacade)
42          {
43              final UseCaseFacade graphUseCase = ((ActivityGraphFacade)graphContext).getUseCase();
44              if (graphUseCase instanceof StrutsUseCase)
45              {
46                  final StrutsUseCase useCase = (StrutsUseCase)graphUseCase;
47                  packageName = useCase.getPackageName();
48              }
49          }
50          return packageName;
51      }
52  
53      /**
54       * @see org.andromda.metafacades.uml.ModelElementFacade#getPackagePath()
55       */
56      public String getPackagePath()
57      {
58          return StringUtils.replace(
59              this.getPackageName(),
60              String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.NAMESPACE_SEPARATOR)),
61              "/");
62      }
63  
64      protected String handleGetMessageKey()
65      {
66          final StringBuffer messageKey = new StringBuffer();
67  
68          if (!normalizeMessages())
69          {
70              final UseCaseFacade useCase = this.getUseCase();
71              if (useCase != null)
72              {
73                  messageKey.append(StringUtilsHelper.toResourceMessageKey(useCase.getName()));
74                  messageKey.append('.');
75              }
76          }
77  
78          messageKey.append(StringUtilsHelper.toResourceMessageKey(getName()));
79          return messageKey.toString();
80      }
81  
82      protected String handleGetMessageValue()
83      {
84          return StringUtilsHelper.toPhrase(getName());
85      }
86  
87      protected String handleGetTitleKey()
88      {
89          return getMessageKey() + ".title";
90      }
91  
92      protected String handleGetTitleValue()
93      {
94          return StringUtilsHelper.toPhrase(getName());
95      }
96  
97      protected String handleGetDocumentationKey()
98      {
99          return getMessageKey() + ".documentation";
100     }
101 
102     protected String handleGetDocumentationValue()
103     {
104         final String value = StringUtilsHelper.toResourceMessage(getDocumentation(""));
105         return (value == null) ? "" : value;
106     }
107 
108     protected String handleGetOnlineHelpKey()
109     {
110         return getMessageKey() + ".online.help";
111     }
112 
113     protected String handleGetOnlineHelpValue()
114     {
115         final String crlf = "<br/>";
116         final StringBuffer buffer = new StringBuffer();
117 
118         final String value = StringUtilsHelper.toResourceMessage(getDocumentation("", 64, false));
119         buffer.append((value == null) ? "No page documentation has been specified" : value);
120         buffer.append(crlf);
121         buffer.append(crlf);
122 
123         return StringUtilsHelper.toResourceMessage(buffer.toString());
124     }
125 
126     protected String handleGetOnlineHelpPagePath()
127     {
128         return this.getFullPath() + "_help";
129     }
130 
131     protected String handleGetOnlineHelpActionPath()
132     {
133         final StringBuffer buffer = new StringBuffer();
134 
135         if (StringUtils.isNotBlank(this.getPackagePath()))
136         {
137             buffer.append('/');
138             buffer.append(this.getPackagePath());
139         }
140         buffer.append('/');
141         buffer.append(StringUtilsHelper.upperCamelCaseName(this.getName()));
142         buffer.append("Help");
143 
144         return buffer.toString();
145     }
146 
147     protected String handleGetFullPath()
148     {
149         return '/' +
150             (getPackageName() + '.' + Bpm4StrutsUtils.toWebFileName(StringUtils.trimToEmpty(getName()))).replace(
151                 '.', '/');
152     }
153 
154     protected boolean handleIsValidationRequired()
155     {
156         final Collection actions = getActions();
157         for (final Iterator actionIterator = actions.iterator(); actionIterator.hasNext();)
158         {
159             final StrutsAction action = (StrutsAction)actionIterator.next();
160             if (action.isValidationRequired())
161             {
162                 return true;
163             }
164         }
165         return false;
166     }
167 
168     protected boolean handleIsDateFieldPresent()
169     {
170         final Collection actions = getActions();
171         for (final Iterator actionIterator = actions.iterator(); actionIterator.hasNext();)
172         {
173             final StrutsAction action = (StrutsAction)actionIterator.next();
174             if (action.isDateFieldPresent())
175             {
176                 return true;
177             }
178         }
179         return false;
180     }
181 
182     protected boolean handleIsCalendarRequired()
183     {
184         final Collection actions = getActions();
185         for (final Iterator actionIterator = actions.iterator(); actionIterator.hasNext();)
186         {
187             final StrutsAction action = (StrutsAction)actionIterator.next();
188             if (action.isCalendarRequired())
189             {
190                 return true;
191             }
192         }
193         return false;
194     }
195 
196     /**
197      * Overridden since StrutsAction does not extend FrontEndAction.
198      *
199      * @see org.andromda.metafacades.uml.FrontEndView#getAllActionParameters()
200      */
201     public List getAllActionParameters()
202     {
203         final List actionParameters = new ArrayList();
204         final Collection actions = getActions();
205         for (final Iterator iterator = actions.iterator(); iterator.hasNext();)
206         {
207             final StrutsAction action = (StrutsAction)iterator.next();
208             actionParameters.addAll(action.getActionParameters());
209         }
210         return actionParameters;
211     }
212 
213     /**
214      * Overridden because StrutsAction does not extend FrontEndAction.
215      *
216      * @see org.andromda.metafacades.uml.FrontEndView#getActions()
217      */
218     public List getActions()
219     {
220         final List actions = new ArrayList();
221         final Collection outgoing = this.getOutgoing();
222 
223         for (final Iterator iterator = outgoing.iterator(); iterator.hasNext();)
224         {
225             final Object object = iterator.next();
226             if (object instanceof StrutsAction)
227                 actions.add(object);
228         }
229 
230         return actions;
231     }
232 
233     protected List handleGetNonActionForwards()
234     {
235         final List actions = new ArrayList();
236         final Collection outgoing = getOutgoing();
237 
238         for (final Iterator iterator = outgoing.iterator(); iterator.hasNext();)
239         {
240             final Object object = iterator.next();
241             if (!(object instanceof StrutsAction))
242             {
243                 actions.add(object);
244             }
245         }
246         return actions;
247     }
248 
249     protected List handleGetPageVariables()
250     {
251         return this.getVariables();
252     }
253 
254     protected List handleGetIncomingActions()
255     {
256         final List incomingActionsList = new ArrayList();
257         collectIncomingActions(this, new LinkedHashSet(), incomingActionsList);
258         return incomingActionsList;
259     }
260 
261     /**
262      * Collects all actions that are entering the argument state vertex.
263      *
264      * @param stateVertex          the statevertex to process
265      * @param processedTransitions the transitions that have already been processed
266      * @param actions              the actions collected so far
267      */
268     private void collectIncomingActions(
269         StateVertexFacade stateVertex,
270         Collection processedTransitions,
271         Collection actions)
272     {
273         final Collection incomingTransitions = stateVertex.getIncoming();
274         for (final Iterator iterator = incomingTransitions.iterator(); iterator.hasNext();)
275         {
276             final TransitionFacade incomingTransition = (TransitionFacade)iterator.next();
277             collectIncomingActions(incomingTransition, processedTransitions, actions);
278         }
279     }
280 
281     /**
282      * Collects all actions that are possibly traversing the argument transitions.
283      *
284      * @param transition           the transition to process
285      * @param processedTransitions the transitions that have already been processed
286      * @param actions              the actions collected so far
287      */
288     private void collectIncomingActions(
289         TransitionFacade transition,
290         Collection processedTransitions,
291         Collection actions)
292     {
293         if (!processedTransitions.contains(transition))
294         {
295             processedTransitions.add(transition);
296             if (transition instanceof StrutsAction)
297             {
298                 actions.add(transition);
299 
300 /*  @todo: TEMPORARILY COMMENTED OUT -- needs verification that isCaseStart() forms are not populated, but I think they are
301                 if (((StrutsAction)transition).isUseCaseStart())
302                 {
303                     Collection finalStates = getUseCase().getFinalStates();// todo: test usecase for null
304                     for (final Iterator iterator = finalStates.iterator(); iterator.hasNext();)
305                     {
306                         FinalStateFacade finalState = (FinalStateFacade) iterator.next();
307                         collectIncomingActions(finalState, processedTransitions, actions);
308                     }
309                 }
310 */
311             }
312             else
313             {
314                 final Collection incomingTransitions = transition.getSource().getIncoming();
315                 for (final Iterator iterator = incomingTransitions.iterator(); iterator.hasNext();)
316                 {
317                     final TransitionFacade incomingTransition = (TransitionFacade)iterator.next();
318                     collectIncomingActions(incomingTransition, processedTransitions, actions);
319                 }
320             }
321         }
322     }
323 
324     protected String handleGetCssFileName()
325     {
326         return getFullPath() + ".css";
327     }
328 
329     protected List handleGetNonTableActions()
330     {
331         final List nonTableActions = new ArrayList();
332 
333         final Collection actions = getActions();
334         for (final Iterator actionIterator = actions.iterator(); actionIterator.hasNext();)
335         {
336             final StrutsAction action = (StrutsAction)actionIterator.next();
337             if (!action.isTableLink())
338             {
339                 nonTableActions.add(action);
340             }
341         }
342 
343         return nonTableActions;
344     }
345 
346     private boolean normalizeMessages()
347     {
348         final String normalizeMessages = (String)getConfiguredProperty(Bpm4StrutsGlobals.PROPERTY_NORMALIZE_MESSAGES);
349         return Boolean.valueOf(normalizeMessages).booleanValue();
350     }
351 }