I removed all validation code from surrounding object and created a validator as described in webflow docs. But still no validation takes place. The code change is as follows:
flow.xml:
Code:
<view-state id="addNode" model="nodeFlowBean.nodeVO" view="addNode.jspx">
<on-entry>
<set name="nodeFlowBean.recordReadOnly" value="false"/>
<set name="nodeFlowBean.nodeVO.operation" value="'INSERT'"/>
</on-entry>
<transition on="save" to="viewEditNode">
<evaluate expression="nodeFlowBean.insert(messageContext)"/>
</transition>
</view-state>
spring container config
Code:
<bean id="nodeFlowBean" class="com.network.flowbean.NodeFlowBean" scope="flow">
<property name="beanResolver">
<ref bean="beanResolver"/>
</property>
<property name="nodeServiceName">
<value>nodeService</value>
</property>
<aop:scoped-proxy/>
</bean>
<bean id="nodeVOValidator" class="com.network.validation.NodeVOValidator">
<property name="nodeService"><ref bean="nodeService"/></property>
</bean>
nodeFlowBean insert method:
Code:
public class NodeFlowBean implements Serializable
{
private static final long serialVersionUID = 1L;
private NodeVO nodeVO;
private String nodeServiceName;
private Boolean recordReadOnly = true;
private BeanResolver beanResolver;
public NodeFlowBean()
{
this.nodeVO = new NodeVO();
}
public boolean insert(MessageContext messageContext)
{
try
{
INodeService nodeService = (INodeService)this.beanResolver.getBean(this.nodeServiceName);
this.nodeVO.setId(nodeService.addNewNode(this.nodeVO));
messageContext.addMessage(new MessageBuilder().info().source("nodeForm").defaultText("Success").build());
return true;
}
catch (RecordAlreadyExistsException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
messageContext.addMessage(new MessageBuilder().info().source("nodeForm").defaultText("Failed").build());
return false;
}
}
}
finally this is the validator:
Code:
public class NodeVOValidator
{
private INodeService nodeService;
public INodeService getNodeService()
{
return nodeService;
}
public void setNodeService(INodeService nodeService)
{
this.nodeService = nodeService;
}
public boolean validateUserId(NodeVO nodeVo)
{
return this.isUserIdUniqueInDB(nodeVo);
}
public void validateAddNode(NodeVO nodeVo, ValidationContext validationContext)
{
this.validateThisNode(nodeVo, validationContext);
}
public void validateViewEditNode(NodeVO nodeVo, ValidationContext validationContext)
{
this.validateThisNode(nodeVo, validationContext);
}
private void validateThisNode(NodeVO nodeVo, ValidationContext validationContext)
{
MessageContext messageContext = validationContext.getMessageContext();
if(!this.isUserIdUniqueInDB(nodeVo))
{
messageContext.addMessage(new MessageBuilder().error().source("nodeForm").defaultText("Node with this User ID already exists. Please use a unique User ID.").build());
}
if(!this.isNodeLatLongUniqueInDB(nodeVo))
{
messageContext.addMessage(new MessageBuilder().error().source("nodeForm").defaultText("Node with these coordinates already exits. Please enter unique coordinates.").build());
}
}
private boolean isUserIdUniqueInDB(final NodeVO nodeVo)
{
List<NodeVO> exist = this.nodeService.findByExample(nodeVo);
if(nodeVo.getOperation() == EEntityOperation.INSERT && exist != null && exist.size() > 0)
return false;
//in case of update need to check if any other node has this user id and since user id HAS to be unique
//and Not-Null; so exist will at max have ONLY 1 item in it.
//so check if that is different from current or not
if(nodeVo.getOperation() == EEntityOperation.UPDATE && exist != null && exist.size() == 1 && !exist.contains(nodeVo))
return false;
return true;
}
private boolean isNodeLatLongUniqueInDB(final NodeVO nodeVoToValidate)
{
NodeVO temp = new NodeVO();
temp.setLatDeg(nodeVoToValidate.getLatDeg());
temp.setLatMin(nodeVoToValidate.getLatMin());
temp.setLatMin(nodeVoToValidate.getLatSec());
temp.setLatDirection(nodeVoToValidate.getLatDirection());
temp.setLongDeg(nodeVoToValidate.getLongDeg());
temp.setLongMin(nodeVoToValidate.getLongMin());
temp.setLongSec(nodeVoToValidate.getLongSec());
temp.setLongDirection(nodeVoToValidate.getLongDirection());
List<NodeVO> exist = nodeService.findByExample(temp);
if(nodeVoToValidate.getOperation() == EEntityOperation.INSERT && exist != null && exist.size() > 0)
return false;
//in case of update need to check if any other node has these coordiates and since coordinates HAVE to be unique
//and Not-Null; so exist will at max have ONLY 1 item in it.
//so check if that is different from current or not
if(nodeVoToValidate.getOperation() == EEntityOperation.UPDATE && exist != null && exist.size() == 1 && !exist.contains(nodeVoToValidate))
return false;
return true;
}
}
One thing i do not understand is why this dependence on names of validators why not flexibility to specify validator in flow.xml
Anyways what am I missing now?