
Chapter 3
133
Modifying hooks for a model
Every model extends the PO class, which provides various hooks (methods) that one can
override. They are afterSave, beforeSave, afterDelete, and beforeDelete. The
syntax for these methods are:
protected boolean afterSave(boolean newRecord, boolean success)
protected
boolean afterDelete(boolean success)
protected
boolean beforeSave (boolean newRecord)
protected
boolean beforeDelete()
The main use of these methods is to perform additional tasks while a particular row of a table
is being saved or deleted a particular row of a table. Let us say, we want to audit the activity,
like create/update and delete, of the C_Project table. To accomplish this, we shall override
the beforeSave and beforeDelete methods so that the audit information can be captured
before the actual project information is updated or deleted.
In this recipe, we'll see how to override these hooks.
How to do it...
1. Implement the afterSave method in the Mmom model class to send the e-mail
to the participants.
package org.compiere.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.util.DB;
import org.compiere.util.EMail;
//extend the generated x_c_mom model
public class Mmom extends X_c_mom {
public Mmom(Properties ctx, int c_mom_ID, String trxName) {
super(ctx, c_mom_ID, trxName);
}
protected boolean afterSave(boolean newRecord, boolean success)
{