1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.subform;
20
21 import java.util.Iterator;
22 import java.util.List;
23
24 import javax.faces.component.EditableValueHolder;
25 import javax.faces.component.NamingContainer;
26 import javax.faces.component.UIComponent;
27 import javax.faces.component.UIComponentBase;
28 import javax.faces.context.FacesContext;
29 import javax.faces.event.ActionEvent;
30 import javax.faces.event.FacesEvent;
31 import javax.faces.event.PhaseId;
32
33 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public abstract class AbstractSubForm extends UIComponentBase
73 implements NamingContainer
74 {
75
76 public static final String COMPONENT_TYPE = "org.apache.myfaces.SubForm";
77 public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.SubForm";
78 public static final String COMPONENT_FAMILY = "org.apache.myfaces.SubForm";
79
80 private static final String PARTIAL_ENABLED = "org.apache.myfaces.IsPartialPhaseExecutionEnabled";
81 private boolean _submitted;
82
83 public AbstractSubForm()
84 {
85 super.setRendererType(DEFAULT_RENDERER_TYPE);
86 }
87
88 public String getFamily()
89 {
90 return COMPONENT_FAMILY;
91 }
92
93 public boolean isSubmitted()
94 {
95 return _submitted;
96 }
97
98 public void setSubmitted(boolean submitted)
99 {
100 _submitted = submitted;
101 }
102
103
104
105
106
107
108
109
110 public abstract Boolean getPreserveSubmittedValues();
111
112
113 public void processDecodes(FacesContext context)
114 {
115 super.processDecodes(context);
116 if (!isRendered()) return;
117
118 if (!_submitted && Boolean.FALSE.equals(getPreserveSubmittedValues()))
119 {
120
121
122
123
124 resetSubmittedValues(this, context);
125 }
126 }
127
128 public void processValidators(FacesContext context)
129 {
130 if (context == null) throw new NullPointerException("context");
131 if (!isRendered()) return;
132
133 boolean partialEnabled = isPartialEnabled(context, PhaseId.PROCESS_VALIDATIONS);
134
135 if(partialEnabled || (_submitted && isEmptyList(context)))
136 {
137 for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
138 {
139 UIComponent childOrFacet = (UIComponent)it.next();
140 childOrFacet.processValidators(context);
141 }
142 }
143 else
144 {
145 processSubFormValidators(this,context);
146 }
147 }
148
149 public void processUpdates(FacesContext context)
150 {
151 if (context == null) throw new NullPointerException("context");
152 if (!isRendered()) return;
153
154 boolean partialEnabled = isPartialEnabled(context,PhaseId.UPDATE_MODEL_VALUES);
155
156 if(partialEnabled || _submitted)
157 {
158 for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
159 {
160 UIComponent childOrFacet = (UIComponent)it.next();
161 childOrFacet.processUpdates(context);
162 }
163 }
164 else
165 {
166 processSubFormUpdates(this,context);
167 }
168 }
169
170 private static void resetSubmittedValues(UIComponent comp, FacesContext context)
171 {
172 for (Iterator it = comp.getFacetsAndChildren(); it.hasNext(); )
173 {
174 UIComponent childOrFacet = (UIComponent)it.next();
175 if (childOrFacet instanceof AbstractSubForm)
176 {
177
178 continue;
179 }
180
181 if (childOrFacet instanceof EditableValueHolder)
182 {
183 ((EditableValueHolder) childOrFacet).setSubmittedValue(null);
184 }
185
186 resetSubmittedValues(childOrFacet, context);
187 }
188 }
189
190 private static void processSubFormUpdates(UIComponent comp, FacesContext context)
191 {
192 for (Iterator it = comp.getFacetsAndChildren(); it.hasNext(); )
193 {
194 UIComponent childOrFacet = (UIComponent)it.next();
195
196 if(childOrFacet instanceof AbstractSubForm)
197 {
198 childOrFacet.processUpdates(context);
199 }
200 else
201 {
202 processSubFormUpdates(childOrFacet, context);
203 }
204 }
205 }
206
207 private static void processSubFormValidators(UIComponent comp, FacesContext context)
208 {
209 for (Iterator it = comp.getFacetsAndChildren(); it.hasNext(); )
210 {
211 UIComponent childOrFacet = (UIComponent)it.next();
212
213 if(childOrFacet instanceof AbstractSubForm)
214 {
215 childOrFacet.processValidators(context);
216 }
217 else
218 {
219 processSubFormValidators(childOrFacet, context);
220 }
221 }
222 }
223
224 public void queueEvent(FacesEvent event)
225 {
226 if(event instanceof ActionEvent)
227 {
228 _submitted = true;
229 }
230
231
232
233
234
235
236
237
238
239 if (PhaseId.APPLY_REQUEST_VALUES.compareTo(event.getPhaseId()) < 0)
240 {
241 setSubmitted(true);
242 }
243
244 super.queueEvent(event);
245 }
246
247 protected boolean isEmptyList(FacesContext context)
248 {
249
250 List li = (List) context.getExternalContext().getRequestMap().get(
251 RendererUtils.ACTION_FOR_LIST);
252
253 return li==null || li.size()==0;
254 }
255
256
257
258
259
260
261
262 protected boolean isPartialEnabled(FacesContext context, PhaseId phaseId)
263 {
264
265
266
267
268
269
270
271 boolean partialEnabled = false;
272
273
274 List li = (List) context.getExternalContext().getRequestMap().get(
275 RendererUtils.ACTION_FOR_LIST);
276
277
278
279 if(li != null && li.contains(getClientId(context)))
280 {
281 if(!context.getExternalContext().getRequestMap().containsKey(PARTIAL_ENABLED))
282 {
283 partialEnabled=true;
284 }
285 }
286
287 if(partialEnabled)
288 {
289
290 List phaseList = (List) context.getExternalContext().getRequestMap().get(
291 RendererUtils.ACTION_FOR_PHASE_LIST);
292
293 if(phaseList != null && !phaseList.isEmpty() && !phaseList.contains(phaseId))
294 {
295 partialEnabled=false;
296 }
297 }
298
299 return partialEnabled;
300 }
301
302
303 }