KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > cmp > transaction > RegistrationSessionBean


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 /**
10  * Implementation Class for the RegistrationSession bean.
11  */

12 public class RegistrationSessionBean extends AbstractSessionBean
13   implements SessionSynchronization {
14
15   /**
16    * Home Interface for the Course CMP bean.
17    */

18   private CourseHome courseHome = null;
19
20   /**
21    * Home Interface for the Student CMP bean.
22    */

23   private StudentHome studentHome = null;
24
25   /**
26    * The calling Principal.
27    */

28   private Student student;
29
30   /**
31    * True for the finalizing method.
32    */

33   private boolean isCompleting;
34   /**
35    * True when the registration has completed and mail has been sent.
36    */

37   private boolean isComplete;
38
39   /**
40    * Currently selected Courses. Note that these are in-memory only and will
41    * not be committed to the database until <code>finalizeRegistration</code>
42    * is called.
43    */

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   /**
50    * Standard Constructor.
51    */

52   public RegistrationSessionBean()
53   {
54   }
55
56   /**
57    * Tries to resolve the calling <code<Principal</code>.
58    */

59   public void ejbCreate()
60     throws CreateException
61   {
62     try {
63       // The JNDI context containing EJBs
64
Context ejb = (Context) new InitialContext().lookup("java:comp/env/cmp");
65
66       // get the bean stubs
67
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   /**
81    * Called by Resin-CMP after a new transaction has begun -- required by the
82    * SessionSynchronization interface.
83    */

84   public void afterBegin()
85     throws EJBException
86   {
87   }
88
89   /**
90    * Called by Resin-CMP just before a transaction is committed --
91    * required by the SessionSynchronization interface.
92    *
93    * <p>This call might update the database or do some validation.
94    * Because the transaction might still be rolled back, it should be
95    * possible to roll back any operatin in the before completion.
96    */

97   public void beforeCompletion()
98     throws EJBException
99   {
100   }
101
102   /**
103    * Called by Resin-CMP when a transaction has completed -- required by the
104    * SessionSynchronization interface.
105    *
106    * <p>In order to keep the transaction Atomic, Consistent, Independent, and
107    * Durable, we do nothing in this method unless the transaction has been
108    * committed.
109    *
110    * <p>If the transaction was rolled back, we will treat it as if it never
111    * happened.
112    */

113   public void afterCompletion(boolean committed)
114     throws EJBException
115   {
116     if (committed && isCompleting) {
117       // Normally, an application would do something like send a
118
// confirmation email. Here we'll just set a flag.
119
isComplete = true;
120     }
121
122     isCompleting = false;
123   }
124
125   /**
126    * Returns the name of the Student who is selecting Courses.
127    */

128   public String getStudentName()
129   {
130     if (student == null)
131       return "Anonymous";
132     else
133       return student.getName();
134   }
135
136   /**
137    * Returns a <code>Collection</code> of Courses that the now registering
138    * Student is currently enrolled in.
139    */

140   public Collection getEnrolledCourses()
141   {
142     if (student == null)
143       return new ArrayList();
144     else
145       return student.getCourseList();
146   }
147
148   /**
149    * Adds a course to the set of selected courses for this session. Note that
150    * this method does not commit data to the database.
151    */

152   public void addCourse(Course course)
153     throws FinderException
154   {
155     if (! selectedCourses.contains(course))
156       selectedCourses.add(course);
157   }
158
159   /**
160    * Deletes a course from the set of selected courses for this session.
161    */

162   public void removeCourse(Course course)
163     throws FinderException
164   {
165     if (selectedCourses.contains(course))
166       selectedCourses.remove(course);
167   }
168
169   /**
170    * Returns a <code>Collection</code> of all Courses offered
171    * (including those that the student has already selected).
172    */

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   /**
184    * Returns a <code>Collection</code> of all courses currently selected.
185    */

186   public Collection getSelectedCourses() {
187     return this.selectedCourses;
188   }
189
190   /**
191    * Returns true if the registration is complete.
192    */

193   public boolean isComplete()
194   {
195     return isComplete;
196   }
197
198   /**
199    * Executes a transaction that will commit the selected courses to the
200    * persistant store unless an error occurs.
201    */

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