ADF(BC)--获取数据库记录字段的旧值

来源:互联网 发布:arcgis能安装在mac吗 编辑:程序博客网 时间:2024/06/05 08:28

在ADF应用中,有时需要在提交数据前比较某个字段和数据库的该字段旧值,看该字段值是否发生改变,使用UI的valuehangeListener事件,仅仅能比较UI当前值和提交值,而不能实现和数据库原值进行比较,特别时存在未提交事务时进行多次修改的情况下。

最原始的方式有,通过再一次查询该行记录,得到该字段的旧值,和提交事务时的值进行比较。但我们在使用ADF框架时,通过ADFBC的一些api也可以方便的获取数据库记录的旧值。其中之一是通过EO的getPostedAttribute(int index)方法获取数据库原值。但该方法为EntityImpl的protected方法,我们在代码中不能直接调用,因此需要做一些工作。

1、首先,可建立一个自定义实体基类BaseEntity,继承EntityImpl,在该基类里添加如下方法:

public Object getOldValueByAttribut(StringattrName){

         int index = this.getAttributeIndexOf(attrName);

        if(index<0)

            return null;

        return this.getPostedAttribute(index);

    }


2、新建一个视图行的基类BaseViewRowImpl,继承ViewRowImpl,在该类里添加如下方法

/**

     * @param entityIndex 实体索引位置,如该视图行只有一个实体,该参数设为0

     * @param attrName 属性名

     * @return 返回数据库字段旧值,如果无实体或字段名无效则返回null.

     */

   public Object getOldValueByAttribut(int entityIndex, String attrName) {

       Entity[] entities = this.getEntities();

       if (null == entities || entityIndex >= entities.length || entityIndex< 0)

           return null;

       BaseEntity entity;

       try {

           entity = (BaseEntity)entities[entityIndex];

       } catch (Exception e) {

           ErrorMessageHandler.handlerException(e);

           return null;

       }

       return entity.getOldValueByAttribut(attrName);

    }


3、在jdev里配置实体和视图行的基类为上述BaseEntity,BaseViewRowImpl。

4、创建实体和视图,使用方式如下:

BaseViewRowImpl br = (BaseViewRowImpl)vo.getCuurentRow();

String oldValue = (String)br. getOldValueByAttribut(0,"Department");


0 0