Package bank

Source Code of bank.BankApplicationImpl

package bank;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;

/**
* @author S.Chassande-Barrioz
*/
public class BankApplicationImpl implements SessionBean {

    protected SessionContext sessionContext = null;

    public static PersistenceManagerFactory pmf = null;

    private static Bank bank = null;

    private static final String SOCIETE_GENERALE = "SG";

    private static final String PMF_CTX_NAME = "java:comp/env/jdo/pmf";

    public static Bank getBank(PersistenceManager pm) {
        if (bank != null) {
            return bank;
        }
        synchronized (BankApplicationImpl.class) {
            if (bank != null) {
                return bank;
            }
            Query q = pm.newQuery(Bank.class);
            q.declareParameters("String bn");
            q.setFilter("(name==bn)");
            Collection c = (Collection) q.execute(SOCIETE_GENERALE);
            if (c.isEmpty()) {
                //First execution: create the bank
                bank = new Bank(SOCIETE_GENERALE);
                pm.makePersistent(bank);
            } else {
                //Load from the persistence support
                bank = (Bank) c.iterator().next();
            }
            q.closeAll();
        }
        return bank;
    }

    // IMPLEMENTATION OF THE BankApplication INTERFACE //
    //-------------------------------------------------//

    public void createAgency(String name) throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        getBank(pm).createAgency(name);
        pm.close();
    }

    public List getAgencies() throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        Iterator it = getBank(pm).agencies.iterator();
        ArrayList res = new ArrayList(bank.agencies.size());
        while (it.hasNext()) {
            res.add(((Agency) it.next()).name);
        }
        pm.close();
        return res;
    }

    public boolean removeAgency(String name) throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        Iterator it = getBank(pm).agencies.iterator();
        while (it.hasNext()) {
            Agency a = (Agency) it.next();
            if (a.name.equals(name) && a.clients.isEmpty()) {
                bank.agencies.remove(a);
                pm.deletePersistent(a);
                pm.close();
                return true;
            }
        }
        pm.close();
        return false;
    }

    public void createClient(ClientId cid, String agencyName)
            throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        Agency a = getBank(pm).getAgency(agencyName);
        if (a == null) {
            pm.close();
            throw new RemoteException("No agency '" + agencyName + "' found.");
        }
        a.createClient(cid.firstName, cid.lastName, cid.address);
        pm.close();
    }

    public List getClients(String agencyName) throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        Agency a = getBank(pm).getAgency(agencyName);
        if (a == null) {
            pm.close();
            throw new RemoteException("No agency '" + agencyName + "' found.");
        }
        Iterator it = a.clients.iterator();
        ArrayList res = new ArrayList(a.clients.size());
        while (it.hasNext()) {
            res.add(new ClientId((Client) it.next()));
        }
        pm.close();
        return res;
    }

    public boolean removeClient(String agencyName, ClientId cid)
            throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        Agency a = getBank(pm).getAgency(agencyName);
        if (a == null) {
            pm.close();
            throw new RemoteException("No agency '" + agencyName + "' found.");
        }
        Client c = a.getClient(cid.firstName, cid.lastName, cid.address);
        boolean remove = c != null && c.accounts.isEmpty();
        if (remove) {
            a.removeClient(c);
            pm.deletePersistent(c);
        }
        pm.close();
        return remove;
    }

    public String createAccount(ClientId cid, String agencyName)
            throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        Agency a = getBank(pm).getAgency(agencyName);
        if (a == null) {
            pm.close();
            throw new RemoteException("No agency '" + agencyName + "' found.");
        }
        Client c = a.getClient(cid.firstName, cid.lastName, cid.address);
        if (c == null) {
            pm.close();
            throw new RemoteException("No client '" + cid + "' found.");
        }
        String number = c.createAccount(a.getNextAccountNumber()).number;
        pm.close();
        return number;
    }

    public AccountInfo getAccountInfo(String number, ClientId cid,
            String agencyName) throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        Agency a = getBank(pm).getAgency(agencyName);
        if (a == null) {
            pm.close();
            throw new RemoteException("No agency '" + agencyName + "' found.");
        }
        Client c = a.getClient(cid.firstName, cid.lastName, cid.address);
        if (c == null) {
            pm.close();
            throw new RemoteException("No client '" + cid + "' found.");
        }
        Iterator it = c.accounts.iterator();
        while (it.hasNext()) {
            Account ac = (Account) it.next();
            if (ac.number.equals(number)) {
                return new AccountInfo(ac.number, ac.solde);
            }
        }
        pm.close();
        return null;
    }

    public List getAccounts(String agencyName, ClientId cid)
            throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        Agency a = getBank(pm).getAgency(agencyName);
        if (a == null) {
            pm.close();
            throw new RemoteException("No agency '" + agencyName + "' found.");
        }
        Client c = a.getClient(cid.firstName, cid.lastName, cid.address);
        if (c == null) {
            pm.close();
            throw new RemoteException("No client '" + cid + "' found.");
        }
        Iterator it = c.accounts.iterator();
        ArrayList res = new ArrayList(c.accounts.size());
        while (it.hasNext()) {
            Account ac = (Account) it.next();
            res.add(new AccountInfo(ac.number, ac.solde));
        }
        pm.close();
        return res;
    }

    public AccountInfo closeAccount(String agencyName, ClientId cid,
            String accountNumber) throws RemoteException {
        PersistenceManager pm = pmf.getPersistenceManager();
        Agency a = getBank(pm).getAgency(agencyName);
        if (a == null) {
            pm.close();
            throw new RemoteException("No agency '" + agencyName + "' found.");
        }
        Client c = a.getClient(cid.firstName, cid.lastName, cid.address);
        if (c == null) {
            pm.close();
            throw new RemoteException("No client '" + cid + "' found.");
        }
        Iterator it = c.accounts.iterator();
        while (it.hasNext()) {
            Account ac = (Account) it.next();
            if (ac.number.equals(accountNumber)) {
                c.removeAccount(ac);
                pm.deletePersistent(ac);
                AccountInfo ai = new AccountInfo(ac.number, ac.solde);
                pm.close();
                return ai;
            }
        }
        pm.close();
        return null;
    }

    // IMPLEMENTATION OF THE SessionBean INTERFACE //
    //---------------------------------------------//

    public void setSessionContext(SessionContext sessionContext)
            throws EJBException, RemoteException {
        if (pmf == null) {
            synchronized (BankApplicationImpl.class) {
                if (pmf == null) {
                    try {
                        pmf = (PersistenceManagerFactory) new InitialContext()
                                .lookup(PMF_CTX_NAME);
                    } catch (NamingException e) {
                        throw new EJBException(
                                "No PersistenceMangerFactory registered"
                                        + " in JNDI with the name '"
                                        + PMF_CTX_NAME + "'");
                    }
                }
            }
        }
    }

    public void ejbCreate() throws RemoteException {
    }

    public void ejbRemove() throws EJBException, RemoteException {
    }

    public void ejbActivate() throws EJBException, RemoteException {
    }

    public void ejbPassivate() throws EJBException, RemoteException {
    }
}
TOP

Related Classes of bank.BankApplicationImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.