001: /*
002: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
003: * Government Rights - Commercial software. Government users are subject
004: * to the Sun Microsystems, Inc. standard license agreement and
005: * applicable provisions of the FAR and its supplements. Use is subject
006: * to license terms.
007: *
008: * This distribution may include materials developed by third parties.
009: * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
010: * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
011: * other countries.
012: *
013: * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
014: *
015: * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
016: * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
017: * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
018: * en vigueur de la FAR (Federal Acquisition Regulations) et des
019: * supplements a celles-ci. Distribue par des licences qui en
020: * restreignent l'utilisation.
021: *
022: * Cette distribution peut comprendre des composants developpes par des
023: * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
024: * sont des marques de fabrique ou des marques deposees de Sun
025: * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
026: */
027:
028: package enroller;
029:
030: import java.sql.*;
031: import javax.sql.*;
032: import java.util.*;
033: import javax.ejb.*;
034: import javax.naming.*;
035: import javax.rmi.PortableRemoteObject;
036:
037: public class CourseBean implements EntityBean, CourseRemoteBusiness {
038: private static final String dbName = "jdbc/pointbase";
039: private String courseId;
040: private String name;
041: private ArrayList studentIds;
042: private Connection con;
043: private EntityContext context;
044: private EnrollerRemoteHome enrollerHome;
045:
046: public ArrayList getStudentIds() {
047: System.out.println("in getStudentIds");
048:
049: return studentIds;
050: }
051:
052: public String getName() {
053: return name;
054: }
055:
056: public void setName(String name) {
057: this .name = name;
058: }
059:
060: public String ejbCreate(String courseId, String name)
061: throws CreateException {
062: System.out.println("in ejbCreate");
063:
064: try {
065: insertCourse(courseId, name);
066: } catch (Exception ex) {
067: throw new EJBException("ejbCreate: " + ex.getMessage());
068: }
069:
070: this .courseId = courseId;
071: this .name = name;
072: System.out.println("about to leave ejbCreate");
073:
074: return courseId;
075: }
076:
077: public String ejbFindByPrimaryKey(String primaryKey)
078: throws FinderException {
079: boolean result;
080:
081: try {
082: result = selectByPrimaryKey(primaryKey);
083: } catch (Exception ex) {
084: throw new EJBException("ejbFindByPrimaryKey: "
085: + ex.getMessage());
086: }
087:
088: if (result) {
089: return primaryKey;
090: } else {
091: throw new ObjectNotFoundException("Row for id "
092: + primaryKey + " not found.");
093: }
094: }
095:
096: public void ejbRemove() {
097: try {
098: deleteCourse(courseId);
099: } 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: /*********************** 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 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: "
190: + ex.getMessage());
191: }
192: }
193:
194: private void insertCourse(String courseId, String name)
195: throws SQLException {
196: makeConnection();
197:
198: String insertStatement = "insert into course values ( ? , ? )";
199: PreparedStatement prepStmt = con
200: .prepareStatement(insertStatement);
201:
202: prepStmt.setString(1, courseId);
203: prepStmt.setString(2, name);
204:
205: prepStmt.executeUpdate();
206: prepStmt.close();
207: releaseConnection();
208: }
209:
210: private boolean selectByPrimaryKey(String primaryKey)
211: throws SQLException {
212: makeConnection();
213:
214: String selectStatement = "select courseid "
215: + "from course where courseid = ? ";
216: PreparedStatement prepStmt = con
217: .prepareStatement(selectStatement);
218:
219: prepStmt.setString(1, primaryKey);
220:
221: ResultSet rs = prepStmt.executeQuery();
222: boolean result = rs.next();
223:
224: prepStmt.close();
225: releaseConnection();
226:
227: return result;
228: }
229:
230: private void deleteCourse(String courseId) throws SQLException {
231: makeConnection();
232:
233: String deleteStatement = "delete from course "
234: + "where courseid = ?";
235: PreparedStatement prepStmt = con
236: .prepareStatement(deleteStatement);
237:
238: prepStmt.setString(1, courseId);
239: prepStmt.executeUpdate();
240: prepStmt.close();
241: releaseConnection();
242: }
243:
244: private void loadCourse() throws SQLException {
245: makeConnection();
246:
247: String selectStatement = "select name "
248: + "from course where courseid = ? ";
249: PreparedStatement prepStmt = con
250: .prepareStatement(selectStatement);
251:
252: prepStmt.setString(1, courseId);
253:
254: ResultSet rs = prepStmt.executeQuery();
255:
256: if (rs.next()) {
257: name = rs.getString(1);
258: prepStmt.close();
259: } else {
260: prepStmt.close();
261: throw new NoSuchEntityException("Row for courseId "
262: + courseId + " not found in database.");
263: }
264:
265: releaseConnection();
266: }
267:
268: private void storeCourse() throws SQLException {
269: makeConnection();
270:
271: String updateStatement = "update course set name = ? "
272: + "where courseid = ?";
273: PreparedStatement prepStmt = con
274: .prepareStatement(updateStatement);
275:
276: prepStmt.setString(1, name);
277: prepStmt.setString(2, courseId);
278:
279: int rowCount = prepStmt.executeUpdate();
280:
281: prepStmt.close();
282:
283: if (rowCount == 0) {
284: throw new EJBException("Storing row for courseId "
285: + courseId + " failed.");
286: }
287:
288: releaseConnection();
289: }
290:
291: private enroller.EnrollerRemoteHome lookupEnrollerBean() {
292: try {
293: javax.naming.Context c = new javax.naming.InitialContext();
294: Object remote = c.lookup("ejb/SimpleEnroller");
295: enroller.EnrollerRemoteHome rv = (enroller.EnrollerRemoteHome) javax.rmi.PortableRemoteObject
296: .narrow(remote, enroller.EnrollerRemoteHome.class);
297: return rv;
298: } catch (javax.naming.NamingException ne) {
299: java.util.logging.Logger.getLogger(getClass().getName())
300: .log(java.util.logging.Level.SEVERE,
301: "exception caught", ne);
302: throw new RuntimeException(ne);
303: }
304:
305: }
306: }
307: // CourseBean
|