1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.orchestra.conversation.jsf;
21
22 import java.util.Iterator;
23
24 import javax.faces.component.UICommand;
25 import javax.faces.component.UIComponent;
26 import javax.faces.context.FacesContext;
27 import javax.faces.el.MethodBinding;
28
29 import org.apache.myfaces.orchestra.conversation.Conversation;
30 import org.apache.myfaces.orchestra.conversation.ConversationManager;
31 import org.apache.myfaces.orchestra.conversation.jsf.components.AbstractConversationComponent;
32 import org.apache.myfaces.orchestra.conversation.jsf.components.UIEndConversation;
33 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
34
35 /***
36 * <p>Various utilities used by the conversation framework</p>
37 * <p>Notice: this class is not meant to be used outside of this library</p>
38 */
39 public class _JsfConversationUtils
40 {
41 private _JsfConversationUtils()
42 {
43 }
44
45 /***
46 * Find the first parent which is a command
47 */
48 public static UICommand findParentCommand(UIComponent base)
49 {
50 UIComponent parent = base;
51 do
52 {
53 parent = parent.getParent();
54 if (parent instanceof UICommand)
55 {
56 return (UICommand) parent;
57 }
58 }
59 while (parent != null);
60
61 return null;
62 }
63
64 /***
65 * Find a child start or end conversation component for the given conversation name
66 */
67 public static AbstractConversationComponent findEndConversationComponent(UIComponent component, String conversationName)
68 {
69 Iterator iterComponents = component.getFacetsAndChildren();
70 while (iterComponents.hasNext())
71 {
72 Object child = iterComponents.next();
73 AbstractConversationComponent conversation;
74
75 if (child instanceof UIEndConversation)
76 {
77 conversation = (AbstractConversationComponent) child;
78 if (conversation.getName().equals(conversationName))
79 {
80 return conversation;
81 }
82 }
83 else if (child instanceof UIComponent)
84 {
85 conversation = findEndConversationComponent((UIComponent) child, conversationName);
86 if (conversation != null)
87 {
88 return conversation;
89 }
90 }
91 }
92
93 return null;
94 }
95
96 /***
97 * end and restart a conversation
98 */
99 public static void endAndRestartConversation(FacesContext context, String conversationName, Boolean restart, MethodBinding restartAction)
100 {
101 ConversationManager conversationManager = ConversationManager.getInstance();
102 Conversation conversation = conversationManager.getConversation(conversationName);
103 if (conversation != null)
104 {
105 conversation.invalidate();
106 }
107
108 if (restart != null && restart.booleanValue())
109 {
110 FrameworkAdapter.getCurrentInstance().getBean(conversationName);
111
112 if (restartAction != null)
113 {
114 restartAction.invoke(context, null);
115 }
116 }
117 }
118 }