1 27 28 package salesrep; 29 30 import java.sql.*; 31 import javax.sql.*; 32 import java.util.*; 33 import javax.ejb.*; 34 import javax.naming.*; 35 import javax.rmi.PortableRemoteObject ; 36 37 38 public class SalesRepBean implements EntityBean { 39 private static final String dbName = "java:comp/env/jdbc/SalesDB"; 40 private String salesRepId; 41 private String name; 42 private ArrayList customerIds; 43 private Connection con; 44 private EntityContext context; 45 private CustomerRemoteHome customerHome; 46 47 public ArrayList getCustomerIds() { 48 System.out.println("in getCustomerIds"); 49 50 return customerIds; 51 } 52 53 public String getName() { 54 return name; 55 } 56 57 public void setName(String name) { 58 this.name = name; 59 } 60 61 public String ejbCreate(String salesRepId, String name) 62 throws CreateException { 63 System.out.println("in ejbCreate"); 64 65 try { 66 insertSalesRep(salesRepId, name); 67 } catch (Exception ex) { 68 throw new EJBException("ejbCreate: " + ex.getMessage()); 69 } 70 71 this.salesRepId = salesRepId; 72 this.name = name; 73 System.out.println("about to leave ejbCreate"); 74 75 return salesRepId; 76 } 77 78 public String ejbFindByPrimaryKey(String primaryKey) 79 throws FinderException { 80 boolean result; 81 82 try { 83 result = selectByPrimaryKey(primaryKey); 84 } catch (Exception ex) { 85 throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); 86 } 87 88 if (result) { 89 return primaryKey; 90 } else { 91 throw new ObjectNotFoundException("Row for id " + primaryKey + 92 " not found."); 93 } 94 } 95 96 public void ejbRemove() { 97 try { 98 deleteSalesRep(salesRepId); 99 } catch (Exception ex) { 100 throw new EJBException("ejbRemove: " + ex.getMessage()); 101 } 102 } 103 104 public void setEntityContext(EntityContext context) { 105 System.out.println("in setEntityContext"); 106 this.context = context; 107 customerIds = new ArrayList(); 108 109 try { 110 Context initial = new InitialContext(); 111 Object objref = initial.lookup("java:comp/env/ejb/Customer"); 112 113 customerHome = 114 (CustomerRemoteHome) PortableRemoteObject.narrow(objref, 115 CustomerRemoteHome.class); 116 } catch (Exception ex) { 117 throw new EJBException("setEntityContext: " + ex.getMessage()); 118 } 119 120 System.out.println("leaving setEntityContext"); 121 } 122 123 public void unsetEntityContext() { 124 } 125 126 public void ejbActivate() { 127 salesRepId = (String ) context.getPrimaryKey(); 128 } 129 130 public void ejbPassivate() { 131 salesRepId = null; 132 } 133 134 public void ejbLoad() { 135 System.out.println("in ejbLoad"); 136 137 try { 138 loadSalesRep(); 139 loadCustomerIds(); 140 } catch (Exception ex) { 141 throw new EJBException("ejbLoad: " + ex.getMessage()); 142 } 143 144 System.out.println("leaving ejbLoad"); 145 } 146 147 private void loadCustomerIds() { 148 System.out.println("in loadCustomerIds"); 149 customerIds.clear(); 150 151 try { 152 Collection c = customerHome.findBySalesRep(salesRepId); 153 Iterator i = c.iterator(); 154 155 while (i.hasNext()) { 156 CustomerRemote customer = (CustomerRemote) i.next(); 157 String id = (String ) customer.getPrimaryKey(); 158 159 System.out.println("adding " + id + " to list"); 160 customerIds.add(id); 161 } 162 } catch (Exception ex) { 163 throw new EJBException("Exception in loadCustomerIds: " + 164 ex.getMessage()); 165 } 166 } 167 168 public void ejbStore() { 169 System.out.println("in ejbStore"); 170 171 try { 172 storeSalesRep(); 173 } catch (Exception ex) { 174 throw new EJBException("ejbStore: " + ex.getMessage()); 175 } 176 177 System.out.println("leaving ejbStore"); 178 } 179 180 public void ejbPostCreate(String salesRepId, String name) { 181 } 182 183 184 private void makeConnection() { 185 try { 186 InitialContext ic = new InitialContext(); 187 DataSource ds = (DataSource) ic.lookup(dbName); 188 189 con = ds.getConnection(); 190 } catch (Exception ex) { 191 throw new EJBException("Unable to connect to database. " + 192 ex.getMessage()); 193 } 194 } 195 196 private void releaseConnection() { 197 try { 198 con.close(); 199 } catch (SQLException ex) { 200 throw new EJBException("releaseConnection: " + ex.getMessage()); 201 } 202 } 203 204 private void insertSalesRep(String salesRepId, String name) 205 throws SQLException { 206 makeConnection(); 207 System.out.println("in insertSalesRep"); 208 209 String insertStatement = "insert into salesrep values ( ? , ? )"; 210 PreparedStatement prepStmt = con.prepareStatement(insertStatement); 211 212 prepStmt.setString(1, salesRepId); 213 prepStmt.setString(2, name); 214 215 prepStmt.executeUpdate(); 216 prepStmt.close(); 217 System.out.println("leaving insertSalesRep"); 218 releaseConnection(); 219 } 220 221 private boolean selectByPrimaryKey(String primaryKey) 222 throws SQLException { 223 makeConnection(); 224 225 String selectStatement = 226 "select salesrepid " + "from salesrep where salesrepid = ? "; 227 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 228 229 prepStmt.setString(1, primaryKey); 230 231 ResultSet rs = prepStmt.executeQuery(); 232 boolean result = rs.next(); 233 234 prepStmt.close(); 235 releaseConnection(); 236 237 return result; 238 } 239 240 private void deleteSalesRep(String salesRepId) throws SQLException { 241 makeConnection(); 242 243 String deleteStatement = 244 "delete from salesrep " + "where salesrepid = ?"; 245 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 246 247 prepStmt.setString(1, salesRepId); 248 prepStmt.executeUpdate(); 249 prepStmt.close(); 250 releaseConnection(); 251 } 252 253 private void loadSalesRep() throws SQLException { 254 makeConnection(); 255 256 String selectStatement = 257 "select name " + "from salesRep where salesrepid = ? "; 258 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 259 260 prepStmt.setString(1, salesRepId); 261 262 ResultSet rs = prepStmt.executeQuery(); 263 264 if (rs.next()) { 265 name = rs.getString(1); 266 prepStmt.close(); 267 } else { 268 prepStmt.close(); 269 throw new NoSuchEntityException("Row for salesRepId " + salesRepId + 270 " not found in database."); 271 } 272 273 releaseConnection(); 274 } 275 276 private void storeSalesRep() throws SQLException { 277 makeConnection(); 278 279 String updateStatement = 280 "update salesrep set name = ? " + "where salesrepid = ?"; 281 PreparedStatement prepStmt = con.prepareStatement(updateStatement); 282 283 prepStmt.setString(1, name); 284 prepStmt.setString(2, salesRepId); 285 286 int rowCount = prepStmt.executeUpdate(); 287 288 prepStmt.close(); 289 290 if (rowCount == 0) { 291 throw new EJBException("Storing row for salesRepId " + salesRepId + 292 " failed."); 293 } 294 295 releaseConnection(); 296 } 297 } 298
| Popular Tags
|