KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > ejb > CustomerBean


1 package test.ejb;
2
3 import test.interfaces.AccountHome;
4 import test.interfaces.AddressLocalHome;
5 import test.interfaces.ApplicationException;
6 import test.interfaces.Customer;
7 import test.interfaces.CustomerData;
8 import test.interfaces.CustomerLightValue;
9 import test.interfaces.CustomerNormalValue;
10 import test.interfaces.CustomerPK;
11
12 import javax.ejb.CreateException JavaDoc;
13 import javax.ejb.EJBException JavaDoc;
14 import javax.ejb.EntityContext JavaDoc;
15 import javax.naming.InitialContext JavaDoc;
16 import java.util.Collection JavaDoc;
17
18 /**
19  * This is a customer bean. It is an example of how to use the XDoclet tags.
20  *
21  * @ejb.bean
22  * cmp-version="1.x"
23  * jndi-name="${ejb.prefix}/bank/Customer"
24  * name="Customer"
25  * type="CMP"
26  * use-soft-locking="true"
27  * view-type="remote"
28  * @ejb.ejb-ref
29  * ejb-name="Account"
30  * ref-name="ejb/bank/Account"
31  * @ejb.security-role-ref
32  * role-link="Administrator"
33  * role-name="admin"
34  * @ejb.permission
35  * role-name="Teller"
36  * @ejb.transaction
37  * type="Required"
38  * @ejb.data-object
39  * use-super-equals="true"
40  * @ejb.value-object
41  * match="light"
42  * name="CustomerLight"
43  * @ejb.value-object
44  * match="normal"
45  * name="CustomerNormal"
46  * @ejb.value-object
47  * match="*"
48  * @jboss.ejb-ref-jndi
49  * jndi-name="ejb/bank/Account"
50  * ref-name="bank/Account"
51  * @ejb.persistence table-name="customer"
52  * @jboss.create-table "true"
53  * @jboss.remove-table "true"
54  * @jboss.tuned-updates "true"
55  * @jboss.read-only "true"
56  * @jboss.time-out "100"
57  * @weblogic.transaction-isolation TRANSACTION_SERIALIZABLE
58  *
59  * @jonas.bean ejb-name="Customer"
60  * jndi-name="CustomerHome"
61  * @jonas.ejb-ref ejb-ref-name="ejb/bank/Account"
62  * jndi-name="Account"
63  * @jonas.jdbc-mapping
64  * jndi-name="jdbc_1"
65  * jdbc-table-name="customer"
66  *
67  * @todo This too should not appear in CMP class
68  */

69
70 public abstract class CustomerBean
71         extends PersonBean {
72     private EntityContext JavaDoc ctx;
73
74     /**
75      * Overriden from Person.
76      *
77      * @todo This todo should not appear in interfaces
78      * @ejb.interface-method
79      * @weblogic.transaction-isolation TRANSACTION_READ_COMMITTED
80      */

81     public void talkTo() {
82     }
83
84     /**
85      * Overloaded works too.
86      *
87      * @ejb.interface-method
88      */

89     public void talkTo(String JavaDoc somebody) {
90     }
91
92     /**
93      * Credit limit
94      *
95      * @ejb.persistent-field
96      * @ejb.persistence
97      * column-name="credit"
98      * @ejb.value-object
99      * match="normal"
100      * @ejb.value-object
101      * match="light"
102      */

103     public abstract float getCredit();
104
105     /**
106      * Array
107      *
108      * @ejb.persistent-field
109      * @ejb.persistence
110      * column-name="array"
111      * @ejb.value-object
112      * match="normal"
113      * @ejb.value-object
114      * match="light"
115      */

116     public abstract String JavaDoc[][] getArray();
117
118     /**
119      * Array
120      *
121      * @ejb.persistent-field
122      * @ejb.persistence
123      * column-name="image"
124      * @ejb.value-object
125      * match="normal"
126      * @ejb.value-object
127      * match="light"
128      */

129     public abstract byte[] getImage();
130
131     /**
132      * Tax Code
133      *
134      * @ejb.persistent-field
135      * @ejb.persistence
136      * column-name="tax"
137      * @ejb.value-object
138      * match="normal"
139      */

140     public abstract float getTax();
141
142     /**
143      * Credit limit
144      *
145      */

146     public abstract void setCredit(float credit);
147
148     /**
149      * Accounts of this customer
150      *
151      * @ejb.interface-method
152      * @ejb.transaction
153      * type="Supports"
154      * @ejb.value-object
155      * aggregate="test.interfaces.AccountValue"
156      * aggregate-name="AccountView"
157      * match="normal"
158      * members="test.interfaces.AccountLocal"
159      * members-name="Account"
160      * relation="external"
161      * type="java.util.Collection"
162      */

163     public java.util.Collection JavaDoc getAccounts() {
164         // Some code here under that will flame the guy who break multiple method tags
165
CustomerNormalValue normal = new CustomerNormalValue();
166         normal.getCredit();
167         CustomerLightValue light = new CustomerLightValue();
168         light.getCredit();
169         try {
170             AccountHome home = (AccountHome) new InitialContext JavaDoc().lookup("java:comp/env/ejb/bank/Account");
171             return home.findByOwner((Customer) ctx.getEJBObject());
172         }
173         catch (Exception JavaDoc e) {
174             throw new EJBException JavaDoc(e);
175         }
176     }
177
178     /**
179      * Just to show that generated methods can be exported to remote/local interafce
180      *
181      * @ejb.interface-method
182      */

183     public abstract void addAccount(test.interfaces.AccountValue added)
184             throws javax.ejb.FinderException JavaDoc;
185
186     /**
187      * Generated bulk accessor. Not remote, but could be.
188      *
189      */

190     public void setData(CustomerData data) {
191         try {
192             super.setData(data);
193             setCredit(data.getCredit());
194         }
195         catch (Exception JavaDoc e) {
196             throw new javax.ejb.EJBException JavaDoc(e);
197         }
198     }
199
200     /**
201      * @ejb.interface-method
202      * view-type="remote"
203      */

204     public abstract void setCustomerNormalValue(CustomerNormalValue value)
205             throws ApplicationException;
206
207     /**
208      * @ejb.interface-method
209      * view-type="remote"
210      */

211     public abstract CustomerNormalValue getCustomerNormalValue();
212
213     /**
214      * This method to show how validation errors can be reported to the user. This is Business Logic validation error.
215      *
216      */

217     protected void validate(CustomerNormalValue value)
218             throws ApplicationException {
219         /* This exception should better be done in the client itself because
220            it requires no ejb connection. Anyway this is an "example" on how
221            business validation can be done */

222         if (value.getTax() < 0)
223             throw new ApplicationException("A Customer can not have a negative Tax");
224     }
225
226     /**
227      * Generated bulk accessor. This is set as remote to allow clients to get all data in one call.
228      *
229      * @ejb.interface-method
230      * view-type="remote"
231      * @ejb.transaction
232      * type="Supports"
233      */

234     public CustomerData getData() {
235         return null;
236     }
237
238     /**
239      * @ejb:interface-method
240      * @ejb:permission role-name="Administrator"
241      * @ejb:transaction type="Supports"
242      * @ejb:value-object match="*" compose="test.interfaces.AddressValue" compose-name="AddressValue"
243      */

244 // public abstract test.interfaces.AddressLocal getAddress();
245

246     /**
247      * @ejb:interface-method
248      */

249 // public abstract void setAddress(test.interfaces.AddressLocal addr);
250

251     /**
252      * Accounts of this customer
253      *
254      * @ejb.interface-method
255      * @ejb.transaction
256      * type="Supports"
257      * @ejb.value-object
258      * compose="test.interfaces.AddressValue"
259      * compose-name="ShippingAddressValue"
260      * match="normal"
261      * members="test.interfaces.AddressLocal"
262      * members-name="ShippingAddress"
263      * relation="external"
264      * type="java.util.Collection"
265      */

266     public java.util.Collection JavaDoc getShippingAddresses() {
267         try {
268             AddressLocalHome home = (AddressLocalHome) new InitialContext JavaDoc().lookup("java:comp/env/ejb/bank/Address");
269             return home.findByOwner((Customer) ctx.getEJBObject());
270         }
271         catch (Exception JavaDoc e) {
272             throw new EJBException JavaDoc(e);
273         }
274     }
275
276     /**
277      * Create customer.
278      *
279      * @ejb.create-method
280      * @ejb.permission
281      * role-name="Administrator"
282      */

283     public java.lang.Object JavaDoc ejbCreate(CustomerNormalValue data)
284             throws CreateException JavaDoc {
285         try {
286             setId(data.getId());
287 // data.getAddressValue().setId(data.getId());
288
// AddressUtil.getLocalHome().create(data.getAddressValue());
289
return null;
290         }
291         catch (Exception JavaDoc e) {
292             throw new EJBException JavaDoc(e);
293         }
294     }
295
296 // Let's test the ejbPC auto add feature
297
// public void ejbPostCreate(CustomerNormalValue data)
298
// throws CreateException {}
299

300     /**
301      * Create customer.
302      *
303      */

304     public void ejbPostCreate(CustomerData data)
305             throws CreateException JavaDoc {
306     }
307
308     // EntityBean implementation -------------------------------------
309
/**
310      * Remove
311      *
312      * @ejb:transaction type="Mandatory"
313      */

314 // public void ejbRemove() throws RemoveException {
315
// getAddress().remove();
316
// }
317

318     /**
319      * To see if abstract works
320      *
321      */

322     public abstract void ejbLoad();
323
324     /**
325      * Custom Finders
326      *
327      */

328     public java.util.Collection JavaDoc ejbFindCustomFinderInSuper() {
329         return null;
330     }
331
332     /**
333      * @ejb:home-method
334      */

335     public void ejbHomeCopyToArchive(CustomerPK pk) {
336         // does nothing with CMP, BMP will override
337
}
338
339 }
340
Popular Tags