DbForms provides the
DbEventInterceptor
interface, which is capable of intercepting database operations (before and after they are executed).
This interface provides the following methods
Example 15.1. Methods defined in interface DbEventInterceptor
public int preInsert(HttpServletRequest request, Table table, FieldValues fieldValues,
DbFormsConfig config, Connection con)
throws ValidationException;
public void postInsert(HttpServletRequest request, DbFormsConfig config, Connection con);
public int preUpdate(HttpServletRequest request, Table table, FieldValues fieldValues,
DbFormsConfig config, Connection con)
throws ValidationException;
public void postUpdate(HttpServletRequest request, DbFormsConfig config, Connection con);
public int preDelete(HttpServletRequest request, Table table, FieldValues fieldValues,
DbFormsConfig config, Connection con)
throws ValidationException;
public void
postDelete(HttpServletRequest request, DbFormsConfig config, Connection con);
public int
preSelect(HttpServletRequest request, DbFormsConfig config, Connection con)
throws ValidationException;
public void
postSelect(HttpServletRequest request, DbFormsConfig config, Connection con);
(Note: the following pre/postAddRow methods are used to make a custom filter by selectively ignoring the results of our query,
sum values, or just merge some columns which can not be merged in sql. See IGNORE_OPERATION
below. Also, they are useful for counting rows -- see howtoAddRowCountSupport.jsp under /bookstore/howto/)
public int preAddRow(DbEventInterceptorData data)
throws ValidationException, MultipleValidationException;
public void postAddRow(DbEventInterceptorData data);
As the names indicate:
The preXxx() methods get called BEFORE the respective database operation is performed, and return a value indicating if the operation should be performed or not
There are three possible values which could be returned, as well as an exception that could be thrown:
DENY_OPERATION
If DENY_OPERATION
is given back by the interceptor, a SQL exception is raised
and the transaction is rolled back if set to autoCommit = false
GRANT_OPERATION
If GRANT_OPERATION
is returned by the interceptor, the event will be performed and in cases where autoCommit = false
, the transaction will be committed.
IGNORE_OPERATION
If IGNORE_OPERATION
is returned by the interceptor, the DBForms event (UPDATE, INSERT, SELECT, DELETE) will not be performed, However in cases where autoCommit = false
, the transaction will be committed.
For preAddRow and postAddRow methods, returning IGNORE_OPERATION
means that the row is
not written to the ResultSetVector and allows you to make filters which are not possible via SQL alone.
ValidationException
If the Interceptor raises a ValidationException
the transaction is rolled
back too.
So if you have an exception inside your interceptor, you have two
possibilities:
Return DENY_OPERATION
which generates SQLException
with an NOGRANT message
or throw a ValidationExeception
with your own error text.
The postXxx() methods get called AFTER the operation has finished, and do not return a value, as the operation has already completed.