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:
036: public class CustomerBean implements EntityBean, CustomerRemoteBusiness {
037: private static final String dbName = "java:comp/env/jdbc/SalesDB";
038: private String customerId;
039: private String salesRepId;
040: private String name;
041: private Connection con;
042: private EntityContext context;
043:
044: public String getSalesRepId() {
045: return salesRepId;
046: }
047:
048: public String getName() {
049: System.out.println("entering getName()");
050:
051: return name;
052: }
053:
054: public void setSalesRepId(String salesRepId) {
055: this .salesRepId = salesRepId;
056: }
057:
058: public void setName(String name) {
059: this .name = name;
060: }
061:
062: public String ejbCreate(String customerId, String salesRepId,
063: String name) throws CreateException {
064: System.out.println("in ejbCreate");
065:
066: try {
067: insertCustomer(customerId, salesRepId, name);
068: } catch (Exception ex) {
069: throw new EJBException("ejbCreate: " + ex.getMessage());
070: }
071:
072: this .customerId = customerId;
073: this .salesRepId = salesRepId;
074: this .name = name;
075:
076: System.out.println("about to leave ejbCreate");
077:
078: return customerId;
079: }
080:
081: public String ejbFindByPrimaryKey(String primaryKey)
082: throws FinderException {
083: boolean result;
084:
085: try {
086: result = selectByPrimaryKey(primaryKey);
087: } catch (Exception ex) {
088: throw new EJBException("ejbFindByPrimaryKey: "
089: + ex.getMessage());
090: }
091:
092: if (result) {
093: return primaryKey;
094: } else {
095: throw new ObjectNotFoundException("Row for id "
096: + primaryKey + " not found.");
097: }
098: }
099:
100: public Collection ejbFindBySalesRep(String salesRepId)
101: throws FinderException {
102: Collection result;
103:
104: try {
105: result = selectBySalesRep(salesRepId);
106: } catch (Exception ex) {
107: throw new EJBException("ejbFindBySalesRep: "
108: + ex.getMessage());
109: }
110:
111: return result;
112: }
113:
114: public void ejbRemove() {
115: try {
116: deleteCustomer(customerId);
117: } catch (Exception ex) {
118: throw new EJBException("ejbRemove: " + ex.getMessage());
119: }
120: }
121:
122: public void setEntityContext(EntityContext context) {
123: this .context = context;
124: }
125:
126: public void unsetEntityContext() {
127: }
128:
129: public void ejbActivate() {
130: customerId = (String) context.getPrimaryKey();
131: }
132:
133: public void ejbPassivate() {
134: customerId = null;
135: }
136:
137: public void ejbLoad() {
138: System.out.println("in ejbLoad");
139:
140: try {
141: loadCustomer();
142: } catch (Exception ex) {
143: throw new EJBException("ejbLoad: " + ex.getMessage());
144: }
145:
146: System.out.println("leaving ejbLoad");
147: }
148:
149: public void ejbStore() {
150: System.out.println("in ejbStore");
151:
152: try {
153: storeCustomer();
154: } catch (Exception ex) {
155: throw new EJBException("ejbStore: " + ex.getMessage());
156: }
157:
158: System.out.println("leaving ejbStore");
159: }
160:
161: public void ejbPostCreate(String customerId, String salesRepId,
162: String name) {
163: }
164:
165: /*********************** Database Routines *************************/
166: private void makeConnection() {
167: try {
168: InitialContext ic = new InitialContext();
169: DataSource ds = (DataSource) ic.lookup(dbName);
170:
171: con = ds.getConnection();
172: } catch (Exception ex) {
173: throw new EJBException("Unable to connect to database. "
174: + ex.getMessage());
175: }
176: }
177:
178: private void releaseConnection() {
179: try {
180: con.close();
181: } catch (SQLException ex) {
182: throw new EJBException("releaseConnection: "
183: + ex.getMessage());
184: }
185: }
186:
187: private void insertCustomer(String customerId, String salesRepId,
188: String name) throws SQLException {
189: makeConnection();
190:
191: String insertStatement = "insert into customer values ( ? , ? , ? )";
192: PreparedStatement prepStmt = con
193: .prepareStatement(insertStatement);
194:
195: prepStmt.setString(1, customerId);
196: prepStmt.setString(2, salesRepId);
197: prepStmt.setString(3, name);
198:
199: prepStmt.executeUpdate();
200: prepStmt.close();
201: releaseConnection();
202: }
203:
204: private boolean selectByPrimaryKey(String primaryKey)
205: throws SQLException {
206: makeConnection();
207:
208: String selectStatement = "select customerid "
209: + "from customer where customerid = ? ";
210: PreparedStatement prepStmt = con
211: .prepareStatement(selectStatement);
212:
213: prepStmt.setString(1, primaryKey);
214:
215: ResultSet rs = prepStmt.executeQuery();
216: boolean result = rs.next();
217:
218: prepStmt.close();
219: releaseConnection();
220:
221: return result;
222: }
223:
224: private Collection selectBySalesRep(String salesRepId)
225: throws SQLException {
226: makeConnection();
227:
228: String selectStatement = "select customerid "
229: + "from customer where salesrepid = ? ";
230: PreparedStatement prepStmt = con
231: .prepareStatement(selectStatement);
232:
233: prepStmt.setString(1, salesRepId);
234:
235: ResultSet rs = prepStmt.executeQuery();
236: ArrayList a = new ArrayList();
237:
238: while (rs.next()) {
239: String id = rs.getString(1);
240:
241: a.add(id);
242: }
243:
244: prepStmt.close();
245: releaseConnection();
246:
247: return a;
248: }
249:
250: private void deleteCustomer(String customerId) throws SQLException {
251: makeConnection();
252:
253: String deleteStatement = "delete from customer "
254: + "where customerid = ?";
255: PreparedStatement prepStmt = con
256: .prepareStatement(deleteStatement);
257:
258: prepStmt.setString(1, customerId);
259: prepStmt.executeUpdate();
260: prepStmt.close();
261: releaseConnection();
262: }
263:
264: private void loadCustomer() throws SQLException {
265: makeConnection();
266:
267: String selectStatement = "select customerid, salesRepid, name "
268: + "from customer where customerid = ? ";
269: PreparedStatement prepStmt = con
270: .prepareStatement(selectStatement);
271:
272: prepStmt.setString(1, customerId);
273:
274: ResultSet rs = prepStmt.executeQuery();
275:
276: if (rs.next()) {
277: customerId = rs.getString(1);
278: salesRepId = rs.getString(2);
279: name = rs.getString(3);
280: prepStmt.close();
281: } else {
282: prepStmt.close();
283: throw new NoSuchEntityException("Row for customerId "
284: + customerId + " not found in database.");
285: }
286:
287: releaseConnection();
288: }
289:
290: private void storeCustomer() throws SQLException {
291: makeConnection();
292: System.out.println("entering storeCustomer");
293:
294: String updateStatement = "update customer "
295: + "set salesRepid = ? , name = ? "
296: + "where customerid = ?";
297: PreparedStatement prepStmt = con
298: .prepareStatement(updateStatement);
299:
300: prepStmt.setString(1, salesRepId);
301: prepStmt.setString(2, name);
302: prepStmt.setString(3, customerId);
303:
304: int rowCount = prepStmt.executeUpdate();
305:
306: prepStmt.close();
307:
308: if (rowCount == 0) {
309: throw new EJBException("Storing row for customerId "
310: + customerId + " failed.");
311: }
312:
313: releaseConnection();
314: System.out.println("leaving storeCustomer");
315: }
316: }
317: // CustomerBean
|