KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > ejb > entity > cmp > stocktx > StockTxBean


1 /*
2  * StockOnline: EJB 1.1 Benchmark.
3  *
4  * Copyright © Commonwealth Scientific and Industrial Research Organisation (CSIRO - www.csiro.au), Australia 2001, 2002, 2003.
5  *
6  * Contact: Paul.Brebner@csiro.au
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or any
11  * later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * Originally developed for the CSIRO Middleware Technology Evaluation (MTE) Project, by
23  * the Software Architectures and Component Technologies Group, CSIRO Mathematical and Information Sciences
24  * Canberra and Sydney, Australia
25  *
26  * www.cmis.csiro.au/sact/
27  * www.cmis.csiro.au/adsat/mte.htm
28  *
29  * Initial developer(s): Shiping Chen, Paul Brebner, Lei Hu, Shuping Ran, Ian Gorton, Anna Liu.
30  * Contributor(s): ______________________.
31  */

32
33
34 //
35
//
36
// History:
37
// 10/08/2001 Shiping Initial coding based on the existing code
38
//
39
//
40
//
41
//
42

43 package stockonline.ejb.entity.cmp.stocktx;
44
45 import javax.naming.*;
46 import javax.rmi.*;
47 import javax.ejb.*;
48 import javax.sql.*;
49
50 import java.sql.*;
51 import java.util.*;
52 import java.rmi.*;
53
54 import stockonline.ejb.sql.SeqGenerator;
55 import stockonline.ejb.entity.interf.*;
56
57 /**
58  * Bean-Managed Persistent Entity Bean.
59  * This Entity Bean represents a StockTx.
60  */

61 public class StockTxBean implements EntityBean
62 {
63     final static boolean verbose = false;
64     final static String JavaDoc BEAN_NAME = "cmpStockTx";
65     final static String JavaDoc RESOURCE_NAME = "java:comp/env/jdbc/StockDB";
66
67     // Container-managed state fields
68
public int trans_id; // Primary key
69
public String JavaDoc trans_type;
70     public int sub_accno;
71     public int stock_id;
72     public int amount;
73     public float price;
74     public String JavaDoc trans_date;
75
76     protected EntityContext ctx;
77     private transient boolean isDirty; // "dirty" flag
78
private java.text.SimpleDateFormat JavaDoc dateFormatter = new java.text.SimpleDateFormat JavaDoc ("dd/MM/yy");
79
80     public StockTxBean()
81     {
82         if (verbose) System.out.println(BEAN_NAME + " instantiated");
83     }
84
85     //----------------------------------------------------------------
86
// Begin business methods
87
//----------------------------------------------------------------
88
// Getter/setter methods on Entity Bean fields
89

90     public int getTxID()
91     {
92         return trans_id;
93     }
94
95     //----------------------------------------------------------------
96
// End public business methods
97
//----------------------------------------------------------------
98

99     //----------------------------------------------------------------
100
// Begin EJB-Required methods. The methods below are called
101
// by the Container, and never called by client code.
102
//----------------------------------------------------------------
103

104     /**
105      * Associates this Bean instance with a particular context.
106      * We can query the Bean properties which customize the Bean here.
107      */

108     public void setEntityContext(EntityContext ctx)
109     {
110         if (verbose)
111             System.out.println(BEAN_NAME + ".setEntityContext() called");
112
113         this.ctx = ctx;
114     }
115
116     /**
117      * Disassociates this Bean instance with a
118      * particular context environment.
119      */

120     public void unsetEntityContext()
121     {
122         if (verbose)
123             System.out.println(BEAN_NAME + ".unsetEntityContext() called");
124
125         this.ctx = null;
126     }
127
128     /**
129      * Implementation can acquire needed resources.
130      */

131     public void ejbActivate()
132     {
133         if (verbose)
134             System.out.println(BEAN_NAME + ".ejbActivate() called.");
135
136         setModified(true);
137     }
138
139     /**
140      * Releases held resources for passivation.
141      */

142     public void ejbPassivate()
143     {
144         if (verbose)
145             System.out.println(BEAN_NAME + ".ejbPassivate () called.");
146     }
147
148     /**
149      * This is the initialization method that corresponds to the
150      * create() method in the Home Interface.
151      *
152      * @return The primary key for this StockTx
153      */

154     public StockTxPK ejbCreate(String JavaDoc trans_type, int sub_accno, int stock_id, int amount, float price)
155         throws CreateException
156     {
157         if (verbose)
158             System.out.println(BEAN_NAME + ".ejbCreate() called.");
159
160         try
161         {
162             this.trans_id = SeqGenerator.getNextTxID(RESOURCE_NAME);
163             this.trans_type = trans_type;
164             this.sub_accno = sub_accno;
165             this.stock_id = stock_id;
166             this.amount = amount;
167             this.price = price;
168             this.trans_date = dateFormatter.format( new java.util.Date JavaDoc() );
169
170             if(verbose) System.out.println("trans_date = " + trans_date);
171
172             return null;
173         }
174         catch(Exception JavaDoc ex)
175         {
176             String JavaDoc msg = BEAN_NAME + ".ejbCreate(): Failed to get a sequence number, due to: " + ex.toString();
177
178             System.err.println(msg);
179             throw new CreateException(msg);
180         }
181     }
182
183     /**
184      * Called after ejbCreate(). Now, the Bean can retrieve its EJBObject
185      * from its context, and pass it as a 'this' argument.
186      */

187     public void ejbPostCreate(String JavaDoc trans_type, int sub_accno, int stock_id, int amount, float price)
188     {
189         setModified(false);
190     }
191
192     /**
193      * This is the initialization method that corresponds to the
194      * create() method in the Home Interface.
195      *
196      * @return The primary key for this StockTx
197      */

198     public StockTxPK ejbCreate(int trans_id, String JavaDoc trans_type, int sub_accno, int stock_id, int amount, float price)
199         throws CreateException
200     {
201         if (verbose)
202             System.out.println(BEAN_NAME + ".ejbCreate() called.");
203
204         try
205         {
206             String JavaDoc currentDate = dateFormatter.format( new java.util.Date JavaDoc() );
207             if(verbose) System.out.println(currentDate);
208
209             this.trans_id = trans_id;
210             this.trans_type = trans_type;
211             this.sub_accno = sub_accno;
212             this.stock_id = stock_id;
213             this.amount = amount;
214             this.price = price;
215             this.trans_date = currentDate;
216
217             return null;
218         }
219         catch(Exception JavaDoc ex)
220         {
221             String JavaDoc msg = BEAN_NAME + ".ejbCreate(): Failed to get a sequence number, due to: " + ex.toString();
222
223             System.err.println(msg);
224             throw new CreateException(msg);
225         }
226     }
227
228     /**
229      * Called after ejbCreate(). Now, the Bean can retrieve its EJBObject
230      * from its context, and pass it as a 'this' argument.
231      */

232     public void ejbPostCreate(int trans_id, String JavaDoc trans_type, int sub_accno, int stock_id, int amount, float price)
233     {
234         setModified(false);
235     }
236
237     /**
238      * Removes the Entity Bean from the database.
239      * Corresponds to when client calls home.remove().
240      */

241     public void ejbRemove()
242     {
243         if (verbose)
244             System.out.println(BEAN_NAME + ".ejbRemove() called.");
245     }
246
247
248     /**
249      * Updates the in-memory Entity Bean object
250      * to reflect the current value stored in the database.
251      */

252     public void ejbLoad() throws RemoteException
253     {
254         if (verbose)
255             System.out.println(BEAN_NAME + ".ejbLoad() called.");
256
257         setModified(false);
258     }
259
260     /**
261      * Updates the database to reflect the
262      * current values of this in-memory Entity Bean object representation.
263      */

264     public void ejbStore() throws RemoteException
265     {
266         if (verbose)
267             System.out.println(BEAN_NAME + ".ejbStore() called.");
268
269         // Cannot be modified, so do nothing!
270
}
271
272     /**
273      * Returns whether the EJBean has been modified or not.
274      *
275      * @return boolean isDirty
276      */

277     public boolean isModified()
278     {
279         if (verbose)
280             System.out.println (BEAN_NAME + ".isModified(): isDirty = " + (isDirty ? "true" : "false"));
281
282         return isDirty;
283     }
284
285     /**
286      * Sets the EJBean as modified.
287      *
288      * @param flag boolean Flag
289      */

290     public void setModified(boolean flag)
291     {
292         isDirty = flag;
293     }
294 }
295
Popular Tags