001: package org.andromda.cartridges.jsf.metafacades;
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: import java.util.TreeMap;
012:
013: import org.andromda.cartridges.jsf.JSFGlobals;
014: import org.andromda.cartridges.jsf.JSFProfile;
015: import org.andromda.cartridges.jsf.JSFUtils;
016: import org.andromda.metafacades.uml.AssociationEndFacade;
017: import org.andromda.metafacades.uml.ClassifierFacade;
018: import org.andromda.metafacades.uml.FrontEndAction;
019: import org.andromda.metafacades.uml.FrontEndActivityGraph;
020: import org.andromda.metafacades.uml.FrontEndFinalState;
021: import org.andromda.metafacades.uml.FrontEndForward;
022: import org.andromda.metafacades.uml.FrontEndUseCase;
023: import org.andromda.metafacades.uml.FrontEndView;
024: import org.andromda.metafacades.uml.ModelElementFacade;
025: import org.andromda.utils.StringUtilsHelper;
026: import org.apache.commons.lang.ObjectUtils;
027: import org.apache.commons.lang.StringUtils;
028:
029: /**
030: * MetafacadeLogic implementation for org.andromda.cartridges.jsf.metafacades.JSFUseCase.
031: *
032: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase
033: */
034: public class JSFUseCaseLogicImpl extends JSFUseCaseLogic {
035: public JSFUseCaseLogicImpl(Object metaObject, String context) {
036: super (metaObject, context);
037: }
038:
039: /**
040: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getPath()
041: */
042: protected java.lang.String handleGetPath() {
043: String actionPath = null;
044: final FrontEndActivityGraph graph = this .getActivityGraph();
045: if (graph != null) {
046: final JSFAction action = (JSFAction) graph
047: .getInitialAction();
048: if (action != null) {
049: actionPath = action.getPath();
050: }
051: }
052: return actionPath;
053: }
054:
055: /**
056: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getPathRoot()
057: */
058: protected String handleGetPathRoot() {
059: final StringBuffer pathRoot = new StringBuffer("/");
060: final String packagePath = this .getPackagePath();
061: final String prefix = packagePath != null ? packagePath.trim()
062: : "";
063: pathRoot.append(prefix);
064: return pathRoot.toString();
065: }
066:
067: /**
068: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getPathRoot()
069: */
070: protected String handleGetForwardName() {
071: return JSFUtils.toWebResourceName(this .getName())
072: + JSFGlobals.USECASE_FORWARD_NAME_SUFFIX;
073: }
074:
075: /**
076: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getTitleKey()
077: */
078: protected String handleGetTitleKey() {
079: return StringUtilsHelper.toResourceMessageKey(this
080: .isNormalizeMessages() ? this .getTitleValue() : this
081: .getName())
082: + '.' + JSFGlobals.TITLE_MESSAGE_KEY_SUFFIX;
083: }
084:
085: /**
086: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getTitleValue()
087: */
088: protected String handleGetTitleValue() {
089: return StringUtilsHelper.toPhrase(getName());
090: }
091:
092: /**
093: * Indicates whether or not we should normalize messages.
094: *
095: * @return true/false
096: */
097: private final boolean isNormalizeMessages() {
098: final String normalizeMessages = (String) getConfiguredProperty(JSFGlobals.NORMALIZE_MESSAGES);
099: return Boolean.valueOf(normalizeMessages).booleanValue();
100: }
101:
102: /**
103: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getAllMessages()
104: */
105: protected Map handleGetAllMessages() {
106:
107: final boolean normalize = this .isNormalizeMessages();
108: final Map messages = (normalize) ? (Map) new TreeMap()
109: : (Map) new LinkedHashMap();
110:
111: // - only retrieve the messages for the entry use case (i.e. the use case
112: // where the application begins)
113: if (this .isEntryUseCase()) {
114: final List useCases = this .getAllUseCases();
115: for (int ctr = 0; ctr < useCases.size(); ctr++) {
116: // - usecase
117: final JSFUseCase useCase = (JSFUseCase) useCases
118: .get(ctr);
119: messages.put(useCase.getTitleKey(), useCase
120: .getTitleValue());
121:
122: final List views = useCase.getViews();
123: for (int ctr2 = 0; ctr2 < views.size(); ctr2++) {
124: // - view
125: final JSFView view = (JSFView) views.get(ctr2);
126: messages.put(view.getTitleKey(), view
127: .getTitleValue());
128: messages.put(view.getMessageKey(), view
129: .getMessageValue());
130: messages.put(view.getDocumentationKey(), view
131: .getDocumentationValue());
132:
133: final List viewVariables = view.getVariables();
134: for (int ctr3 = 0; ctr3 < viewVariables.size(); ctr3++) {
135: // - page variables
136: final Object object = viewVariables.get(ctr3);
137: if (object instanceof JSFParameter) {
138: final JSFParameter parameter = (JSFParameter) object;
139:
140: final Collection attributes = parameter
141: .getAttributes();
142: if (!attributes.isEmpty()) {
143: for (final Iterator iterator = attributes
144: .iterator(); iterator.hasNext();) {
145: final JSFAttribute attribute = (JSFAttribute) iterator
146: .next();
147: messages.put(attribute
148: .getMessageKey(), attribute
149: .getMessageValue());
150: }
151: }
152: final Collection associationEnds = parameter
153: .getNavigableAssociationEnds();
154: if (!associationEnds.isEmpty()) {
155: for (final Iterator iterator = associationEnds
156: .iterator(); iterator.hasNext();) {
157: final AssociationEndFacade end = (AssociationEndFacade) iterator
158: .next();
159: final ClassifierFacade type = end
160: .getType();
161: if (type != null) {
162: final Collection typeAttributes = type
163: .getAttributes();
164: if (!attributes.isEmpty()) {
165: for (final Iterator attributeIterator = typeAttributes
166: .iterator(); attributeIterator
167: .hasNext();) {
168: final JSFAttribute attribute = (JSFAttribute) attributeIterator
169: .next();
170: messages
171: .put(
172: attribute
173: .getMessageKey(),
174: attribute
175: .getMessageValue());
176: }
177: }
178: }
179: }
180: }
181: messages.put(parameter.getMessageKey(),
182: parameter.getMessageValue());
183:
184: // - table
185: if (parameter.isTable()) {
186: final Collection columnNames = parameter
187: .getTableColumnNames();
188: for (final Iterator columnNameIterator = columnNames
189: .iterator(); columnNameIterator
190: .hasNext();) {
191: final String columnName = (String) columnNameIterator
192: .next();
193: messages
194: .put(
195: parameter
196: .getTableColumnMessageKey(columnName),
197: parameter
198: .getTableColumnMessageValue(columnName));
199: }
200: }
201: }
202: }
203:
204: final List actions = useCase.getActions();
205: for (int ctr3 = 0; ctr3 < actions.size(); ctr3++) {
206: // - action
207: final JSFAction action = (JSFAction) actions
208: .get(ctr3);
209:
210: // - event/trigger
211: final Object trigger = action.getTrigger();
212: if (trigger instanceof JSFEvent) {
213: final JSFEvent event = (JSFEvent) trigger;
214: if (event != null) {
215: // only add these when a trigger is present, otherwise it's no use having them
216: messages.put(action
217: .getDocumentationKey(), action
218: .getDocumentationValue());
219:
220: // the regular trigger messages
221: messages.put(
222: event.getResetMessageKey(),
223: event.getResetMessageValue());
224:
225: // this one is the same as doing: action.getMessageKey()
226: messages.put(event.getMessageKey(),
227: event.getMessageValue());
228:
229: // - IMAGE LINK
230:
231: /*if (action.isImageLink())
232: {
233: messages.put(
234: action.getImageMessageKey(),
235: action.getImagePath());
236: }*/
237: }
238: }
239:
240: // - forwards
241:
242: final List transitions = action
243: .getTransitions();
244: for (final Iterator iterator = transitions
245: .iterator(); iterator.hasNext();) {
246: final Object transition = iterator.next();
247: if (transition instanceof JSFForward) {
248: final JSFForward forwardTransition = (JSFForward) transition;
249: messages.putAll(forwardTransition
250: .getSuccessMessages());
251: messages.putAll(forwardTransition
252: .getWarningMessages());
253: } else {
254: final JSFAction actionTransition = (JSFAction) transition;
255: messages.putAll(actionTransition
256: .getSuccessMessages());
257: messages.putAll(actionTransition
258: .getWarningMessages());
259: }
260:
261: }
262:
263: // - action parameters
264: final List parameters = action.getParameters();
265: for (int l = 0; l < parameters.size(); l++) {
266: final Object object = parameters.get(l);
267: if (object instanceof JSFParameter) {
268: final JSFParameter parameter = (JSFParameter) object;
269: final Collection attributes = parameter
270: .getAttributes();
271: if (!attributes.isEmpty()) {
272: for (final Iterator iterator = attributes
273: .iterator(); iterator
274: .hasNext();) {
275: final JSFAttribute attribute = (JSFAttribute) iterator
276: .next();
277: messages
278: .put(
279: attribute
280: .getMessageKey(),
281: attribute
282: .getMessageValue());
283: }
284: }
285: final Collection associationEnds = parameter
286: .getNavigableAssociationEnds();
287: if (!associationEnds.isEmpty()) {
288: for (final Iterator iterator = associationEnds
289: .iterator(); iterator
290: .hasNext();) {
291: final AssociationEndFacade end = (AssociationEndFacade) iterator
292: .next();
293: final ClassifierFacade type = end
294: .getType();
295: if (type != null) {
296: final Collection typeAttributes = type
297: .getAttributes();
298: if (!attributes.isEmpty()) {
299: for (final Iterator attributeIterator = typeAttributes
300: .iterator(); attributeIterator
301: .hasNext();) {
302: final JSFAttribute attribute = (JSFAttribute) attributeIterator
303: .next();
304: messages
305: .put(
306: attribute
307: .getMessageKey(),
308: attribute
309: .getMessageValue());
310: }
311: }
312: }
313: }
314: }
315: messages.put(parameter.getMessageKey(),
316: parameter.getMessageValue());
317: messages
318: .put(
319: parameter
320: .getDocumentationKey(),
321: parameter
322: .getDocumentationValue());
323:
324: // - submittable input table
325: if (parameter.isInputTable()) {
326: final Collection columnNames = parameter
327: .getTableColumnNames();
328: for (final Iterator columnNameIterator = columnNames
329: .iterator(); columnNameIterator
330: .hasNext();) {
331: final String columnName = (String) columnNameIterator
332: .next();
333: messages
334: .put(
335: parameter
336: .getTableColumnMessageKey(columnName),
337: parameter
338: .getTableColumnMessageValue(columnName));
339: }
340: }
341: /*if (parameter.getValidWhen() != null)
342: {
343: // this key needs to be fully qualified since the valid when value can be different
344: final String completeKeyPrefix =
345: (normalize)
346: ? useCase.getTitleKey() + '.' + view.getMessageKey() + '.' +
347: action.getMessageKey() + '.' + parameter.getMessageKey() : parameter.getMessageKey();
348: messages.put(
349: completeKeyPrefix + "_validwhen",
350: "{0} is only valid when " + parameter.getValidWhen());
351: }*/
352: /*if (parameter.getOptionCount() > 0)
353: {
354: final List optionKeys = parameter.getOptionKeys();
355: final List optionValues = parameter.getOptionValues();
356:
357: for (int m = 0; m < optionKeys.size(); m++)
358: {
359: messages.put(
360: optionKeys.get(m),
361: optionValues.get(m));
362: messages.put(
363: optionKeys.get(m) + ".title",
364: optionValues.get(m));
365: }
366: }*/
367: }
368: }
369:
370: // - exception forwards
371:
372: /*
373: final List exceptions = action.getActionExceptions();
374:
375: if (normalize)
376: {
377: if (exceptions.isEmpty())
378: {
379: messages.put("exception.occurred", "{0}");
380: }
381: else
382: {
383: for (int l = 0; l < exceptions.size(); l++)
384: {
385: final FrontEndExceptionHandler exception =
386: (FrontEndExceptionHandler)exceptions.get(l);
387: messages.put(action.getMessageKey() + '.' + exception.getExceptionKey(), "{0}");
388: }
389: }
390: }
391: else
392: {
393: if (exceptions.isEmpty())
394: {
395: if (!action.isUseCaseStart())
396: {
397: messages.put(action.getMessageKey() + ".exception", "{0} (java.lang.Exception)");
398: }
399: }
400: else
401: {
402: for (int l = 0; l < exceptions.size(); l++)
403: {
404: final FrontEndExceptionHandler exception =
405: (FrontEndExceptionHandler)exceptions.get(l);
406:
407: // we construct the key using the action message too because the exception can
408: // belong to more than one action (therefore it cannot return the correct value
409: // in .getExceptionKey())
410: messages.put(
411: action.getMessageKey() + '.' + exception.getExceptionKey(),
412: "{0} (" + exception.getExceptionType() + ")");
413: }
414: }
415: }*/
416: }
417: }
418: }
419: }
420: return messages;
421: }
422:
423: /**
424: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getActionForwards()
425: */
426: protected List handleGetActionForwards() {
427: final Set actionForwards = new LinkedHashSet();
428: final List views = this .getViews();
429: for (final Iterator iterator = views.iterator(); iterator
430: .hasNext();) {
431: final JSFView view = (JSFView) iterator.next();
432: actionForwards.addAll(view.getActionForwards());
433: }
434: return new ArrayList(actionForwards);
435: }
436:
437: /**
438: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getForwards()
439: */
440: protected List handleGetForwards() {
441: final Map forwards = new LinkedHashMap();
442: for (final Iterator iterator = this .getActions().iterator(); iterator
443: .hasNext();) {
444: final FrontEndAction action = (FrontEndAction) iterator
445: .next();
446: for (final Iterator forwardIterator = action
447: .getActionForwards().iterator(); forwardIterator
448: .hasNext();) {
449: final Object forward = forwardIterator.next();
450: if (forward instanceof JSFForward) {
451: forwards.put(((ModelElementFacade) forward)
452: .getName(), forward);
453: }
454: }
455: }
456: return new ArrayList(forwards.values());
457: }
458:
459: /**
460: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getAllForwards()
461: */
462: protected List handleGetAllForwards() {
463: final Map forwards = new LinkedHashMap();
464: for (final Iterator iterator = this .getActionForwards()
465: .iterator(); iterator.hasNext();) {
466: final ModelElementFacade forward = (ModelElementFacade) iterator
467: .next();
468: forwards.put(forward.getName(), forward);
469: }
470: for (final Iterator iterator = this .getForwards().iterator(); iterator
471: .hasNext();) {
472: final ModelElementFacade forward = (ModelElementFacade) iterator
473: .next();
474: forwards.put(forward.getName(), forward);
475: }
476: return new ArrayList(forwards.values());
477: }
478:
479: /**
480: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getActionClassName()
481: */
482: protected String handleGetActionClassName() {
483: return StringUtilsHelper.upperCamelCaseName(this .getName());
484: }
485:
486: /**
487: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getFullyQualifiedActionClassPath()
488: */
489: protected String handleGetFullyQualifiedActionClassPath() {
490: return this .getFullyQualifiedActionClassName()
491: .replace('.', '/')
492: + ".java";
493: }
494:
495: /**
496: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getControllerAction()
497: */
498: protected String handleGetControllerAction() {
499: return StringUtilsHelper.lowerCamelCaseName(this .getName());
500: }
501:
502: /**
503: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getFullyQualifiedActionClassName()
504: */
505: protected String handleGetFullyQualifiedActionClassName() {
506: final StringBuffer path = new StringBuffer();
507: final String packageName = this .getPackageName();
508: if (StringUtils.isNotBlank(packageName)) {
509: path.append(packageName);
510: path.append('.');
511: }
512: path.append(this .getActionClassName());
513: return path.toString();
514: }
515:
516: /**
517: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getFormKey()
518: */
519: protected String handleGetFormKey() {
520: final Object formKeyValue = this
521: .findTaggedValue(JSFProfile.TAGGEDVALUE_ACTION_FORM_KEY);
522: return formKeyValue == null ? ObjectUtils.toString(this
523: .getConfiguredProperty(JSFGlobals.ACTION_FORM_KEY))
524: : String.valueOf(formKeyValue);
525: }
526:
527: /**
528: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getInitialTargetPath()
529: */
530: protected String handleGetInitialTargetPath() {
531: String path = null;
532: final Object target = this .getInitialTarget();
533: if (target instanceof JSFView) {
534: path = ((JSFView) target).getPath();
535: } else if (target instanceof JSFUseCase) {
536: path = ((JSFUseCase) target).getPath();
537: }
538: return path;
539: }
540:
541: /**
542: * Gets the initial target when this use case is entered.
543: *
544: * @return the initial target.
545: */
546: private final Object getInitialTarget() {
547: Object initialTarget = null;
548: final FrontEndActivityGraph graph = this .getActivityGraph();
549: final FrontEndAction action = graph != null ? this
550: .getActivityGraph().getInitialAction() : null;
551: final Collection forwards = action != null ? action
552: .getActionForwards() : null;
553: if (forwards != null && !forwards.isEmpty()) {
554: final FrontEndForward forward = (FrontEndForward) forwards
555: .iterator().next();
556: final Object target = forward.getTarget();
557: if (target instanceof FrontEndView) {
558: initialTarget = target;
559: } else if (target instanceof FrontEndFinalState) {
560: final FrontEndFinalState finalState = (FrontEndFinalState) target;
561: final FrontEndUseCase targetUseCase = finalState
562: .getTargetUseCase();
563: if (targetUseCase != null
564: && !targetUseCase.equals(this .THIS())) {
565: initialTarget = targetUseCase;
566: }
567: }
568: }
569: return initialTarget;
570: }
571:
572: /**
573: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#isValidationRequired()
574: */
575: protected boolean handleIsValidationRequired() {
576: boolean required = false;
577: final Collection views = this .getViews();
578: for (final Iterator iterator = views.iterator(); iterator
579: .hasNext();) {
580: final JSFView view = (JSFView) iterator.next();
581: if (view.isValidationRequired()) {
582: required = true;
583: break;
584: }
585: }
586: return required;
587: }
588:
589: /**
590: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#isInitialTargetView()
591: */
592: protected boolean handleIsInitialTargetView() {
593: return this .getInitialTarget() instanceof JSFView;
594: }
595:
596: /**
597: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#isInitialTargetView()
598: */
599: protected boolean handleIsApplicationValidationRequired() {
600: boolean required = false;
601: final Collection useCases = this .getAllUseCases();
602: for (final Iterator iterator = useCases.iterator(); iterator
603: .hasNext();) {
604: final JSFUseCase useCase = (JSFUseCase) iterator.next();
605: if (useCase.isValidationRequired()) {
606: required = true;
607: break;
608: }
609: }
610: return required;
611: }
612:
613: /**
614: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#isViewHasNameOfUseCase()
615: */
616: protected boolean handleIsViewHasNameOfUseCase() {
617: boolean sameName = false;
618: for (final Iterator iterator = this .getViews().iterator(); iterator
619: .hasNext();) {
620: final JSFView view = (JSFView) iterator.next();
621: sameName = view.isHasNameOfUseCase();
622: if (sameName) {
623: break;
624: }
625: }
626: return sameName;
627: }
628:
629: /**
630: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#isRegistrationUseCase()
631: */
632: protected boolean handleIsRegistrationUseCase() {
633: return this
634: .hasStereotype(JSFProfile.STEREOTYPE_FRONT_END_REGISTRATION);
635: }
636:
637: /**
638: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getRegistrationUseCases()
639: */
640: protected List handleGetRegistrationUseCases() {
641: final List useCases = new ArrayList(this .getAllUseCases());
642: for (final Iterator iterator = useCases.iterator(); iterator
643: .hasNext();) {
644: final Object useCase = iterator.next();
645: if (useCase instanceof JSFUseCase) {
646: if (!((JSFUseCase) useCase).isRegistrationUseCase()) {
647: iterator.remove();
648: }
649: } else {
650: iterator.remove();
651: }
652: }
653: return useCases;
654: }
655:
656: /**
657: * The suffix for the forwards class name.
658: */
659: private static final String FORWARDS_CLASS_NAME_SUFFIX = "Forwards";
660:
661: /**
662: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getForwardsClassName()
663: */
664: protected String handleGetForwardsClassName() {
665: return StringUtilsHelper.upperCamelCaseName(this .getName())
666: + FORWARDS_CLASS_NAME_SUFFIX;
667: }
668:
669: /**
670: * @see org.andromda.cartridges.jsf.metafacades.JSFUseCase#getNavigationRules()
671: */
672: protected Collection handleGetNavigationRules() {
673: final Map rules = new LinkedHashMap();
674: final Collection views = this .getViews();
675: for (final Iterator iterator = views.iterator(); iterator
676: .hasNext();) {
677: final JSFView view = (JSFView) iterator.next();
678: rules.put(view.getFromOutcome(), view);
679: for (final Iterator forwardIterator = view.getForwards()
680: .iterator(); forwardIterator.hasNext();) {
681: final Object forward = forwardIterator.next();
682: String name;
683: if (forward instanceof JSFForward) {
684: name = ((JSFForward) forward).getFromOutcome();
685: } else {
686: name = ((JSFAction) forward).getFromOutcome();
687: }
688: rules.put(name, forward);
689: }
690: }
691: return rules.values();
692: }
693: }
|