/**
* The AccountSetPeer provides the runtime class name for the accounts
* it is responsible for as well as the SQL for restoring them.
*/
package bank.server;
import imaginary.persist.PersistentSet;
import imaginary.persist.Transaction;
import java.util.Hashtable;
public class AccountSetPeer extends imaginary.persist.DatabaseSetPeer {
/**
* Depending on the account type for the account being restored,
* this method will return either "CheckingAccount" or
* "SavingsAccount".
* @param h the values from the database
* @return the class name to instantiate for this row
*/
public String getPersistentClass(Hashtable h) {
String tmp = (String)h.get("t_accounts.account_type");
if( tmp.equals("C") ) {
return "bank.server.CheckingAccount";
}
else {
return "bank.server.SavingsAccount";
}
}
/**
* Provides the proper SQL SELECT statement based on the values
* that come from a database row stored in the Hashtable. If the
* Hashtable is null, then all accounts are retrieved.
* If a customer ID is passed, then all accounts for that customer
* ID are provided.
* @param h a Hashtable containing the customer ID
* @return the SELECT SQL to get the accounts from the database with
*/
public String getSql(Hashtable h) {
if( h == null ) { // If no query criteria are specified
return "SELECT * from t_accounts";
}
else {
String tmp = "SELECT * from t_accounts WHERE ";
int cust_id = -1;
// SELECT * from t_accounts
// WHERE cust_id = 1
if( h.containsKey("cust_id") ) {
cust_id = ((Integer)h.get("cust_id")).intValue();
}
return tmp + "cust_id = " + cust_id;
}
}
}
|