1 package org.andromda.cartridges.jsf2.metafacades;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.LinkedHashMap;
7 import java.util.LinkedHashSet;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11 import java.util.TreeMap;
12 import org.andromda.cartridges.jsf2.JSFGlobals;
13 import org.andromda.cartridges.jsf2.JSFProfile;
14 import org.andromda.cartridges.jsf2.JSFUtils;
15 import org.andromda.metafacades.uml.AssociationEndFacade;
16 import org.andromda.metafacades.uml.AttributeFacade;
17 import org.andromda.metafacades.uml.ClassifierFacade;
18 import org.andromda.metafacades.uml.DependencyFacade;
19 import org.andromda.metafacades.uml.FrontEndAction;
20 import org.andromda.metafacades.uml.FrontEndActivityGraph;
21 import org.andromda.metafacades.uml.FrontEndFinalState;
22 import org.andromda.metafacades.uml.FrontEndForward;
23 import org.andromda.metafacades.uml.FrontEndParameter;
24 import org.andromda.metafacades.uml.FrontEndUseCase;
25 import org.andromda.metafacades.uml.FrontEndView;
26 import org.andromda.metafacades.uml.IncludeFacade;
27 import org.andromda.metafacades.uml.ModelElementFacade;
28 import org.andromda.metafacades.uml.Role;
29 import org.andromda.metafacades.uml.UseCaseFacade;
30 import org.andromda.utils.StringUtilsHelper;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.apache.commons.collections.Predicate;
33 import org.apache.commons.collections.Transformer;
34 import org.apache.commons.lang.ObjectUtils;
35 import org.apache.commons.lang.StringUtils;
36
37
38
39
40
41
42 public class JSFUseCaseLogicImpl
43 extends JSFUseCaseLogic
44 {
45 private static final long serialVersionUID = 34L;
46
47
48
49
50 public JSFUseCaseLogicImpl(
51 Object metaObject,
52 String context)
53 {
54 super(metaObject, context);
55 }
56
57
58
59
60
61 protected String handleGetPath()
62 {
63 String actionPath = null;
64 final FrontEndActivityGraph graph = this.getActivityGraph();
65 if (graph != null)
66 {
67 final JSFAction action = (JSFAction)graph.getInitialAction();
68 if (action != null)
69 {
70 actionPath = action.getPath();
71 }
72 }
73 return actionPath;
74 }
75
76
77
78
79
80 protected String handleGetPathRoot()
81 {
82 final StringBuilder pathRoot = new StringBuilder("/");
83 final String packagePath = this.getPackagePath();
84 final String prefix = packagePath != null ? packagePath.trim() : "";
85 pathRoot.append(prefix);
86 return pathRoot.toString();
87 }
88
89
90
91
92
93 protected String handleGetForwardName()
94 {
95 return JSFUtils.toWebResourceName(this.getName()) + JSFGlobals.USECASE_FORWARD_NAME_SUFFIX;
96 }
97
98
99
100
101
102 protected String handleGetTitleKey()
103 {
104 return StringUtilsHelper.toResourceMessageKey(
105 this.isNormalizeMessages() ? this.getTitleValue() : this.getName()) + '.' +
106 JSFGlobals.TITLE_MESSAGE_KEY_SUFFIX;
107 }
108
109
110
111
112
113 protected String handleGetTitleValue()
114 {
115 return StringUtilsHelper.toPhrase(getName());
116 }
117
118
119
120
121
122
123 private boolean isNormalizeMessages()
124 {
125 final String normalizeMessages = (String)getConfiguredProperty(JSFGlobals.NORMALIZE_MESSAGES);
126 return Boolean.valueOf(normalizeMessages).booleanValue();
127 }
128
129
130
131
132
133 protected Map handleGetAllMessages()
134 {
135 final boolean normalize = this.isNormalizeMessages();
136 final Map<String, String> messages = normalize ?
137 new TreeMap<String, String>() : new LinkedHashMap<String, String>();
138
139
140
141 if (this.isEntryUseCase())
142 {
143 final List<FrontEndUseCase> useCases = this.getAllUseCases();
144 for (int ctr = 0; ctr < useCases.size(); ctr++)
145 {
146
147 final JSFUseCase useCase = (JSFUseCase)useCases.get(ctr);
148 messages.put(
149 useCase.getTitleKey(),
150 useCase.getTitleValue());
151
152 final List<FrontEndView> views = useCase.getViews();
153 for (int ctr2 = 0; ctr2 < views.size(); ctr2++)
154 {
155
156 final JSFView view = (JSFView)views.get(ctr2);
157 messages.put(
158 view.getTitleKey(),
159 view.getTitleValue());
160 messages.put(
161 view.getMessageKey(),
162 view.getMessageValue());
163 messages.put(
164 view.getDocumentationKey(),
165 view.getDocumentationValue());
166
167 final List<FrontEndParameter> viewVariables = view.getVariables();
168 for (int ctr3 = 0; ctr3 < viewVariables.size(); ctr3++)
169 {
170
171 final Object object = viewVariables.get(ctr3);
172 if (object instanceof JSFParameter)
173 {
174 final JSFParameter parameter = (JSFParameter)object;
175
176 final Collection<ClassifierFacade> resolvingTypes = new ArrayList<ClassifierFacade>();
177 this.collectAttributeMessages(messages, parameter.getAttributes(), resolvingTypes);
178 this.collectAssociationEndMessages(messages,
179 parameter.getNavigableAssociationEnds(), resolvingTypes);
180 messages.put(
181 parameter.getMessageKey(),
182 parameter.getMessageValue());
183
184
185 if (parameter.isTable())
186 {
187 for (String columnName : parameter.getTableColumnNames())
188 {
189 messages.put(
190 parameter.getTableColumnMessageKey(columnName),
191 parameter.getTableColumnMessageValue(columnName));
192 }
193 }
194 }
195 }
196
197 final List<FrontEndAction> actions = useCase.getActions();
198 for (int ctr3 = 0; ctr3 < actions.size(); ctr3++)
199 {
200
201 final JSFAction action = (JSFAction)actions.get(ctr3);
202
203
204 final Object trigger = action.getTrigger();
205 if (trigger != null && trigger instanceof JSFEvent)
206 {
207 final JSFEvent event = (JSFEvent)trigger;
208
209 messages.put(
210 action.getDocumentationKey(),
211 action.getDocumentationValue());
212
213
214 messages.put(
215 event.getResetMessageKey(),
216 event.getResetMessageValue());
217
218
219 messages.put(
220 event.getMessageKey(),
221 event.getMessageValue());
222
223
224
225
226
227
228
229
230
231 }
232
233
234 for (final FrontEndForward forward : action.getTransitions())
235 {
236 if (forward instanceof JSFForward)
237 {
238 final JSFForward forwardTransition = (JSFForward)forward;
239 messages.putAll(forwardTransition.getSuccessMessages());
240 messages.putAll(forwardTransition.getWarningMessages());
241 }
242 else
243 {
244 final JSFAction actionTransition = (JSFAction)forward;
245 messages.putAll(actionTransition.getSuccessMessages());
246 messages.putAll(actionTransition.getWarningMessages());
247 }
248
249 }
250
251
252 final List<FrontEndParameter> parameters = action.getParameters();
253 for (int l = 0; l < parameters.size(); l++)
254 {
255 final Object object = parameters.get(l);
256 if (object instanceof JSFParameter)
257 {
258 final JSFParameter parameter = (JSFParameter)object;
259 final Collection attributes = parameter.getAttributes();
260 if (!attributes.isEmpty())
261 {
262 for (final Iterator iterator = attributes.iterator(); iterator.hasNext();)
263 {
264 final JSFAttribute attribute = (JSFAttribute)iterator.next();
265 messages.put(
266 attribute.getMessageKey(),
267 attribute.getMessageValue());
268 }
269 }
270 final Collection associationEnds = parameter.getNavigableAssociationEnds();
271 if (!associationEnds.isEmpty())
272 {
273 for (final Iterator iterator = associationEnds.iterator(); iterator.hasNext();)
274 {
275 final AssociationEndFacade end = (AssociationEndFacade)iterator.next();
276 final ClassifierFacade type = end.getType();
277 if (type != null)
278 {
279 final Collection<AttributeFacade> typeAttributes = type.getAttributes();
280 if (!attributes.isEmpty())
281 {
282 for (final Iterator<AttributeFacade> attributeIterator
283 = typeAttributes.iterator(); attributeIterator.hasNext(); )
284 {
285 final JSFAttribute attribute = (JSFAttribute)attributeIterator.next();
286 messages.put(
287 attribute.getMessageKey(),
288 attribute.getMessageValue());
289 }
290 }
291 }
292 }
293 }
294 messages.put(
295 parameter.getMessageKey(),
296 parameter.getMessageValue());
297 messages.put(
298 parameter.getDocumentationKey(),
299 parameter.getDocumentationValue());
300
301
302 if (parameter.isInputTable())
303 {
304 final Collection<String> columnNames = parameter.getTableColumnNames();
305 for (final Iterator<String> columnNameIterator = columnNames.iterator();
306 columnNameIterator.hasNext();)
307 {
308 final String columnName = columnNameIterator.next();
309 messages.put(
310 parameter.getTableColumnMessageKey(columnName),
311 parameter.getTableColumnMessageValue(columnName));
312 }
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340 }
341 }
342
343
344 final JSFPortletPreferences preferences = useCase.getPreferences();
345 if (preferences != null)
346 {
347 final Collection<AttributeFacade> attributes = preferences.getAttributes(true);
348 if (!attributes.isEmpty())
349 {
350 for (final Iterator iterator = attributes.iterator(); iterator.hasNext();)
351 {
352 final JSFAttribute attribute = (JSFAttribute)iterator.next();
353 messages.put(
354 attribute.getMessageKey(),
355 attribute.getMessageValue());
356 }
357 }
358 }
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 }
407 }
408 }
409 }
410 return messages;
411 }
412
413
414
415
416
417
418
419
420 private void collectAttributeMessages(Map<String,String> messages, Collection attributes,
421 final Collection<ClassifierFacade> resolvingTypes)
422 {
423 if (attributes != null && !attributes.isEmpty())
424 {
425 for (final Iterator iterator = attributes.iterator(); iterator.hasNext(); )
426 {
427 final JSFAttribute attribute = (JSFAttribute)iterator.next();
428 messages.put(
429 attribute.getMessageKey(),
430 attribute.getMessageValue());
431
432 this.collectTypeMessages(messages, attribute.getType(), resolvingTypes);
433 }
434 }
435 }
436
437
438
439
440
441
442
443
444 private void collectAssociationEndMessages(Map<String,String> messages, Collection associationEnds,
445 final Collection<ClassifierFacade> resolvingTypes)
446 {
447 if (associationEnds != null && !associationEnds.isEmpty())
448 {
449 for (final Iterator iterator = associationEnds.iterator(); iterator.hasNext();)
450 {
451 final AssociationEndFacade end = (AssociationEndFacade)iterator.next();
452 this.collectTypeMessages(messages, end.getType(), resolvingTypes);
453 }
454 }
455 }
456
457 private void collectTypeMessages(Map<String,String> messages, ClassifierFacade type,
458 final Collection<ClassifierFacade> resolvingTypes)
459 {
460 if (type != null)
461 {
462 if (!resolvingTypes.contains(type))
463 {
464 resolvingTypes.add(type);
465 if (type.isArrayType())
466 {
467 type = type.getNonArray();
468 }
469
470 if (!resolvingTypes.contains(type))
471 {
472 this.collectAttributeMessages(messages, type.getAttributes(), resolvingTypes);
473 this.collectAssociationEndMessages(messages, type.getNavigableConnectingEnds(), resolvingTypes);
474 }
475 }
476 resolvingTypes.remove(type);
477 }
478 }
479
480
481
482
483
484 protected List<JSFAction> handleGetActionForwards()
485 {
486 final Set<JSFAction> actionForwards = new LinkedHashSet<JSFAction>();
487 for (final FrontEndView view : this.getViews())
488 {
489 actionForwards.addAll(((JSFView)view).getActionForwards());
490 }
491 return new ArrayList<JSFAction>(actionForwards);
492 }
493
494
495
496
497
498 protected List<JSFForward> handleGetForwards()
499 {
500 final Map<String, JSFForward> forwards = new LinkedHashMap<String, JSFForward>();
501 for (final FrontEndAction action : this.getActions())
502 {
503 for (final FrontEndForward forward : action.getActionForwards())
504 {
505 if (forward instanceof JSFForward)
506 {
507 forwards.put(forward.getName(), (JSFForward) forward);
508 }
509 }
510 }
511 return new ArrayList(forwards.values());
512 }
513
514
515
516
517
518 @SuppressWarnings("unchecked")
519 protected List<ModelElementFacade> handleGetAllForwards()
520 {
521 final Map<String, ModelElementFacade> forwards = new LinkedHashMap<String, ModelElementFacade>();
522 for (final JSFAction forward : this.getActionForwards())
523 {
524 forwards.put(forward.getName(), forward);
525 }
526 for (final JSFForward forward : this.getForwards())
527 {
528 forwards.put(forward.getName(), forward);
529 }
530 return new ArrayList<ModelElementFacade>(forwards.values());
531 }
532
533
534
535
536
537 protected String handleGetActionClassName()
538 {
539 return StringUtilsHelper.upperCamelCaseName(this.getName());
540 }
541
542
543
544
545
546 protected String handleGetFullyQualifiedActionClassPath()
547 {
548 return this.getFullyQualifiedActionClassName().replace(
549 '.',
550 '/') + ".java";
551 }
552
553
554
555
556
557 protected String handleGetControllerAction()
558 {
559 return StringUtilsHelper.lowerCamelCaseName(this.getName());
560 }
561
562
563
564
565
566 protected String handleGetFullyQualifiedActionClassName()
567 {
568 final StringBuilder path = new StringBuilder();
569 final String packageName = this.getPackageName();
570 if (StringUtils.isNotBlank(packageName))
571 {
572 path.append(packageName);
573 path.append('.');
574 }
575 path.append(this.getActionClassName());
576 return path.toString();
577 }
578
579
580
581
582
583 protected String handleGetFormKey()
584 {
585 final Object formKeyValue = this.findTaggedValue(JSFProfile.TAGGEDVALUE_ACTION_FORM_KEY);
586 return formKeyValue == null ? ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.ACTION_FORM_KEY))
587 : String.valueOf(formKeyValue);
588 }
589
590
591
592
593
594 protected String handleGetInitialTargetPath()
595 {
596 String path = null;
597 final Object target = this.getInitialTarget();
598 if (target instanceof JSFView)
599 {
600 path = ((JSFView)target).getPath();
601 }
602 else if (target instanceof JSFUseCase)
603 {
604 path = ((JSFUseCase)target).getPath();
605 }
606 return path;
607 }
608
609
610
611
612
613
614 private Object getInitialTarget()
615 {
616 Object initialTarget = null;
617 final FrontEndActivityGraph graph = this.getActivityGraph();
618 final FrontEndAction action = graph != null ? this.getActivityGraph().getInitialAction() : null;
619 final Collection<FrontEndForward> forwards = action != null ? action.getActionForwards() : null;
620 if (forwards != null && !forwards.isEmpty())
621 {
622 final Object target = forwards.iterator().next().getTarget();
623 if (target instanceof FrontEndView)
624 {
625 initialTarget = target;
626 }
627 else if (target instanceof FrontEndFinalState)
628 {
629 final FrontEndFinalState finalState = (FrontEndFinalState)target;
630 final FrontEndUseCase targetUseCase = finalState.getTargetUseCase();
631 if (targetUseCase != null && !targetUseCase.equals(this.THIS()))
632 {
633 initialTarget = targetUseCase;
634 }
635 }
636 }
637 return initialTarget;
638 }
639
640
641
642
643
644 protected boolean handleIsValidationRequired()
645 {
646 boolean required = false;
647 for (final FrontEndView feView : this.getViews())
648 {
649 final JSFView view = (JSFView)feView;
650 if (view.isValidationRequired())
651 {
652 required = true;
653 break;
654 }
655 }
656 return required;
657 }
658
659
660
661
662
663 protected boolean handleIsInitialTargetView()
664 {
665 return this.getInitialTarget() instanceof JSFView;
666 }
667
668
669
670
671
672 protected boolean handleIsApplicationValidationRequired()
673 {
674 boolean required = false;
675 for (final FrontEndUseCase feUseCase : this.getAllUseCases())
676 {
677 final JSFUseCase useCase = (JSFUseCase)feUseCase;
678 if (useCase.isValidationRequired())
679 {
680 required = true;
681 break;
682 }
683 }
684 return required;
685 }
686
687
688
689
690
691 protected boolean handleIsViewHasNameOfUseCase()
692 {
693 boolean sameName = false;
694 for (final FrontEndView view : this.getViews())
695 {
696 sameName = ((JSFView)view).isHasNameOfUseCase();
697 if (sameName)
698 {
699 break;
700 }
701 }
702 return sameName;
703 }
704
705
706
707
708
709 protected boolean handleIsRegistrationUseCase()
710 {
711 return this.hasStereotype(JSFProfile.STEREOTYPE_FRONT_END_REGISTRATION);
712 }
713
714
715
716
717
718 @SuppressWarnings("unchecked")
719 protected List<FrontEndUseCase> handleGetRegistrationUseCases()
720 {
721 final List<FrontEndUseCase> useCases = new ArrayList<FrontEndUseCase>(this.getAllUseCases());
722 for (final Iterator<FrontEndUseCase> iterator = useCases.iterator(); iterator.hasNext();)
723 {
724 final FrontEndUseCase useCase = iterator.next();
725 if (useCase instanceof JSFUseCase)
726 {
727 if (!((JSFUseCase)useCase).isRegistrationUseCase())
728 {
729 iterator.remove();
730 }
731 }
732 else
733 {
734 iterator.remove();
735 }
736 }
737 return useCases;
738 }
739
740
741
742
743 private static final String FORWARDS_CLASS_NAME_SUFFIX = "Forwards";
744
745
746
747
748
749 protected String handleGetForwardsClassName()
750 {
751 return StringUtilsHelper.upperCamelCaseName(this.getName()) + FORWARDS_CLASS_NAME_SUFFIX;
752 }
753
754
755
756
757
758 @SuppressWarnings("unchecked")
759 protected Collection<Object> handleGetNavigationRules()
760 {
761 final Map<String, Object> rules = new LinkedHashMap<String, Object>();
762 for (final FrontEndView feView : this.getViews())
763 {
764 final JSFView view = (JSFView)feView;
765 rules.put(view.getFromOutcome(), view);
766 for (final Iterator forwardIterator = view.getForwards().iterator(); forwardIterator.hasNext();)
767 {
768 final Object forward = forwardIterator.next();
769 String name;
770 if (forward instanceof JSFForward)
771 {
772 name = ((JSFForward)forward).getFromOutcome();
773 }
774 else
775 {
776 name = ((JSFAction)forward).getFromOutcome();
777 }
778 rules.put(name, forward);
779 }
780 }
781 return rules.values();
782 }
783
784
785
786
787
788 protected Collection<UseCaseFacade> handleGetNavigationChildren()
789 {
790 return CollectionUtils.collect(getIncludes(), new Transformer()
791 {
792 public Object transform(Object object)
793 {
794 final IncludeFacade include = (IncludeFacade)object;
795 return include.getAddition();
796 }
797 });
798 }
799
800 private static boolean isParent(final JSFUseCase useCase1, final JSFUseCase useCase2)
801 {
802 return CollectionUtils.exists(useCase2.getIncludes(), new Predicate()
803 {
804 public boolean evaluate(Object object)
805 {
806 final IncludeFacade include = (IncludeFacade)object;
807 return include.getAddition().equals(useCase1);
808 }
809 });
810 }
811
812
813
814
815
816 protected Collection<FrontEndUseCase> handleGetNavigationParents()
817 {
818 final JSFUseCase theUseCase = this;
819 return CollectionUtils.select(getAllUseCases(),new Predicate() {
820 @SuppressWarnings("synthetic-access")
821 public boolean evaluate(Object o)
822 {
823 final JSFUseCase useCase = (JSFUseCase)o;
824 if (theUseCase.equals(useCase))
825 {
826 return false;
827 }
828 return isParent(theUseCase, useCase);
829 }
830 });
831 }
832
833
834
835
836
837 protected String handleGetActionRoles()
838 {
839 final StringBuilder rolesBuffer = new StringBuilder();
840 boolean first = true;
841 for (final Role role : this.getRoles())
842 {
843 if (first)
844 {
845 first = false;
846 }
847 else
848 {
849 rolesBuffer.append(',');
850 }
851 rolesBuffer.append(role.getName());
852 }
853 return rolesBuffer.toString();
854 }
855
856
857
858
859 @Override
860 protected Object handleGetPreferences()
861 {
862 JSFPortletPreferences preferences = null;
863 final Collection<DependencyFacade> dependencies = this.getSourceDependencies();
864 if (dependencies != null && !dependencies.isEmpty())
865 {
866 for (final DependencyFacade dependency : dependencies)
867 {
868 final Object target = dependency.getTargetElement();
869 if (dependency.getTargetElement() instanceof JSFPortletPreferences)
870 {
871 preferences = (JSFPortletPreferences)target;
872 break;
873 }
874 }
875 }
876 return preferences;
877 }
878
879
880
881
882 @Override
883 protected String handleGetPortletEditForwardName()
884 {
885 return this.getWebResourceName() + "-portlet-edit";
886 }
887
888
889
890
891 @Override
892 protected String handleGetPortletEditPath()
893 {
894 return this.getPathRoot() + "/" + this.getPortletEditForwardName();
895 }
896
897
898
899
900 @Override
901 protected String handleGetPortletHelpForwardName()
902 {
903 return this.getWebResourceName() + "-portlet-help";
904 }
905
906
907
908
909 @Override
910 protected String handleGetPortletHelpPath()
911 {
912 return this.getPathRoot() + "/" + this.getPortletHelpForwardName();
913 }
914
915
916
917
918 @Override
919 protected String handleGetPortletViewForwardName()
920 {
921 return this.getWebResourceName() + "-portlet-view";
922 }
923
924 private String getWebResourceName()
925 {
926 return JSFUtils.toWebResourceName(this.getName());
927 }
928
929
930
931
932 @Override
933 protected String handleGetPortletViewPath()
934 {
935 return this.getPath();
936 }
937
938
939
940
941 @SuppressWarnings("unchecked")
942 @Override
943 protected Collection<FrontEndView> handleGetAllViews()
944 {
945 final Set<FrontEndView> allViews = new LinkedHashSet<FrontEndView>();
946 for (final FrontEndUseCase useCase : this.getAllUseCases())
947 {
948 allViews.addAll(useCase.getViews());
949 }
950 return allViews;
951 }
952 }