OAF_EO系列2 - Validation数据校验验证机制(概念)

2014-06-12 Created By BaoXinjian

一、摘要


1. 在Update数据之前,我们往往要对待更新的记录进行有效性的校验,校验级别包括

  • Attribute Level Validation: 字段级的校验
  • Entity Level Validation: 记录级的校验

2. 字段级校验-只涉及单个字段的校验

字段级别的校验一般情况下写在EO的set<Attribute name>()方法中,

根据传入的value来决定是执行setAttributeInternal() 还是throw Exception.(代码应写在setAttributeInternal()之前)

例如如果一个订单的状态为CLOSE, 则不允许更改OrderPrice. 伪码如下:

Public void   validateEntity()

{

   super.validateEntity();

   String status = getOrderStatus();

If (“CLOSE”.equals(status))

{

 Number ldorderprice = (Number)getPostedAttribute(ORDERPRICE);

 Number neworderprice = (Number)getOrderPrice();

 If (oldorderprice.compareto(neworderprice)!=0)

 {

  throw new Exception(“Error: Order Price can not be edited!”);

 }

}

}

3. 记录级校验-只涉及多个字段组合的校验

当校验涉及多个Attribute时,你就不能使用字段级的校验了,需使用记录级的校验,即Entity Level Validation. 此校验在validateEntity()方法中实现.

校验代码须写在   super.validateEntity() 之后

例如如果一个订单的状态为CLOSE, 则不允许更改OrderPrice. 伪码如下:

Public void   validateEntity()

{

   super.validateEntity();

   String status = getOrderStatus();

If (“CLOSE”.equals(status))

{

 Number ldorderprice = (Number)getPostedAttribute(ORDERPRICE);

 Number neworderprice = (Number)getOrderPrice();

 If (oldorderprice.compareto(neworderprice)!=0)

 {

  throw new Exception(“Error: Order Price can not be edited!”);

 }

}

}

 

二、字段级校验案例


案例. 在EO的字段添加异常,如果Total为0值,则抛出异常

1. 字段级验证异常调用

 

2. 字段级验证异常测试

 

 

三、记录级校验案例


案例. 在EO的记录级validateEntity()添加异常,将多个字段组合进行判断,则抛出异常

1. 记录级验证异常调用

 

2. 记录级验证异常调用测试

 

四、异常触发后如何对transaction进行rollback


不要试图在EOValidation 里执行rollback() 方法或clearcache() 方法. 当出现校验失败时,有下面两种做法:

Bad Method:

  • 在EO Module

public void validateEntity(){

 ….

 transaction.rollback()

 throw new OAException….

}

Right Method:

  • In EO Module

public void validateEntity(){

 throw new OAException…

}

  • In AM Module

Try

{  transaction.commit();

}Catch ( OAException ex)

{transaction.rollback();}

 

Thanks and Regards

参考:Tony Liu - http://blog.itpub.net/10359218/viewspace-677447/

posted on 2014-06-14 08:34  东方瀚海  阅读(1313)  评论(1编辑  收藏  举报