001package org.andromda.cartridges.bpm4struts.metafacades;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Iterator;
006import java.util.LinkedHashSet;
007import java.util.List;
008import org.andromda.cartridges.bpm4struts.Bpm4StrutsGlobals;
009import org.andromda.cartridges.bpm4struts.Bpm4StrutsUtils;
010import org.andromda.metafacades.uml.ActivityGraphFacade;
011import org.andromda.metafacades.uml.StateMachineFacade;
012import org.andromda.metafacades.uml.StateVertexFacade;
013import org.andromda.metafacades.uml.TransitionFacade;
014import org.andromda.metafacades.uml.UMLMetafacadeProperties;
015import org.andromda.metafacades.uml.UseCaseFacade;
016import org.andromda.utils.StringUtilsHelper;
017import org.apache.commons.lang.StringUtils;
018
019/**
020 * MetafacadeLogic implementation.
021 *
022 * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsJsp
023 * @author Bob Fields
024 */
025public class StrutsJspLogicImpl
026    extends StrutsJspLogic
027{
028    private static final long serialVersionUID = 34L;
029    /**
030     * @param metaObject
031     * @param context
032     */
033    public StrutsJspLogicImpl(
034        Object metaObject,
035        String context)
036    {
037        super(metaObject, context);
038    }
039
040    /**
041     * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsJspLogic#getPackageName()
042     */
043    public String getPackageName()
044    {
045        String packageName = null;
046
047        final StateMachineFacade graphContext = getStateMachine();
048        if (graphContext instanceof ActivityGraphFacade)
049        {
050            final UseCaseFacade graphUseCase = ((ActivityGraphFacade)graphContext).getUseCase();
051            if (graphUseCase instanceof StrutsUseCase)
052            {
053                final StrutsUseCase useCase = (StrutsUseCase)graphUseCase;
054                packageName = useCase.getPackageName();
055            }
056        }
057        return packageName;
058    }
059
060    /**
061     * @see org.andromda.metafacades.uml.ModelElementFacade#getPackagePath()
062     */
063    public String getPackagePath()
064    {
065        return StringUtils.replace(
066            this.getPackageName(),
067            String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.NAMESPACE_SEPARATOR)),
068            "/");
069    }
070
071    /**
072     * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsJspLogic#handleGetMessageKey()
073     */
074    protected String handleGetMessageKey()
075    {
076        final StringBuilder messageKey = new StringBuilder();
077
078        if (!normalizeMessages())
079        {
080            final UseCaseFacade useCase = this.getUseCase();
081            if (useCase != null)
082            {
083                messageKey.append(StringUtilsHelper.toResourceMessageKey(useCase.getName()));
084                messageKey.append('.');
085            }
086        }
087
088        messageKey.append(StringUtilsHelper.toResourceMessageKey(getName()));
089        return messageKey.toString();
090    }
091
092    /**
093     * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsJspLogic#handleGetMessageValue()
094     */
095    protected String handleGetMessageValue()
096    {
097        return StringUtilsHelper.toPhrase(getName());
098    }
099
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}