no bind action anymore

This commit is contained in:
Keith Donald
2008-03-20 06:11:48 +00:00
parent e57e4c20ab
commit 29b14ef58b
4 changed files with 1 additions and 247 deletions

View File

@@ -1,79 +0,0 @@
package org.springframework.webflow.action;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.binding.convert.ConversionService;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ExpressionParser;
import org.springframework.binding.expression.support.ParserContextImpl;
import org.springframework.binding.mapping.MappingResult;
import org.springframework.binding.mapping.MappingResults;
import org.springframework.binding.mapping.MappingResultsCriteria;
import org.springframework.binding.mapping.impl.DefaultMapper;
import org.springframework.binding.mapping.impl.DefaultMapping;
import org.springframework.binding.mapping.results.TargetAccessError;
import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
public class BindAction extends AbstractAction {
private static final Log logger = LogFactory.getLog(BindAction.class);
private static final MappingResultsCriteria PROPERTY_NOT_FOUND_ERRORS = new PropertyNotFoundErrors();
private Expression target;
private ExpressionParser expressionParser;
private ConversionService conversionService;
public BindAction(Expression target, ExpressionParser expressionParser, ConversionService conversionService) {
this.target = target;
this.expressionParser = expressionParser;
this.conversionService = conversionService;
}
protected Event doExecute(final RequestContext context) throws Exception {
Object target = this.target.getValue(context);
if (target == null) {
throw new IllegalStateException(
"The bind target cannot be null - check your expression. Bind target expression = " + target);
}
DefaultMapper mapper = new DefaultMapper();
mapper.setConversionService(conversionService);
AttributeMap eventAttributes = context.getLastEvent().getAttributes();
if (logger.isDebugEnabled()) {
logger.debug("Binding event '" + context.getLastEvent().getId() + "' attributes " + eventAttributes
+ " to target " + target);
}
for (Iterator it = eventAttributes.asMap().keySet().iterator(); it.hasNext();) {
String name = (String) it.next();
Expression sourceAttribute = expressionParser.parseExpression(name, new ParserContextImpl()
.eval(AttributeMap.class));
Expression targetAttribute = expressionParser.parseExpression(name, new ParserContextImpl().eval(target
.getClass()));
mapper.addMapping(new DefaultMapping(sourceAttribute, targetAttribute));
}
MappingResults results = mapper.map(context.getLastEvent().getAttributes(), target);
if (!results.hasErrorResults()) {
return success();
} else {
if (results.getResults(PROPERTY_NOT_FOUND_ERRORS).size() == results.getErrorResults().size()) {
// all errors are 'property not found' -- acceptable
return success();
} else {
return error();
}
}
}
private static class PropertyNotFoundErrors implements MappingResultsCriteria {
public boolean test(MappingResult result) {
return result.getResult() instanceof TargetAccessError
&& result.getResult().getErrorCode().equals("propertyNotFound");
}
}
}

View File

@@ -55,7 +55,6 @@ import org.springframework.util.xml.DomUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.webflow.action.ActionResultExposer;
import org.springframework.webflow.action.BindAction;
import org.springframework.webflow.action.EvaluateAction;
import org.springframework.webflow.action.ExternalRedirectAction;
import org.springframework.webflow.action.FlowDefinitionRedirectAction;
@@ -676,9 +675,7 @@ public class XmlFlowBuilder extends AbstractFlowBuilder implements ResourceHolde
if (!(childNode instanceof Element)) {
continue;
}
if (DomUtils.nodeNameEquals(childNode, "bind")) {
actions.add(parseBindAction((Element) childNode));
} else if (DomUtils.nodeNameEquals(childNode, "evaluate")) {
if (DomUtils.nodeNameEquals(childNode, "evaluate")) {
actions.add(parseEvaluateAction((Element) childNode));
} else if (DomUtils.nodeNameEquals(childNode, "render")) {
actions.add(parseRenderAction((Element) childNode));
@@ -689,13 +686,6 @@ public class XmlFlowBuilder extends AbstractFlowBuilder implements ResourceHolde
return (Action[]) actions.toArray(new Action[actions.size()]);
}
private Action parseBindAction(Element element) {
String targetString = element.getAttribute("target");
ExpressionParser parser = getExpressionParser();
Expression target = parser.parseExpression(targetString, new ParserContextImpl().eval(RequestContext.class));
return new BindAction(target, parser, getConversionService());
}
private Action parseEvaluateAction(Element element) {
String expressionString = element.getAttribute("expression");
Expression expression = getExpressionParser().parseExpression(expressionString,

View File

@@ -121,15 +121,6 @@ Goes out of scope when the governing conversation ends.
<xsd:group name="actionTypes">
<xsd:choice>
<xsd:element ref="bind">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
Binds current event parameters to a target object associated with the flow. Also capable of performing a validation post-binding.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element ref="evaluate">
<xsd:annotation>
<xsd:documentation>
@@ -659,20 +650,6 @@ Handles exceptions that occur within this state.
</xsd:complexType>
</xsd:element>
<xsd:element name="bind">
<xsd:complexType>
<xsd:attribute name="target" type="expression" use="required">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The bind target.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="evaluate">
<xsd:complexType>
<xsd:attribute name="expression" type="expression" use="required">

View File

@@ -1,134 +0,0 @@
package org.springframework.webflow.action;
import junit.framework.TestCase;
import org.springframework.binding.convert.ConversionService;
import org.springframework.binding.convert.support.DefaultConversionService;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ExpressionParser;
import org.springframework.binding.expression.support.ParserContextImpl;
import org.springframework.webflow.core.collection.LocalAttributeMap;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.expression.DefaultExpressionParserFactory;
import org.springframework.webflow.test.MockRequestContext;
public class BindActionTests extends TestCase {
private ExpressionParser expressionParser = DefaultExpressionParserFactory.getExpressionParser();
private ConversionService conversionService = new DefaultConversionService();
private BindAction action;
public void testSuccessfulBind() throws Exception {
MockRequestContext context = new MockRequestContext();
context.getFlowScope().put("bindTarget", new BindBean());
Expression target = expressionParser.parseExpression("bindTarget", new ParserContextImpl()
.eval(RequestContext.class));
action = new BindAction(target, expressionParser, conversionService);
LocalAttributeMap eventData = new LocalAttributeMap();
eventData.put("stringProperty", "foo");
eventData.put("integerProperty", "3");
Event event = new Event(this, "submit", eventData);
context.setLastEvent(event);
Event result = action.execute(context);
assertEquals("success", result.getId());
BindBean bean = (BindBean) context.getFlowScope().get("bindTarget");
assertEquals("foo", bean.getStringProperty());
assertEquals(new Integer(3), bean.getIntegerProperty());
}
public void testBindNonexistantProperties() throws Exception {
MockRequestContext context = new MockRequestContext();
context.getFlowScope().put("bindTarget", new BindBean());
Expression target = expressionParser.parseExpression("bindTarget", new ParserContextImpl()
.eval(RequestContext.class));
action = new BindAction(target, expressionParser, conversionService);
LocalAttributeMap eventData = new LocalAttributeMap();
eventData.put("stringProperty", "foo");
eventData.put("bogusProperty", "bar");
eventData.put("integerProperty", "3");
Event event = new Event(this, "submit", eventData);
context.setLastEvent(event);
Event result = action.execute(context);
assertEquals("success", result.getId());
BindBean bean = (BindBean) context.getFlowScope().get("bindTarget");
assertEquals("foo", bean.getStringProperty());
assertEquals(new Integer(3), bean.getIntegerProperty());
assertEquals(0, context.getMessageContext().getMessages().length);
}
public void testBindWithTypeConversionErrors() throws Exception {
MockRequestContext context = new MockRequestContext();
context.getFlowScope().put("bindTarget", new BindBean());
Expression target = expressionParser.parseExpression("bindTarget", new ParserContextImpl()
.eval(RequestContext.class));
action = new BindAction(target, expressionParser, conversionService);
LocalAttributeMap eventData = new LocalAttributeMap();
eventData.put("stringProperty", "foo");
eventData.put("integerProperty", "malformed");
Event event = new Event(this, "submit", eventData);
context.setLastEvent(event);
Event result = action.execute(context);
assertEquals("error", result.getId());
BindBean bean = (BindBean) context.getFlowScope().get("bindTarget");
assertEquals("foo", bean.getStringProperty());
assertEquals(new Integer(3), bean.getIntegerProperty());
}
public void testBindWithEmptyAttributes() throws Exception {
MockRequestContext context = new MockRequestContext();
context.getFlowScope().put("bindTarget", new BindBean());
Expression target = expressionParser.parseExpression("bindTarget", new ParserContextImpl()
.eval(RequestContext.class));
action = new BindAction(target, expressionParser, conversionService);
LocalAttributeMap eventData = new LocalAttributeMap();
eventData.put("stringProperty", "");
eventData.put("integerProperty", null);
Event event = new Event(this, "submit", eventData);
context.setLastEvent(event);
Event result = action.execute(context);
System.out.println(context.getMessageContext());
assertEquals("success", result.getId());
BindBean bean = (BindBean) context.getFlowScope().get("bindTarget");
assertEquals("", bean.getStringProperty());
assertEquals(null, bean.getIntegerProperty());
assertEquals(0, context.getMessageContext().getMessages().length);
}
public static class BindBean {
private String stringProperty;
private Integer integerProperty = new Integer(3);
public String getStringProperty() {
return stringProperty;
}
public void setStringProperty(String stringProperty) {
this.stringProperty = stringProperty;
}
public Integer getIntegerProperty() {
return integerProperty;
}
public void setIntegerProperty(Integer integerProperty) {
this.integerProperty = integerProperty;
}
}
}