KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > ejb > entity > cmp > stockholding > StockHoldingBean


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 package stockonline.ejb.entity.cmp.stockholding;
43
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.util.*;
55 import stockonline.ejb.entity.interf.*;
56
57 /**
58  * Bean-Managed Persistent Entity Bean.
59  * This Entity Bean represents a stockholding.
60  */

61 public class StockHoldingBean implements EntityBean
62 {
63     final static boolean verbose = false;
64     final static String JavaDoc BEAN_NAME = "cmpStockHolding";
65
66     private transient boolean isDirty; // "dirty" flag
67

68     // Bean-managed state fields
69
public int sub_accno;
70     public int stock_id;
71     public int amount;
72
73     protected EntityContext ctx;
74
75     public StockHoldingBean()
76     {
77         if (verbose)
78             System.out.println(BEAN_NAME + " instantiated");
79     }
80
81     //----------------------------------------------------------------
82
// Begin business methods
83
//----------------------------------------------------------------
84

85     /**
86      * Increase the amount of stock the subscriber is holding.
87      */

88     public void increase(int amount)
89     {
90         if (verbose)
91             System.out.println(BEAN_NAME + ".increase(" + amount + ") called.");
92
93         this.amount += amount;
94
95         setModified(true);
96     }
97
98     /**
99      * Decrease the amount of stock the subscriber is holding.
100      */

101     public void decrease(int amount)
102     {
103         if (verbose)
104             System.out.println(BEAN_NAME + ".decrease(" + amount + ") called.");
105
106         this.amount -= amount;
107
108         setModified(true);
109     }
110
111     /**
112      * Get amount
113      */

114     public int getAmount()
115     {
116         if (verbose)
117             System.out.println(BEAN_NAME + ".getAmount() called.");
118
119         return amount;
120     }
121
122     /**
123      * Get holding
124      */

125     public Holding getHolding()
126     {
127         if (verbose)
128             System.out.println(BEAN_NAME + ".getHolding() called.");
129
130         Holding holding = new Holding();
131
132         holding.stock_id = stock_id;
133         holding.amount = amount;
134
135         return holding;
136     }
137
138     //----------------------------------------------------------------
139
// End public business methods
140
//----------------------------------------------------------------
141

142     //----------------------------------------------------------------
143
// Begin EJB-Required methods. The methods below are called
144
// by the Container, and never called by client code.
145
//----------------------------------------------------------------
146

147     /**
148      * Associates this Bean instance with a particular context.
149      * We can query the Bean properties which customize the Bean here.
150      */

151     public void setEntityContext(EntityContext ctx)
152     {
153         if (verbose)
154             System.out.println(BEAN_NAME + ".setEntityContext called");
155
156         this.ctx = ctx;
157     }
158
159     /**
160      * Disassociates this Bean instance with a
161      * particular context environment.
162      */

163     public void unsetEntityContext()
164     {
165         if (verbose)
166             System.out.println(BEAN_NAME + ".unsetEntityContext called");
167
168         this.ctx = null;
169     }
170
171     /**
172      * Implementation can acquire needed resources.
173      */

174     public void ejbActivate()
175     {
176         if (verbose)
177             System.out.println(BEAN_NAME + ".ejbActivate() called.");
178
179         setModified(true);
180     }
181
182     /**
183      * Releases held resources for passivation.
184      */

185     public void ejbPassivate()
186     {
187         if (verbose)
188             System.out.println(BEAN_NAME + ".ejbPassivate () called.");
189     }
190
191     /**
192      * This is the initialization method that corresponds to the
193      * create() method in the Home Interface.
194      *
195      * @return The primary key for this stockholding
196      */

197     public StockHoldingPK ejbCreate(int sub_accno, int stock_id, int amount) throws CreateException
198     {
199         if (verbose)
200             System.out.println(BEAN_NAME + ".ejbCreate() called.");
201
202         this.sub_accno = sub_accno;
203         this.stock_id = stock_id;
204         this.amount = amount;
205
206         return null;
207     }
208
209     /**
210      * Called after ejbCreate(). Now, the Bean can retrieve its EJBObject
211      * from its context, and pass it as a 'this' argument.
212      */

213     public void ejbPostCreate(int sub_accno, int stock_id, int amount)
214     {
215         setModified(false);
216     }
217
218     /**
219      * Removes the Entity Bean from the database.
220      * Corresponds to when client calls home.remove().
221      */

222     public void ejbRemove() throws RemoteException
223     {
224         if (verbose)
225             System.out.println(BEAN_NAME + ".ejbRemove() called.");
226     }
227
228     /**
229      * Updates the in-memory Entity Bean object
230      * to reflect the current value stored in the database.
231      */

232     public void ejbLoad()
233     {
234         if (verbose)
235             System.out.println(BEAN_NAME + ".ejbLoad() called.");
236
237         if (!isModified())
238         {
239             return;
240         }
241
242         setModified(false);
243     }
244
245     /**
246      * Updates the database to reflect the
247      * current values of this in-memory Entity Bean object representation.
248      */

249     public void ejbStore()
250     {
251         if (verbose)
252             System.out.println(BEAN_NAME + ".ejbStore() called.");
253
254         if (!isModified())
255         {
256             return;
257         }
258
259         setModified(false);
260     }
261
262     /**
263      * Returns whether the EJBean has been modified or not.
264      *
265      * @return boolean isDirty
266      */

267     public boolean isModified()
268     {
269         if (verbose)
270             System.out.println (BEAN_NAME + ".isModified(): isDirty = " + (isDirty ? "true" : "false"));
271
272         return isDirty;
273     }
274
275     /**
276      * Sets the EJBean as modified.
277      *
278      * @param flag boolean Flag
279      */

280     public void setModified(boolean flag)
281     {
282         isDirty = flag;
283     }
284
285     //----------------------------------------------------------------
286
// End EJB-Required methods.
287
//----------------------------------------------------------------
288
}
289
Popular Tags