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 salesrep;
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 SalesRepBean implements EntityBean {
038: private static final String dbName = "java:comp/env/jdbc/SalesDB";
039: private String salesRepId;
040: private String name;
041: private ArrayList customerIds;
042: private Connection con;
043: private EntityContext context;
044: private CustomerRemoteHome customerHome;
045:
046: public ArrayList getCustomerIds() {
047: System.out.println("in getCustomerIds");
048:
049: return customerIds;
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 salesRepId, String name)
061: throws CreateException {
062: System.out.println("in ejbCreate");
063:
064: try {
065: insertSalesRep(salesRepId, name);
066: } catch (Exception ex) {
067: throw new EJBException("ejbCreate: " + ex.getMessage());
068: }
069:
070: this .salesRepId = salesRepId;
071: this .name = name;
072: System.out.println("about to leave ejbCreate");
073:
074: return salesRepId;
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: deleteSalesRep(salesRepId);
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: customerIds = new ArrayList();
108:
109: try {
110: Context initial = new InitialContext();
111: Object objref = initial
112: .lookup("java:comp/env/ejb/Customer");
113:
114: customerHome = (CustomerRemoteHome) PortableRemoteObject
115: .narrow(objref, CustomerRemoteHome.class);
116: } catch (Exception ex) {
117: throw new EJBException("setEntityContext: "
118: + ex.getMessage());
119: }
120:
121: System.out.println("leaving setEntityContext");
122: }
123:
124: public void unsetEntityContext() {
125: }
126:
127: public void ejbActivate() {
128: salesRepId = (String) context.getPrimaryKey();
129: }
130:
131: public void ejbPassivate() {
132: salesRepId = null;
133: }
134:
135: public void ejbLoad() {
136: System.out.println("in ejbLoad");
137:
138: try {
139: loadSalesRep();
140: loadCustomerIds();
141: } catch (Exception ex) {
142: throw new EJBException("ejbLoad: " + ex.getMessage());
143: }
144:
145: System.out.println("leaving ejbLoad");
146: }
147:
148: private void loadCustomerIds() {
149: System.out.println("in loadCustomerIds");
150: customerIds.clear();
151:
152: try {
153: Collection c = customerHome.findBySalesRep(salesRepId);
154: Iterator i = c.iterator();
155:
156: while (i.hasNext()) {
157: CustomerRemote customer = (CustomerRemote) i.next();
158: String id = (String) customer.getPrimaryKey();
159:
160: System.out.println("adding " + id + " to list");
161: customerIds.add(id);
162: }
163: } catch (Exception ex) {
164: throw new EJBException("Exception in loadCustomerIds: "
165: + ex.getMessage());
166: }
167: }
168:
169: public void ejbStore() {
170: System.out.println("in ejbStore");
171:
172: try {
173: storeSalesRep();
174: } catch (Exception ex) {
175: throw new EJBException("ejbStore: " + ex.getMessage());
176: }
177:
178: System.out.println("leaving ejbStore");
179: }
180:
181: public void ejbPostCreate(String salesRepId, String name) {
182: }
183:
184: /*********************** Database Routines *************************/
185: private void makeConnection() {
186: try {
187: InitialContext ic = new InitialContext();
188: DataSource ds = (DataSource) ic.lookup(dbName);
189:
190: con = ds.getConnection();
191: } catch (Exception ex) {
192: throw new EJBException("Unable to connect to database. "
193: + ex.getMessage());
194: }
195: }
196:
197: private void releaseConnection() {
198: try {
199: con.close();
200: } catch (SQLException ex) {
201: throw new EJBException("releaseConnection: "
202: + ex.getMessage());
203: }
204: }
205:
206: private void insertSalesRep(String salesRepId, String name)
207: throws SQLException {
208: makeConnection();
209: System.out.println("in insertSalesRep");
210:
211: String insertStatement = "insert into salesrep values ( ? , ? )";
212: PreparedStatement prepStmt = con
213: .prepareStatement(insertStatement);
214:
215: prepStmt.setString(1, salesRepId);
216: prepStmt.setString(2, name);
217:
218: prepStmt.executeUpdate();
219: prepStmt.close();
220: System.out.println("leaving insertSalesRep");
221: releaseConnection();
222: }
223:
224: private boolean selectByPrimaryKey(String primaryKey)
225: throws SQLException {
226: makeConnection();
227:
228: String selectStatement = "select salesrepid "
229: + "from salesrep where salesrepid = ? ";
230: PreparedStatement prepStmt = con
231: .prepareStatement(selectStatement);
232:
233: prepStmt.setString(1, primaryKey);
234:
235: ResultSet rs = prepStmt.executeQuery();
236: boolean result = rs.next();
237:
238: prepStmt.close();
239: releaseConnection();
240:
241: return result;
242: }
243:
244: private void deleteSalesRep(String salesRepId) throws SQLException {
245: makeConnection();
246:
247: String deleteStatement = "delete from salesrep "
248: + "where salesrepid = ?";
249: PreparedStatement prepStmt = con
250: .prepareStatement(deleteStatement);
251:
252: prepStmt.setString(1, salesRepId);
253: prepStmt.executeUpdate();
254: prepStmt.close();
255: releaseConnection();
256: }
257:
258: private void loadSalesRep() throws SQLException {
259: makeConnection();
260:
261: String selectStatement = "select name "
262: + "from salesRep where salesrepid = ? ";
263: PreparedStatement prepStmt = con
264: .prepareStatement(selectStatement);
265:
266: prepStmt.setString(1, salesRepId);
267:
268: ResultSet rs = prepStmt.executeQuery();
269:
270: if (rs.next()) {
271: name = rs.getString(1);
272: prepStmt.close();
273: } else {
274: prepStmt.close();
275: throw new NoSuchEntityException("Row for salesRepId "
276: + salesRepId + " not found in database.");
277: }
278:
279: releaseConnection();
280: }
281:
282: private void storeSalesRep() throws SQLException {
283: makeConnection();
284:
285: String updateStatement = "update salesrep set name = ? "
286: + "where salesrepid = ?";
287: PreparedStatement prepStmt = con
288: .prepareStatement(updateStatement);
289:
290: prepStmt.setString(1, name);
291: prepStmt.setString(2, salesRepId);
292:
293: int rowCount = prepStmt.executeUpdate();
294:
295: prepStmt.close();
296:
297: if (rowCount == 0) {
298: throw new EJBException("Storing row for salesRepId "
299: + salesRepId + " failed.");
300: }
301:
302: releaseConnection();
303: }
304: }
305: // SalesRepBean
|