1 package ejava.examples.ejbsessionbank.jpa; 2 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 7 import javax.persistence.EntityManager; 8 import javax.persistence.Query; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import ejava.examples.ejbsessionbank.bo.Account; 14 import ejava.examples.ejbsessionbank.bo.Ledger; 15 import ejava.examples.ejbsessionbank.dao.AccountDAO; 16 import ejava.examples.ejbsessionbank.dao.DAOException; 17 18 public class JPAAccountDAO implements AccountDAO { 19 private static Log log = LogFactory.getLog(JPAAccountDAO.class); 20 public static final String ACCOUNT_BY_NUM = "getAccountsByAccountNumber"; 21 public static final String ACCOUNT_NUM_PARAM = "accountNumber"; 22 public static final String GET_LEDGER = "getLedger"; 23 public static final String GET_LEDGER_AVE = "getLedgerAveBalance"; 24 public static final String GET_LEDGER_CNT = "getLedgerCount"; 25 public static final String GET_LEDGER_SUM = "getLedgerSum"; 26 27 private EntityManager em; 28 29 public void setEntityManager(EntityManager em) { 30 this.em = em; 31 } 32 33 public Account createAccount(Account account) throws DAOException { 34 try { 35 em.persist(account); 36 return account; 37 } 38 catch (Throwable ex) { 39 log.fatal("error in createAccount", ex); 40 throw new DAOException( 41 "error creating account:" + account,ex); 42 } 43 } 44 45 @SuppressWarnings("unchecked") 46 public List<Account> findAccounts(String queryName, 47 Map<String, Object> params, int index, int count) 48 throws DAOException { 49 try { 50 Query query = 51 em.createNamedQuery(queryName) 52 .setFirstResult(index) 53 .setMaxResults(count); 54 if (params != null && params.size() != 0) { 55 for(String key: params.keySet()) { 56 query.setParameter(key, params.get(key)); 57 } 58 } 59 log.debug("named query:" + queryName + ", params=" + params); 60 return query.getResultList(); 61 } 62 catch (Throwable ex) { 63 log.fatal("error in findAccounts", ex); 64 throw new DAOException( 65 "error executing named query:" + queryName,ex); 66 } 67 } 68 69 public Account getAccountById(long id) throws DAOException { 70 try { 71 return em.find(Account.class, id); 72 } 73 catch (Throwable ex) { 74 log.fatal("error in getAccountById", ex); 75 throw new DAOException( 76 "error finding account:" + id,ex); 77 } 78 } 79 80 public Account getAccountByNum(String acctNum) throws DAOException { 81 Map<String, Object> params = new HashMap<String, Object>(); 82 params.put(ACCOUNT_NUM_PARAM, acctNum); 83 List<Account> accounts = findAccounts(ACCOUNT_BY_NUM, params, 0, 1); 84 return (accounts.size() == 1) ? accounts.get(0) : null; 85 } 86 87 public Account removeAccount(Account account) throws DAOException { 88 try { 89 if (!em.contains(account)) { 90 account = em.find(Account.class, account.getId()); 91 } 92 if (account != null) { 93 em.remove(account); 94 } 95 return account; 96 } 97 catch (Throwable ex) { 98 log.fatal("error in removeAccount", ex); 99 throw new DAOException( 100 "error removing account:" + account,ex); 101 } 102 } 103 104 public Account updateAccount(Account account) throws DAOException { 105 try { 106 return em.merge(account); 107 } 108 catch (Throwable ex) { 109 log.fatal("error in updateAccount", ex); 110 throw new DAOException( 111 "error removing account:" + account,ex); 112 } 113 } 114 115 public Ledger getLedger() throws DAOException { 116 try { 117 return (Ledger) em.createNamedQuery(GET_LEDGER) 118 .getSingleResult(); 119 } 120 catch (Throwable ex) { 121 log.fatal("error in getLedger", ex); 122 throw new DAOException( 123 "error getting ledger",ex); 124 } 125 } 126 127 public double getLedgerAveBalance() throws DAOException { 128 try { 129 return (Double) em.createNamedQuery(GET_LEDGER_AVE) 130 .getSingleResult(); 131 } 132 catch (Throwable ex) { 133 log.fatal("error in getLedgerBalance", ex); 134 throw new DAOException( 135 "error getting ledger ave balance",ex); 136 } 137 } 138 139 public long getLedgerCount() throws DAOException { 140 try { 141 Object count = em.createNamedQuery(GET_LEDGER_CNT) 142 .getSingleResult(); 143 log.fatal("getLedgerCount data type=" + count.getClass()); 144 return ((Long)count).longValue(); 145 } 146 catch (Throwable ex) { 147 log.fatal("error in getLedgerCount", ex); 148 throw new DAOException( 149 "error getting ledger count",ex); 150 } 151 } 152 153 public double getLedgerSum() throws DAOException { 154 try { 155 return (Double) em.createNamedQuery(GET_LEDGER_SUM) 156 .getSingleResult(); 157 } 158 catch (Throwable ex) { 159 log.fatal("error in getLedgerSum", ex); 160 throw new DAOException( 161 "error getting ledger sum",ex); 162 } 163 } 164 }