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