Saturday, February 2, 2013

ADF: Auditing Changes to the same Database table. (recording history of changes)


Auditing the changes to a database record can be done in multiple ways. One of the ways is to store the changed record and the original record in the same table. A column would differentiate the active record from the audit record.

This can be easily acheived in ADF BC. Below example demonstrates the steps needed.

1. Data Model.
ACTIVE - indicates the if the record is a current record or a audit row.
ACTIVE_ID - stores the original id

 2. Create ADF BC EO/VO as below. Configure the EntityObject with history columns, primary key genration logic etc.



3. Create a subclass of EntityImpl and override doDML(). The logic below captures Update operation and splits the row into two. The current row contains the updated values and a new row contains the old values. Delete operation is captured and propagated as update operation with setting active flag to "N".



4. Configure the CustAuditEO to use the above base class.

5. Create a View Criteria to filter active records.



6. Create a view link within the same view object, joining on Id and AuditId



7. Expose the View Object Instances in AM Data Model as below. And configure the base view object to show only active records.



8. Run the UI project. Create a new row. save the changes. Update the row and save the changes. Click the view history button to see the complete audit trail of changes.


You can download the example here.

2 comments:

  1. Hi,

    Can you explain this same functionality with a custom table storing the chnages with the who columns

    ReplyDelete
  2. To save changes to column in the same table and same row. Below is the pseduo code for the same

    @override
    protected void doDML(int i, TransactionEvent e){
    // "Col1" index = 2
    if(i==DML_UPDATE && getAttribute(2)!=getPostedAttribute(2)){
    setAttribute("historycolumn",getAttribute(2));
    }

    }

    Please note that you'll need to have one additional column per each column data that you want to save. And you can only save one historical value.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...