/**
* This class is the business object that represents a bank customer.
*/
package bank.server;
import imaginary.persist.PersistenceException;
import imaginary.persist.Persistent;
import imaginary.persist.PersistentPeer;
import imaginary.persist.RemotePersistent;
import imaginary.persist.RemoteLockHolder;
import imaginary.persist.Transaction;
import java.rmi.RemoteException;
import java.util.Hashtable;
public class Customer extends Persistent implements RemoteCustomer {
// Only one peer is needed to share among customers
static private final CustomerPeer peer = new CustomerPeer();
// A set of all accounts belonging to this customer
private AccountSet accounts = null;
// The customer's first name
private String first_name = null;
// The customer's last name
private String last_name = null;
/**
* Constructs a new Customer.
*/
public Customer() throws RemoteException {
super();
}
/**
* Each customer has one or more accounts stored in an account set.
* This method provides those accounts in an array format.
* @return an array of customer accounts
*/
public RemoteAccount[] getAccounts() {
// getPersistents() is all the synchronization we need
RemotePersistent[] obs = accounts.getPersistents();
RemoteAccount[] accts = new RemoteAccount[obs.length];
// This trick is needed to make a Persistent[] an Account[]
for(int i=0; i<obs.length; i++) {
accts[i] = (RemoteAccount)obs[i];
}
return accts;
}
/**
* @return the AccountSet of accounts for this customer
*/
public synchronized RemoteAccountSet getAccountSet() {
return accounts;
}
/**
* @return the customer's first name
*/
public synchronized String getFirstName() {
return first_name;
}
/**
* The Persistent interface mandates that implementors
* provide a setId(Hashtable) method that pulls the ID out of
* a Hashtable and calls setId(int).
* @param h a Hashtable of values from the data store
* @see imaginary.persist.Persistent#setId
*/
public synchronized void setId(Hashtable h) {
// CustomerPeer defines the key for cust_id in the data store
setId(((Integer)h.get(CustomerPeer.CUST_ID)).intValue());
}
/**
* @return the customer's last name
*/
public synchronized String getLastName() {
return last_name;
}
/**
* @return this object's PersistentPeer
*/
protected synchronized PersistentPeer getPersistentPeer() {
return Customer.peer;
}
/**
* The Customer implementation of the Persistent interface's
* restore() method. This takes a Hashtable of values and assigns
* them to attributes in the Customer object.
* @param trans the Transaction being used for the restore
* @param data the Hashtable of data pulled from the data store
* @exception imaginary.persist.PersistenceException An error occurred
* restoring the Customer
*/
public synchronized void restore(Transaction trans, Hashtable data)
throws PersistenceException {
Hashtable hash = new Hashtable();
/*
* Depending on your data store and table structure, the
* Hashtable may have different keys
*/
// Set the ID
setId(((Integer)data.get(CustomerPeer.CUST_ID)).intValue());
// Set the last name
last_name = (String)data.get(CustomerPeer.LAST_NAME);
// Set the first name
first_name = (String)data.get(CustomerPeer.FIRST_NAME);
// Create an account set
try {
accounts = new AccountSet();
}
catch( RemoteException e ) {
throw new PersistenceException(e);
}
// Create a Hashtable that tells the AccountSet what customer ID
// to use for restoring the accounts
hash.put("cust_id", new Integer(getId()));
// Restore all accounts for this customer
accounts.restore(trans, hash);
}
}
|