KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > storagebin > StorageBinBean


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 storagebin;
29
30 import java.sql.*;
31 import javax.sql.*;
32 import java.util.*;
33 import javax.ejb.*;
34 import javax.naming.*;
35
36
37 public class StorageBinBean implements EntityBean, StorageBinRemoteBusiness {
38     private static final String JavaDoc dbName = "jdbc/pointbase";
39     private String JavaDoc storageBinId;
40     private String JavaDoc widgetId;
41     private int quantity;
42     private EntityContext context;
43     private Connection con;
44
45     public String JavaDoc getWidgetId() {
46         return widgetId;
47     }
48
49     public int getQuantity() {
50         return quantity;
51     }
52
53     public String JavaDoc ejbCreate(String JavaDoc storageBinId, String JavaDoc widgetId, int quantity)
54         throws CreateException {
55         try {
56             insertRow(storageBinId, widgetId, quantity);
57         } catch (Exception JavaDoc ex) {
58             throw new EJBException("ejbCreate: " + ex.getMessage());
59         }
60
61         this.storageBinId = storageBinId;
62         this.widgetId = widgetId;
63         this.quantity = quantity;
64
65         return storageBinId;
66     }
67
68     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc primaryKey)
69         throws FinderException {
70         boolean result;
71
72         try {
73             result = selectByPrimaryKey(primaryKey);
74         } catch (Exception JavaDoc ex) {
75             throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
76         }
77
78         if (result) {
79             return primaryKey;
80         } else {
81             throw new ObjectNotFoundException("Row for id " + primaryKey +
82                 " not found.");
83         }
84     }
85
86     public String JavaDoc ejbFindByWidgetId(String JavaDoc widgetId) throws FinderException {
87         String JavaDoc storageBinId;
88
89         try {
90             storageBinId = selectByWidgetId(widgetId);
91         } catch (Exception JavaDoc ex) {
92             throw new EJBException("ejbFindByWidgetId: " + ex.getMessage());
93         }
94
95         return storageBinId;
96     }
97
98     public void ejbRemove() {
99         try {
100             deleteRow(storageBinId);
101         } catch (Exception JavaDoc ex) {
102             throw new EJBException("ejbRemove: " + ex.getMessage());
103         }
104     }
105
106     public void setEntityContext(EntityContext context) {
107         this.context = context;
108     }
109
110     public void unsetEntityContext() {
111     }
112
113     public void ejbActivate() {
114         storageBinId = (String JavaDoc) context.getPrimaryKey();
115     }
116
117     public void ejbPassivate() {
118         storageBinId = null;
119     }
120
121     public void ejbLoad() {
122         try {
123             loadRow();
124         } catch (Exception JavaDoc ex) {
125             throw new EJBException("ejbLoad: " + ex.getMessage());
126         }
127     }
128
129     public void ejbStore() {
130         try {
131             storeRow();
132         } catch (Exception JavaDoc ex) {
133             throw new EJBException("ejbLoad: " + ex.getMessage());
134         }
135     }
136
137     public void ejbPostCreate(String JavaDoc storageBinId, String JavaDoc widgetId, int quantity) {
138     }
139
140     /*********************** Database Routines *************************/
141     private void makeConnection() {
142         try {
143             InitialContext ic = new InitialContext();
144             DataSource ds = (DataSource) ic.lookup(dbName);
145
146             con = ds.getConnection();
147         } catch (Exception JavaDoc ex) {
148             throw new EJBException("Unable to connect to database. " +
149                 ex.getMessage());
150         }
151     }
152
153     private void releaseConnection() {
154         try {
155             con.close();
156         } catch (SQLException ex) {
157             throw new EJBException("releaseConnection: " + ex.getMessage());
158         }
159     }
160
161     private void insertRow(String JavaDoc storageBinId, String JavaDoc widgetId, int quantity)
162         throws SQLException {
163         makeConnection();
164
165         String JavaDoc insertStatement = "insert into storagebin values ( ? , ? , ? )";
166         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
167
168         prepStmt.setString(1, storageBinId);
169         prepStmt.setString(2, widgetId);
170         prepStmt.setInt(3, quantity);
171
172         prepStmt.executeUpdate();
173         prepStmt.close();
174         releaseConnection();
175     }
176
177     private void deleteRow(String JavaDoc storageBinId) throws SQLException {
178         makeConnection();
179
180         String JavaDoc deleteStatement =
181             "delete from storagebin where storagebinid = ? ";
182         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
183
184         prepStmt.setString(1, storageBinId);
185         prepStmt.executeUpdate();
186         prepStmt.close();
187         releaseConnection();
188     }
189
190     private boolean selectByPrimaryKey(String JavaDoc primaryKey)
191         throws SQLException {
192         makeConnection();
193
194         String JavaDoc selectStatement =
195             "select storagebinid " + "from storagebin where storagebinid = ? ";
196         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
197
198         prepStmt.setString(1, primaryKey);
199
200         ResultSet rs = prepStmt.executeQuery();
201         boolean result = rs.next();
202
203         prepStmt.close();
204         releaseConnection();
205
206         return result;
207     }
208
209     private String JavaDoc selectByWidgetId(String JavaDoc widgetId) throws SQLException {
210         makeConnection();
211
212         String JavaDoc storageBinId;
213
214         String JavaDoc selectStatement =
215             "select storagebinid " + "from storagebin where widgetid = ? ";
216         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
217
218         prepStmt.setString(1, widgetId);
219
220         ResultSet rs = prepStmt.executeQuery();
221
222         if (rs.next()) {
223             storageBinId = rs.getString(1);
224         } else {
225             storageBinId = null;
226         }
227
228         prepStmt.close();
229         releaseConnection();
230
231         return storageBinId;
232     }
233
234     private void loadRow() throws SQLException {
235         makeConnection();
236
237         String JavaDoc selectStatement =
238             "select widgetId, quantity " +
239             "from storagebin where storagebinid = ? ";
240         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
241
242         prepStmt.setString(1, this.storageBinId);
243
244         ResultSet rs = prepStmt.executeQuery();
245
246         if (rs.next()) {
247             this.widgetId = rs.getString(1);
248             this.quantity = rs.getInt(2);
249             prepStmt.close();
250         } else {
251             prepStmt.close();
252             throw new NoSuchEntityException("Row for storageBinId " +
253                 storageBinId + " not found in database.");
254         }
255
256         releaseConnection();
257     }
258
259     private void storeRow() throws SQLException {
260         makeConnection();
261
262         String JavaDoc updateStatement =
263             "update storagebin set widgetId = ? , " + "quantity = ? " +
264             "where storagebinid = ?";
265         PreparedStatement prepStmt = con.prepareStatement(updateStatement);
266
267         prepStmt.setString(1, widgetId);
268         prepStmt.setInt(2, quantity);
269         prepStmt.setString(3, storageBinId);
270
271         int rowCount = prepStmt.executeUpdate();
272
273         prepStmt.close();
274
275         if (rowCount == 0) {
276             throw new EJBException("Storing row for storageBinId " +
277                 storageBinId + " failed.");
278         }
279
280         releaseConnection();
281     }
282 }
283  // StorageBinBean
284
Popular Tags