Custom Java Objects
   
 
|An Example
   
2
 
 
An example of an extension - If you want to create an entry in a login-history table every time a user logs in. To accomplish this, you can create a class LoginUpdate derived from DbObjectStore. The derived class overrides the getObject method, and in the overriding implementation, makes an entry in the login history table if the object being returned is valid. If this derived class is used as the data port for the non-leaf node in the login page, the desired effect is achieved. Correspondingly, of course, you must have a descriptor class for the LoginUpdate class. The source code for this example is given below

/** * * Copyright
(C) 2000-2001 PK4 Corp.
* All rights reserved
* No part of this code may be used for any purpose or modified without
* explicit written permission from PK4 Corp.
* */

package com.Sales;
import com.ampercorp.zeroCode.udm.DbObjectStore;
import com.ampercorp.zeroCode.dbSupport.DbUtils;
import com.ampercorp.zeroCode.dbSupport.DbConnection;
import com.ampercorp.zeroCode.dbSupport.ConnectionPool;
import com.ampercorp.zeroCode.udm.SearchCondition;
import com.ampercorp.zeroCode.udm.DbObject;
 
import java.util.Date;
import javax.servlet.http.*;
import com.sun.java.util.collections.Map;
/**
* This class is the login history table updater. It illustrates a hook that
* updates the login history table every time a user logs in. *
* This class works by overriding the DbObjectStore's getObject method. When
* the method is called, it invokes super.getObject, and if the call is
* successful, updates the login history table. For this to work, we need to
* use this class as the DataStore for the main node of the homePage UDM.
*
* We need only override one method here.
*/ public class LoginUpdate extends DbObjectStore {
/**
* Override the inherited method. The overriding method simply invokes the
* parent method being overridden. If the latter returns a non-null
* object, it means the login was valid, so it updates the login history
* table. */
public Object getObject (SearchCondition condition, Map parameterMap, HttpServletRequest req)
throws Exception {
Object o = super.getObject (condition, parameterMap, req);
if (o != null) {
ConnectionPool pool = connectionPool();
DbConnection connection = pool.getConnection();
Date now = new Date();
String sqlString =
"insert into login_history values (" +
"login_history_sq.nextval, " +
DbUtils.sqlRep (now) + "," +
DbUtils.sqlRep (req.getRemoteAddr()) + "," +
((DbObject) o).id() +
")";
connection.executeSql (sqlString);
}
return o;
}
}
 
   
2