KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > ejbql > CustomerBean


1 package org.objectweb.jonas.jtests.beans.ejbql;
2
3 import javax.ejb.FinderException JavaDoc;
4 import javax.ejb.EJBException JavaDoc;
5 import javax.ejb.EntityContext JavaDoc;
6 import javax.ejb.CreateException JavaDoc;
7 import javax.naming.InitialContext JavaDoc;
8 import javax.naming.NamingException JavaDoc;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.Vector JavaDoc;
16 import java.rmi.RemoteException JavaDoc;
17
18 public abstract class CustomerBean implements javax.ejb.EntityBean JavaDoc {
19
20     private CruiseHomeLocal cruiseHomeLocal = null;
21
22     private CustomerHomeLocal customerHomeLocal = null;
23
24     private CreditCardHomeLocal creditCardHomeLocal = null;
25
26     private CreditCompanyHomeLocal creditCompanyHomeLocal = null;
27
28     private AddressHomeLocal addressHomeLocal = null;
29
30     public Integer JavaDoc ejbCreate(Integer JavaDoc id) throws javax.ejb.CreateException JavaDoc {
31         this.setId(id);
32         return null;
33     }
34
35     public void ejbPostCreate(Integer JavaDoc id) {
36     }
37
38
39     // business methods
40

41     public Name getName() {
42         Name name = new Name(getLastName(), getFirstName());
43         return name;
44     }
45     public void setName(Name name) {
46         setLastName(name.getLastName());
47         setFirstName(name.getFirstName());
48     }
49
50     public void setAddress(String JavaDoc street, String JavaDoc city, String JavaDoc state, String JavaDoc zip)
51         throws EJBException JavaDoc {
52
53         AddressLocal addr = this.getHomeAddress();
54
55         try {
56
57             if (addr == null) {
58                 // Customer doesn't have an address yet. Create a new one.
59
InitialContext JavaDoc cntx = new InitialContext JavaDoc();
60                 AddressHomeLocal addrHome =
61                     (AddressHomeLocal) cntx.lookup("java:comp/env/ejb/AddressHomeLocal");
62                 addr = addrHome.createAddress(street, city, state, zip);
63                 this.setHomeAddress(addr);
64             } else {
65                 // Customer already has an address. Change its fields
66
addr.setStreet(street);
67                 addr.setCity(city);
68                 addr.setState(state);
69                 addr.setZip(zip);
70             }
71         } catch (NamingException JavaDoc ne) {
72             throw new EJBException JavaDoc(ne);
73         } catch (CreateException JavaDoc ce) {
74             throw new EJBException JavaDoc(ce);
75         }
76     }
77
78     public void setAddress(AddressDO addrValue) throws CreateException JavaDoc, NamingException JavaDoc {
79         String JavaDoc street = addrValue.getStreet();
80         String JavaDoc city = addrValue.getCity();
81         String JavaDoc state = addrValue.getState();
82         String JavaDoc zip = addrValue.getZip();
83
84         setAddress(street, city, state, zip);
85     }
86
87
88     public AddressDO getAddress() {
89
90         AddressLocal addrLocal = this.getHomeAddress();
91         if (addrLocal == null) {
92             return null;
93         }
94         String JavaDoc street = addrLocal.getStreet();
95         String JavaDoc city = addrLocal.getCity();
96         String JavaDoc state = addrLocal.getState();
97         String JavaDoc zip = addrLocal.getZip();
98         AddressDO addrValue = new AddressDO(street, city, state, zip);
99         return addrValue;
100     }
101
102     public void addPhoneNumber(String JavaDoc number, byte type)
103         throws NamingException JavaDoc, CreateException JavaDoc, RemoteException JavaDoc {
104         InitialContext JavaDoc jndiEnc = new InitialContext JavaDoc();
105         PhoneHomeLocal phoneHome = (PhoneHomeLocal) (jndiEnc.lookup("java:comp/env/ejb/PhoneHomeLocal"));
106
107         PhoneLocal phone = phoneHome.create(number, type);
108         Collection JavaDoc phoneNumbers = this.getPhoneNumbers();
109         phoneNumbers.add(phone);
110     }
111
112     public void removePhoneNumber(byte typeToRemove) {
113
114         Collection JavaDoc phoneNumbers = this.getPhoneNumbers();
115         Iterator JavaDoc iterator = phoneNumbers.iterator();
116
117         while (iterator.hasNext()) {
118             PhoneLocal phone = (PhoneLocal) iterator.next();
119             if (phone.getType() == typeToRemove) {
120                 phoneNumbers.remove(phone);
121                 break;
122             }
123
124         }
125     }
126
127     public void updatePhoneNumber(String JavaDoc number, byte typeToUpdate) {
128
129
130         Collection JavaDoc phoneNumbers = this.getPhoneNumbers();
131         Iterator JavaDoc iterator = phoneNumbers.iterator();
132         while (iterator.hasNext()) {
133             PhoneLocal phone = (PhoneLocal) iterator.next();
134             if (phone.getType() == typeToUpdate) {
135                 phone.setNumber(number);
136                 break;
137             }
138         }
139     }
140
141     public Vector JavaDoc getPhoneList() {
142
143         Vector JavaDoc vv = new Vector JavaDoc();
144         Collection JavaDoc phoneNumbers = this.getPhoneNumbers();
145
146         Iterator JavaDoc iterator = phoneNumbers.iterator();
147         while (iterator.hasNext()) {
148             PhoneLocal phone = (PhoneLocal) iterator.next();
149             String JavaDoc ss = "Type=" + phone.getType() + " Number=" + phone.getNumber();
150             vv.add(ss);
151         }
152
153         return vv;
154     }
155
156     public Collection JavaDoc ejbHomeCallFindOnCruise(Integer JavaDoc cruiseId) throws FinderException JavaDoc {
157
158         ArrayList JavaDoc customers_id = new ArrayList JavaDoc();
159         CruiseLocal crloc = null;
160         if (cruiseId != null) {
161             crloc = cruiseHomeLocal.findByPrimaryKey(cruiseId);
162         }
163         Collection JavaDoc customers = customerHomeLocal.findOnCruise(crloc);
164         Iterator JavaDoc customer = customers.iterator();
165         while (customer.hasNext()) {
166             CustomerLocal customer_local = (CustomerLocal) customer.next();
167             customers_id.add(customer_local.getId());
168         }
169         return customers_id;
170     }
171
172     public Collection JavaDoc ejbHomeCallFindByAddressLocal(Integer JavaDoc add) throws FinderException JavaDoc {
173         ArrayList JavaDoc customers_id = new ArrayList JavaDoc();
174         AddressLocal addloc = null;
175         if (add != null) {
176             addloc = addressHomeLocal.findByPrimaryKey(add);
177         }
178         Collection JavaDoc customers = customerHomeLocal.findByAddressLocal(addloc);
179         Iterator JavaDoc customer = customers.iterator();
180         while (customer.hasNext()) {
181             CustomerLocal customer_local = (CustomerLocal)customer.next();
182             customers_id.add(customer_local.getId());
183         }
184         return customers_id;
185     }
186
187     public Collection JavaDoc ejbHomeCallFindByParameterIsNull(Integer JavaDoc cc) throws FinderException JavaDoc {
188         ArrayList JavaDoc customers_id = new ArrayList JavaDoc();
189         CreditCardLocal ccloc = null;
190         if (cc != null) {
191             ccloc = creditCardHomeLocal.findByPrimaryKey(cc);
192         }
193         Collection JavaDoc customers = customerHomeLocal.findByParameterIsNull(ccloc);
194         Iterator JavaDoc customer = customers.iterator();
195         while (customer.hasNext()) {
196             CustomerLocal customer_local = (CustomerLocal)customer.next();
197             customers_id.add(customer_local.getId());
198         }
199         return customers_id;
200     }
201
202     public void setCreditCard(Date JavaDoc expCreditcard, String JavaDoc numbCreditcard, String JavaDoc nameCreditcard, String JavaDoc orgCreditcard , String JavaDoc nameCreditCompany, String JavaDoc streetAddress, String JavaDoc cityAddress, String JavaDoc stateAddress, String JavaDoc zipAddress ) {
203
204         // Create the CreditCompany, the Address and the CreditCard
205
// Associate the Address to the CreditCompany, the CreditCompany to the
206
// CreditCard and the CreditCard to the current customer
207
try {
208             CreditCardLocal creditcard = creditCardHomeLocal.create(expCreditcard, numbCreditcard, nameCreditcard, orgCreditcard);
209             this.setCreditCard(creditcard);
210             CreditCompanyLocal creditcompany = creditCompanyHomeLocal.create(nameCreditCompany);
211             creditcard.setCreditCompany(creditcompany);
212             AddressLocal address = addressHomeLocal.createAddress(streetAddress, cityAddress, stateAddress, zipAddress);
213             creditcompany.setAddress(address);
214         } catch (CreateException JavaDoc e) {
215             System.out.println ("Probleme dans creation du CreditCompany/Address/CrediCard");
216             e.printStackTrace(System.out);
217         }
218
219     }
220
221     // persistent relationships
222

223     public abstract AddressLocal getHomeAddress();
224     public abstract void setHomeAddress(AddressLocal address);
225
226     public abstract CreditCardLocal getCreditCard();
227     public abstract void setCreditCard(CreditCardLocal card);
228
229     public abstract java.util.Collection JavaDoc getPhoneNumbers( );
230     public abstract void setPhoneNumbers(java.util.Collection JavaDoc phones);
231
232     // abstract accessor methods
233
public abstract Integer JavaDoc getId();
234     public abstract void setId(Integer JavaDoc id);
235  
236     public abstract String JavaDoc getLastName( );
237     public abstract void setLastName(String JavaDoc lname);
238
239     public abstract String JavaDoc getFirstName( );
240     public abstract void setFirstName(String JavaDoc fname);
241
242     public abstract boolean getHasGoodCredit();
243     public abstract void setHasGoodCredit(boolean flag);
244
245     // abstract ejbSelect() methods
246
public abstract String JavaDoc ejbSelectAddrCity(Integer JavaDoc id) throws FinderException JavaDoc;
247     public abstract String JavaDoc ejbSelectLastName(Integer JavaDoc id) throws FinderException JavaDoc;
248     public abstract Set JavaDoc ejbSelectFirstName() throws FinderException JavaDoc;
249     public abstract Collection JavaDoc ejbSelectDistinctFirstName() throws FinderException JavaDoc;
250     public abstract AddressLocal ejbSelectAddr(Integer JavaDoc id) throws FinderException JavaDoc;
251     public abstract Collection JavaDoc ejbSelectAllCreditCardAddr() throws FinderException JavaDoc;
252     public abstract Collection JavaDoc ejbSelectRogerAddr() throws FinderException JavaDoc;
253     public abstract long ejbSelectCountCreditCard() throws FinderException JavaDoc;
254     public abstract Collection JavaDoc ejbSelectCreditCardNumbers() throws FinderException JavaDoc;
255     public abstract long ejbSelectCountOfCustomersWithId1(Integer JavaDoc id) throws FinderException JavaDoc;
256     public abstract long ejbSelectCountOfCustomersWithId2(int id) throws FinderException JavaDoc;
257     public abstract long ejbSelectCountOfCustomersRingo() throws FinderException JavaDoc;
258
259     //
260
// Public Home method required to test the private ejbSelectXXXX method
261
public String JavaDoc ejbHomeSelectAddrCity(Integer JavaDoc id) throws FinderException JavaDoc {
262         return this.ejbSelectAddrCity(id);
263     }
264     public String JavaDoc ejbHomeSelectLastName(Integer JavaDoc id) throws FinderException JavaDoc {
265         return this.ejbSelectLastName(id);
266     }
267     public Set JavaDoc ejbHomeSelectFirstName() throws FinderException JavaDoc {
268         return this.ejbSelectFirstName();
269     }
270     public Collection JavaDoc ejbHomeSelectDistinctFirstName() throws FinderException JavaDoc {
271         return this.ejbSelectDistinctFirstName();
272     }
273     public Integer JavaDoc ejbHomeSelectAddr(Integer JavaDoc id) throws FinderException JavaDoc {
274         AddressLocal address = this.ejbSelectAddr(id);
275         return address.getId();
276     }
277     public Collection JavaDoc ejbHomeSelectAllCreditCardAddr() throws FinderException JavaDoc {
278         Collection JavaDoc addresses = this.ejbSelectAllCreditCardAddr();
279         ArrayList JavaDoc addresses_id = new ArrayList JavaDoc();
280         Iterator JavaDoc iAddr = addresses.iterator();
281         while (iAddr.hasNext()) {
282             AddressLocal address = (AddressLocal) iAddr.next();
283             addresses_id.add(address.getId());
284         }
285         return addresses_id;
286     }
287     public Collection JavaDoc ejbHomeSelectRogerAddr() throws FinderException JavaDoc {
288         Collection JavaDoc addresses = this.ejbSelectRogerAddr();
289         ArrayList JavaDoc addresses_id = new ArrayList JavaDoc();
290         Iterator JavaDoc iAddr = addresses.iterator();
291         while (iAddr.hasNext()) {
292             AddressLocal address = (AddressLocal) iAddr.next();
293             addresses_id.add(address.getId());
294         }
295         return addresses_id;
296     }
297     public long ejbHomeGetCountCreditCard() throws FinderException JavaDoc {
298         return this.ejbSelectCountCreditCard();
299     }
300     public Collection JavaDoc ejbHomeGetCreditCardNumbers() throws FinderException JavaDoc {
301         return this.ejbSelectCreditCardNumbers();
302     }
303     public long ejbHomeGetCountCustomersWithId1(Integer JavaDoc id) throws FinderException JavaDoc {
304         return this.ejbSelectCountOfCustomersWithId1(id);
305     }
306     public long ejbHomeGetCountCustomersWithId2(int id) throws FinderException JavaDoc {
307         return this.ejbSelectCountOfCustomersWithId2(id);
308     }
309     public long ejbHomeGetCountCustomersRingo() throws FinderException JavaDoc {
310         return this.ejbSelectCountOfCustomersRingo();
311     }
312
313     // standard call back methods
314

315     public void setEntityContext(EntityContext JavaDoc ec) {
316         try {
317             InitialContext JavaDoc cntx = new InitialContext JavaDoc();
318             cruiseHomeLocal =
319                 (CruiseHomeLocal) cntx.lookup("java:comp/env/ejb/CruiseHomeLocal");
320             customerHomeLocal =
321                 (CustomerHomeLocal) cntx.lookup("java:comp/env/ejb/CustomerHomeLocal");
322             creditCardHomeLocal =
323                 (CreditCardHomeLocal) cntx.lookup("java:comp/env/ejb/CreditCardHomeLocal");
324             creditCompanyHomeLocal =
325                 (CreditCompanyHomeLocal) cntx.lookup("java:comp/env/ejb/CreditCompanyHomeLocal");
326             addressHomeLocal =
327                 (AddressHomeLocal) cntx.lookup("java:comp/env/ejb/AddressHomeLocal");
328         } catch (Exception JavaDoc e) {
329             throw new javax.ejb.EJBException JavaDoc(e);
330         }
331     }
332
333     public void unsetEntityContext() { }
334     public void ejbLoad() { }
335     public void ejbStore() { }
336     public void ejbActivate() { }
337     public void ejbPassivate() { }
338     public void ejbRemove() throws javax.ejb.RemoveException JavaDoc { }
339
340 }
341
Popular Tags