package extended.forms; import java.util.Iterator; import javax.jcr.Node; import javax.jcr.RepositoryException; import org.apache.commons.lang.StringUtils; import info.magnolia.context.MgnlContext; import info.magnolia.jcr.util.NodeUtil; import info.magnolia.module.form.engine.FormField; import info.magnolia.module.form.engine.FormStepState; import info.magnolia.module.form.templates.components.DefaultFormDataBinder; import info.magnolia.util.EscapeUtil; /** * Custom form data binder which supports binding without validation. */ public class ExtendedFormDataBinder extends DefaultFormDataBinder { private static final String CONTENT_NAME_TEXT_FIELD_GROUP = "edits"; /** * Binds the given component to a form step. * @param component the component * @return the {@link FormStepState} * @throws RepositoryException if anything goes wrong while reading from the given node. */ public FormStepState bind(Node component) throws RepositoryException { FormStepState step = new FormStepState(); step.setParagraphUuid(NodeUtil.getNodeIdentifierIfPossible(component)); if (component.hasNode("fieldsets")) { Iterator itFieldsets = NodeUtil.getNodes(component.getNode("fieldsets")).iterator(); bindFieldset(itFieldsets, step); } return step; } private void bindFieldset(Iterator itFieldsets, FormStepState step) throws RepositoryException { while (itFieldsets.hasNext()) { Node fieldset = itFieldsets.next(); if (fieldset.hasNode("fields")) { Iterator iterator = NodeUtil.getNodes(fieldset.getNode("fields")).iterator(); bindFields(iterator, step); } } } private void bindFields(Iterator iterator, FormStepState step) throws RepositoryException { while (iterator.hasNext()) { final Node node = iterator.next(); if (node.hasProperty("controlName")) { final String controlName = node.getProperty("controlName").getString(); final String value = EscapeUtil.escapeXss(StringUtils.join( MgnlContext.getParameterValues(controlName), "__")); FormField field = new FormField(); field.setName(controlName); field.setValue(value); step.add(field); if (node.hasNode(CONTENT_NAME_TEXT_FIELD_GROUP)) { Iterator textFieldGroup = NodeUtil.getNodes( node.getNode(CONTENT_NAME_TEXT_FIELD_GROUP)).iterator(); bindFields(textFieldGroup, step); } } } } }