Filling audit columns automatically with ADF Business Components
In a previous post I blogged about the use of sequences for the ID column as primary key. This post goes a bit further and describes how you could have audit columns of a table fill automatically. With audit columns I mean the columns in which information is stored about who changed a row and when (or created one).
Of course it is possible to get this functionality by setting the attribute in the entity object to 'history column' and choose the corresponding type. But with this example it is done automatically by matching the column name, which can be handy when you have a lot of tables with auditing columns, which otherwise you have to configure by hand, one by one.
For this purpose I created a specific base class called 'AuditTrailEntityImpl'. In my example the audit columns are named 'CREATED_BY', 'CREATED_DATE', 'MODIFIED_BY' and 'MODIFIED_DATE'. This is my code:
-
/**
-
* Extends the base ADF EntityImpl to add support for dynamic filling of audit columns.
-
* The CREATED_BY and CREATED_DATE attributes are filled in the create()-method,
-
* the MODIFIED_BY and MODIFIED_DATE attributes are filled in the prepareForDML() method
-
* (only if incoming operation equals DML_UPDATE).
-
*
-
* If you wish to use this functionality, edit your Entity Object and extend from
-
* base<code>.adf.model.businessobjects.AuditTrailEntityImpl</code>.
-
*
-
* @author Pascal Alma
-
*
-
* $Log: AuditTrailEntityImpl.java,v $
-
*/
-
public class AuditTrailEntityImpl extends BaseEntityImpl
-
{
-
-
private final Log _log = LogFactory.getLog(this.getClass());
-
-
/**
-
* Overridden method.
-
*
-
* Adds support for filling CREATED-attributes.
-
* @param nameValuePair
-
*/
-
{
-
_log.debug(method);</samp>
-
-
_log.debug("Create instance for '" + getEntityDef().getName() + "'");
-
super.create(nameValuePair);
-
-
// set 'create' history attributes
-
setAttribute(CREATED_BY, getCurrentUser());
-
}
-
-
/**
-
* Overridden method.
-
*
-
* Adds support for filling MODIFIED-attributes.
-
* @param e
-
* @param operation
-
*/
-
protected void prepareForDML(int operation, TransactionEvent e)
-
{
-
_log.debug(method);</samp>
-
-
super.prepareForDML(operation, e);
-
-
// only if current operation == UPDATE, do update related history columns.
-
if (operation == DML_UPDATE)
-
{
-
setAttribute(MODIFIED_BY, getCurrentUser());
-
}
-
}
-
-
/**
-
* Get current user from application module.
-
* Uses getUserPrincipalName to derive current user. If getUserPrincipalName
-
* returns null, the default username is used.
-
* @return Name of current user
-
*/
-
{
-
_log.debug("getCurrentUser");
-
String username = ((ApplicationModuleImpl)getDBTransaction().getRootApplicationModule()).getUserPrincipalName();
-
if (username == null)
-
{
-
username = "unknown";
-
}
-
return username;
-
}
-
-
}
Now for every table with audit columns just use this Entity Object as base class
and your audit information is stored.
One condition which much be met is that you have to use some kind of JAAS login functionality to have the username filled in the Application Module context.
Tags: Oracle JHeadstart









