1 22 package org.jboss.test.cmp2.commerce; 23 24 import java.util.Collection ; 25 import javax.ejb.CreateException ; 26 import javax.ejb.EJBException ; 27 import javax.ejb.EntityBean ; 28 import javax.ejb.EntityContext ; 29 import javax.ejb.FinderException ; 30 import javax.naming.InitialContext ; 31 import javax.rmi.PortableRemoteObject ; 32 33 import org.jboss.varia.autonumber.AutoNumberFactory; 34 35 public abstract class CustomerBean implements EntityBean { 36 transient private EntityContext ctx; 37 38 public Long ejbCreate() throws CreateException { 39 setId(new Long (AutoNumberFactory.getNextInteger("Customer").longValue())); 40 return null; 41 } 42 43 public void ejbPostCreate() { } 44 45 public abstract Long getId(); 46 public abstract void setId(Long id); 47 48 public abstract String getName(); 49 public abstract void setName(String name); 50 51 public User getUser() { 52 return userLocalToUser(getUserLocal()); 53 } 54 public void setUser(User user) { 55 setUserLocal(userToUserLocal(user)); 56 } 57 58 public abstract UserLocal getUserLocal(); 59 public abstract void setUserLocal(UserLocal user); 60 61 public abstract Collection getOrders(); 62 public abstract void setOrders(Collection c); 63 64 public abstract Collection getAddresses(); 65 public abstract void setAddresses(Collection c); 66 67 public abstract Collection ejbSelectAddressesInCAForCustomer(Long id) 68 throws FinderException ; 69 70 public void setEntityContext(EntityContext ctx) { 71 this.ctx = ctx; 72 } 73 public void unsetEntityContext() { 74 this.ctx = null; 75 } 76 public void ejbActivate() { } 77 public void ejbPassivate() { } 78 public void ejbLoad() { } 79 public void ejbStore() { } 80 public void ejbRemove() { } 81 82 protected User userLocalToUser(UserLocal userLocal) { 83 if(userLocal == null) { 84 return null; 85 } 86 UserHome userHome = getUserHome(); 87 try { 88 return userHome.findByPrimaryKey((String )userLocal.getPrimaryKey()); 89 } catch(Exception e) { 90 throw new EJBException ("Error converting user local into user", e); 91 } 92 } 93 94 protected UserLocal userToUserLocal(User user) { 95 if(user == null) { 96 return null; 97 } 98 UserLocalHome userLocalHome = getUserLocalHome(); 99 try { 100 return userLocalHome.findByPrimaryKey((String )user.getPrimaryKey()); 101 } catch(Exception e) { 102 throw new EJBException ("Error converting user into user local", e); 103 } 104 } 105 106 107 private UserHome getUserHome() { 108 try { 109 InitialContext jndiContext = new InitialContext (); 110 111 Object ref = jndiContext.lookup("java:comp/env/ejb/User"); 112 113 return (UserHome)PortableRemoteObject.narrow (ref, UserHome.class); 115 } catch(Exception e) { 116 throw new EJBException ("Exception in getUserHome: ", e); 117 } 118 } 119 120 private UserLocalHome getUserLocalHome() { 121 try { 122 InitialContext jndiContext = new InitialContext (); 123 124 return (UserLocalHome) 125 jndiContext.lookup("java:comp/env/ejb/UserLocal"); 126 } catch(Exception e) { 127 throw new EJBException ("Exception in getUserLocalHome: ", e); 128 } 129 } 130 131 } 132 | Popular Tags |