1 25 26 package org.objectweb.jonas.jtests.beans.relation.cascade; 27 28 import java.rmi.RemoteException ; 29 import java.util.Collection ; 30 import java.util.Date ; 31 import java.util.Iterator ; 32 import java.util.Vector ; 33 34 import javax.ejb.CreateException ; 35 import javax.ejb.EntityBean ; 36 import javax.ejb.EntityContext ; 37 import javax.naming.InitialContext ; 38 import javax.naming.NamingException ; 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 48 public abstract class CustomerBean implements EntityBean { 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 ejbContext = null; 56 57 public Integer ejbCreate(Integer id) throws javax.ejb.CreateException { 58 logger.log(BasicLevel.DEBUG, ""); 59 this.setId(id); 60 return null; 61 } 62 63 public void ejbPostCreate(Integer id) { 64 logger.log(BasicLevel.DEBUG, ""); 65 } 66 67 public Integer ejbCreateWithAddress(Integer id, AddressDO addr) throws javax.ejb.CreateException { 68 logger.log(BasicLevel.DEBUG, ""); 69 this.setId(id); 70 return null; 71 } 72 73 public void ejbPostCreateWithAddress(Integer id, AddressDO addr) throws javax.ejb.CreateException { 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 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 street, String city, String state, String zip) throws CreateException , NamingException { 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 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 , NamingException { 113 logger.log(BasicLevel.DEBUG, ""); 114 String street = addrValue.getStreet(); 115 String city = addrValue.getCity(); 116 String state = addrValue.getState(); 117 String 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 street = addrL.getStreet(); 129 String city = addrL.getCity(); 130 String state = addrL.getState(); 131 String zip = addrL.getZip(); 132 AddressDO addrValue = new AddressDO(street, city, state, zip); 133 return addrValue; 134 } 135 136 public void addPhoneNumber(String number, byte type) throws NamingException , CreateException , RemoteException { 137 logger.log(BasicLevel.DEBUG, ""); 138 PhoneL phone = phoneHL.create(number, type, getName().getLastName()); 139 Collection phoneNumbers = this.getPhoneNumbers(); 140 phoneNumbers.add(phone); 141 } 142 143 public void addCar(String number, byte type) throws NamingException , CreateException , RemoteException { 144 logger.log(BasicLevel.DEBUG, ""); 145 CarL car = carHL.create(number, type, getName().getLastName()); 146 Collection cars = this.getCars(); 147 cars.add(car); 148 } 149 150 public void setCreditCard(Date date, String num, String name) throws NamingException , CreateException , RemoteException { 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 phoneNumbers = this.getPhoneNumbers(); 160 Iterator 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 cars = this.getCars(); 175 Iterator 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 number, byte typeToUpdate) { 187 logger.log(BasicLevel.DEBUG, "typeToUpdate=" + typeToUpdate); 188 189 Collection phoneNumbers = this.getPhoneNumbers(); 190 Iterator 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 carnumber, String invoicenumber) throws RemoteException , CreateException , NamingException { 201 logger.log(BasicLevel.DEBUG, "invoicenumber=" + invoicenumber); 202 203 CarL car = null; 205 boolean found = false; 206 for (Iterator 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 ("Car not found:" + carnumber); 215 } 216 217 car.addInvoice(invoicenumber); 219 } 220 221 public void updateCar(String number, byte typeToUpdate) { 222 logger.log(BasicLevel.DEBUG, "typeToUpdate=" + typeToUpdate); 223 224 Collection cars = this.getCars(); 225 Iterator 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 getPhoneList() { 236 logger.log(BasicLevel.DEBUG, ""); 237 238 Vector vv = new Vector (); 239 Collection phoneNumbers = this.getPhoneNumbers(); 240 241 Iterator iterator = phoneNumbers.iterator(); 242 while (iterator.hasNext()) { 243 PhoneL phone = (PhoneL) iterator.next(); 244 String ss = "Type=" + phone.getType() + " Number=" + phone.getNumber(); 245 vv.add(ss); 246 } 247 return vv; 248 } 249 250 public Vector getCarList() { 251 logger.log(BasicLevel.DEBUG, ""); 252 253 Vector vv = new Vector (); 254 Collection cars = this.getCars(); 255 256 Iterator iterator = cars.iterator(); 257 while (iterator.hasNext()) { 258 CarL car = (CarL) iterator.next(); 259 String ss = "Type=" + car.getType() + " Number=" + car.getNumber(); 260 vv.add(ss); 261 } 262 return vv; 263 } 264 265 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 getPhoneNumbers(); 274 public abstract void setPhoneNumbers(java.util.Collection phones); 275 276 public abstract java.util.Collection getCars(); 277 public abstract void setCars(java.util.Collection cars); 278 279 public abstract Integer getId(); 281 public abstract void setId(Integer id); 282 283 public abstract String getLastName( ); 284 public abstract void setLastName(String lname); 285 286 public abstract String getFirstName( ); 287 public abstract void setFirstName(String fname); 288 289 291 public void setEntityContext(EntityContext 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 cntx = new InitialContext (); 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 e) { 303 throw new javax.ejb.EJBException (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 { 331 logger.log(BasicLevel.DEBUG, ""); 332 checkCMRAccess(); 333 } 334 335 protected void checkCMRAccess() { 336 logger.log(BasicLevel.DEBUG, ""); 337 Collection col = getCars(); 338 } 339 } 340 | Popular Tags |