1 27 28 29 package com.sun.ebank.ejb.tx; 30 31 import java.sql.*; 32 import javax.sql.*; 33 import java.util.*; 34 import java.math.*; 35 import javax.ejb.*; 36 import javax.naming.*; 37 import com.sun.ebank.ejb.exception.MissingPrimaryKeyException; 38 import com.sun.ebank.util.Debug; 39 import com.sun.ebank.util.TxDetails; 40 import com.sun.ebank.util.CodedNames; 41 import com.sun.ebank.util.DBHelper; 42 43 44 public class TxBean implements EntityBean { 45 private String txId; 46 private String accountId; 47 private java.util.Date timeStamp; 48 private BigDecimal amount; 49 private BigDecimal balance; 50 private String description; 51 private EntityContext context; 52 private Connection con; 53 54 public TxDetails getDetails() { 56 Debug.print("TxBean getDetails"); 57 58 return new TxDetails(txId, accountId, timeStamp, amount, balance, 59 description); 60 } 61 62 public String ejbCreate(String txId, String accountId, 64 java.util.Date timeStamp, BigDecimal amount, BigDecimal balance, 65 String description) throws CreateException, MissingPrimaryKeyException { 66 Debug.print("TxBean ejbCreate"); 67 68 if ((txId == null) || (txId.trim() 69 .length() == 0)) { 70 throw new MissingPrimaryKeyException( 71 "ejbCreate: txId arg is null or empty"); 72 } 73 74 this.txId = txId; 75 this.accountId = accountId; 76 this.timeStamp = timeStamp; 77 this.amount = amount; 78 this.balance = balance; 79 this.description = description; 80 81 try { 82 insertRow(); 83 } catch (Exception ex) { 84 throw new EJBException("ejbCreate: " + ex.getMessage()); 85 } 86 87 return txId; 88 } 89 90 public String ejbFindByPrimaryKey(String primaryKey) 91 throws FinderException { 92 Debug.print("TxBean ejbFindByPrimaryKey"); 93 94 boolean result; 95 96 try { 97 result = selectByPrimaryKey(primaryKey); 98 } catch (Exception ex) { 99 throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); 100 } 101 102 if (result) { 103 return primaryKey; 104 } else { 105 throw new ObjectNotFoundException("Row for id " + primaryKey + 106 " not found."); 107 } 108 } 109 110 public Collection ejbFindByAccountId(java.util.Date startDate, 111 java.util.Date endDate, String accountId) throws FinderException { 112 Debug.print("TxBean ejbFindByAccountId"); 113 114 Collection result; 115 116 try { 117 result = selectByAccountId(accountId, startDate, endDate); 118 } catch (Exception ex) { 119 throw new EJBException("ejbFindByAccountId " + ex.getMessage()); 120 } 121 122 return result; 123 } 124 125 public void ejbRemove() { 126 Debug.print("TxBean ejbRemove"); 127 128 try { 129 deleteRow(txId); 130 } catch (Exception ex) { 131 throw new EJBException("ejbRemove: " + ex.getMessage()); 132 } 133 } 134 135 public void setEntityContext(EntityContext context) { 136 Debug.print("TxBean setEntityContext"); 137 this.context = context; 138 } 139 140 public void unsetEntityContext() { 141 Debug.print("TxBean unsetEntityContext"); 142 } 143 144 public void ejbLoad() { 145 Debug.print("TxBean ejbLoad"); 146 147 try { 148 loadTx(); 149 } catch (Exception ex) { 150 throw new EJBException("ejbLoad: " + ex.getMessage()); 151 } 152 } 153 154 public void ejbStore() { 155 Debug.print("TxBean ejbStore"); 156 157 try { 158 storeTx(); 159 } catch (Exception ex) { 160 throw new EJBException("ejbStore: " + ex.getMessage()); 161 } 162 } 163 164 public void ejbActivate() { 165 Debug.print("TxBean ejbActivate"); 166 txId = (String ) context.getPrimaryKey(); 167 } 168 169 public void ejbPassivate() { 170 Debug.print("TxBean ejbPassivate"); 171 txId = null; 172 } 173 174 public void ejbPostCreate(String txId, String accountId, 175 java.util.Date timeStamp, BigDecimal amount, BigDecimal balance, 176 String description) { 177 } 178 179 180 private void makeConnection() { 181 Debug.print("TxBean makeConnection"); 182 183 try { 184 InitialContext ic = new InitialContext(); 185 DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE); 186 con = ds.getConnection(); 187 } catch (Exception ex) { 188 throw new EJBException("Unable to connect to database. " + 189 ex.getMessage()); 190 } 191 } 192 194 private void releaseConnection() { 195 Debug.print("TxBean releaseConnection"); 196 197 try { 198 con.close(); 199 } catch (SQLException ex) { 200 throw new EJBException("releaseConnection: " + ex.getMessage()); 201 } 202 } 203 205 private void insertRow() throws SQLException { 206 Debug.print("TxBean insertRow"); 207 208 makeConnection(); 209 210 String insertStatement = 211 "insert into tx values ( ? , ? , ? , ? , ? , ? )"; 212 PreparedStatement prepStmt = con.prepareStatement(insertStatement); 213 214 prepStmt.setString(1, txId); 215 prepStmt.setString(2, accountId); 216 prepStmt.setDate(3, DBHelper.toSQLDate(timeStamp)); 217 prepStmt.setBigDecimal(4, amount); 218 prepStmt.setBigDecimal(5, balance); 219 prepStmt.setString(6, description); 220 221 prepStmt.executeUpdate(); 222 prepStmt.close(); 223 releaseConnection(); 224 } 225 226 private void deleteRow(String id) throws SQLException { 227 Debug.print("TxBean deleteRow"); 228 229 makeConnection(); 230 231 String deleteStatement = "delete from tx where tx_id = ? "; 232 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 233 234 prepStmt.setString(1, id); 235 prepStmt.executeUpdate(); 236 prepStmt.close(); 237 releaseConnection(); 238 } 239 240 private boolean selectByPrimaryKey(String primaryKey) 241 throws SQLException { 242 Debug.print("TxBean selectByPrimaryKey"); 243 244 makeConnection(); 245 246 String selectStatement = "select tx_id " + "from tx where tx_id = ? "; 247 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 248 prepStmt.setString(1, primaryKey); 249 250 ResultSet rs = prepStmt.executeQuery(); 251 boolean result = rs.next(); 252 prepStmt.close(); 253 releaseConnection(); 254 255 return result; 256 } 257 258 private Collection selectByAccountId(String accountId, 259 java.util.Date startDate, java.util.Date endDate) 260 throws SQLException { 261 Debug.print("TxBean selectByAccountId"); 262 263 makeConnection(); 264 265 String selectStatement = 266 "select tx_id " + "from tx " + "where (account_id = ?) " + 267 "and ((time_stamp >= ?) and (time_stamp <= ?)) "; 268 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 269 270 prepStmt.setString(1, accountId); 271 prepStmt.setDate(2, DBHelper.toSQLDate(startDate)); 272 prepStmt.setDate(3, DBHelper.toSQLDate(endDate)); 273 274 ResultSet rs = prepStmt.executeQuery(); 275 ArrayList a = new ArrayList(); 276 277 while (rs.next()) { 278 a.add(rs.getString(1)); 279 } 280 281 prepStmt.close(); 282 releaseConnection(); 283 284 return a; 285 } 286 287 private void loadTx() throws SQLException { 288 Debug.print("TxBean loadTx"); 289 290 makeConnection(); 291 292 String selectStatement = 293 "select account_id, time_stamp, amount, balance, description " + 294 "from tx where tx_id = ? "; 295 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 296 297 prepStmt.setString(1, txId); 298 299 ResultSet rs = prepStmt.executeQuery(); 300 301 if (rs.next()) { 302 accountId = rs.getString(1); 303 timeStamp = rs.getDate(2); 304 amount = rs.getBigDecimal(3); 305 balance = rs.getBigDecimal(4); 306 description = rs.getString(5); 307 prepStmt.close(); 308 releaseConnection(); 309 } else { 310 prepStmt.close(); 311 releaseConnection(); 312 throw new NoSuchEntityException("Row for id " + txId + 313 " not found in database."); 314 } 315 } 316 317 private void storeTx() throws SQLException { 318 Debug.print("TxBean storeTx"); 319 320 makeConnection(); 321 322 String updateStatement = 323 "update tx " + "set account_id = ? , time_stamp = ? , " + 324 "amount = ? , balance = ? , description = ? " + 325 "where tx_id = ? "; 326 PreparedStatement prepStmt = con.prepareStatement(updateStatement); 327 328 prepStmt.setString(1, accountId); 329 prepStmt.setDate(2, DBHelper.toSQLDate(timeStamp)); 330 prepStmt.setBigDecimal(3, amount); 331 prepStmt.setBigDecimal(4, balance); 332 prepStmt.setString(5, description); 333 prepStmt.setString(6, txId); 334 335 int rowCount = prepStmt.executeUpdate(); 336 prepStmt.close(); 337 releaseConnection(); 338 339 if (rowCount == 0) { 340 throw new EJBException("Storing row for id " + txId + " failed."); 341 } 342 } 343 } 344 | Popular Tags |