1 27 28 29 package com.sun.ebank.ejb.customer; 30 31 import java.sql.*; 32 import javax.sql.*; 33 import java.util.*; 34 import javax.ejb.*; 35 import javax.naming.*; 36 import java.rmi.RemoteException ; 37 import com.sun.ebank.ejb.exception.CustomerNotFoundException; 38 import com.sun.ebank.ejb.exception.InvalidParameterException; 39 import com.sun.ebank.util.Debug; 40 import com.sun.ebank.util.DBHelper; 41 import com.sun.ebank.util.CustomerDetails; 42 import com.sun.ebank.util.EJBGetter; 43 import com.sun.ebank.util.CodedNames; 44 45 46 public class CustomerControllerBean implements SessionBean { 47 private String customerId; 48 private CustomerHome customerHome; 49 private Customer customer; 50 private Connection con; 51 52 public CustomerControllerBean() { 53 } 54 55 public String createCustomer(String lastName, String firstName, 57 String middleInitial, String street, String city, String state, 58 String zip, String phone, String email) 59 throws InvalidParameterException { 60 Debug.print("CustomerControllerBean createCustomer"); 63 64 if (lastName == null) { 65 throw new InvalidParameterException("null lastName"); 66 } 67 68 if (firstName == null) { 69 throw new InvalidParameterException("null firstName"); 70 } 71 72 try { 73 makeConnection(); 74 customerId = DBHelper.getNextCustomerId(con); 75 customer = 76 customerHome.create(customerId, lastName, firstName, 77 middleInitial, street, city, state, zip, phone, email); 78 releaseConnection(); 79 } catch (Exception ex) { 80 releaseConnection(); 81 throw new EJBException("createCustomer: " + ex.getMessage()); 82 } 83 84 return customerId; 85 } 86 88 public void removeCustomer(String customerId) 89 throws CustomerNotFoundException, InvalidParameterException { 90 Debug.print("CustomerControllerBean removeCustomer"); 92 93 if (customerId == null) { 94 throw new InvalidParameterException("null customerId"); 95 } 96 97 if (customerExists(customerId) == false) { 98 throw new CustomerNotFoundException(customerId); 99 } 100 101 try { 102 makeConnection(); 103 deleteAllCustomerInXref(customerId); 104 customer.remove(); 105 releaseConnection(); 106 } catch (Exception ex) { 107 releaseConnection(); 108 throw new EJBException("removeCustomer: " + ex.getMessage()); 109 } 110 } 111 113 public ArrayList getCustomersOfAccount(String accountId) 115 throws CustomerNotFoundException, InvalidParameterException { 116 Debug.print("CustomerControllerBean getCustomersOfAccount"); 119 120 Collection customerIds; 121 122 if (accountId == null) { 123 throw new InvalidParameterException("null accountId"); 124 } 125 126 try { 127 customerIds = customerHome.findByAccountId(accountId); 128 129 if (customerIds.isEmpty()) { 130 throw new CustomerNotFoundException(); 131 } 132 } catch (Exception ex) { 133 throw new CustomerNotFoundException(); 134 } 135 136 ArrayList customerList = new ArrayList(); 137 138 Iterator i = customerIds.iterator(); 139 140 while (i.hasNext()) { 141 Customer customer = (Customer) i.next(); 142 CustomerDetails customerDetail = customer.getDetails(); 143 customerList.add(customerDetail); 144 } 145 146 return customerList; 147 } 148 150 public CustomerDetails getDetails(String customerId) 151 throws CustomerNotFoundException, InvalidParameterException { 152 Debug.print("CustomerControllerBean getDetails"); 154 155 CustomerDetails result; 156 157 if (customerId == null) { 158 throw new InvalidParameterException("null customerId"); 159 } 160 161 if (customerExists(customerId) == false) { 162 throw new CustomerNotFoundException(customerId); 163 } 164 165 result = customer.getDetails(); 166 167 return result; 168 } 169 171 public ArrayList getCustomersOfLastName(String lastName) 172 throws InvalidParameterException { 173 Debug.print("CustomerControllerBean getCustomersOfCustomer"); 177 178 Collection customerIds; 179 ArrayList customerList = new ArrayList(); 180 181 if (lastName == null) { 182 throw new InvalidParameterException("null lastName"); 183 } 184 185 try { 186 customerIds = customerHome.findByLastName(lastName); 187 } catch (Exception ex) { 188 return customerList; 189 } 190 191 Iterator i = customerIds.iterator(); 192 193 while (i.hasNext()) { 194 Customer customer = (Customer) i.next(); 195 CustomerDetails customerDetail = customer.getDetails(); 196 customerList.add(customerDetail); 197 } 198 199 return customerList; 200 } 201 203 public void setName(String lastName, String firstName, 205 String middleInitial, String customerId) 206 throws CustomerNotFoundException, InvalidParameterException { 207 Debug.print("CustomerControllerBean setName"); 208 209 if (lastName == null) { 210 throw new InvalidParameterException("null lastName"); 211 } 212 213 if (firstName == null) { 214 throw new InvalidParameterException("null firstName"); 215 } 216 217 if (customerId == null) { 218 throw new InvalidParameterException("null customerId"); 219 } 220 221 if (customerExists(customerId) == false) { 222 throw new CustomerNotFoundException(customerId); 223 } 224 225 try { 226 customer.setLastName(lastName); 227 customer.setFirstName(firstName); 228 customer.setMiddleInitial(middleInitial); 229 } catch (Exception ex) { 230 throw new EJBException(ex.getMessage()); 231 } 232 } 233 235 public void setAddress(String street, String city, String state, 236 String zip, String phone, String email, String customerId) 237 throws CustomerNotFoundException, InvalidParameterException { 238 Debug.print("CustomerControllerBean setAddress"); 239 240 if (street == null) { 241 throw new InvalidParameterException("null street"); 242 } 243 244 if (city == null) { 245 throw new InvalidParameterException("null city"); 246 } 247 248 if (state == null) { 249 throw new InvalidParameterException("null state"); 250 } 251 252 if (customerId == null) { 253 throw new InvalidParameterException("null customerId"); 254 } 255 256 if (customerExists(customerId) == false) { 257 throw new CustomerNotFoundException(customerId); 258 } 259 260 try { 261 customer.setStreet(street); 262 customer.setCity(city); 263 customer.setState(state); 264 customer.setZip(zip); 265 customer.setPhone(phone); 266 customer.setEmail(email); 267 } catch (Exception ex) { 268 throw new EJBException(ex.getMessage()); 269 } 270 } 271 273 public void ejbCreate() { 275 Debug.print("CustomerControllerBean ejbCreate"); 276 277 try { 278 customerHome = EJBGetter.getCustomerHome(); 279 } catch (Exception ex) { 280 Debug.print("CustomerControllerBean catch"); 281 throw new EJBException("ejbCreate: " + ex.getMessage()); 282 } 283 284 customer = null; 285 customerId = null; 286 } 287 289 public void ejbRemove() { 290 } 291 292 public void ejbActivate() { 293 } 294 295 public void ejbPassivate() { 296 } 297 298 public void setSessionContext(SessionContext sc) { 299 } 300 301 private boolean customerExists(String customerId) { 303 Debug.print("CustomerControllerBean customerExists"); 308 309 if (customerId.equals(this.customerId) == false) { 310 try { 311 customer = customerHome.findByPrimaryKey(customerId); 312 this.customerId = customerId; 313 } catch (Exception ex) { 314 return false; 315 } 316 } 317 319 return true; 320 } 321 323 324 private void makeConnection() { 325 Debug.print("CustomerControllerBean makeConnection"); 326 327 try { 328 InitialContext ic = new InitialContext(); 329 DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE); 330 con = ds.getConnection(); 331 } catch (Exception ex) { 332 throw new EJBException("Unable to connect to database. " + 333 ex.getMessage()); 334 } 335 } 336 338 private void releaseConnection() { 339 Debug.print("CustomerControllerBean releaseConnection"); 340 341 try { 342 con.close(); 343 } catch (SQLException ex) { 344 throw new EJBException("releaseConnection: " + ex.getMessage()); 345 } 346 } 347 349 private void deleteAllCustomerInXref(String customerId) 350 throws SQLException { 351 Debug.print("CustomerControllerBean deleteAllCustomerInXref"); 352 353 String deleteStatement = 354 "delete from customer_account_xref " + "where customer_id = ? "; 355 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 356 357 prepStmt.setString(1, customerId); 358 prepStmt.executeUpdate(); 359 prepStmt.close(); 360 } 361 } 362 | Popular Tags |