KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > commerce > CustomerBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.commerce;
23
24 import java.util.Collection JavaDoc;
25 import javax.ejb.CreateException JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.EntityBean JavaDoc;
28 import javax.ejb.EntityContext JavaDoc;
29 import javax.ejb.FinderException JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32
33 import org.jboss.varia.autonumber.AutoNumberFactory;
34
35 public abstract class CustomerBean implements EntityBean JavaDoc {
36     transient private EntityContext JavaDoc ctx;
37
38     public Long JavaDoc ejbCreate() throws CreateException JavaDoc {
39         setId(new Long JavaDoc(AutoNumberFactory.getNextInteger("Customer").longValue()));
40         return null;
41     }
42
43     public void ejbPostCreate() { }
44
45     public abstract Long JavaDoc getId();
46     public abstract void setId(Long JavaDoc id);
47     
48     public abstract String JavaDoc getName();
49     public abstract void setName(String JavaDoc 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 JavaDoc getOrders();
62     public abstract void setOrders(Collection JavaDoc c);
63
64     public abstract Collection JavaDoc getAddresses();
65     public abstract void setAddresses(Collection JavaDoc c);
66
67    public abstract Collection JavaDoc ejbSelectAddressesInCAForCustomer(Long JavaDoc id)
68       throws FinderException JavaDoc;
69
70     public void setEntityContext(EntityContext JavaDoc 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 JavaDoc)userLocal.getPrimaryKey());
89         } catch(Exception JavaDoc e) {
90             throw new EJBException JavaDoc("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 JavaDoc)user.getPrimaryKey());
101         } catch(Exception JavaDoc e) {
102             throw new EJBException JavaDoc("Error converting user into user local", e);
103         }
104     }
105
106
107     private UserHome getUserHome() {
108         try {
109             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
110
111             Object JavaDoc ref = jndiContext.lookup("java:comp/env/ejb/User");
112
113          // remote interfaces have to be narrowed
114
return (UserHome)PortableRemoteObject.narrow (ref, UserHome.class);
115         } catch(Exception JavaDoc e) {
116             throw new EJBException JavaDoc("Exception in getUserHome: ", e);
117         }
118     }
119     
120     private UserLocalHome getUserLocalHome() {
121         try {
122             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
123
124             return (UserLocalHome)
125                jndiContext.lookup("java:comp/env/ejb/UserLocal");
126         } catch(Exception JavaDoc e) {
127             throw new EJBException JavaDoc("Exception in getUserLocalHome: ", e);
128         }
129     }
130
131 }
132
Popular Tags