KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > ejb > AccountBean


1 package test.ejb;
2
3 import test.interfaces.Account;
4 import test.interfaces.AccountData;
5 import test.interfaces.AccountPK;
6 import test.interfaces.AccountValue;
7 import test.interfaces.Customer;
8
9 import javax.ejb.*;
10 import java.util.Date JavaDoc;
11
12 /**
13  * This is an account bean. It is an example of how to use the XDoclet tags.
14  * There are several jboss tags that cannot be used together: autoincrement and unknown-pk for instance.
15  *
16  * @ejb.bean
17  * name="Account"
18  * jndi-name="ejb/bank/Account"
19  *
20  * @ejb.finder
21  * signature="Collection findAll()"
22  * transaction-type="NotSupported"
23  * unchecked="true"
24  *
25  * @ejb.finder
26  * signature="Collection findByOwner(test.interfaces.Customer owner)"
27  * role-name="Teller"
28  * transaction-type="Supports"
29  *
30  * @ejb.finder
31  * signature="Collection findLargeAccounts(int balance)"
32  * role-name="Teller,IRS"
33  *
34  * @ejb.interface
35  * remote-class="test.interfaces.Account"
36  *
37  * @ejb.persistence
38  * table-name="acct"
39  *
40  * @jboss.create-table "${jboss.create.table}"
41  * @jboss.remove-table "${jboss.remove.table}"
42  * @jboss.tuned-updates "${jboss.tuned.updates}"
43  * @jboss.read-only "${jboss.read.only}"
44  *
45  * @jboss.finder-query
46  * name="findLargeAccounts"
47  * order="balance"
48  * query="$1 > 1000"
49  * read-ahead="true"
50  *
51  * @jboss.unknown-pk
52  * class="java.lang.Integer"
53  * column-name="generated_id"
54  * jdbc-type="INTEGER"
55  * sql-type="INTEGER"
56  * auto-increment="true"
57  * @jboss.entity-command
58  * name="pk-sql"
59  * @jboss.entity-command-attribute
60  * name="pk-sql"
61  * value="SELECT SEQ_ACCOUNT.nextval FROM DUAL"
62  * @jboss.optimistic-locking
63  * key-generator-factory="java:/keygenererator"
64  * field-type="java.lang.Integer"
65  * field-name="Id"
66  * column-name="ID"
67  * jdbc-type="INTEGER"
68  * sql-type="DECIMAL(18)"
69  *
70  * @jboss.audit-created-by
71  * field-name="createdBy"
72  * column-name="CREATED_BY"
73  * jdbc-type="VARCHAR"
74  * sql-type="VARCHAR(128)"
75  * @jboss.audit-created-time
76  * field-name="createdTime"
77  * column-name="CREATED_TIMESTAMP"
78  * jdbc-type="TIMESTAMP"
79  * sql-type="TIMESTAMP"
80  * @jboss.audit-updated-by
81  * field-name="updatedBy"
82  * column-name="UPDATED_BY"
83  * jdbc-type="VARCHAR"
84  * sql-type="VARCHAR(128)"
85  * @jboss.audit-updated-time
86  * field-name="updatedTime"
87  * column-name="UPDATED_TIME"
88  * jdbc-type="TIMESTAMP"
89  * sql-type="TIMESTAMP"
90  *
91  * @weblogic.data-source-name xdoclet.database
92  *
93  * @ejb.value-object
94  * match="*"
95  * name="Account"
96  *
97  * @jonas.bean
98  * ejb-name="Account"
99  * jndi-name="AccountHome"
100  * @jonas.jdbc-mapping
101  * jndi-name="jdbc_1"
102  * jdbc-table-name="acct"
103  * @jonas.finder-method-jdbc-mapping
104  * method-name="findAll"
105  * jdbc-where-clause=""
106  * @jonas.finder-method-jdbc-mapping
107  * method-name="findLargeAccounts"
108  * jdbc-where-clause="c_balance > 1000"
109  *
110  * @version $Revision: 1.14 $
111  * @author <a HREF="mailto:youremail@yourdomain.com">youremail@yourdomain.com</a>
112  */

113 public abstract class AccountBean extends BaseEntityBean implements EntityBean {
114     /**
115      * Id of this account. This is not remote since the primary key can be extracted by other means.
116      *
117      * @ejb.pk-field
118      * @ejb.persistent-field
119      * @ejb.interface-method
120      *
121      * @ejb.persistence column-name="account_id"
122      *
123      * @jboss.auto-increment
124      *
125      * @jonas.cmp-field-jdbc-mapping
126      * field-name="id"
127      * jdbc-field-name="account_id"
128      */

129     public abstract Integer JavaDoc getId();
130
131     /**
132      * No interface method for setId(..). See page 130 of the EJB 2.0 specification:
133      * "Once the primary key for an entity bean has been set, the Bean Provider must
134      * not attempt to change it by use of set accessor methods on the primary key
135      * cmp-fields. The Bean provider should therefore not expose the set accessor
136      * methods for the primary key cmp-fields in the component interface of the
137      * entity bean.". A work around would be to remove and then an re-create the bean.
138      */

139     public abstract void setId(Integer JavaDoc id);
140
141     /**
142      * Deposit money.
143      *
144      * @ejb.interface-method
145      */

146     public void deposit(float amount) {
147         setBalance(getBalance() + amount);
148         setLastModificationDate(new Date JavaDoc());
149     }
150
151     /**
152      * Withdraw money.
153      *
154      * @ejb.interface-method
155      */

156     public void withdraw(float amount) {
157         if (amount <= 0)
158             throw new IllegalArgumentException JavaDoc("Invalid amount: " + amount + ", must be > 0");
159
160         setBalance(getBalance() - amount);
161         setLastModificationDate(new Date JavaDoc());
162     }
163
164     /**
165      * @ejb.value-object match="*"
166      * relation="external"
167      */

168     public float getTotalBalance() {
169         // do some wicked dynamic calculation
170
return 0;
171     }
172
173     /**
174      * Owner of this account.
175      *
176      * @ejb.persistent-field
177      * @ejb.interface-method
178      *
179      * @ejb.persistence column-name="last_modif"
180      * sql-type="TIMESTAMP"
181
182      * @ejb.permission role-name="Administrator"
183      *
184      * @ejb.transaction type="Supports"
185      *
186      * @ejb.value-object
187      * aggregate="test.interfaces.CustomerNormalValue"
188      * aggregate-name="OwnerNormalValue"
189      * match="*"
190      */

191     public abstract test.interfaces.Customer getOwner();
192
193     /**
194      * Owner of this account.
195      */

196     public abstract void setOwner(test.interfaces.Customer owner);
197
198     /**
199      * Balance of the account.
200      *
201      * @ejb.interface-method
202      * @ejb.persistent-field
203      *
204      * @ejb.transaction type="Supports"
205      * @ejb.permission role-name="Customer,Administrator"
206      *
207      * @jboss.sql-type DOUBLE
208      * @jboss.jdbc-type DOUBLE
209      * @weblogic.dbms-column balance
210      *
211      * @jonas.cmp-field-jdbc-mapping
212      * field-name="balance"
213      * jdbc-field-name="balance"
214      */

215     public abstract float getBalance();
216
217     /**
218      * Balance of the account. This is not remote since changing the balance of the account is done by calling "withdraw" or "deposit".
219      */

220     public abstract void setBalance(float balance);
221
222     /**
223      * @ejb.permission
224      * role-name="Administrator"
225      * @ejb.transaction
226      * type="Supports"
227      * @ejb.value-object
228      * exclude="true"
229      * match="*"
230      * @ejb.persistent-field
231      * @ejb.persistence
232      * column-name="last_modif"
233      * sql-type="TIMESTAMP"
234      *
235      * @jonas.cmp-field-jdbc-mapping
236      * field-name="lastModificationDate"
237      * jdbc-field-name="last_modif"
238      */

239     public abstract Date JavaDoc getLastModificationDate();
240
241     public abstract void setLastModificationDate(Date JavaDoc d);
242
243     /**
244      * A getter without a corresponding setter.
245      *
246      * @ejb.persistent-field
247      * @ejb.persistence
248      * column-name="accountid"
249      * sql-type="VARCHAR(20)"
250      *
251      * @jonas.cmp-field-jdbc-mapping
252      * field-name="accountid"
253      * jdbc-field-name="accountid"
254      */

255     public abstract String JavaDoc getAccountId();
256
257     /**
258      * Generated bulk accessor. Not remote, but could be.
259      *
260      */

261     public abstract void setData(AccountData data);
262
263     /**
264      * Generated bulk accessor. This is set as remote to allow clients to get all data in one call.
265      *
266      * @ejb.interface-method
267      * @ejb.permission
268      * role-name="Administrator"
269      * @ejb.transaction
270      * type="Supports"
271      */

272     public abstract AccountData getData();
273
274     /**
275      * @ejb.interface-method
276      * view-type="both"
277      */

278     public abstract AccountValue getAccountValue();
279
280     /**
281      * @ejb.interface-method
282      * view-type="both"
283      */

284     public abstract void setAccountValue(AccountValue value);
285
286     /**
287      * Create account.
288      *
289      * @ejb.create-method
290      * @ejb.permission
291      * role-name="Administrator"
292      */

293     public AccountPK ejbCreate(AccountValue data)
294             throws CreateException {
295         setId(data.getId());
296         setAccountValue(data);
297
298         return null;
299     }
300
301     /**
302      * Create account.
303      */

304     public void ejbPostCreate(AccountValue data)
305             throws CreateException {
306     }
307
308     /**
309      * Transfer money
310      *
311      * @ejb.permission
312      * role-name="Administrator"
313      * @ejb.home-method
314      * view-type="remote"
315      */

316     public void ejbHomeTransfer(Account from, Account to, float amount) {
317         try {
318             from.withdraw(amount);
319             to.deposit(amount);
320         }
321         catch (java.rmi.RemoteException JavaDoc e) {
322             throw new EJBException(e);
323         }
324     }
325
326         /**
327          * This is to get the unknown primary key. We just get the primary key
328          * from the entity context and cast it to the "known" class.
329          *
330          * @ejb.interface-method
331          * view-type="both"
332          */

333         public Integer JavaDoc getGeneratedPrimaryKey() {
334                 return (Integer JavaDoc) entityContext.getPrimaryKey();
335         }
336 }
337
Popular Tags