KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > cascade > CustomerBean


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: CustomerBean.java,v 1.7 2004/07/02 16:04:32 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.relation.cascade;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import javax.ejb.CreateException JavaDoc;
35 import javax.ejb.EntityBean JavaDoc;
36 import javax.ejb.EntityContext JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39
40 import org.objectweb.jonas.common.Log;
41 import org.objectweb.util.monolog.api.BasicLevel;
42 import org.objectweb.util.monolog.api.Logger;
43
44 /**
45  * Implementation for bean Customer
46  * @author Ph Durieux
47  */

48 public abstract class CustomerBean implements EntityBean JavaDoc {
49
50     static protected Logger logger = null;
51     private CreditCardHL creditCardHL = null;
52     private AddressHL addressHL = null;
53     private PhoneHL phoneHL = null;
54     private CarHL carHL = null;
55     private EntityContext JavaDoc ejbContext = null;
56
57     public Integer JavaDoc ejbCreate(Integer JavaDoc id) throws javax.ejb.CreateException JavaDoc {
58         logger.log(BasicLevel.DEBUG, "");
59         this.setId(id);
60         return null;
61     }
62
63     public void ejbPostCreate(Integer JavaDoc id) {
64         logger.log(BasicLevel.DEBUG, "");
65     }
66
67     public Integer JavaDoc ejbCreateWithAddress(Integer JavaDoc id, AddressDO addr) throws javax.ejb.CreateException JavaDoc {
68         logger.log(BasicLevel.DEBUG, "");
69         this.setId(id);
70         return null;
71     }
72
73     public void ejbPostCreateWithAddress(Integer JavaDoc id, AddressDO addr) throws javax.ejb.CreateException JavaDoc {
74         logger.log(BasicLevel.DEBUG, "");
75         CustomerL myLocalInterface = (CustomerL) ejbContext.getEJBLocalObject();
76         AddressL addrl = addressHL.create(addr, myLocalInterface);
77         setHomeAddress(addrl);
78     }
79
80
81     // business methods
82

83     public Name getName() {
84         logger.log(BasicLevel.DEBUG, "");
85         Name name = new Name(getLastName(), getFirstName());
86         return name;
87     }
88
89     public void setName(Name name) {
90         logger.log(BasicLevel.DEBUG, "");
91         setLastName(name.getLastName());
92         setFirstName(name.getFirstName());
93     }
94
95     public void setAddress(String JavaDoc street, String JavaDoc city, String JavaDoc state, String JavaDoc zip) throws CreateException JavaDoc, NamingException JavaDoc {
96         logger.log(BasicLevel.DEBUG, "");
97
98         AddressL addr = this.getHomeAddress();
99
100         if (addr == null) {
101             addr = addressHL.create(street, city, state, zip);
102             setHomeAddress(addr);
103         } else {
104             // Customer already has an address. Change its fields
105
addr.setStreet(street);
106             addr.setCity(city);
107             addr.setState(state);
108             addr.setZip(zip);
109         }
110     }
111
112     public void setAddress(AddressDO addrValue) throws CreateException JavaDoc, NamingException JavaDoc {
113         logger.log(BasicLevel.DEBUG, "");
114         String JavaDoc street = addrValue.getStreet();
115         String JavaDoc city = addrValue.getCity();
116         String JavaDoc state = addrValue.getState();
117         String JavaDoc zip = addrValue.getZip();
118
119         setAddress(street, city, state, zip);
120     }
121
122     public AddressDO getAddress() {
123         logger.log(BasicLevel.DEBUG, "");
124         AddressL addrL = this.getHomeAddress();
125         if (addrL == null) {
126             return null;
127         }
128         String JavaDoc street = addrL.getStreet();
129         String JavaDoc city = addrL.getCity();
130         String JavaDoc state = addrL.getState();
131         String JavaDoc zip = addrL.getZip();
132         AddressDO addrValue = new AddressDO(street, city, state, zip);
133         return addrValue;
134     }
135
136     public void addPhoneNumber(String JavaDoc number, byte type) throws NamingException JavaDoc, CreateException JavaDoc, RemoteException JavaDoc {
137         logger.log(BasicLevel.DEBUG, "");
138         PhoneL phone = phoneHL.create(number, type, getName().getLastName());
139         Collection JavaDoc phoneNumbers = this.getPhoneNumbers();
140         phoneNumbers.add(phone);
141     }
142
143     public void addCar(String JavaDoc number, byte type) throws NamingException JavaDoc, CreateException JavaDoc, RemoteException JavaDoc {
144         logger.log(BasicLevel.DEBUG, "");
145         CarL car = carHL.create(number, type, getName().getLastName());
146         Collection JavaDoc cars = this.getCars();
147         cars.add(car);
148     }
149
150     public void setCreditCard(Date JavaDoc date, String JavaDoc num, String JavaDoc name) throws NamingException JavaDoc, CreateException JavaDoc, RemoteException JavaDoc {
151         logger.log(BasicLevel.DEBUG, "");
152         CreditCardL cc = creditCardHL.create(date, num, name);
153         setCreditCard(cc);
154     }
155
156     public void removePhoneNumber(byte typeToRemove) {
157         logger.log(BasicLevel.DEBUG, "typeToRemove=" + typeToRemove);
158
159         Collection JavaDoc phoneNumbers = this.getPhoneNumbers();
160         Iterator JavaDoc iterator = phoneNumbers.iterator();
161
162         while (iterator.hasNext()) {
163             PhoneL phone = (PhoneL) iterator.next();
164             if (phone.getType() == typeToRemove) {
165                 phoneNumbers.remove(phone);
166                 break;
167             }
168         }
169     }
170
171     public void removeCar(byte typeToRemove) {
172         logger.log(BasicLevel.DEBUG, "typeToRemove=" + typeToRemove);
173
174         Collection JavaDoc cars = this.getCars();
175         Iterator JavaDoc iterator = cars.iterator();
176
177         while (iterator.hasNext()) {
178             CarL car = (CarL) iterator.next();
179             if (car.getType() == typeToRemove) {
180                 cars.remove(car);
181                 break;
182             }
183         }
184     }
185
186     public void updatePhoneNumber(String JavaDoc number, byte typeToUpdate) {
187         logger.log(BasicLevel.DEBUG, "typeToUpdate=" + typeToUpdate);
188
189         Collection JavaDoc phoneNumbers = this.getPhoneNumbers();
190         Iterator JavaDoc iterator = phoneNumbers.iterator();
191         while (iterator.hasNext()) {
192             PhoneL phone = (PhoneL) iterator.next();
193             if (phone.getType() == typeToUpdate) {
194                 phone.setNumber(number);
195                 break;
196             }
197         }
198     }
199
200     public void accident(String JavaDoc carnumber, String JavaDoc invoicenumber) throws RemoteException JavaDoc, CreateException JavaDoc, NamingException JavaDoc {
201         logger.log(BasicLevel.DEBUG, "invoicenumber=" + invoicenumber);
202
203         // retrieve car in the list
204
CarL car = null;
205         boolean found = false;
206         for (Iterator JavaDoc i = getCars().iterator(); i.hasNext();) {
207             car = (CarL) i.next();
208             if (car.getNumber().equals(carnumber)) {
209                 found = true;
210                 break;
211             }
212         }
213         if (! found) {
214             throw new RemoteException JavaDoc("Car not found:" + carnumber);
215         }
216                
217         // Create new invoice
218
car.addInvoice(invoicenumber);
219     }
220
221     public void updateCar(String JavaDoc number, byte typeToUpdate) {
222         logger.log(BasicLevel.DEBUG, "typeToUpdate=" + typeToUpdate);
223
224         Collection JavaDoc cars = this.getCars();
225         Iterator JavaDoc iterator = cars.iterator();
226         while (iterator.hasNext()) {
227             CarL car = (CarL) iterator.next();
228             if (car.getType() == typeToUpdate) {
229                 car.setNumber(number);
230                 break;
231             }
232         }
233     }
234
235     public Vector JavaDoc getPhoneList() {
236         logger.log(BasicLevel.DEBUG, "");
237
238         Vector JavaDoc vv = new Vector JavaDoc();
239         Collection JavaDoc phoneNumbers = this.getPhoneNumbers();
240
241         Iterator JavaDoc iterator = phoneNumbers.iterator();
242         while (iterator.hasNext()) {
243             PhoneL phone = (PhoneL) iterator.next();
244             String JavaDoc ss = "Type=" + phone.getType() + " Number=" + phone.getNumber();
245             vv.add(ss);
246         }
247         return vv;
248     }
249
250     public Vector JavaDoc getCarList() {
251         logger.log(BasicLevel.DEBUG, "");
252
253         Vector JavaDoc vv = new Vector JavaDoc();
254         Collection JavaDoc cars = this.getCars();
255
256         Iterator JavaDoc iterator = cars.iterator();
257         while (iterator.hasNext()) {
258             CarL car = (CarL) iterator.next();
259             String JavaDoc ss = "Type=" + car.getType() + " Number=" + car.getNumber();
260             vv.add(ss);
261         }
262         return vv;
263     }
264
265     // persistent relationships
266

267     public abstract AddressL getHomeAddress();
268     public abstract void setHomeAddress(AddressL address);
269
270     public abstract CreditCardL getCreditCard();
271     public abstract void setCreditCard(CreditCardL card);
272
273     public abstract java.util.Collection JavaDoc getPhoneNumbers();
274     public abstract void setPhoneNumbers(java.util.Collection JavaDoc phones);
275
276     public abstract java.util.Collection JavaDoc getCars();
277     public abstract void setCars(java.util.Collection JavaDoc cars);
278
279     // abstract accessor methods
280
public abstract Integer JavaDoc getId();
281     public abstract void setId(Integer JavaDoc id);
282  
283     public abstract String JavaDoc getLastName( );
284     public abstract void setLastName(String JavaDoc lname);
285
286     public abstract String JavaDoc getFirstName( );
287     public abstract void setFirstName(String JavaDoc fname);
288
289     // standard call back methods
290

291     public void setEntityContext(EntityContext JavaDoc ec) {
292         if (logger == null)
293             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
294         logger.log(BasicLevel.DEBUG, "");
295         try {
296             ejbContext = ec;
297             InitialContext JavaDoc cntx = new InitialContext JavaDoc();
298             creditCardHL = (CreditCardHL) cntx.lookup("java:comp/env/ejb/CreditCardHomeLocal");
299             addressHL = (AddressHL) cntx.lookup("java:comp/env/ejb/AddressHomeLocal");
300             phoneHL = (PhoneHL) cntx.lookup("java:comp/env/ejb/PhoneHomeLocal");
301             carHL = (CarHL) cntx.lookup("java:comp/env/ejb/CarHomeLocal");
302         } catch (Exception JavaDoc e) {
303             throw new javax.ejb.EJBException JavaDoc(e);
304         }
305     }
306
307     public void unsetEntityContext() {
308         logger.log(BasicLevel.DEBUG, "");
309         ejbContext = null;
310     }
311
312     public void ejbLoad() {
313         logger.log(BasicLevel.DEBUG, "");
314         checkCMRAccess();
315     }
316
317     public void ejbStore() {
318         logger.log(BasicLevel.DEBUG, "");
319         checkCMRAccess();
320     }
321
322     public void ejbActivate() {
323         logger.log(BasicLevel.DEBUG, "");
324     }
325
326     public void ejbPassivate() {
327         logger.log(BasicLevel.DEBUG, "");
328     }
329
330     public void ejbRemove() throws javax.ejb.RemoveException JavaDoc {
331         logger.log(BasicLevel.DEBUG, "");
332         checkCMRAccess();
333     }
334
335     protected void checkCMRAccess() {
336         logger.log(BasicLevel.DEBUG, "");
337         Collection JavaDoc col = getCars();
338     }
339 }
340
Popular Tags