001package org.andromda.cartridges.jsf2.metafacades;
002
003import java.util.Collection;
004import java.util.Collections;
005import java.util.Iterator;
006import java.util.LinkedHashMap;
007import java.util.Map;
008import org.andromda.cartridges.jsf2.JSFGlobals;
009import org.andromda.cartridges.jsf2.JSFProfile;
010import org.andromda.cartridges.jsf2.JSFUtils;
011import org.andromda.metafacades.uml.StateVertexFacade;
012import org.andromda.utils.StringUtilsHelper;
013
014/**
015 * MetafacadeLogic implementation for org.andromda.cartridges.jsf2.metafacades.JSFForward.
016 *
017 * @see org.andromda.cartridges.jsf2.metafacades.JSFForward
018 */
019public class JSFForwardLogicImpl
020    extends JSFForwardLogic
021{
022    private static final long serialVersionUID = 34L;
023    /**
024     * @param metaObject
025     * @param context
026     */
027    public JSFForwardLogicImpl(Object metaObject, String context)
028    {
029        super(metaObject, context);
030    }
031
032    /**
033     * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
034     */
035    public String getName()
036    {
037        StringBuilder name = new StringBuilder(super.getName());
038        final Object target = this.getTarget();
039        if (target instanceof JSFFinalState)
040        {
041            name.append(JSFGlobals.USECASE_FORWARD_NAME_SUFFIX);
042        }
043        else
044        {
045            name.insert(0, this.getUseCase().getName() + "-");
046        }
047        return JSFUtils.toWebResourceName(name.toString());
048    }
049
050    /**
051     * @return forwardPath
052     * @see org.andromda.cartridges.jsf2.metafacades.JSFForward#getPath()
053     */
054    protected String handleGetPath()
055    {
056        String forwardPath = null;
057        final StateVertexFacade target = getTarget();
058        if (this.isEnteringView())
059        {
060            forwardPath = ((JSFView)target).getPath();
061        }
062        else if (this.isEnteringFinalState())
063        {
064            forwardPath = ((JSFFinalState)target).getPath();
065        }
066
067        return forwardPath;
068    }
069
070    /**
071     * @see org.andromda.cartridges.jsf2.metafacades.JSFForwardLogic#handleIsSuccessMessagesPresent()
072     */
073    protected boolean handleIsSuccessMessagesPresent()
074    {
075        return !this.getSuccessMessages().isEmpty();
076    }
077
078    /**
079     * @see org.andromda.cartridges.jsf2.metafacades.JSFForwardLogic#handleIsWarningMessagesPresent()
080     */
081    protected boolean handleIsWarningMessagesPresent()
082    {
083        return !this.getWarningMessages().isEmpty();
084    }
085
086    /**
087     * Collects specific messages in a map.
088     *
089     * @param taggedValue the tagged value from which to read the message
090     * @return maps message keys to message values, but only those that match the arguments
091     *         will have been recorded
092     */
093    @SuppressWarnings("unchecked")
094    private Map<String, String> getMessages(String taggedValue)
095    {
096        Map<String, String> messages;
097
098        final Collection taggedValues = this.findTaggedValues(taggedValue);
099        if (taggedValues.isEmpty())
100        {
101            messages = Collections.EMPTY_MAP;
102        }
103        else
104        {
105            messages = new LinkedHashMap<String, String>(); // we want to keep the order
106
107            for (final Iterator iterator = taggedValues.iterator(); iterator.hasNext();)
108            {
109                final String value = (String)iterator.next();
110                messages.put(StringUtilsHelper.toResourceMessageKey(value), value);
111            }
112        }
113
114        return messages;
115    }
116
117    /**
118     * @see org.andromda.cartridges.jsf2.metafacades.JSFForwardLogic#handleGetSuccessMessages()
119     */
120    protected Map handleGetSuccessMessages()
121    {
122        return this.getMessages(JSFProfile.TAGGEDVALUE_ACTION_SUCCESS_MESSAGE);
123    }
124
125    /**
126     * @see org.andromda.cartridges.jsf2.metafacades.JSFForwardLogic#handleGetWarningMessages()
127     */
128    protected Map handleGetWarningMessages()
129    {
130        return this.getMessages(JSFProfile.TAGGEDVALUE_ACTION_WARNING_MESSAGE);
131    }
132
133    /**
134     * @return getTarget() instanceof JSFFinalState
135     * @see org.andromda.cartridges.jsf2.metafacades.JSFForward#isFinalStateTarget()
136     */
137    protected boolean handleIsFinalStateTarget()
138    {
139        return this.getTarget() instanceof JSFFinalState;
140    }
141
142    /**
143     * @return getName()
144     * @see org.andromda.cartridges.jsf2.metafacades.JSFForward#getFromOutcome()
145     */
146    protected String handleGetFromOutcome()
147    {
148        return this.getName();
149    }
150}