001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: CustomerBean.java 5061 2004-07-02 16:04:32Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.relation.cascade;
027:
028: import java.rmi.RemoteException;
029: import java.util.Collection;
030: import java.util.Date;
031: import java.util.Iterator;
032: import java.util.Vector;
033:
034: import javax.ejb.CreateException;
035: import javax.ejb.EntityBean;
036: import javax.ejb.EntityContext;
037: import javax.naming.InitialContext;
038: import javax.naming.NamingException;
039:
040: import org.objectweb.jonas.common.Log;
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043:
044: /**
045: * Implementation for bean Customer
046: * @author Ph Durieux
047: */
048: public abstract class CustomerBean implements EntityBean {
049:
050: static protected Logger logger = null;
051: private CreditCardHL creditCardHL = null;
052: private AddressHL addressHL = null;
053: private PhoneHL phoneHL = null;
054: private CarHL carHL = null;
055: private EntityContext ejbContext = null;
056:
057: public Integer ejbCreate(Integer id)
058: throws javax.ejb.CreateException {
059: logger.log(BasicLevel.DEBUG, "");
060: this .setId(id);
061: return null;
062: }
063:
064: public void ejbPostCreate(Integer id) {
065: logger.log(BasicLevel.DEBUG, "");
066: }
067:
068: public Integer ejbCreateWithAddress(Integer id, AddressDO addr)
069: throws javax.ejb.CreateException {
070: logger.log(BasicLevel.DEBUG, "");
071: this .setId(id);
072: return null;
073: }
074:
075: public void ejbPostCreateWithAddress(Integer id, AddressDO addr)
076: throws javax.ejb.CreateException {
077: logger.log(BasicLevel.DEBUG, "");
078: CustomerL myLocalInterface = (CustomerL) ejbContext
079: .getEJBLocalObject();
080: AddressL addrl = addressHL.create(addr, myLocalInterface);
081: setHomeAddress(addrl);
082: }
083:
084: // business methods
085:
086: public Name getName() {
087: logger.log(BasicLevel.DEBUG, "");
088: Name name = new Name(getLastName(), getFirstName());
089: return name;
090: }
091:
092: public void setName(Name name) {
093: logger.log(BasicLevel.DEBUG, "");
094: setLastName(name.getLastName());
095: setFirstName(name.getFirstName());
096: }
097:
098: public void setAddress(String street, String city, String state,
099: String zip) throws CreateException, NamingException {
100: logger.log(BasicLevel.DEBUG, "");
101:
102: AddressL addr = this .getHomeAddress();
103:
104: if (addr == null) {
105: addr = addressHL.create(street, city, state, zip);
106: setHomeAddress(addr);
107: } else {
108: // Customer already has an address. Change its fields
109: addr.setStreet(street);
110: addr.setCity(city);
111: addr.setState(state);
112: addr.setZip(zip);
113: }
114: }
115:
116: public void setAddress(AddressDO addrValue) throws CreateException,
117: NamingException {
118: logger.log(BasicLevel.DEBUG, "");
119: String street = addrValue.getStreet();
120: String city = addrValue.getCity();
121: String state = addrValue.getState();
122: String zip = addrValue.getZip();
123:
124: setAddress(street, city, state, zip);
125: }
126:
127: public AddressDO getAddress() {
128: logger.log(BasicLevel.DEBUG, "");
129: AddressL addrL = this .getHomeAddress();
130: if (addrL == null) {
131: return null;
132: }
133: String street = addrL.getStreet();
134: String city = addrL.getCity();
135: String state = addrL.getState();
136: String zip = addrL.getZip();
137: AddressDO addrValue = new AddressDO(street, city, state, zip);
138: return addrValue;
139: }
140:
141: public void addPhoneNumber(String number, byte type)
142: throws NamingException, CreateException, RemoteException {
143: logger.log(BasicLevel.DEBUG, "");
144: PhoneL phone = phoneHL.create(number, type, getName()
145: .getLastName());
146: Collection phoneNumbers = this .getPhoneNumbers();
147: phoneNumbers.add(phone);
148: }
149:
150: public void addCar(String number, byte type)
151: throws NamingException, CreateException, RemoteException {
152: logger.log(BasicLevel.DEBUG, "");
153: CarL car = carHL.create(number, type, getName().getLastName());
154: Collection cars = this .getCars();
155: cars.add(car);
156: }
157:
158: public void setCreditCard(Date date, String num, String name)
159: throws NamingException, CreateException, RemoteException {
160: logger.log(BasicLevel.DEBUG, "");
161: CreditCardL cc = creditCardHL.create(date, num, name);
162: setCreditCard(cc);
163: }
164:
165: public void removePhoneNumber(byte typeToRemove) {
166: logger.log(BasicLevel.DEBUG, "typeToRemove=" + typeToRemove);
167:
168: Collection phoneNumbers = this .getPhoneNumbers();
169: Iterator iterator = phoneNumbers.iterator();
170:
171: while (iterator.hasNext()) {
172: PhoneL phone = (PhoneL) iterator.next();
173: if (phone.getType() == typeToRemove) {
174: phoneNumbers.remove(phone);
175: break;
176: }
177: }
178: }
179:
180: public void removeCar(byte typeToRemove) {
181: logger.log(BasicLevel.DEBUG, "typeToRemove=" + typeToRemove);
182:
183: Collection cars = this .getCars();
184: Iterator iterator = cars.iterator();
185:
186: while (iterator.hasNext()) {
187: CarL car = (CarL) iterator.next();
188: if (car.getType() == typeToRemove) {
189: cars.remove(car);
190: break;
191: }
192: }
193: }
194:
195: public void updatePhoneNumber(String number, byte typeToUpdate) {
196: logger.log(BasicLevel.DEBUG, "typeToUpdate=" + typeToUpdate);
197:
198: Collection phoneNumbers = this .getPhoneNumbers();
199: Iterator iterator = phoneNumbers.iterator();
200: while (iterator.hasNext()) {
201: PhoneL phone = (PhoneL) iterator.next();
202: if (phone.getType() == typeToUpdate) {
203: phone.setNumber(number);
204: break;
205: }
206: }
207: }
208:
209: public void accident(String carnumber, String invoicenumber)
210: throws RemoteException, CreateException, NamingException {
211: logger.log(BasicLevel.DEBUG, "invoicenumber=" + invoicenumber);
212:
213: // retrieve car in the list
214: CarL car = null;
215: boolean found = false;
216: for (Iterator i = getCars().iterator(); i.hasNext();) {
217: car = (CarL) i.next();
218: if (car.getNumber().equals(carnumber)) {
219: found = true;
220: break;
221: }
222: }
223: if (!found) {
224: throw new RemoteException("Car not found:" + carnumber);
225: }
226:
227: // Create new invoice
228: car.addInvoice(invoicenumber);
229: }
230:
231: public void updateCar(String number, byte typeToUpdate) {
232: logger.log(BasicLevel.DEBUG, "typeToUpdate=" + typeToUpdate);
233:
234: Collection cars = this .getCars();
235: Iterator iterator = cars.iterator();
236: while (iterator.hasNext()) {
237: CarL car = (CarL) iterator.next();
238: if (car.getType() == typeToUpdate) {
239: car.setNumber(number);
240: break;
241: }
242: }
243: }
244:
245: public Vector getPhoneList() {
246: logger.log(BasicLevel.DEBUG, "");
247:
248: Vector vv = new Vector();
249: Collection phoneNumbers = this .getPhoneNumbers();
250:
251: Iterator iterator = phoneNumbers.iterator();
252: while (iterator.hasNext()) {
253: PhoneL phone = (PhoneL) iterator.next();
254: String ss = "Type=" + phone.getType() + " Number="
255: + phone.getNumber();
256: vv.add(ss);
257: }
258: return vv;
259: }
260:
261: public Vector getCarList() {
262: logger.log(BasicLevel.DEBUG, "");
263:
264: Vector vv = new Vector();
265: Collection cars = this .getCars();
266:
267: Iterator iterator = cars.iterator();
268: while (iterator.hasNext()) {
269: CarL car = (CarL) iterator.next();
270: String ss = "Type=" + car.getType() + " Number="
271: + car.getNumber();
272: vv.add(ss);
273: }
274: return vv;
275: }
276:
277: // persistent relationships
278:
279: public abstract AddressL getHomeAddress();
280:
281: public abstract void setHomeAddress(AddressL address);
282:
283: public abstract CreditCardL getCreditCard();
284:
285: public abstract void setCreditCard(CreditCardL card);
286:
287: public abstract java.util.Collection getPhoneNumbers();
288:
289: public abstract void setPhoneNumbers(java.util.Collection phones);
290:
291: public abstract java.util.Collection getCars();
292:
293: public abstract void setCars(java.util.Collection cars);
294:
295: // abstract accessor methods
296: public abstract Integer getId();
297:
298: public abstract void setId(Integer id);
299:
300: public abstract String getLastName();
301:
302: public abstract void setLastName(String lname);
303:
304: public abstract String getFirstName();
305:
306: public abstract void setFirstName(String fname);
307:
308: // standard call back methods
309:
310: public void setEntityContext(EntityContext ec) {
311: if (logger == null)
312: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
313: logger.log(BasicLevel.DEBUG, "");
314: try {
315: ejbContext = ec;
316: InitialContext cntx = new InitialContext();
317: creditCardHL = (CreditCardHL) cntx
318: .lookup("java:comp/env/ejb/CreditCardHomeLocal");
319: addressHL = (AddressHL) cntx
320: .lookup("java:comp/env/ejb/AddressHomeLocal");
321: phoneHL = (PhoneHL) cntx
322: .lookup("java:comp/env/ejb/PhoneHomeLocal");
323: carHL = (CarHL) cntx
324: .lookup("java:comp/env/ejb/CarHomeLocal");
325: } catch (Exception e) {
326: throw new javax.ejb.EJBException(e);
327: }
328: }
329:
330: public void unsetEntityContext() {
331: logger.log(BasicLevel.DEBUG, "");
332: ejbContext = null;
333: }
334:
335: public void ejbLoad() {
336: logger.log(BasicLevel.DEBUG, "");
337: checkCMRAccess();
338: }
339:
340: public void ejbStore() {
341: logger.log(BasicLevel.DEBUG, "");
342: checkCMRAccess();
343: }
344:
345: public void ejbActivate() {
346: logger.log(BasicLevel.DEBUG, "");
347: }
348:
349: public void ejbPassivate() {
350: logger.log(BasicLevel.DEBUG, "");
351: }
352:
353: public void ejbRemove() throws javax.ejb.RemoveException {
354: logger.log(BasicLevel.DEBUG, "");
355: checkCMRAccess();
356: }
357:
358: protected void checkCMRAccess() {
359: logger.log(BasicLevel.DEBUG, "");
360: Collection col = getCars();
361: }
362: }
|