KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > enroller > CourseBean


1 /*
2  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

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 JavaDoc;
36
37
38 public class CourseBean implements EntityBean, CourseRemoteBusiness {
39     private static final String JavaDoc dbName = "jdbc/pointbase";
40     private String JavaDoc courseId;
41     private String JavaDoc 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 JavaDoc getName() {
54         return name;
55     }
56
57     public void setName(String JavaDoc name) {
58         this.name = name;
59     }
60
61     public String JavaDoc ejbCreate(String JavaDoc courseId, String JavaDoc name)
62         throws CreateException {
63         System.out.println("in ejbCreate");
64
65         try {
66             insertCourse(courseId, name);
67         } catch (Exception JavaDoc 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 JavaDoc ejbFindByPrimaryKey(String JavaDoc primaryKey)
79         throws FinderException {
80         boolean result;
81
82         try {
83             result = selectByPrimaryKey(primaryKey);
84         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc ex) {
163             throw new EJBException("ejbStore: " + ex.getMessage());
164         }
165
166         System.out.println("leaving ejbStore");
167     }
168
169     public void ejbPostCreate(String JavaDoc courseId, String JavaDoc name) {
170     }
171
172     /*********************** Database Routines *************************/
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 JavaDoc 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 JavaDoc courseId, String JavaDoc name)
194         throws SQLException {
195         makeConnection();
196
197         String JavaDoc 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 JavaDoc primaryKey)
209         throws SQLException {
210         makeConnection();
211
212         String JavaDoc 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 JavaDoc courseId) throws SQLException {
228         makeConnection();
229
230         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc c = new javax.naming.InitialContext JavaDoc();
287             Object JavaDoc 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 JavaDoc ne) {
292             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
293             throw new RuntimeException JavaDoc(ne);
294         }
295      
296     }
297 }
298  // CourseBean
299
Popular Tags