1 package example.cmp.transaction; 2 3 import javax.ejb.*; 4 import javax.naming.*; 5 import java.util.*; 6 7 import com.caucho.ejb.AbstractSessionBean; 8 9 12 public class RegistrationSessionBean extends AbstractSessionBean 13 implements SessionSynchronization { 14 15 18 private CourseHome courseHome = null; 19 20 23 private StudentHome studentHome = null; 24 25 28 private Student student; 29 30 33 private boolean isCompleting; 34 37 private boolean isComplete; 38 39 44 private Collection selectedCourses = new ArrayList(20); 45 46 public static final int TRANSACTION_COMMITTED = 0; 47 public static final int TRANSACTION_ROLLEDBACK = 1; 48 49 52 public RegistrationSessionBean() 53 { 54 } 55 56 59 public void ejbCreate() 60 throws CreateException 61 { 62 try { 63 Context ejb = (Context) new InitialContext().lookup("java:comp/env/cmp"); 65 66 courseHome = (CourseHome) ejb.lookup("transaction_course"); 68 studentHome = (StudentHome) ejb.lookup("transaction_student"); 69 } catch (Exception e) { 70 throw new CreateException("can't initialize home interfaces\n" + e); 71 } 72 73 student = (Student) getSessionContext().getCallerPrincipal(); 74 if (student == null) 75 throw new CreateException("Cannot create " + getClass().getName() + 76 ": caller is not authenticated. You need to authenticate yourself " + 77 "through Resin's security framework." ); 78 } 79 80 84 public void afterBegin() 85 throws EJBException 86 { 87 } 88 89 97 public void beforeCompletion() 98 throws EJBException 99 { 100 } 101 102 113 public void afterCompletion(boolean committed) 114 throws EJBException 115 { 116 if (committed && isCompleting) { 117 isComplete = true; 120 } 121 122 isCompleting = false; 123 } 124 125 128 public String getStudentName() 129 { 130 if (student == null) 131 return "Anonymous"; 132 else 133 return student.getName(); 134 } 135 136 140 public Collection getEnrolledCourses() 141 { 142 if (student == null) 143 return new ArrayList(); 144 else 145 return student.getCourseList(); 146 } 147 148 152 public void addCourse(Course course) 153 throws FinderException 154 { 155 if (! selectedCourses.contains(course)) 156 selectedCourses.add(course); 157 } 158 159 162 public void removeCourse(Course course) 163 throws FinderException 164 { 165 if (selectedCourses.contains(course)) 166 selectedCourses.remove(course); 167 } 168 169 173 public Collection getAvailableCourses() 174 { 175 Collection availableCourses = null; 176 try { 177 availableCourses = courseHome.findAll(); 178 } catch(FinderException e) { 179 } 180 return availableCourses; 181 } 182 183 186 public Collection getSelectedCourses() { 187 return this.selectedCourses; 188 } 189 190 193 public boolean isComplete() 194 { 195 return isComplete; 196 } 197 198 202 public void finalizeRegistration() 203 throws RegistrationDeniedException 204 { 205 student.getCourseList().clear(); 206 207 isCompleting = true; 208 209 Iterator iter = selectedCourses.iterator(); 210 while (iter.hasNext()) { 211 Course course = (Course)iter.next(); 212 if (course.isFull()) { 213 getSessionContext().setRollbackOnly(); 214 throw new RegistrationDeniedException ( 215 course.getName() + " is full. Please drop this class and try again."); 216 } 217 else if (! student.getCourseList().contains(course)) 218 student.getCourseList().add(course); 219 } 220 } 221 } 222 | Popular Tags |