001: package org.andromda.cartridges.bpm4struts.metafacades;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Collections;
006: import java.util.Iterator;
007: import java.util.LinkedHashMap;
008: import java.util.LinkedHashSet;
009: import java.util.Map;
010: import java.util.Set;
011:
012: import org.andromda.cartridges.bpm4struts.Bpm4StrutsProfile;
013: import org.andromda.metafacades.uml.EventFacade;
014: import org.andromda.metafacades.uml.GuardFacade;
015: import org.andromda.metafacades.uml.PseudostateFacade;
016: import org.andromda.metafacades.uml.StateVertexFacade;
017: import org.andromda.utils.StringUtilsHelper;
018: import org.apache.commons.lang.StringUtils;
019:
020: /**
021: * MetafacadeLogic implementation.
022: *
023: * @see org.andromda.cartridges.bpm4struts.metafacades.StrutsForward
024: */
025: public class StrutsForwardLogicImpl extends StrutsForwardLogic {
026: public StrutsForwardLogicImpl(java.lang.Object metaObject,
027: java.lang.String context) {
028: super (metaObject, context);
029: }
030:
031: protected String handleGetGuardName() {
032: final GuardFacade guard = this .getGuard();
033: return (guard == null) ? null : guard.getName();
034: }
035:
036: protected boolean handleIsEnteringPage() {
037: return this .isEnteringView();
038: }
039:
040: protected java.lang.String handleGetForwardName() {
041: return StringUtilsHelper.toResourceMessageKey(this
042: .resolveName());
043: }
044:
045: protected java.lang.String handleGetForwardPath() {
046: String forwardPath = null;
047:
048: final StateVertexFacade target = this .getTarget();
049: if (isEnteringPage()) {
050: forwardPath = ((StrutsJsp) target).getFullPath() + ".jsp";
051: } else if (isEnteringFinalState()) {
052: forwardPath = ((StrutsFinalState) target).getFullPath();
053: }
054:
055: return forwardPath;
056: }
057:
058: protected String handleGetActionMethodName() {
059: return StringUtilsHelper.lowerCamelCaseName(this .resolveName());
060: }
061:
062: protected String handleGetTargetNameKey() {
063: if (this .isEnteringPage()) {
064: return ((StrutsJsp) this .getTarget()).getTitleKey();
065: } else if (this .isEnteringFinalState()) {
066: return ((StrutsUseCase) ((StrutsFinalState) this
067: .getTarget()).getTargetUseCase()).getTitleKey();
068: }
069: return null;
070: }
071:
072: /**
073: * If this forward has a trigger this method returns that trigger's name, otherwise if this forward
074: * has a name this method returns that name, otherwise if this forward's target has a name this
075: * method returns that name, otherwise simply returns <code>"unknown"</code>
076: */
077: private String resolveName() {
078: String forwardName = null;
079: //trigger
080: final EventFacade trigger = this .getTrigger();
081: if (trigger != null)
082: forwardName = trigger.getName();
083: //name
084: if (StringUtils.isEmpty(forwardName))
085: forwardName = this .getName();
086: //target
087: if (StringUtils.isEmpty(forwardName))
088: forwardName = this .getTarget().getName();
089: // else
090: if (StringUtils.isEmpty(forwardName))
091: forwardName = "unknown";
092: // return
093: return forwardName;
094: }
095:
096: protected boolean handleIsExitingPage() {
097: return this .isExitingView();
098: }
099:
100: protected boolean handleIsSuccessMessagesPresent() {
101: return !this .getSuccessMessages().isEmpty();
102: }
103:
104: protected boolean handleIsWarningMessagesPresent() {
105: return !this .getWarningMessages().isEmpty();
106: }
107:
108: /**
109: * Collects specific messages in a map.
110: *
111: * @param taggedValue the tagged value from which to read the message
112: * @return maps message keys to message values, but only those that match the arguments
113: * will have been recorded
114: */
115: private Map getMessages(String taggedValue) {
116: Map messages;
117:
118: final Collection taggedValues = this
119: .findTaggedValues(taggedValue);
120: if (taggedValues.isEmpty()) {
121: messages = Collections.EMPTY_MAP;
122: } else {
123: messages = new LinkedHashMap(); // we want to keep the order
124:
125: for (final Iterator iterator = taggedValues.iterator(); iterator
126: .hasNext();) {
127: final String value = (String) iterator.next();
128: messages.put(StringUtilsHelper
129: .toResourceMessageKey(value), value);
130: }
131: }
132:
133: return messages;
134: }
135:
136: protected Map handleGetSuccessMessages() {
137: return this
138: .getMessages(Bpm4StrutsProfile.TAGGEDVALUE_ACTION_SUCCESS_MESSAGE);
139: }
140:
141: protected Map handleGetWarningMessages() {
142: return this
143: .getMessages(Bpm4StrutsProfile.TAGGEDVALUE_ACTION_WARNING_MESSAGE);
144: }
145:
146: protected Object handleGetStrutsActivityGraph() {
147: return this .getFrontEndActivityGraph();
148: }
149:
150: /**
151: * Overridden since StrutsAction doesn't extend FrontEndAction.
152: *
153: * @see org.andromda.metafacades.uml.FrontEndForward#getActions()
154: */
155: public java.util.List getActions() {
156: final Set actions = new LinkedHashSet();
157: this .findActions(actions, new LinkedHashSet());
158: return new ArrayList(actions);
159: }
160:
161: /**
162: * Recursively finds all actions for this forward, what this means depends on the context in which
163: * this forward is used: if the source is a page action state it will collect all actions going out
164: * of this page, if the source is a regular action state it will collect all actions that might traverse
165: * this action state, if the source is the initial state it will collect all actions forwarding to this
166: * forward's use-case (please not that those actions most likely are defined in other use-cases).
167: *
168: * @param actions the default set of actions, duplicates will not be recorded
169: * @param handledForwards the forwards already processed
170: */
171: private final void findActions(final Set actions,
172: final Set handledForwards) {
173: if (!handledForwards.contains(this )) {
174: handledForwards.add(this );
175:
176: if (this instanceof StrutsAction) // @todo this is not so nice because StrutsAction extends StrutsForward, solution would be to override in StrutsAction
177: {
178: actions.add(this );
179: } else {
180: final StateVertexFacade vertex = getSource();
181: if (vertex instanceof StrutsJsp) {
182: final StrutsJsp jsp = (StrutsJsp) vertex;
183: actions.addAll(jsp.getActions());
184: } else if (vertex instanceof StrutsActionState) {
185: final StrutsActionState actionState = (StrutsActionState) vertex;
186: actions.addAll(actionState.getContainerActions());
187: } else if (vertex instanceof PseudostateFacade) {
188: final PseudostateFacade pseudostate = (PseudostateFacade) vertex;
189: if (!pseudostate.isInitialState()) {
190: final Collection incomingForwards = pseudostate
191: .getIncoming();
192: for (final Iterator forwardIterator = incomingForwards
193: .iterator(); forwardIterator.hasNext();) {
194: final StrutsForward forward = (StrutsForward) forwardIterator
195: .next();
196: actions.addAll(forward.getActions());
197: }
198: }
199: }
200: }
201: }
202: }
203: }
|