/
EnhancedRepeaterJXPathBinding.java
106 lines (89 loc) · 4.42 KB
/
EnhancedRepeaterJXPathBinding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package org.apache.cocoon.forms.binding;
import java.util.Iterator;
import org.apache.avalon.framework.CascadingRuntimeException;
import org.apache.cocoon.forms.binding.JXPathBindingBuilderBase.CommonAttributes;
import org.apache.cocoon.forms.formmodel.EnhancedRepeater;
import org.apache.cocoon.forms.formmodel.Repeater;
import org.apache.cocoon.forms.formmodel.Widget;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
public class EnhancedRepeaterJXPathBinding extends RepeaterJXPathBinding {
private String adapterClass;
public EnhancedRepeaterJXPathBinding(CommonAttributes commonAtts,
String repeaterId,
String repeaterPath,
String rowPath, String rowPathForInsert,
JXPathBindingBase[] childBindings,
JXPathBindingBase insertBinding,
JXPathBindingBase[] deleteBindings,
JXPathBindingBase[] identityBindings,
String adapterClass) {
super(commonAtts, repeaterId, repeaterPath, rowPath, rowPathForInsert,
childBindings, insertBinding, deleteBindings, identityBindings);
this.adapterClass = adapterClass;
}
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
Repeater repeater = (Repeater) selectWidget(frmModel, super.getId());
if (!(repeater instanceof EnhancedRepeater)) {
super.doLoad(frmModel, jxpc);
return;
}
EnhancedRepeater rep = (EnhancedRepeater) repeater;
RepeaterAdapter adapter;
if (this.adapterClass != null) {
try {
adapter = (RepeaterAdapter) Class.forName(this.adapterClass).newInstance();
} catch (Exception e) {
throw new CascadingRuntimeException("Cannot instantiate adapter class for advanced repeater binding", e);
}
} else {
adapter = new RepeaterJXPathAdapter();
}
RepeaterJXPathCollection collection = new RepeaterJXPathCollection();
//Pointer ptr = jxpc.getPointer(super.getRepeaterPath());
//JXPathContext repeaterContext = jxpc.getRelativeContext(ptr);
collection.init(jxpc, super.getRowPath(), adapter);
adapter.setBinding(this);
adapter.setJXCollection(collection);
rep.setCollection(collection);
rep.doPageLoad();
}
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
Repeater repeater = (Repeater) selectWidget(frmModel, super.getId());
if (!(repeater instanceof EnhancedRepeater)) {
super.doSave(frmModel, jxpc);
return;
}
EnhancedRepeater rep = (EnhancedRepeater) repeater;
rep.doPageSave();
Pointer ptr = jxpc.getPointer(super.getRepeaterPath());
JXPathContext repeaterContext = jxpc.getRelativeContext(ptr);
RepeaterJXPathCollection collection = rep.getCollection();
// iterate updated rows. note: we don't iterate over the whole context
for (Iterator iter = collection.getUpdatedRows().iterator(); iter.hasNext();) {
RepeaterItem item = (RepeaterItem) iter.next();
getRowBinding().saveFormToModel(item.getRow(), item.getContext());
}
for (Iterator iter = collection.getDeletedRows().iterator(); iter.hasNext();) {
RepeaterItem item = (RepeaterItem) iter.next();
jxpc.removePath(item.getContext().createPath(".").asPath());
}
// insert rows
int indexCount = collection.getOriginalCollectionSize() - collection.getDeletedRows().size();
for (Iterator iter = collection.getInsertedRows().iterator(); iter.hasNext();) {
indexCount++;
RepeaterItem item= (RepeaterItem) iter.next();
// Perform the insert row binding.
if (getInsertRowBinding() != null) {
getInsertRowBinding().saveFormToModel(item.getRow(), repeaterContext);
}
// --> create the path to let the context be created
Pointer newRowContextPointer = repeaterContext.createPath(
super.getInsertRowPath() + "[" + indexCount + "]");
JXPathContext newRowContext =
repeaterContext.getRelativeContext(newRowContextPointer);
// + rebind to children for update
super.getRowBinding().saveFormToModel(item.getRow(), newRowContext);
}
}
}