ADF系列-2.EO的高级属性

在上一篇博客 ADF系列-1.EO的各个属性初探 中介绍了EO的一些常用简单属性。本次将介绍EO中一些比较常用的一些高级属性

一.基于Sequence创建EO,一下介绍三种方式(以HR用户的Employee表为例)

   1.双击EmployeesEO的主键(EmployeeId),打开到Entity Attribute选项卡,value type选中Expression,单击Edit,在Expression中输入

     (new oracle.jbo.server.SequenceImpl("EMPLOYEES_SEQ", adf.object.getDBTransaction())).getSequenceNumber()

     其中EMPLOYEES_SEQ是在数据库中定义的Sequence

      

2.先生产EmployeesEOImpl.java文件,后添加自定义的方法如下

  

    protected oracle.jbo.domain.Number nextValFormId(String sequenceName) {
      SequenceImpl s = new SequenceImpl(sequenceName,getDBTransaction());
      return s.getSequenceNumber();
    }

然后把 nextValFormId方法注册到Groovy表达式中,入下图

3.在双击EmployeeId,在Custom Properties选项卡中,添加EMPLOYEES_SEQ

然后在EmployeesEOImpl中复写create方法

    /**
     * Add attribute defaulting logic in this method.
     * @param attributeList list of attribute names/values to initialize the row
     */
    protected void create(AttributeList attributeList) {
        super.create(attributeList);
        for (AttributeDef def : getEntityDef().getAttributeDefs()) {
             String sequenceName = (String) def.getProperty("SequenceName");
             if (sequenceName != null) {
               SequenceImpl s = new SequenceImpl(sequenceName, getDBTransaction());
               setAttribute(def.getIndex(), s.getSequenceNumber());
             }
           }
    }

二.增加验证

  1.Attribute Level validationg rule:属性级验证规则,在更新实体属性之前触发

     (1).使用内建验证,例如List验证如下

          

    (2).自定义验证

          新建一个Validation Rule

           

            所生产的代码如下,绿色部分就是验证逻辑

            

package com.test.model.rule;

import oracle.jbo.ValidationException;
import oracle.jbo.rules.JboValidatorContext;
import oracle.jbo.rules.JboValidatorInterface;

public class TestValidationRule implements JboValidatorInterface {
    private String description = "";

    public TestValidationRule() {
    }

    /**
     * Return true if value is valid.
     */
    public boolean validateValue(Object value) {
        return true;
    }

    /**
     * Invoked by framework for validation.
     */
    public void validate(JboValidatorContext ctx) {
        if (!validateValue(ctx.getNewValue())) {
            throw new ValidationException("com.test.model.rule.TestValidationRule validation failed");
        }
    }

    /**
     * Description of what this class validates.
     */
    public String getDescription() {
        return description;
    }

    /**
     * Description of what this class validates.
     */
    public void setDescription(String str) {
        description = str;
    }
}

            然后点击EO中的属性,选择

            

  2.Entity Level validation rule:实体级验证,既可以在改变了行选择时触发,也可以在提交事物时触发具体应用

          (1).使用内建验证(EO定义xml -->Business rules --> Entity Validators添加按钮)

                

          (2).自建验证java类

                

              扩展改方法代码如下,必须FirtName为"sun"才能验证通过

         

    /**
     * Validation method for EmployeesEO.
     */
    public boolean validateEmployeesEO() {
        if("sun".equals(this.getFirstName())){
            return true ;    
        }else{
            return false;    
        }   
    }

          为该验证注册如下错误消息

          

         测试结果如下截图

          

           

原文地址:https://www.cnblogs.com/qizhelongdeyang/p/4323988.html