1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.conversation;
20
21 import java.util.Iterator;
22
23 import javax.faces.component.UICommand;
24 import javax.faces.component.UIComponent;
25 import javax.faces.el.ValueBinding;
26 import javax.faces.el.MethodBinding;
27 import javax.faces.context.FacesContext;
28
29 public class ConversationUtils
30 {
31 private ConversationUtils()
32 {
33 }
34
35
36
37
38 public static UICommand findParentCommand(UIComponent base)
39 {
40 UIComponent parent = base;
41 do
42 {
43 parent = parent.getParent();
44 if (parent instanceof UICommand)
45 {
46 return (UICommand) parent;
47 }
48 }
49 while (parent != null);
50
51 return null;
52 }
53
54
55
56
57 public static AbstractConversationComponent findStartOrEndConversationComponent(UIComponent component, String conversationName)
58 {
59 Iterator iterComponents = component.getFacetsAndChildren();
60 while (iterComponents.hasNext())
61 {
62 Object child = iterComponents.next();
63 AbstractConversationComponent conversation;
64
65 if (child instanceof UIStartConversation || child instanceof UIEndConversation)
66 {
67 conversation = (AbstractConversationComponent) child;
68 if (conversation.getName().equals(conversationName))
69 {
70 return conversation;
71 }
72 }
73 else if (child instanceof UIComponent)
74 {
75 conversation = findStartOrEndConversationComponent((UIComponent) child, conversationName);
76 if (conversation != null)
77 {
78 return conversation;
79 }
80 }
81 }
82
83 return null;
84 }
85
86 public static String extractBeanName(ValueBinding vb)
87 {
88 String valueBinding = vb.getExpressionString();
89 return valueBinding.substring(2, valueBinding.length()-1);
90 }
91
92
93
94
95 static void endAndRestartConversation(FacesContext context, String conversationName, Boolean restart, MethodBinding restartAction)
96 {
97 ConversationManager conversationManager = ConversationManager.getInstance(context);
98 Conversation conversation = conversationManager.getConversation(conversationName);
99
100 conversationManager.endConversation(conversationName, true);
101
102 if (restart != null && restart.booleanValue() && conversation != null)
103 {
104 conversationManager.startConversation(conversationName, conversation.isPersistence());
105
106 if (restartAction != null)
107 {
108 restartAction.invoke(context, null);
109 }
110 }
111 }
112 }