Monday, September 30, 2013

How to get only modified attributes in an Entity ?

This blog explains about performing customized audit s on a specific tables

Scenario : Audit only modified columns associated data into Audit table, rather than unmodified field values into history/audit table.

 - get only modified attribute name, old value and new value for an entity
 - get the table name associated to an entity
 - get the database column name associated to an entity
- store only modified columns associated data into audit table.

Implementation

- Create a BaseEntityImpl class which extends to oracle.jbo.server.EntityImpl, override doDML() method as shown below

- Get the entity state, based on the entity state perform database CRUD operations.


       

           @Override
    protected void doDML(int i, TransactionEvent transactionEvent) {
        String userName =
            ADFContext.getCurrent().getSecurityContext().getUserName();
        SimpleDateFormat sdf = new SimpleDateFormat("d-MMM-yyyy HH:mm:ss");
        String currentTime = sdf.format(System.currentTimeMillis());

        if (EntityImpl.DML_DELETE == i) {
            System.out.println("Delete operation");
            Long timeInMillis = System.currentTimeMillis();
            try {

                String sql =
                    "INSERT INTO XXATDTPR_HISTORY_DATA(ID,TABLE_NAME,KEY_NAME,KEY_VALUE,OPERATION,CREATED_BY,CREATED_ON,UPDATED_BY,UPDATED_ON) VALUES(" +
                    timeInMillis + ",'" + this.getEntityDef().getSource() +
                    "','" +
                    ((BaseDefImpl)this.getEntityDef()).getPrimaryKeys()[0].getColumnName() +
                    "','" +
                    getAttribute(((BaseDefImpl)this.getEntityDef()).getPrimaryKeys()[0].getIndex()) +
                    "','" + "DELETE" + "','" + userName + "','" + currentTime +
                    "','" + userName + "','" + userName + "')";
                System.out.println("SQL :: " + sql);
                getDBTransaction().executeCommand(sql);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (EntityImpl.DML_INSERT == i) {
            System.out.println("Insert operation");

            Long timeInMillis = System.currentTimeMillis();
            try {

                String sql =
                    "INSERT INTO XXATDTPR_HISTORY_DATA(ID,TABLE_NAME,KEY_NAME,KEY_VALUE,OPERATION,CREATED_BY,CREATED_ON,UPDATED_BY,UPDATED_ON) VALUES(" +
                    timeInMillis + ",'" + this.getEntityDef().getSource() +
                    "','" +
                    ((BaseDefImpl)this.getEntityDef()).getPrimaryKeys()[0].getColumnName() +
                    "','" +
                    getAttribute(((BaseDefImpl)this.getEntityDef()).getPrimaryKeys()[0].getIndex()) +
                    "','" + "INSERT" + "','" + userName + "','" + currentTime +
                    "','" + userName + "','" + userName + "')";
                System.out.println("SQL :: " + sql);
                getDBTransaction().executeCommand(sql);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (EntityImpl.DML_UPDATE == i) {
            System.out.println("Update operation");

            AttributeDef[] attributeDefs =
                this.getEntityDef().getAttributeDefs();
            for (AttributeDef attributeDef : attributeDefs) {
                if (isAttributeChanged(attributeDef.getIndex())) {
                    Object oldValue =
                        getPostedAttribute(attributeDef.getIndex());
                    Object newValue = getAttribute(attributeDef.getIndex());
                    //use sequence generator here to generate unique prmary key
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                    }

                    Long timeInMillis = System.currentTimeMillis();
                    try {

                        String sql =
                            "INSERT INTO XXATDTPR_HISTORY_DATA(ID,TABLE_NAME,KEY_NAME,KEY_VALUE,COLUMN_NAME,OLD_VALUE,NEW_VALUE,OPERATION,CREATED_BY,CREATED_ON,UPDATED_BY,UPDATED_ON) VALUES(" +
                            timeInMillis + ",'" +
                            this.getEntityDef().getSource() + "','" +
                            ((BaseDefImpl)this.getEntityDef()).getPrimaryKeys()[0].getColumnName() +
                            "','" +
                            getAttribute(((BaseDefImpl)this.getEntityDef()).getPrimaryKeys()[0].getIndex()) +
                            "','" + attributeDef.getName() + "','" +
                            oldValue.toString() + "','" + newValue.toString() +
                            "','" + "UPDATE" + "','" + userName + "','" +
                            currentTime + "','" + userName + "','" + userName +
                            "')";
                        System.out.println("SQL :: " + sql);
                        getDBTransaction().executeCommand(sql);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        super.doDML(i, transactionEvent);
    }
       
 




No comments:

Post a Comment