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 javax.faces.component.UICommand;
22 import javax.faces.context.FacesContext;
23 import javax.faces.el.ValueBinding;
24 import javax.faces.el.VariableResolver;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
30
31
32
33
34
35
36
37
38
39 public class UIStartConversation extends AbstractConversationComponent
40 {
41 public final static String COMPONENT_TYPE = "org.apache.myfaces.StartConversation";
42 private final static String CONVERSATION_SYSTEM_SETUP = "org.apache.myfaces.ConversationSystemSetup";
43
44 private boolean inited;
45 private Boolean persistence;
46
47 public static class ConversationStartAction extends AbstractConversationActionListener
48 {
49 private boolean persistence;
50 private List beanToElevate;
51
52 public void doConversationAction(AbstractConversationComponent abstractConversationComponent)
53 {
54 ConversationManager conversationManager = ConversationManager.getInstance();
55 conversationManager.startConversation(getConversationName(), isPersistence());
56
57 if (beanToElevate != null)
58 {
59 FacesContext context = FacesContext.getCurrentInstance();
60
61 Iterator iterBeans = beanToElevate.iterator();
62 while (iterBeans.hasNext())
63 {
64 String vb = (String) iterBeans.next();
65 UIConversation.elevateBean(
66 context,
67 getConversationName(),
68 context.getApplication().createValueBinding(vb));
69 }
70 }
71 }
72
73 public boolean isPersistence()
74 {
75 return persistence;
76 }
77
78 public void setPersistence(boolean persistence)
79 {
80 this.persistence = persistence;
81 }
82
83 public void addBeanToElevate(String beanBinding)
84 {
85 if (beanToElevate == null)
86 {
87 beanToElevate = new ArrayList();
88 }
89 beanToElevate.add(beanBinding);
90 }
91
92
93 public Object saveState(FacesContext context)
94 {
95 return new Object[]
96 {
97 super.saveState(context),
98 Boolean.valueOf(persistence),
99 beanToElevate
100 };
101 }
102
103 public void restoreState(FacesContext context, Object state)
104 {
105 Object[] states = (Object[]) state;
106 super.restoreState(context, states[0]);
107 persistence = ((Boolean) states[1]).booleanValue();
108 beanToElevate = (List) states[2];
109 }
110 }
111
112 public void encodeEnd(FacesContext context) throws IOException
113 {
114 super.encodeEnd(context);
115
116 setupConversationSystem(context);
117
118 UICommand command = ConversationUtils.findParentCommand(this);
119 if (command != null)
120 {
121 if (!inited)
122 {
123 ConversationStartAction actionListener = new ConversationStartAction();
124 actionListener.setConversationName(getName());
125 actionListener.setPersistence(toBoolean(getPersistence()));
126 command.addActionListener(actionListener);
127
128 Iterator iterChildren = getChildren().iterator();
129 while (iterChildren.hasNext())
130 {
131 Object child = iterChildren.next();
132 if (child instanceof UIConversation)
133 {
134 UIConversation conversation = (UIConversation) child;
135 actionListener.addBeanToElevate(conversation.getBeanBinding().getExpressionString());
136 }
137 }
138
139 inited = true;
140 }
141 }
142 else
143 {
144 ConversationManager conversationManager = ConversationManager.getInstance();
145 conversationManager.startConversation(getName(), toBoolean(getPersistence()));
146 }
147 }
148
149 protected boolean toBoolean(Boolean bool)
150 {
151 if (bool != null)
152 {
153 return bool.booleanValue();
154 }
155 return false;
156 }
157
158 protected void setupConversationSystem(FacesContext context)
159 {
160 if (Boolean.TRUE.equals(context.getExternalContext().getApplicationMap().get(CONVERSATION_SYSTEM_SETUP)))
161 {
162 return;
163 }
164
165 VariableResolver originalVR = context.getApplication().getVariableResolver();
166 context.getApplication().setVariableResolver(new ConversationVariableResolver(originalVR));
167
168 context.getExternalContext().getApplicationMap().put(CONVERSATION_SYSTEM_SETUP, Boolean.TRUE);
169 }
170
171 public void restoreState(FacesContext context, Object state)
172 {
173 Object[] states = (Object[]) state;
174 super.restoreState(context, states[0]);
175 inited = ((Boolean) states[1]).booleanValue();
176 persistence = (Boolean) states[2];
177 }
178
179 public Object saveState(FacesContext context)
180 {
181 return new Object[]
182 {
183 super.saveState(context),
184 inited?Boolean.TRUE:Boolean.FALSE,
185 persistence
186 };
187 }
188
189
190
191
192
193
194
195 public Boolean getPersistence()
196 {
197 if (persistence != null)
198 {
199 return persistence;
200 }
201 ValueBinding vb = getValueBinding("persistence");
202 return vb != null ? (Boolean) vb.getValue(getFacesContext()) : null;
203 }
204
205 public void setPersistence(Boolean persistence)
206 {
207 this.persistence = persistence;
208 }
209 }