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