001: package org.andromda.metafacades.uml14;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Collections;
006: import java.util.LinkedHashMap;
007: import java.util.Iterator;
008: import java.util.LinkedHashSet;
009: import java.util.List;
010: import java.util.Map;
011:
012: import org.andromda.metafacades.uml.ActivityGraphFacade;
013: import org.andromda.metafacades.uml.AssociationEndFacade;
014: import org.andromda.metafacades.uml.ClassifierFacade;
015: import org.andromda.metafacades.uml.FrontEndAction;
016: import org.andromda.metafacades.uml.FrontEndActivityGraph;
017: import org.andromda.metafacades.uml.FrontEndFinalState;
018: import org.andromda.metafacades.uml.FrontEndForward;
019: import org.andromda.metafacades.uml.FrontEndParameter;
020: import org.andromda.metafacades.uml.FrontEndUseCase;
021: import org.andromda.metafacades.uml.FrontEndView;
022: import org.andromda.metafacades.uml.Role;
023: import org.andromda.metafacades.uml.UMLProfile;
024:
025: /**
026: * MetafacadeLogic implementation for org.andromda.metafacades.uml.FrontEndUseCase.
027: *
028: * @see org.andromda.metafacades.uml.FrontEndUseCase
029: */
030: public class FrontEndUseCaseLogicImpl extends FrontEndUseCaseLogic {
031: public FrontEndUseCaseLogicImpl(Object metaObject, String context) {
032: super (metaObject, context);
033: }
034:
035: /**
036: * @see org.andromda.metafacades.uml.FrontEndUseCase#isEntryUseCase()
037: */
038: protected boolean handleIsEntryUseCase() {
039: return hasStereotype(UMLProfile.STEREOTYPE_FRONT_END_APPLICATION);
040: }
041:
042: /**
043: * @see org.andromda.metafacades.uml.FrontEndUseCase#getController()
044: */
045: protected java.lang.Object handleGetController() {
046: final FrontEndActivityGraph graph = this .getActivityGraph();
047: return graph == null ? null : graph.getController();
048: }
049:
050: /**
051: * @see org.andromda.metafacades.uml.FrontEndUseCase#getActivityGraph()
052: */
053: protected java.lang.Object handleGetActivityGraph() {
054: ActivityGraphFacade activityGraph = null;
055:
056: // - in case there is a tagged value pointing to an activity graph, and this graph is found,
057: // return it.
058: final Object activity = this
059: .findTaggedValue(UMLProfile.TAGGEDVALUE_PRESENTATION_USECASE_ACTIVITY);
060: if (activity != null) {
061: final String activityName = String.valueOf(activity
062: .toString());
063: activityGraph = this .getModel().findActivityGraphByName(
064: activityName);
065: }
066:
067: // - otherwise just take the first one in this use-case's namespace.
068: if (activityGraph == null) {
069: final Collection ownedElements = this .getOwnedElements();
070: for (final Iterator iterator = ownedElements.iterator(); iterator
071: .hasNext();) {
072: final Object object = iterator.next();
073: if (object instanceof FrontEndActivityGraph) {
074: return object;
075: }
076: }
077: }
078: return activityGraph;
079: }
080:
081: /**
082: * @see org.andromda.metafacades.uml.FrontEndUseCase#getReferencingFinalStates()
083: */
084: protected List handleGetReferencingFinalStates() {
085: return new ArrayList(this .getModel()
086: .findFinalStatesWithNameOrHyperlink(this ));
087: }
088:
089: /**
090: * @see org.andromda.metafacades.uml.FrontEndUseCase#getAllUseCases()
091: */
092: protected List handleGetAllUseCases() {
093: final List useCases = new ArrayList();
094: for (final Iterator useCaseIterator = getModel()
095: .getAllUseCases().iterator(); useCaseIterator.hasNext();) {
096: final Object object = useCaseIterator.next();
097: if (object instanceof FrontEndUseCase) {
098: useCases.add(object);
099: }
100: }
101: return useCases;
102: }
103:
104: /**
105: * Gets those roles directly associated to this use-case.
106: */
107: private Collection getAssociatedRoles() {
108: final Collection usersList = new ArrayList();
109: final Collection associationEnds = getAssociationEnds();
110: for (final Iterator iterator = associationEnds.iterator(); iterator
111: .hasNext();) {
112: final AssociationEndFacade associationEnd = (AssociationEndFacade) iterator
113: .next();
114: final ClassifierFacade classifier = associationEnd
115: .getOtherEnd().getType();
116: if (classifier instanceof Role) {
117: usersList.add(classifier);
118: }
119: }
120: return usersList;
121: }
122:
123: /**
124: * Recursively collects all roles generalizing the argument user, in the specified collection.
125: */
126: private void collectRoles(final Role role, final Collection roles) {
127: if (!roles.contains(role)) {
128: roles.add(role);
129: final Collection childUsers = role.getGeneralizedByActors();
130: for (final Iterator iterator = childUsers.iterator(); iterator
131: .hasNext();) {
132: final Role childUser = (Role) iterator.next();
133: this .collectRoles(childUser, roles);
134: }
135: }
136: }
137:
138: /**
139: * @see org.andromda.metafacades.uml.FrontEndUseCase#getRoles()
140: */
141: protected List handleGetRoles() {
142: final Collection allRoles = new LinkedHashSet();
143: final Collection associatedUsers = this .getAssociatedRoles();
144: for (final Iterator iterator = associatedUsers.iterator(); iterator
145: .hasNext();) {
146: final Role user = (Role) iterator.next();
147: this .collectRoles(user, allRoles);
148: }
149: return new ArrayList(allRoles);
150: }
151:
152: /**
153: * @see org.andromda.metafacades.uml.FrontEndUseCase#getAllRoles()
154: */
155: protected List handleGetAllRoles() {
156: final Collection allRoles = new LinkedHashSet();
157: for (final Iterator iterator = this .getAllUseCases().iterator(); iterator
158: .hasNext();) {
159: allRoles.addAll(((FrontEndUseCase) iterator.next())
160: .getRoles());
161: }
162: return new ArrayList(allRoles);
163: }
164:
165: /**
166: * @see org.andromda.metafacades.uml.FrontEndUseCase#isSecured()
167: */
168: protected boolean handleIsSecured() {
169: return !this .getRoles().isEmpty();
170: }
171:
172: /**
173: * @see org.andromda.metafacades.uml.FrontEndUseCase#getViews()
174: */
175: protected List handleGetViews() {
176: List views;
177: final ActivityGraphFacade graph = this .getActivityGraph();
178: if (graph == null) {
179: views = Collections.EMPTY_LIST;
180: } else {
181: views = new ArrayList(getModel()
182: .getAllActionStatesWithStereotype(graph,
183: UMLProfile.STEREOTYPE_FRONT_END_VIEW));
184: }
185: return views;
186: }
187:
188: /**
189: * @see org.andromda.metafacades.uml.FrontEndUseCase#getViews()
190: */
191: protected List handleGetActions() {
192: final Collection actions = new LinkedHashSet();
193: final Collection pages = this .getViews();
194: for (final Iterator pageIterator = pages.iterator(); pageIterator
195: .hasNext();) {
196: final FrontEndView view = (FrontEndView) pageIterator
197: .next();
198: actions.addAll(view.getActions());
199: }
200:
201: final FrontEndActivityGraph graph = this .getActivityGraph();
202: if (graph != null) {
203: final FrontEndAction action = graph.getInitialAction();
204: if (action != null) {
205: actions.add(action);
206: }
207: }
208: return new ArrayList(actions);
209: }
210:
211: /**
212: * @see org.andromda.metafacades.uml.FrontEndUseCase#getInitialView()
213: */
214: protected Object handleGetInitialView() {
215: FrontEndView view = null;
216: final FrontEndActivityGraph graph = this .getActivityGraph();
217: final FrontEndAction action = graph != null ? this
218: .getActivityGraph().getInitialAction() : null;
219: final Collection forwards = action != null ? action
220: .getActionForwards() : null;
221: if (forwards != null) {
222: for (final Iterator iterator = forwards.iterator(); iterator
223: .hasNext();) {
224: final FrontEndForward forward = (FrontEndForward) iterator
225: .next();
226: final Object target = forward.getTarget();
227: if (target instanceof FrontEndView) {
228: view = (FrontEndView) target;
229: } else if (target instanceof FrontEndFinalState) {
230: final FrontEndFinalState finalState = (FrontEndFinalState) target;
231: final FrontEndUseCase targetUseCase = finalState
232: .getTargetUseCase();
233: if (targetUseCase != null
234: && !targetUseCase.equals(this .THIS())) {
235: view = targetUseCase.getInitialView();
236: }
237: }
238: }
239: }
240: return view;
241: }
242:
243: /**
244: * @see org.andromda.metafacades.uml.FrontEndUseCase#getViewVariables()
245: */
246: protected List handleGetViewVariables() {
247: final Map pageVariableMap = new LinkedHashMap();
248:
249: // - page variables can occur twice or more in the usecase if their
250: // names are the same for different forms, storing them in a map
251: // solves this issue because those names do not have the action-name prefix
252: final Collection views = this .getViews();
253: for (final Iterator pageIterator = views.iterator(); pageIterator
254: .hasNext();) {
255: final FrontEndView view = (FrontEndView) pageIterator
256: .next();
257: final Collection variables = view.getVariables();
258: for (final Iterator variableIterator = variables.iterator(); variableIterator
259: .hasNext();) {
260: FrontEndParameter variable = (FrontEndParameter) variableIterator
261: .next();
262: final String name = variable.getName();
263: if (name != null && name.trim().length() > 0) {
264: final FrontEndParameter existingVariable = (FrontEndParameter) pageVariableMap
265: .get(name);
266: if (existingVariable != null) {
267: if (existingVariable.isTable()) {
268: variable = existingVariable;
269: }
270: }
271: pageVariableMap.put(name, variable);
272: }
273: }
274: }
275: return new ArrayList(pageVariableMap.values());
276: }
277: }
|