001package org.andromda.cartridges.jsf2.metafacades;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Iterator;
006import java.util.LinkedHashMap;
007import java.util.LinkedHashSet;
008import java.util.List;
009import java.util.Map;
010import java.util.Set;
011import java.util.TreeMap;
012import org.andromda.cartridges.jsf2.JSFGlobals;
013import org.andromda.cartridges.jsf2.JSFProfile;
014import org.andromda.cartridges.jsf2.JSFUtils;
015import org.andromda.metafacades.uml.AssociationEndFacade;
016import org.andromda.metafacades.uml.AttributeFacade;
017import org.andromda.metafacades.uml.ClassifierFacade;
018import org.andromda.metafacades.uml.DependencyFacade;
019import org.andromda.metafacades.uml.FrontEndAction;
020import org.andromda.metafacades.uml.FrontEndActivityGraph;
021import org.andromda.metafacades.uml.FrontEndFinalState;
022import org.andromda.metafacades.uml.FrontEndForward;
023import org.andromda.metafacades.uml.FrontEndParameter;
024import org.andromda.metafacades.uml.FrontEndUseCase;
025import org.andromda.metafacades.uml.FrontEndView;
026import org.andromda.metafacades.uml.IncludeFacade;
027import org.andromda.metafacades.uml.ModelElementFacade;
028import org.andromda.metafacades.uml.Role;
029import org.andromda.metafacades.uml.UseCaseFacade;
030import org.andromda.utils.StringUtilsHelper;
031import org.apache.commons.collections.CollectionUtils;
032import org.apache.commons.collections.Predicate;
033import org.apache.commons.collections.Transformer;
034import org.apache.commons.lang.ObjectUtils;
035import org.apache.commons.lang.StringUtils;
036
037/**
038 * MetafacadeLogic implementation for org.andromda.cartridges.jsf2.metafacades.JSFUseCase.
039 *
040 * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase
041 */
042public class JSFUseCaseLogicImpl
043    extends JSFUseCaseLogic
044{
045    private static final long serialVersionUID = 34L;
046    /**
047     * @param metaObject
048     * @param context
049     */
050    public JSFUseCaseLogicImpl(
051        Object metaObject,
052        String context)
053    {
054        super(metaObject, context);
055    }
056
057    /**
058     * @return actionPath
059     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getPath()
060     */
061    protected String handleGetPath()
062    {
063        String actionPath = null;
064        final FrontEndActivityGraph graph = this.getActivityGraph();
065        if (graph != null)
066        {
067            final JSFAction action = (JSFAction)graph.getInitialAction();
068            if (action != null)
069            {
070                actionPath = action.getPath();
071            }
072        }
073        return actionPath;
074    }
075
076    /**
077     * @return pathRoot
078     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getPathRoot()
079     */
080    protected String handleGetPathRoot()
081    {
082        final StringBuilder pathRoot = new StringBuilder("/");
083        final String packagePath = this.getPackagePath();
084        final String prefix = packagePath != null ? packagePath.trim() : "";
085        pathRoot.append(prefix);
086        return pathRoot.toString();
087    }
088
089    /**
090     * @return forwardName
091     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getPathRoot()
092     */
093    protected String handleGetForwardName()
094    {
095        return JSFUtils.toWebResourceName(this.getName()) + JSFGlobals.USECASE_FORWARD_NAME_SUFFIX;
096    }
097
098    /**
099     * @return titleKey
100     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getTitleKey()
101     */
102    protected String handleGetTitleKey()
103    {
104        return StringUtilsHelper.toResourceMessageKey(
105            this.isNormalizeMessages() ? this.getTitleValue() : this.getName()) + '.' +
106            JSFGlobals.TITLE_MESSAGE_KEY_SUFFIX;
107    }
108
109    /**
110     * @return toPhrase(getName())
111     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getTitleValue()
112     */
113    protected String handleGetTitleValue()
114    {
115        return StringUtilsHelper.toPhrase(getName());
116    }
117
118    /**
119     * Indicates whether or not we should normalize messages.
120     *
121     * @return true/false
122     */
123    private boolean isNormalizeMessages()
124    {
125        final String normalizeMessages = (String)getConfiguredProperty(JSFGlobals.NORMALIZE_MESSAGES);
126        return Boolean.valueOf(normalizeMessages).booleanValue();
127    }
128
129    /**
130     * @return allMessages
131     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getAllMessages()
132     */
133    protected Map handleGetAllMessages()
134    {
135        final boolean normalize = this.isNormalizeMessages();
136        final Map<String, String> messages = normalize ?
137            new TreeMap<String, String>() : new LinkedHashMap<String, String>();
138
139        // - only retrieve the messages for the entry use case (i.e. the use case
140        //   where the application begins)
141        if (this.isEntryUseCase())
142        {
143            final List<FrontEndUseCase> useCases = this.getAllUseCases();
144            for (int ctr = 0; ctr < useCases.size(); ctr++)
145            {
146                // - usecase
147                final JSFUseCase useCase = (JSFUseCase)useCases.get(ctr);
148                messages.put(
149                    useCase.getTitleKey(),
150                    useCase.getTitleValue());
151
152                final List<FrontEndView> views = useCase.getViews();
153                for (int ctr2 = 0; ctr2 < views.size(); ctr2++)
154                {
155                    // - view
156                    final JSFView view = (JSFView)views.get(ctr2);
157                    messages.put(
158                        view.getTitleKey(),
159                        view.getTitleValue());
160                    messages.put(
161                        view.getMessageKey(),
162                        view.getMessageValue());
163                    messages.put(
164                        view.getDocumentationKey(),
165                        view.getDocumentationValue());
166
167                    final List<FrontEndParameter> viewVariables = view.getVariables();
168                    for (int ctr3 = 0; ctr3 < viewVariables.size(); ctr3++)
169                    {
170                        // - page variables
171                        final Object object = viewVariables.get(ctr3);
172                        if (object instanceof JSFParameter)
173                        {
174                            final JSFParameter parameter = (JSFParameter)object;
175
176                            final Collection<ClassifierFacade> resolvingTypes = new ArrayList<ClassifierFacade>();
177                            this.collectAttributeMessages(messages, parameter.getAttributes(), resolvingTypes);
178                            this.collectAssociationEndMessages(messages,
179                                parameter.getNavigableAssociationEnds(), resolvingTypes);
180                            messages.put(
181                                parameter.getMessageKey(),
182                                parameter.getMessageValue());
183
184                            // - table
185                            if (parameter.isTable())
186                            {
187                                for (String columnName : parameter.getTableColumnNames())
188                                {
189                                     messages.put(
190                                        parameter.getTableColumnMessageKey(columnName),
191                                        parameter.getTableColumnMessageValue(columnName));
192                                }
193                            }
194                        }
195                    }
196
197                    final List<FrontEndAction> actions = useCase.getActions();
198                    for (int ctr3 = 0; ctr3 < actions.size(); ctr3++)
199                    {
200                        // - action
201                        final JSFAction action = (JSFAction)actions.get(ctr3);
202
203                        // - event/trigger
204                        final Object trigger = action.getTrigger();
205                        if (trigger != null && trigger instanceof JSFEvent)
206                        {
207                            final JSFEvent event = (JSFEvent)trigger;
208                            // only add these when a trigger is present, otherwise it's no use having them
209                            messages.put(
210                                action.getDocumentationKey(),
211                                action.getDocumentationValue());
212
213                            // the regular trigger messages
214                            messages.put(
215                                event.getResetMessageKey(),
216                                event.getResetMessageValue());
217
218                            // this one is the same as doing: action.getMessageKey()
219                            messages.put(
220                                event.getMessageKey(),
221                                event.getMessageValue());
222
223                            // - IMAGE LINK
224
225                            /*if (action.isImageLink())
226                            {
227                                messages.put(
228                                    action.getImageMessageKey(),
229                                    action.getImagePath());
230                            }*/
231                        }
232
233                        // - forwards
234                        for (final FrontEndForward forward : action.getTransitions())
235                        {
236                            if (forward instanceof JSFForward)
237                            {
238                                final JSFForward forwardTransition = (JSFForward)forward;
239                                messages.putAll(forwardTransition.getSuccessMessages());
240                                messages.putAll(forwardTransition.getWarningMessages());
241                            }
242                            else
243                            {
244                                final JSFAction actionTransition = (JSFAction)forward;
245                                messages.putAll(actionTransition.getSuccessMessages());
246                                messages.putAll(actionTransition.getWarningMessages());
247                            }
248
249                        }
250
251                        // - action parameters
252                        final List<FrontEndParameter> parameters = action.getParameters();
253                        for (int l = 0; l < parameters.size(); l++)
254                        {
255                            final Object object = parameters.get(l);
256                            if (object instanceof JSFParameter)
257                            {
258                                final JSFParameter parameter = (JSFParameter)object;
259                                final Collection attributes = parameter.getAttributes();
260                                if (!attributes.isEmpty())
261                                {
262                                    for (final Iterator iterator = attributes.iterator(); iterator.hasNext();)
263                                    {
264                                        final JSFAttribute attribute = (JSFAttribute)iterator.next();
265                                        messages.put(
266                                            attribute.getMessageKey(),
267                                            attribute.getMessageValue());
268                                    }
269                                }
270                                final Collection associationEnds = parameter.getNavigableAssociationEnds();
271                                if (!associationEnds.isEmpty())
272                                {
273                                    for (final Iterator iterator = associationEnds.iterator(); iterator.hasNext();)
274                                    {
275                                        final AssociationEndFacade end = (AssociationEndFacade)iterator.next();
276                                        final ClassifierFacade type = end.getType();
277                                        if (type != null)
278                                        {
279                                            final Collection<AttributeFacade> typeAttributes = type.getAttributes();
280                                            if (!attributes.isEmpty())
281                                            {
282                                                for (final Iterator<AttributeFacade> attributeIterator
283                                                    = typeAttributes.iterator(); attributeIterator.hasNext(); )
284                                                {
285                                                    final JSFAttribute attribute = (JSFAttribute)attributeIterator.next();
286                                                    messages.put(
287                                                        attribute.getMessageKey(),
288                                                        attribute.getMessageValue());
289                                                }
290                                            }
291                                        }
292                                    }
293                                }
294                                messages.put(
295                                    parameter.getMessageKey(),
296                                    parameter.getMessageValue());
297                                messages.put(
298                                    parameter.getDocumentationKey(),
299                                    parameter.getDocumentationValue());
300
301                                // - submittable input table
302                                if (parameter.isInputTable())
303                                {
304                                    final Collection<String> columnNames = parameter.getTableColumnNames();
305                                    for (final Iterator<String> columnNameIterator = columnNames.iterator();
306                                        columnNameIterator.hasNext();)
307                                    {
308                                        final String columnName = columnNameIterator.next();
309                                        messages.put(
310                                            parameter.getTableColumnMessageKey(columnName),
311                                            parameter.getTableColumnMessageValue(columnName));
312                                    }
313                                }
314                                /*if (parameter.getValidWhen() != null)
315                                {
316                                    // this key needs to be fully qualified since the valid when value can be different
317                                    final String completeKeyPrefix =
318                                        (normalize)
319                                        ? useCase.getTitleKey() + '.' + view.getMessageKey() + '.' +
320                                        action.getMessageKey() + '.' + parameter.getMessageKey() : parameter.getMessageKey();
321                                    messages.put(
322                                        completeKeyPrefix + "_validwhen",
323                                        "{0} is only valid when " + parameter.getValidWhen());
324                                }*/
325                                /*if (parameter.getOptionCount() > 0)
326                                {
327                                    final List optionKeys = parameter.getOptionKeys();
328                                    final List optionValues = parameter.getOptionValues();
329
330                                    for (int m = 0; m < optionKeys.size(); m++)
331                                    {
332                                        messages.put(
333                                            optionKeys.get(m),
334                                            optionValues.get(m));
335                                        messages.put(
336                                            optionKeys.get(m) + ".title",
337                                            optionValues.get(m));
338                                    }
339                                }*/
340                            }
341                        }
342
343                        // - portlet preferences
344                        final JSFPortletPreferences preferences = useCase.getPreferences();
345                        if (preferences != null)
346                        {
347                            final Collection<AttributeFacade> attributes = preferences.getAttributes(true);
348                            if (!attributes.isEmpty())
349                            {
350                                for (final Iterator iterator = attributes.iterator(); iterator.hasNext();)
351                                {
352                                    final JSFAttribute attribute = (JSFAttribute)iterator.next();
353                                    messages.put(
354                                        attribute.getMessageKey(),
355                                        attribute.getMessageValue());
356                                }
357                            }
358                        }
359
360                        // - exception forwards
361
362                        /*
363                        final List exceptions = action.getActionExceptions();
364
365                        if (normalize)
366                        {
367                            if (exceptions.isEmpty())
368                            {
369                                messages.put("exception.occurred", "{0}");
370                            }
371                            else
372                            {
373                                for (int l = 0; l < exceptions.size(); l++)
374                                {
375                                    final FrontEndExceptionHandler exception =
376                                        (FrontEndExceptionHandler)exceptions.get(l);
377                                    messages.put(action.getMessageKey() + '.' + exception.getExceptionKey(), "{0}");
378                                }
379                            }
380                        }
381                        else
382                        {
383                            if (exceptions.isEmpty())
384                            {
385                                if (!action.isUseCaseStart())
386                                {
387                                    messages.put(action.getMessageKey() + ".exception", "{0} (java.lang.Exception)");
388                                }
389                            }
390                            else
391                            {
392                                for (int l = 0; l < exceptions.size(); l++)
393                                {
394                                    final FrontEndExceptionHandler exception =
395                                        (FrontEndExceptionHandler)exceptions.get(l);
396
397                                    // we construct the key using the action message too because the exception can
398                                    // belong to more than one action (therefore it cannot return the correct value
399                                    // in .getExceptionKey())
400                                    messages.put(
401                                        action.getMessageKey() + '.' + exception.getExceptionKey(),
402                                        "{0} (" + exception.getExceptionType() + ")");
403                                }
404                            }
405                        }*/
406                    }
407                }
408            }
409        }
410        return messages;
411    }
412
413    /**
414     * Collects all attribute messages into the given Map.
415     *
416     * @param messages the Map in which messages are collected.
417     * @param attributes the attributes to collect the messages from.
418     * @param resolvingTypes used to prevent endless recursion.
419     */
420    private void collectAttributeMessages(Map<String,String> messages, Collection attributes,
421        final Collection<ClassifierFacade> resolvingTypes)
422    {
423        if (attributes != null && !attributes.isEmpty())
424        {
425            for (final Iterator iterator = attributes.iterator(); iterator.hasNext(); )
426            {
427                final JSFAttribute attribute = (JSFAttribute)iterator.next();
428                messages.put(
429                    attribute.getMessageKey(),
430                    attribute.getMessageValue());
431                // - lets go another level for nested attributes
432                this.collectTypeMessages(messages, attribute.getType(), resolvingTypes);
433            }
434        }
435    }
436
437    /**
438     * Collects all association end messages into the given Map.
439     *
440     * @param messages the Map in which messages are collected.
441     * @param associationEnds the association ends to collect the messages from.
442     * @param resolvingTypes used to prevent endless recursion.
443     */
444    private void collectAssociationEndMessages(Map<String,String> messages, Collection associationEnds,
445        final Collection<ClassifierFacade> resolvingTypes)
446    {
447        if (associationEnds != null && !associationEnds.isEmpty())
448        {
449            for (final Iterator iterator = associationEnds.iterator(); iterator.hasNext();)
450            {
451                final AssociationEndFacade end = (AssociationEndFacade)iterator.next();
452                this.collectTypeMessages(messages, end.getType(), resolvingTypes);
453            }
454        }
455    }
456
457    private void collectTypeMessages(Map<String,String> messages, ClassifierFacade type,
458         final Collection<ClassifierFacade> resolvingTypes)
459    {
460        if (type != null)
461        {
462            if (!resolvingTypes.contains(type))
463            {
464                resolvingTypes.add(type);
465                if (type.isArrayType())
466                {
467                    type = type.getNonArray();
468                }
469                //check again, since the type can be changed
470                if (!resolvingTypes.contains(type))
471                {
472                    this.collectAttributeMessages(messages, type.getAttributes(), resolvingTypes);
473                    this.collectAssociationEndMessages(messages, type.getNavigableConnectingEnds(), resolvingTypes);
474                }
475            }
476            resolvingTypes.remove(type);
477        }
478    }
479
480    /**
481     * @return actionForwards
482     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getActionForwards()
483     */
484    protected List<JSFAction> handleGetActionForwards()
485    {
486        final Set<JSFAction> actionForwards = new LinkedHashSet<JSFAction>();
487        for (final FrontEndView view : this.getViews())
488        {
489            actionForwards.addAll(((JSFView)view).getActionForwards());
490        }
491        return new ArrayList<JSFAction>(actionForwards);
492    }
493
494    /**
495     * @return forwards
496     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getForwards()
497     */
498    protected List<JSFForward> handleGetForwards()
499    {
500        final Map<String, JSFForward> forwards = new LinkedHashMap<String, JSFForward>();
501        for (final FrontEndAction action : this.getActions())
502        {
503            for (final FrontEndForward forward : action.getActionForwards())
504            {
505                if (forward instanceof JSFForward)
506                {
507                    forwards.put(forward.getName(), (JSFForward) forward);
508                }
509            }
510        }
511        return new ArrayList(forwards.values());
512    }
513
514    /**
515     * @return allForwards
516     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getAllForwards()
517     */
518    @SuppressWarnings("unchecked")
519    protected List<ModelElementFacade> handleGetAllForwards()
520    {
521        final Map<String, ModelElementFacade> forwards = new LinkedHashMap<String, ModelElementFacade>();
522        for (final JSFAction forward : this.getActionForwards())
523        {
524            forwards.put(forward.getName(), forward);
525        }
526        for (final JSFForward forward : this.getForwards())
527        {
528            forwards.put(forward.getName(), forward);
529        }
530        return new ArrayList<ModelElementFacade>(forwards.values());
531    }
532
533    /**
534     * @return upperCamelCaseName(this.getName())
535     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getActionClassName()
536     */
537    protected String handleGetActionClassName()
538    {
539        return StringUtilsHelper.upperCamelCaseName(this.getName());
540    }
541
542    /**
543     * @return getFullyQualifiedActionClassName().replace('.', '/') + ".java"
544     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getFullyQualifiedActionClassPath()
545     */
546    protected String handleGetFullyQualifiedActionClassPath()
547    {
548        return this.getFullyQualifiedActionClassName().replace(
549            '.',
550            '/') + ".java";
551    }
552
553    /**
554     * @return lowerCamelCaseName(this.getName())
555     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getControllerAction()
556     */
557    protected String handleGetControllerAction()
558    {
559        return StringUtilsHelper.lowerCamelCaseName(this.getName());
560    }
561
562    /**
563     * @return fullyQualifiedActionClassName
564     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getFullyQualifiedActionClassName()
565     */
566    protected String handleGetFullyQualifiedActionClassName()
567    {
568        final StringBuilder path = new StringBuilder();
569        final String packageName = this.getPackageName();
570        if (StringUtils.isNotBlank(packageName))
571        {
572            path.append(packageName);
573            path.append('.');
574        }
575        path.append(this.getActionClassName());
576        return path.toString();
577    }
578
579    /**
580     * @return formKeyValue
581     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getFormKey()
582     */
583    protected String handleGetFormKey()
584    {
585        final Object formKeyValue = this.findTaggedValue(JSFProfile.TAGGEDVALUE_ACTION_FORM_KEY);
586        return formKeyValue == null ? ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.ACTION_FORM_KEY))
587                                    : String.valueOf(formKeyValue);
588    }
589
590    /**
591     * @return initialTargetPath
592     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getInitialTargetPath()
593     */
594    protected String handleGetInitialTargetPath()
595    {
596        String path = null;
597        final Object target = this.getInitialTarget();
598        if (target instanceof JSFView)
599        {
600            path = ((JSFView)target).getPath();
601        }
602        else if (target instanceof JSFUseCase)
603        {
604            path = ((JSFUseCase)target).getPath();
605        }
606        return path;
607    }
608
609    /**
610     * Gets the initial target when this use case is entered.
611     *
612     * @return the initial target.
613     */
614    private Object getInitialTarget()
615    {
616        Object initialTarget = null;
617        final FrontEndActivityGraph graph = this.getActivityGraph();
618        final FrontEndAction action = graph != null ? this.getActivityGraph().getInitialAction() : null;
619        final Collection<FrontEndForward> forwards = action != null ? action.getActionForwards() : null;
620        if (forwards != null && !forwards.isEmpty())
621        {
622            final Object target = forwards.iterator().next().getTarget();
623            if (target instanceof FrontEndView)
624            {
625                initialTarget = target;
626            }
627            else if (target instanceof FrontEndFinalState)
628            {
629                final FrontEndFinalState finalState = (FrontEndFinalState)target;
630                final FrontEndUseCase targetUseCase = finalState.getTargetUseCase();
631                if (targetUseCase != null && !targetUseCase.equals(this.THIS()))
632                {
633                    initialTarget = targetUseCase;
634                }
635            }
636        }
637        return initialTarget;
638    }
639
640    /**
641     * @return required
642     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#isValidationRequired()
643     */
644    protected boolean handleIsValidationRequired()
645    {
646        boolean required = false;
647        for (final FrontEndView feView : this.getViews())
648        {
649            final JSFView view = (JSFView)feView;
650            if (view.isValidationRequired())
651            {
652                required = true;
653                break;
654            }
655        }
656        return required;
657    }
658
659    /**
660     * @return getInitialTarget() instanceof JSFView
661     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#isInitialTargetView()
662     */
663    protected boolean handleIsInitialTargetView()
664    {
665        return this.getInitialTarget() instanceof JSFView;
666    }
667
668    /**
669     * @return required
670     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#isInitialTargetView()
671     */
672    protected boolean handleIsApplicationValidationRequired()
673    {
674        boolean required = false;
675        for (final FrontEndUseCase feUseCase : this.getAllUseCases())
676        {
677            final JSFUseCase useCase = (JSFUseCase)feUseCase;
678            if (useCase.isValidationRequired())
679            {
680                required = true;
681                break;
682            }
683        }
684        return required;
685    }
686
687    /**
688     * @return sameName
689     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#isViewHasNameOfUseCase()
690     */
691    protected boolean handleIsViewHasNameOfUseCase()
692    {
693        boolean sameName = false;
694        for (final FrontEndView view : this.getViews())
695        {
696            sameName = ((JSFView)view).isHasNameOfUseCase();
697            if (sameName)
698            {
699                break;
700            }
701        }
702        return sameName;
703    }
704
705    /**
706     * @return hasStereotype(JSFProfile.STEREOTYPE_FRONT_END_REGISTRATION)
707     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#isRegistrationUseCase()
708     */
709    protected boolean handleIsRegistrationUseCase()
710    {
711        return this.hasStereotype(JSFProfile.STEREOTYPE_FRONT_END_REGISTRATION);
712    }
713
714    /**
715     * @return useCases
716     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getRegistrationUseCases()
717     */
718    @SuppressWarnings("unchecked")
719    protected List<FrontEndUseCase> handleGetRegistrationUseCases()
720    {
721        final List<FrontEndUseCase> useCases = new ArrayList<FrontEndUseCase>(this.getAllUseCases());
722        for (final Iterator<FrontEndUseCase> iterator = useCases.iterator(); iterator.hasNext();)
723        {
724            final FrontEndUseCase useCase = iterator.next();
725            if (useCase instanceof JSFUseCase)
726            {
727                if (!((JSFUseCase)useCase).isRegistrationUseCase())
728                {
729                    iterator.remove();
730                }
731            }
732            else
733            {
734                iterator.remove();
735            }
736        }
737        return useCases;
738    }
739
740    /**
741     * The suffix for the forwards class name.
742     */
743    private static final String FORWARDS_CLASS_NAME_SUFFIX = "Forwards";
744
745    /**
746     * @return getName() + FORWARDS_CLASS_NAME_SUFFIX
747     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getForwardsClassName()
748     */
749    protected String handleGetForwardsClassName()
750    {
751        return StringUtilsHelper.upperCamelCaseName(this.getName()) + FORWARDS_CLASS_NAME_SUFFIX;
752    }
753
754    /**
755     * @return navigationRules
756     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getNavigationRules()
757     */
758    @SuppressWarnings("unchecked")
759    protected Collection<Object> handleGetNavigationRules()
760    {
761        final Map<String, Object> rules = new LinkedHashMap<String, Object>();
762        for (final FrontEndView feView : this.getViews())
763        {
764            final JSFView view = (JSFView)feView;
765            rules.put(view.getFromOutcome(), view);
766            for (final Iterator forwardIterator = view.getForwards().iterator(); forwardIterator.hasNext();)
767            {
768                final Object forward = forwardIterator.next();
769                String name;
770                if (forward instanceof JSFForward)
771                {
772                    name = ((JSFForward)forward).getFromOutcome();
773                }
774                else
775                {
776                    name = ((JSFAction)forward).getFromOutcome();
777                }
778                rules.put(name, forward);
779            }
780        }
781        return rules.values();
782    }
783
784    /**
785     * @return navigationChildren
786     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getNavigationChildren()
787     */
788    protected Collection<UseCaseFacade> handleGetNavigationChildren()
789    {
790        return CollectionUtils.collect(getIncludes(), new Transformer()
791        {
792            public Object transform(Object object)
793            {
794                final IncludeFacade include = (IncludeFacade)object;
795                return include.getAddition();
796            }
797        });
798    }
799
800    private static boolean isParent(final JSFUseCase useCase1, final JSFUseCase useCase2)
801    {
802        return CollectionUtils.exists(useCase2.getIncludes(), new Predicate()
803        {
804            public boolean evaluate(Object object)
805            {
806                final IncludeFacade include = (IncludeFacade)object;
807                return include.getAddition().equals(useCase1);
808            }
809        });
810    }
811
812    /**
813     * @return navigationParents
814     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getNavigationParents()
815     */
816    protected Collection<FrontEndUseCase> handleGetNavigationParents()
817    {
818        final JSFUseCase theUseCase = this;
819        return CollectionUtils.select(getAllUseCases(),new Predicate() {
820            @SuppressWarnings("synthetic-access")
821            public boolean evaluate(Object o)
822            {
823                final JSFUseCase useCase = (JSFUseCase)o;
824                if (theUseCase.equals(useCase))
825                {
826                    return false;
827                }
828                return isParent(theUseCase, useCase);
829            }
830        });
831    }
832
833    /**
834     * @return actionRoles
835     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCase#getActionRoles()
836     */
837    protected String handleGetActionRoles()
838    {
839        final StringBuilder rolesBuffer = new StringBuilder();
840        boolean first = true;
841        for (final Role role : this.getRoles())
842        {
843            if (first)
844            {
845                first = false;
846            }
847            else
848            {
849                rolesBuffer.append(',');
850            }
851            rolesBuffer.append(role.getName());
852        }
853        return rolesBuffer.toString();
854    }
855
856    /**
857     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCaseLogic#handleGetPreferences()
858     */
859    @Override
860    protected Object handleGetPreferences()
861    {
862        JSFPortletPreferences preferences = null;
863        final Collection<DependencyFacade> dependencies = this.getSourceDependencies();
864        if (dependencies != null && !dependencies.isEmpty())
865        {
866            for (final DependencyFacade dependency : dependencies)
867            {
868                final Object target = dependency.getTargetElement();
869                if (dependency.getTargetElement() instanceof JSFPortletPreferences)
870                {
871                    preferences = (JSFPortletPreferences)target;
872                    break;
873                }
874            }
875        }
876        return preferences;
877    }
878
879    /**
880     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCaseLogic#handleGetPortletEditForwardName()
881     */
882    @Override
883    protected String handleGetPortletEditForwardName()
884    {
885        return this.getWebResourceName() + "-portlet-edit";
886    }
887
888    /**
889     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCaseLogic#handleGetPortletEditPath()
890     */
891    @Override
892    protected String handleGetPortletEditPath()
893    {
894        return this.getPathRoot() + "/" + this.getPortletEditForwardName();
895    }
896
897    /**
898     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCaseLogic#handleGetPortletHelpForwardName()
899     */
900    @Override
901    protected String handleGetPortletHelpForwardName()
902    {
903        return this.getWebResourceName() + "-portlet-help";
904    }
905
906    /**
907     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCaseLogic#handleGetPortletHelpPath()
908     */
909    @Override
910    protected String handleGetPortletHelpPath()
911    {
912        return this.getPathRoot() + "/" + this.getPortletHelpForwardName();
913    }
914
915    /**
916     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCaseLogic#handleGetPortletViewForwardName()
917     */
918    @Override
919    protected String handleGetPortletViewForwardName()
920    {
921        return this.getWebResourceName() + "-portlet-view";
922    }
923
924    private String getWebResourceName()
925    {
926        return JSFUtils.toWebResourceName(this.getName());
927    }
928
929    /**
930     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCaseLogic#handleGetPortletViewPath()
931     */
932    @Override
933    protected String handleGetPortletViewPath()
934    {
935        return this.getPath();
936    }
937
938    /**
939     * @see org.andromda.cartridges.jsf2.metafacades.JSFUseCaseLogic#handleGetAllViews()
940     */
941    @SuppressWarnings("unchecked")
942    @Override
943    protected Collection<FrontEndView> handleGetAllViews()
944    {
945        final Set<FrontEndView> allViews = new LinkedHashSet<FrontEndView>();
946        for (final FrontEndUseCase useCase : this.getAllUseCases())
947        {
948            allViews.addAll(useCase.getViews());
949        }
950        return allViews;
951    }
952}