001: package org.andromda.metafacades.uml14;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006: import java.util.LinkedHashMap;
007: import java.util.LinkedHashSet;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.Set;
011:
012: import org.andromda.metafacades.uml.ClassifierFacade;
013: import org.andromda.metafacades.uml.EventFacade;
014: import org.andromda.metafacades.uml.FrontEndAction;
015: import org.andromda.metafacades.uml.FrontEndActionState;
016: import org.andromda.metafacades.uml.FrontEndActivityGraph;
017: import org.andromda.metafacades.uml.FrontEndController;
018: import org.andromda.metafacades.uml.FrontEndControllerOperation;
019: import org.andromda.metafacades.uml.FrontEndEvent;
020: import org.andromda.metafacades.uml.FrontEndForward;
021: import org.andromda.metafacades.uml.FrontEndParameter;
022: import org.andromda.metafacades.uml.FrontEndPseudostate;
023: import org.andromda.metafacades.uml.FrontEndUseCase;
024: import org.andromda.metafacades.uml.ModelElementFacade;
025: import org.andromda.metafacades.uml.OperationFacade;
026: import org.andromda.metafacades.uml.ParameterFacade;
027: import org.andromda.metafacades.uml.StateVertexFacade;
028:
029: /**
030: * MetafacadeLogic implementation for org.andromda.metafacades.uml.FrontEndControllerOperation.
031: *
032: * @see org.andromda.metafacades.uml.FrontEndControllerOperation
033: */
034: public class FrontEndControllerOperationLogicImpl extends
035: FrontEndControllerOperationLogic {
036: public FrontEndControllerOperationLogicImpl(Object metaObject,
037: String context) {
038: super (metaObject, context);
039: }
040:
041: /**
042: * Override to return the owner's package as the package name.
043: *
044: * @see org.andromda.metafacades.uml14.ModelElementFacadeLogic#handleGetPackageName()
045: */
046: public String handleGetPackageName() {
047: final ClassifierFacade owner = this .getOwner();
048: return owner != null ? owner.getPackageName() : "";
049: }
050:
051: /**
052: * @see org.andromda.metafacades.uml.FrontEndControllerOperation#isOwnerIsController()
053: */
054: protected boolean handleIsOwnerIsController() {
055: return this .getOwner() instanceof FrontEndController;
056: }
057:
058: /**
059: * @see org.andromda.metafacades.uml.FrontEndControllerOperation#getFormFields()
060: */
061: protected java.util.List handleGetFormFields() {
062: final Map formFieldsMap = new LinkedHashMap();
063:
064: // for quick lookup we use a hashset for the argument names, we only consider parameters with a name
065: // which is also present in this set
066: final Set argumentNames = new LinkedHashSet();
067: final Collection arguments = this .getArguments();
068: for (final Iterator argumentIterator = arguments.iterator(); argumentIterator
069: .hasNext();) {
070: final ModelElementFacade element = (ModelElementFacade) argumentIterator
071: .next();
072: argumentNames.add(element.getName());
073: }
074:
075: // - get all actions deferring to this operation
076: final List deferringActions = this .getDeferringActions();
077: for (final Iterator iterator = deferringActions.iterator(); iterator
078: .hasNext();) {
079: final FrontEndAction action = (FrontEndAction) iterator
080: .next();
081: // store the action parameters
082: final List actionFormFields = action.getFormFields();
083: for (final Iterator fieldIterator = actionFormFields
084: .iterator(); fieldIterator.hasNext();) {
085: final FrontEndParameter parameter = (FrontEndParameter) fieldIterator
086: .next();
087: final String name = parameter.getName();
088: // - only add if the parameter is an action parameter and its an argument of this operation
089: if (parameter.getAction() != null
090: && argumentNames.contains(name)) {
091: formFieldsMap.put(name, parameter);
092: }
093: }
094: // get all forwards and overwrite when we find a table (or add when not yet present)
095: final List forwards = action.getActionForwards();
096: for (final Iterator forwardIterator = forwards.iterator(); forwardIterator
097: .hasNext();) {
098: final FrontEndForward forward = (FrontEndForward) forwardIterator
099: .next();
100: // - only consider forwards directly entering a view
101: if (forward.isEnteringView()) {
102: final List viewVariables = forward
103: .getForwardParameters();
104: for (final Iterator variableIterator = viewVariables
105: .iterator(); variableIterator.hasNext();) {
106: final FrontEndParameter viewVariable = (FrontEndParameter) variableIterator
107: .next();
108: final String name = viewVariable.getName();
109: if (argumentNames.contains(name)) {
110: if (!formFieldsMap.containsKey(name)
111: || viewVariable.isTable()) {
112: formFieldsMap.put(name, viewVariable);
113: }
114: }
115: }
116: }
117: }
118: }
119:
120: // since all arguments need to be present we add those that haven't yet been stored in the map
121: for (final Iterator argumentIterator = arguments.iterator(); argumentIterator
122: .hasNext();) {
123: final FrontEndParameter argument = (FrontEndParameter) argumentIterator
124: .next();
125: final String name = argument.getName();
126: if (!formFieldsMap.containsKey(name)) {
127: formFieldsMap.put(name, argument);
128: }
129: }
130: return new ArrayList(formFieldsMap.values());
131: }
132:
133: /**
134: * @see org.andromda.metafacades.uml.FrontEndControllerOperation#getActivityGraph()
135: */
136: protected Object handleGetActivityGraph() {
137: Object graph = null;
138:
139: final ClassifierFacade owner = getOwner();
140: if (owner instanceof FrontEndController) {
141: final FrontEndController controller = (FrontEndController) owner;
142: if (controller != null) {
143: final FrontEndUseCase useCase = controller.getUseCase();
144: if (useCase != null) {
145: graph = useCase.getActivityGraph();
146: }
147: }
148: }
149: return graph;
150: }
151:
152: /**
153: * @see org.andromda.metafacades.uml.FrontEndControllerOperation#getDeferringActions()
154: */
155: protected java.util.List handleGetDeferringActions() {
156: final Collection deferringActions = new LinkedHashSet();
157:
158: final FrontEndActivityGraph graph = this .getActivityGraph();
159: if (graph != null) {
160: final Collection actionStates = graph.getActionStates();
161: for (final Iterator actionStateIterator = actionStates
162: .iterator(); actionStateIterator.hasNext();) {
163: final Object actionStateObject = actionStateIterator
164: .next();
165: if (actionStateObject instanceof FrontEndActionState) {
166: final FrontEndActionState actionState = (FrontEndActionState) actionStateObject;
167: final Collection controllerCalls = actionState
168: .getControllerCalls();
169: for (final Iterator controllerCallIterator = controllerCalls
170: .iterator(); controllerCallIterator
171: .hasNext();) {
172: final OperationFacade operation = (OperationFacade) controllerCallIterator
173: .next();
174: if (this .equals(operation)) {
175: deferringActions.addAll(actionState
176: .getContainerActions());
177: }
178: }
179: }
180: }
181:
182: final Collection transitions = graph.getTransitions();
183: for (final Iterator transitionIterator = transitions
184: .iterator(); transitionIterator.hasNext();) {
185: final FrontEndForward transition = (FrontEndForward) transitionIterator
186: .next();
187: final EventFacade event = transition.getTrigger();
188: if (event instanceof FrontEndEvent) {
189: final FrontEndEvent trigger = (FrontEndEvent) event;
190: final FrontEndControllerOperation operation = trigger
191: .getControllerCall();
192: if (this .equals(operation)) {
193: // we have two types of controller calls: the ones in action states and the ones for decisions
194: final StateVertexFacade source = transition
195: .getSource();
196: if (source instanceof FrontEndActionState) {
197: final FrontEndActionState sourceActionState = (FrontEndActionState) source;
198: deferringActions.addAll(sourceActionState
199: .getContainerActions());
200: }
201:
202: // test for decision
203: final StateVertexFacade target = transition
204: .getTarget();
205: if (target instanceof FrontEndPseudostate) {
206: final FrontEndPseudostate targetPseudoState = (FrontEndPseudostate) target;
207: if (targetPseudoState.isDecisionPoint()) {
208: deferringActions
209: .addAll(targetPseudoState
210: .getContainerActions());
211: }
212: }
213: }
214: }
215: }
216: }
217: return new ArrayList(deferringActions);
218: }
219:
220: /**
221: * @see org.andromda.metafacades.uml.FrontEndControllerOperation#isAllArgumentsHaveFormFields()
222: */
223: protected boolean handleIsAllArgumentsHaveFormFields() {
224: final Collection arguments = this .getArguments();
225: final Collection deferringActions = this .getDeferringActions();
226:
227: boolean allArgumentsHaveFormFields = true;
228: for (final Iterator iterator = arguments.iterator(); iterator
229: .hasNext()
230: && allArgumentsHaveFormFields;) {
231: final ParameterFacade parameter = (ParameterFacade) iterator
232: .next();
233: final String parameterName = parameter.getName();
234: final ClassifierFacade parameterType = parameter.getType();
235: final String parameterTypeName = parameterType != null ? parameterType
236: .getFullyQualifiedName()
237: : "";
238:
239: boolean actionMissingField = false;
240: for (final Iterator actionIterator = deferringActions
241: .iterator(); actionIterator.hasNext()
242: && !actionMissingField;) {
243: final FrontEndAction action = (FrontEndAction) actionIterator
244: .next();
245: final Collection actionFormFields = action
246: .getFormFields();
247:
248: boolean fieldPresent = false;
249: for (final Iterator fieldIterator = actionFormFields
250: .iterator(); fieldIterator.hasNext()
251: && !fieldPresent;) {
252: final ParameterFacade field = (ParameterFacade) fieldIterator
253: .next();
254: final ClassifierFacade fieldType = field.getType();
255: final String fieldTypeName = fieldType != null ? fieldType
256: .getFullyQualifiedName()
257: : "";
258: if (parameterName.equals(field.getName())
259: && parameterTypeName.equals(fieldTypeName)) {
260: fieldPresent = true;
261: }
262: }
263: actionMissingField = !fieldPresent;
264: }
265: allArgumentsHaveFormFields = !actionMissingField;
266: }
267: return allArgumentsHaveFormFields;
268: }
269: }
|