1 27 28 package enroller; 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 CourseBean implements EntityBean, CourseRemoteBusiness { 39 private static final String dbName = "jdbc/pointbase"; 40 private String courseId; 41 private String name; 42 private ArrayList studentIds; 43 private Connection con; 44 private EntityContext context; 45 private EnrollerRemoteHome enrollerHome; 46 47 public ArrayList getStudentIds() { 48 System.out.println("in getStudentIds"); 49 50 return studentIds; 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 courseId, String name) 62 throws CreateException { 63 System.out.println("in ejbCreate"); 64 65 try { 66 insertCourse(courseId, name); 67 } catch (Exception ex) { 68 throw new EJBException("ejbCreate: " + ex.getMessage()); 69 } 70 71 this.courseId = courseId; 72 this.name = name; 73 System.out.println("about to leave ejbCreate"); 74 75 return courseId; 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 deleteCourse(courseId); 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 studentIds = new ArrayList(); 108 109 try { 110 enrollerHome = lookupEnrollerBean(); 111 } catch (Exception ex) { 112 throw new EJBException("Unable to connect to database. " + 113 ex.getMessage()); 114 } 115 } 116 117 public void unsetEntityContext() { 118 } 119 120 public void ejbActivate() { 121 courseId = (String ) context.getPrimaryKey(); 122 } 123 124 public void ejbPassivate() { 125 courseId = null; 126 } 127 128 public void ejbLoad() { 129 System.out.println("in ejbLoad"); 130 131 try { 132 loadCourse(); 133 loadStudentIds(); 134 } catch (Exception ex) { 135 throw new EJBException("ejbLoad: " + ex.getMessage()); 136 } 137 138 System.out.println("leaving ejbLoad"); 139 } 140 141 private void loadStudentIds() { 142 System.out.println("in loadStudentIds"); 143 studentIds.clear(); 144 145 try { 146 EnrollerRemote enroller = enrollerHome.create(); 147 ArrayList a = enroller.getStudentIds(courseId); 148 149 studentIds.addAll(a); 150 } catch (Exception ex) { 151 throw new EJBException("loadStudentIds: " + ex.getMessage()); 152 } 153 154 System.out.println("leaving loadStudentIds"); 155 } 156 157 public void ejbStore() { 158 System.out.println("in ejbStore"); 159 160 try { 161 storeCourse(); 162 } catch (Exception ex) { 163 throw new EJBException("ejbStore: " + ex.getMessage()); 164 } 165 166 System.out.println("leaving ejbStore"); 167 } 168 169 public void ejbPostCreate(String courseId, String name) { 170 } 171 172 173 private void makeConnection() { 174 try { 175 InitialContext ic = new InitialContext(); 176 DataSource ds = (DataSource) ic.lookup(dbName); 177 178 con = ds.getConnection(); 179 } catch (Exception ex) { 180 throw new EJBException("Unable to connect to database. " + 181 ex.getMessage()); 182 } 183 } 184 185 private void releaseConnection() { 186 try { 187 con.close(); 188 } catch (SQLException ex) { 189 throw new EJBException("releaseConnection: " + ex.getMessage()); 190 } 191 } 192 193 private void insertCourse(String courseId, String name) 194 throws SQLException { 195 makeConnection(); 196 197 String insertStatement = "insert into course values ( ? , ? )"; 198 PreparedStatement prepStmt = con.prepareStatement(insertStatement); 199 200 prepStmt.setString(1, courseId); 201 prepStmt.setString(2, name); 202 203 prepStmt.executeUpdate(); 204 prepStmt.close(); 205 releaseConnection(); 206 } 207 208 private boolean selectByPrimaryKey(String primaryKey) 209 throws SQLException { 210 makeConnection(); 211 212 String selectStatement = 213 "select courseid " + "from course where courseid = ? "; 214 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 215 216 prepStmt.setString(1, primaryKey); 217 218 ResultSet rs = prepStmt.executeQuery(); 219 boolean result = rs.next(); 220 221 prepStmt.close(); 222 releaseConnection(); 223 224 return result; 225 } 226 227 private void deleteCourse(String courseId) throws SQLException { 228 makeConnection(); 229 230 String deleteStatement = "delete from course " + "where courseid = ?"; 231 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 232 233 prepStmt.setString(1, courseId); 234 prepStmt.executeUpdate(); 235 prepStmt.close(); 236 releaseConnection(); 237 } 238 239 private void loadCourse() throws SQLException { 240 makeConnection(); 241 242 String selectStatement = 243 "select name " + "from course where courseid = ? "; 244 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 245 246 prepStmt.setString(1, courseId); 247 248 ResultSet rs = prepStmt.executeQuery(); 249 250 if (rs.next()) { 251 name = rs.getString(1); 252 prepStmt.close(); 253 } else { 254 prepStmt.close(); 255 throw new NoSuchEntityException("Row for courseId " + courseId + 256 " not found in database."); 257 } 258 259 releaseConnection(); 260 } 261 262 private void storeCourse() throws SQLException { 263 makeConnection(); 264 265 String updateStatement = 266 "update course set name = ? " + "where courseid = ?"; 267 PreparedStatement prepStmt = con.prepareStatement(updateStatement); 268 269 prepStmt.setString(1, name); 270 prepStmt.setString(2, courseId); 271 272 int rowCount = prepStmt.executeUpdate(); 273 274 prepStmt.close(); 275 276 if (rowCount == 0) { 277 throw new EJBException("Storing row for courseId " + courseId + 278 " failed."); 279 } 280 281 releaseConnection(); 282 } 283 284 private enroller.EnrollerRemoteHome lookupEnrollerBean() { 285 try { 286 javax.naming.Context c = new javax.naming.InitialContext (); 287 Object remote = c.lookup("ejb/SimpleEnroller"); 288 enroller.EnrollerRemoteHome rv = (enroller.EnrollerRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, enroller.EnrollerRemoteHome.class); 289 return rv; 290 } 291 catch(javax.naming.NamingException ne) { 292 java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne); 293 throw new RuntimeException (ne); 294 } 295 296 } 297 } 298 | Popular Tags |