antonio 2004/03/02 10:48:18
Modified: src/blocks/woody/java/org/apache/cocoon/woody/binding
RepeaterJXPathBindingBuilder.java
RepeaterJXPathBinding.java
src/blocks/woody/conf woody-binding.xconf
Added: src/blocks/woody/java/org/apache/cocoon/woody/binding
UniqueFieldJXPathBindingBuilder.java
UniqueFieldJXPathBinding.java
Log:
Support for multiple unique-row-id in repeater.
The "old style" is still supported, but deprecated.
Revision Changes Path
1.13 +22 -5
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/RepeaterJXPathBindingBuilder.java
Index: RepeaterJXPathBindingBuilder.java
===================================================================
RCS file:
/home/cvs//cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/RepeaterJXPathBindingBuilder.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- RepeaterJXPathBindingBuilder.java 29 Feb 2004 12:36:19 -0000 1.12
+++ RepeaterJXPathBindingBuilder.java 2 Mar 2004 18:48:18 -0000 1.13
@@ -112,9 +112,9 @@
String rowPathForInsert =
DomHelper.getAttribute(bindingElm, "row-path-insert",
rowPath);
String uniqueRowId =
- DomHelper.getAttribute(bindingElm, "unique-row-id");
+ DomHelper.getAttribute(bindingElm, "unique-row-id", null);
String uniqueRowIdPath =
- DomHelper.getAttribute(bindingElm, "unique-path");
+ DomHelper.getAttribute(bindingElm, "unique-path", null);
Convertor convertor = null;
Locale convertorLocale = Locale.US;
@@ -140,9 +140,9 @@
"RepeaterBinding misses '<on-bind>' child definition.
" +
DomHelper.getLocation(bindingElm));
}
-
JXPathBindingBase[] childBindings =
assistant.makeChildBindings(childWrapElement);
+
Element deleteWrapElement = DomHelper.getChildElement(bindingElm,
BindingManager.NAMESPACE, "on-delete-row");
JXPathBindingBase[] deleteBindings = null;
@@ -158,12 +158,29 @@
insertBinding =
assistant.makeChildBindings(insertWrapElement)[0];
}
+ /* New <wb:unique-row> child element builder */
+ Element uniqueFieldWrapElement =
DomHelper.getChildElement(bindingElm,
+ BindingManager.NAMESPACE, "unique-row");
+ JXPathBindingBase[] uniqueFieldBinding = null;
+ if (uniqueFieldWrapElement != null) {
+ uniqueFieldBinding =
assistant.makeChildBindings(uniqueFieldWrapElement);
+ } else if (uniqueRowId == null || uniqueRowIdPath == null) {
+ throw new BindingException(
+ "RepeaterBinding misses '<unique-row>' child
definition. " +
+ DomHelper.getLocation(bindingElm));
+ } else {
+ if (this.getLogger().isInfoEnabled()) {
+ this.getLogger().info("<wb:repeater>: The attributes
'unique-row-id' and " +
+ "'unique-path' are deprecated. Use <unique-row>
child element instead." +
+ " Located at " + DomHelper.getLocation(bindingElm));
+ }
+ }
RepeaterJXPathBinding repeaterBinding =
new RepeaterJXPathBinding(commonAtts, repeaterId, parentPath,
rowPath, rowPathForInsert, uniqueRowId,
uniqueRowIdPath, convertor, convertorLocale,
- childBindings, insertBinding, deleteBindings);
+ childBindings, insertBinding, deleteBindings,
uniqueFieldBinding);
return repeaterBinding;
} catch (BindingException e) {
throw e;
1.21 +133 -49
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/RepeaterJXPathBinding.java
Index: RepeaterJXPathBinding.java
===================================================================
RCS file:
/home/cvs//cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/RepeaterJXPathBinding.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- RepeaterJXPathBinding.java 29 Feb 2004 12:36:19 -0000 1.20
+++ RepeaterJXPathBinding.java 2 Mar 2004 18:48:18 -0000 1.21
@@ -61,6 +61,7 @@
import org.apache.cocoon.woody.datatype.convertor.Convertor;
import org.apache.cocoon.woody.formmodel.Widget;
import org.apache.cocoon.woody.formmodel.Repeater;
+import org.apache.commons.collections.ListUtils;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
@@ -77,14 +78,15 @@
private final String repeaterPath;
private final String rowPath;
private final String rowPathForInsert;
- private final String uniqueRowId;
- private final String uniqueRowIdPath;
- private final Convertor uniqueRowIdConvertor;
- private final Locale uniqueRowIdConvertorLocale;
- private final ValueJXPathBinding uniqueFieldBinding;
+ //private final String uniqueRowId;
+ //private final String uniqueRowIdPath;
+ //private final Convertor uniqueRowIdConvertor;
+ //private final Locale uniqueRowIdConvertorLocale;
+ //private final ValueJXPathBinding uniqueFieldBinding;
private final JXPathBindingBase rowBinding;
private final JXPathBindingBase insertRowBinding;
private final JXPathBindingBase deleteRowBinding;
+ private final List uniqueRowBinding;
/**
* Constructs RepeaterJXPathBinding
@@ -95,10 +97,10 @@
String rowPathForInsert, String uniqueRowId,
String uniqueRowPath, JXPathBindingBase[] childBindings,
JXPathBindingBase insertBinding,
- JXPathBindingBase[] deleteBindings) {
+ JXPathBindingBase[] deleteBindings, JXPathBindingBase[]
uniqueBindings) {
this(commonAtts, repeaterId, repeaterPath, rowPath, rowPathForInsert,
uniqueRowId, uniqueRowPath, null, null, childBindings,
- insertBinding, deleteBindings);
+ insertBinding, deleteBindings, uniqueBindings);
}
/**
@@ -110,19 +112,19 @@
String rowPathForInsert, String uniqueRowId,
String uniqueRowPath, Convertor convertor, Locale
convertorLocale,
JXPathBindingBase[] childBindings, JXPathBindingBase
insertBinding,
- JXPathBindingBase[] deleteBindings) {
+ JXPathBindingBase[] deleteBindings, JXPathBindingBase[]
uniqueBindings) {
super(commonAtts);
this.repeaterId = repeaterId;
this.repeaterPath = repeaterPath;
this.rowPath = rowPath;
this.rowPathForInsert = rowPathForInsert;
- this.uniqueRowId = uniqueRowId;
- this.uniqueRowIdPath = uniqueRowPath;
- this.uniqueFieldBinding = new ValueJXPathBinding(
+ //this.uniqueRowId = uniqueRowId;
+ //this.uniqueRowIdPath = uniqueRowPath;
+ /*this.uniqueFieldBinding = new ValueJXPathBinding(
JXPathBindingBuilderBase.CommonAttributes.DEFAULT,
uniqueRowId,
- uniqueRowPath, null, convertor, convertorLocale);
- this.uniqueRowIdConvertor = convertor;
- this.uniqueRowIdConvertorLocale = convertorLocale;
+ uniqueRowPath, null, convertor, convertorLocale);*/
+ //this.uniqueRowIdConvertor = convertor;
+ //this.uniqueRowIdConvertorLocale = convertorLocale;
this.rowBinding = new ComposedJXPathBindingBase(
JXPathBindingBuilderBase.CommonAttributes.DEFAULT,
childBindings);
@@ -137,6 +139,23 @@
if (this.deleteRowBinding != null) {
this.deleteRowBinding.setParent(this);
}
+ /* New unique key management */
+ uniqueRowBinding = new ArrayList();
+ // Create a UniqueFieldJXPAthBining for the unique define in
old-style
+ if (uniqueRowId != null && uniqueRowPath != null) {
+ uniqueRowBinding.add(new UniqueFieldJXPathBinding(
+ JXPathBindingBuilderBase.CommonAttributes.DEFAULT,
+ uniqueRowId, uniqueRowPath, convertor, convertorLocale));
+ }
+ for (int i=0; i < uniqueBindings.length; i++) {
+ uniqueRowBinding.add(uniqueBindings[i]);
+ }
+ /*this.uniqueRowBinding = new ComposedJXPathBindingBase(
+ JXPathBindingBuilderBase.CommonAttributes.DEFAULT,
+ uniqueBindings);
+ if (this.uniqueRowBinding != null) {
+ this.uniqueRowBinding.setParent(this);
+ }*/
}
/**
@@ -165,10 +184,14 @@
thisRow = repeater.addRow();
}
// make a jxpath ObjectModelSubcontext on the iterated element
- Pointer jxp = (Pointer) rowPointers.next();
+ Pointer jxp = (Pointer)rowPointers.next();
JXPathContext rowContext =
repeaterContext.getRelativeContext(jxp);
// hand it over to children
- this.uniqueFieldBinding.loadFormFromModel(thisRow, rowContext);
+ Iterator iter = this.uniqueRowBinding.iterator();
+ while (iter.hasNext()) {
+
((UniqueFieldJXPathBinding)iter.next()).loadFormFromModel(thisRow, rowContext);
+ }
+ //this.uniqueFieldBinding.loadFormFromModel(thisRow, rowContext);
this.rowBinding.loadFormFromModel(thisRow, rowContext);
}
if (getLogger().isDebugEnabled())
@@ -193,29 +216,30 @@
//create list of rows to insert at end
List rowsToInsert = new ArrayList();
- // iterate rows...
+ // iterate rows in the form model...
int formRowCount = repeater.getSize();
for (int i = 0; i < formRowCount; i++) {
Repeater.RepeaterRow thisRow = repeater.getRow(i);
- Widget rowIdWidget = thisRow.getWidget(this.uniqueRowId);
- Object rowIdValue = rowIdWidget.getValue();
+ // Get the key values
+ List rowIdValues = getUniqueRowValues(thisRow);
+ /*Widget rowIdWidget = thisRow.getWidget(this.uniqueRowId);
+ Object rowIdValue = rowIdWidget.getValue();*/
- if (rowIdValue != null) {
+ if (!isNullAllListElements(rowIdValues)) {
+ // if (rowIdValue != null) {
//if rowIdValue != null --> iterate nodes to find match
- Iterator rowPointers =
- repeaterContext.iteratePointers(this.rowPath);
+ Iterator rowPointers =
repeaterContext.iteratePointers(this.rowPath);
boolean found = false;
while (rowPointers.hasNext()) {
Pointer jxp = (Pointer) rowPointers.next();
- JXPathContext rowContext =
- repeaterContext.getRelativeContext(jxp);
- Object matchId = getMatchId(rowContext);
- if (rowIdValue.equals(matchId)) {
+ JXPathContext rowContext =
repeaterContext.getRelativeContext(jxp);
+ List matchIds = getMatchIdList(rowContext);
+ if (ListUtils.isEqualList(rowIdValues, matchIds)) {
// match! --> bind to children
this.rowBinding.saveFormToModel(thisRow, rowContext);
// --> store rowIdValue in list of
updatedRowIds
- updatedRowIds.add(rowIdValue);
+ updatedRowIds.add(rowIdValues);
found = true;
break;
}
@@ -224,23 +248,22 @@
// this is a new row
rowsToInsert.add(thisRow);
// also add it to the updated row id's so that this row
doesn't get deleted
- updatedRowIds.add(rowIdValue);
+ updatedRowIds.add(rowIdValues);
}
} else {
//if rowId == null --> remember to insert this one later
rowsToInsert.add(thisRow);
}
}
- //again iterate nodes for deletion
+ // Iterate again nodes for deletion
Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);
List rowsToDelete = new ArrayList();
while (rowPointers.hasNext()) {
- Pointer jxp = (Pointer) rowPointers.next();
- JXPathContext rowContext =
- repeaterContext.getRelativeContext((Pointer)jxp.clone());
- Object matchId = getMatchId(rowContext);
+ Pointer jxp = (Pointer)rowPointers.next();
+ JXPathContext rowContext =
repeaterContext.getRelativeContext((Pointer)jxp.clone());
+ List matchIds = getMatchIdList(rowContext);
// check if matchPath was in list of updates, if not --> bind
for delete
- if (!updatedRowIds.contains(matchId)) {
+ if (!isInUpdatedRowSet(updatedRowIds, matchIds)) {
rowsToDelete.add(rowContext);
}
}
@@ -275,20 +298,17 @@
//register the factory!
//this.insertRowBinding.saveFormToModel(repeater,
repeaterContext);
while (rowIterator.hasNext()) {
- Repeater.RepeaterRow thisRow =
- (Repeater.RepeaterRow)rowIterator.next();
+ Repeater.RepeaterRow thisRow =
(Repeater.RepeaterRow)rowIterator.next();
// Perform the insert row binding.
- this.insertRowBinding.saveFormToModel(repeater,
- repeaterContext);
+ this.insertRowBinding.saveFormToModel(repeater,
repeaterContext);
// --> create the path to let the context be created
Pointer newRowContextPointer =
repeaterContext.createPath(
this.rowPathForInsert + "[" + indexCount + "]");
JXPathContext newRowContext =
- repeaterContext.getRelativeContext(
- newRowContextPointer);
- if (getLogger().isDebugEnabled())
- getLogger().debug("inserted row at " +
- newRowContextPointer.asPath());
+
repeaterContext.getRelativeContext(newRowContextPointer);
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("inserted row at " +
newRowContextPointer.asPath());
+ }
// + rebind to children for update
this.rowBinding.saveFormToModel(thisRow, newRowContext);
getLogger().debug("bound new row");
@@ -296,10 +316,8 @@
}
} else {
if (getLogger().isWarnEnabled()) {
- getLogger().warn(
- "RepeaterBinding has detected rows to insert, " +
- "but misses the <on-insert-row> binding to do
it."
- );
+ getLogger().warn("RepeaterBinding has detected rows to
insert, but misses " +
+ "the <on-insert-row> binding to do it.");
}
}
}
@@ -308,6 +326,38 @@
}
}
+ private boolean isInUpdatedRowSet(Set updatedRowIdsSet, List matchIds) {
+ Iterator iter = updatedRowIdsSet.iterator();
+ while (iter.hasNext()) {
+ List updatedRowId = (List)iter.next();
+ if (ListUtils.isEqualList(updatedRowId, matchIds)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ private List getMatchIdList(JXPathContext rowContext) {
+ List matchIdList = new ArrayList();
+ Iterator iter = this.uniqueRowBinding.iterator();
+ while (iter.hasNext()) {
+ UniqueFieldJXPathBinding key =
(UniqueFieldJXPathBinding)iter.next();
+ Object matchId = rowContext.getValue(key.getXpath());
+ if (matchId != null && key.getConvertor() != null) {
+ if (matchId instanceof String) {
+ matchId = key.getConvertor().convertFromString(
+ (String)matchId, key.getConvertorLocale(), null);
+ } else {
+ if (getLogger().isWarnEnabled()) {
+ getLogger().warn("Convertor ignored on backend-value
" +
+ "which isn't of type String.");
+ }
+ }
+ }
+ matchIdList.add(matchId);
+ }
+ return matchIdList;
+ }
+ /*
private Object getMatchId(JXPathContext rowContext) {
Object matchId;
matchId = rowContext.getValue(this.uniqueRowIdPath);
@@ -325,6 +375,36 @@
}
return matchId;
}
+*/
+ private boolean isNullAllListElements(List list) {
+ Iterator iter = list.iterator();
+ while (iter.hasNext()) {
+ if (iter.next() != null) {
+ return false;
+ }
+ }
+ if (list.size() > 0) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+ /**
+ * Get the values of the unique-fields of the given row in the formModel
+ * @param thisRow
+ * @return List
+ */
+ private List getUniqueRowValues(Repeater.RepeaterRow thisRow) {
+ List values = new ArrayList();
+ Iterator iter = this.uniqueRowBinding.iterator();
+ while (iter.hasNext()) {
+ UniqueFieldJXPathBinding key =
(UniqueFieldJXPathBinding)iter.next();
+ Widget rowIdWidget = thisRow.getWidget(key.getFieldId());
+ Object rowIdValue = rowIdWidget.getValue();
+ values.add(rowIdValue);
+ }
+ return values;
+ }
public String toString() {
return "RepeaterJXPathBinding [widget=" + this.repeaterId +
@@ -333,7 +413,7 @@
public void enableLogging(Logger logger) {
super.enableLogging(logger);
- this.uniqueFieldBinding.enableLogging(logger);
+ //this.uniqueFieldBinding.enableLogging(logger);
if (this.deleteRowBinding != null) {
this.deleteRowBinding.enableLogging(logger);
}
@@ -341,5 +421,9 @@
this.insertRowBinding.enableLogging(logger);
}
this.rowBinding.enableLogging(logger);
+ Iterator iter = this.uniqueRowBinding.iterator();
+ while (iter.hasNext()) {
+ ((UniqueFieldJXPathBinding)iter.next()).enableLogging(logger);
+ }
}
}
1.1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/UniqueFieldJXPathBindingBuilder.java
Index: UniqueFieldJXPathBindingBuilder.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.binding;
import org.apache.cocoon.woody.util.DomHelper;
import org.apache.cocoon.woody.Constants;
import org.apache.cocoon.woody.datatype.convertor.Convertor;
import org.apache.cocoon.i18n.I18nUtils;
import org.w3c.dom.Element;
import java.util.Locale;
/**
* UniqueFieldJXPathBindingBuilder provides a helper class for the Factory
* implemented in [EMAIL PROTECTED] JXPathBindingManager} that helps
construct the
* actual [EMAIL PROTECTED] UniqueFieldJXPathBinding} out of the
configuration in the
* provided configElement which looks like:
* <pre><code>
* <wb:unique-field id="<i>widget-id</i>"
path="<i>xpath-expression</i>">
* <!-- optional convertor of these field -->
* <wd:convertor>
* <!-- any convertor -->
* </wd:convertor>
* </wb:unique-field>
* </code></pre>
*
* @version CVS $Id: UniqueFieldJXPathBindingBuilder.java,v 1.1 2004/03/02
18:48:18 antonio Exp $
*/
public class UniqueFieldJXPathBindingBuilder extends JXPathBindingBuilderBase
{
/**
* Creates an instance of [EMAIL PROTECTED] UniqueFieldJXPathBinding}
based on the attributes
* and nested configuration of the provided bindingElm.
*/
public JXPathBindingBase buildBinding(Element bindingElm,
JXPathBindingManager.Assistant assistant) throws BindingException {
try {
CommonAttributes commonAtts =
JXPathBindingBuilderBase.getCommonAttributes(bindingElm);
String widgetId = DomHelper.getAttribute(bindingElm, "id");
String xpath = DomHelper.getAttribute(bindingElm, "path");
Convertor convertor = null;
Locale convertorLocale = Locale.US;
Element convertorEl = DomHelper.getChildElement(bindingElm,
Constants.WD_NS, "convertor");
if (convertorEl != null) {
String datatype = DomHelper.getAttribute(convertorEl,
"datatype");
String localeStr = convertorEl.getAttribute("datatype");
if (!localeStr.equals("")) {
convertorLocale = I18nUtils.parseLocale(localeStr);
}
convertor =
assistant.getDatatypeManager().createConvertor(datatype, convertorEl);
}
UniqueFieldJXPathBinding fieldBinding =
new UniqueFieldJXPathBinding(commonAtts,
widgetId, xpath, convertor, convertorLocale);
return fieldBinding;
} catch (BindingException e) {
throw e;
} catch (Exception e) {
throw new BindingException("Error building binding defined at " +
DomHelper.getLocation(bindingElm), e);
}
}
}
1.1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/UniqueFieldJXPathBinding.java
Index: UniqueFieldJXPathBinding.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.binding;
import org.apache.cocoon.woody.formmodel.Widget;
import org.apache.cocoon.woody.datatype.convertor.Convertor;
import org.apache.commons.jxpath.JXPathContext;
import java.util.Locale;
/**
* UniqueFieldJXPathBinding provides an implementation of a [EMAIL PROTECTED]
Binding}
* that that allows the specification of a uniquefields defined inside a
repeater.
* <p>
* NOTES: <ol>
* <li>This Binding uses the provided widget-id of a defined field in the
repeater.</li>
* </ol>
*
* @version CVS $Id: UniqueFieldJXPathBinding.java,v 1.1 2004/03/02 18:48:18
antonio Exp $
*/
public class UniqueFieldJXPathBinding extends JXPathBindingBase {
/**
* The xpath expression to the objectModel property
*/
private final String xpath;
/**
* The id of the Woody form-widget
*/
private final String fieldId;
/**
* Optional convertor to convert values to and from strings when setting
or reading
* the from the model. Especially used in combination with XML models
where everything
* are strings.
*/
private final Convertor convertor;
/**
* The locale to pass to the [EMAIL PROTECTED] #convertor}.
*/
private final Locale convertorLocale;
/**
* Constructs UniqueFieldJXPathBinding.
*
* @param convertor may be null
*/
public UniqueFieldJXPathBinding(JXPathBindingBuilderBase.CommonAttributes
commonAtts, String widgetId, String xpath,
Convertor convertor, Locale convertorLocale) {
super(commonAtts);
this.fieldId = widgetId;
this.xpath = xpath;
this.convertor = convertor;
this.convertorLocale = convertorLocale;
}
/**
* Actively performs the binding from the ObjectModel wrapped in a jxpath
* context to the Woody-form-widget specified in this object.
*/
public void doLoad(Widget frmModel, JXPathContext jxpc) throws
BindingException {
Widget widget = frmModel.getWidget(this.fieldId);
if (widget == null) {
throw new BindingException("The widget with the ID [" +
this.fieldId
+ "] referenced in the binding does not exist in the form
definition.");
}
Object value = jxpc.getValue(this.xpath);
if (value != null && convertor != null) {
if (value instanceof String) {
value = convertor.convertFromString((String)value,
convertorLocale, null);
} else {
getLogger().warn("Convertor ignored on backend-value which
isn't of type String.");
}
}
widget.setValue(value);
if (getLogger().isDebugEnabled()) {
getLogger().debug("Done loading " + toString() + " -- value= " +
value);
}
}
/**
* Actively performs the binding from the Woody-form to the ObjectModel
* wrapped in a jxpath context
*/
public void doSave(Widget frmModel, JXPathContext jxpc) throws
BindingException {
// Do nothing
}
public String toString() {
return "UniqueFieldJXPathBinding [widget=" + this.fieldId + ",
xpath=" + this.xpath + "]";
}
/*public void enableLogging(Logger logger) {
super.enableLogging(logger);
}*/
/**
* @return Returns the convertor.
*/
public Convertor getConvertor() {
return convertor;
}
/**
* @return Returns the convertorLocale.
*/
public Locale getConvertorLocale() {
return convertorLocale;
}
/**
* @return Returns the fieldId.
*/
public String getFieldId() {
return fieldId;
}
/**
* @return Returns the xpath.
*/
public String getXpath() {
return xpath;
}
}
1.8 +1 -0 cocoon-2.1/src/blocks/woody/conf/woody-binding.xconf
Index: woody-binding.xconf
===================================================================
RCS file: /home/cvs//cocoon-2.1/src/blocks/woody/conf/woody-binding.xconf,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- woody-binding.xconf 17 Feb 2004 09:54:40 -0000 1.7
+++ woody-binding.xconf 2 Mar 2004 18:48:18 -0000 1.8
@@ -15,6 +15,7 @@
<binding name="insert-node"
src="org.apache.cocoon.woody.binding.InsertNodeJXPathBindingBuilder"/>
<binding name="delete-node"
src="org.apache.cocoon.woody.binding.DeleteNodeJXPathBindingBuilder"/>
<binding name="insert-bean"
src="org.apache.cocoon.woody.binding.InsertBeanJXPathBindingBuilder"/>
+ <binding name="unique-field"
src="org.apache.cocoon.woody.binding.UniqueFieldJXPathBindingBuilder"/>
<binding name="javascript"
src="org.apache.cocoon.woody.binding.JavaScriptJXPathBindingBuilder"/>
<binding name="case"
src="org.apache.cocoon.woody.binding.CaseJXPathBindingBuilder"/>
<binding name="class"
src="org.apache.cocoon.woody.binding.ClassJXPathBindingBuilder"/>