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