1 16 package org.apache.cocoon.forms.binding; 17 18 import java.util.Iterator ; 19 20 import org.apache.avalon.framework.logger.Logger; 21 import org.apache.cocoon.forms.formmodel.Repeater; 22 import org.apache.cocoon.forms.formmodel.Widget; 23 import org.apache.commons.jxpath.JXPathContext; 24 import org.apache.commons.jxpath.Pointer; 25 26 36 public class SimpleRepeaterJXPathBinding extends JXPathBindingBase { 37 38 private final String repeaterId; 39 private final String repeaterPath; 40 private final String rowPath; 41 private final boolean clearOnLoad; 42 private final JXPathBindingBase rowBinding; 43 private final boolean deleteIfEmpty; 44 45 public SimpleRepeaterJXPathBinding( 46 JXPathBindingBuilderBase.CommonAttributes commonAtts, 47 String repeaterId, String repeaterPath, String rowPath, 48 boolean clearOnLoad, boolean deleteIfEmpty, 49 JXPathBindingBase rowBinding) { 50 super(commonAtts); 51 this.repeaterId = repeaterId; 52 this.repeaterPath = repeaterPath; 53 this.rowPath = rowPath; 54 this.rowBinding = rowBinding; 55 this.rowBinding.setParent(this); 56 this.clearOnLoad = clearOnLoad; 57 this.deleteIfEmpty = deleteIfEmpty; 58 } 59 60 public String getId() { return repeaterId; } 61 public String getRepeaterPath() { return repeaterPath; } 62 public String getRowPath() { return rowPath; } 63 public boolean getClearOnLoad() { return clearOnLoad; } 64 public boolean getDeleteIfEmpty() { return deleteIfEmpty; } 65 public JXPathBindingBase[] getChildBindings() { return ((ComposedJXPathBindingBase)rowBinding).getChildBindings(); } 66 67 public void doLoad(Widget frmModel, JXPathContext jctx) 68 throws BindingException { 69 Repeater repeater = (Repeater) selectWidget(frmModel, this.repeaterId); 71 72 if (this.clearOnLoad) { 73 repeater.clear(); 74 } 75 76 Pointer ptr = jctx.getPointer(this.repeaterPath); 78 if (ptr.getNode() != null) { 79 81 JXPathContext repeaterContext = jctx.getRelativeContext(ptr); 82 Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath); 84 85 int rowNum = 0; 87 while (rowPointers.hasNext()) { 88 Repeater.RepeaterRow thisRow; 90 if (repeater.getSize() > rowNum) { 91 thisRow = repeater.getRow(rowNum); 92 } else { 93 thisRow = repeater.addRow(); 94 } 95 rowNum++; 96 97 Pointer jxp = (Pointer) rowPointers.next(); 99 JXPathContext rowContext = repeaterContext.getRelativeContext(jxp); 100 101 this.rowBinding.loadFormFromModel(thisRow, rowContext); 102 } 103 } 104 if (getLogger().isDebugEnabled()) { 105 getLogger().debug("done loading rows " + toString()); 106 } 107 } 108 109 public void doSave(Widget frmModel, JXPathContext jctx) 110 throws BindingException { 111 Repeater repeater = (Repeater)selectWidget(frmModel, this.repeaterId); 113 114 if (repeater.getSize() == 0 && this.deleteIfEmpty) { 115 jctx.removeAll(this.repeaterPath); 117 } else { 118 JXPathContext repeaterContext = 121 jctx.getRelativeContext(jctx.createPath(this.repeaterPath)); 122 123 repeaterContext.removeAll(this.rowPath); 125 126 for (int i = 0; i < repeater.getSize(); i++) { 127 Pointer rowPtr = repeaterContext.createPath( 128 this.rowPath + '[' + (i+1) + ']'); 129 JXPathContext rowContext = 130 repeaterContext.getRelativeContext(rowPtr); 131 this.rowBinding.saveFormToModel(repeater.getRow(i), 132 rowContext); 133 } 134 } 135 } 136 137 public String toString() { 138 return this.getClass().getName()+ " [widget=" + this.repeaterId + 139 ", xpath=" + this.repeaterPath + "]"; 140 } 141 142 public void enableLogging(Logger logger) { 143 super.enableLogging(logger); 144 this.rowBinding.enableLogging(logger); 145 } 146 } 147 | Popular Tags |