KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > titan > customer > CustomerBean


1 package com.titan.customer;
2
3 import com.titan.phone.*;
4 import com.titan.address.AddressLocal;
5 import com.titan.address.AddressHomeLocal;
6
7 import javax.naming.InitialContext;
8 import javax.ejb.EJBException;
9 import javax.ejb.EntityContext;
10 import javax.ejb.CreateException;
11 import javax.naming.NamingException;
12 import java.util.Date;
13 import java.util.Collection;
14 import java.util.Iterator;
15 import java.util.Vector;
16
17 public abstract class CustomerBean implements javax.ejb.EntityBean {
18
19     public Integer ejbCreate(Integer id) throws javax.ejb.CreateException{
20         this.setId(id);
21         return null;
22     }
23
24     public void ejbPostCreate(Integer id){
25     }
26
27
28     // business methods
29

30     public Name getName() {
31         Name name = new Name(getLastName(),getFirstName());
32         return name;
33     }
34     public void setName(Name name) {
35         setLastName(name.getLastName());
36         setFirstName(name.getFirstName());
37     }
38
39     public void setAddress(String street, String city, String state, String zip)
40         throws EJBException {
41         
42         AddressLocal addr = this.getHomeAddress( );
43
44         try {
45
46             if (addr == null) {
47                 // Customer doesn't have an address yet. Create a new one.
48
InitialContext cntx = new InitialContext( );
49                 AddressHomeLocal addrHome =
50                     (AddressHomeLocal)cntx.lookup("java:comp/env/ejb/AddressHomeLocal");
51                 addr = addrHome.createAddress(street, city, state, zip);
52                 this.setHomeAddress(addr);
53             } else {
54                 // Customer already has an address. Change its fields
55
addr.setStreet(street);
56                 addr.setCity(city);
57                 addr.setState(state);
58                 addr.setZip(zip);
59             }
60         } catch (NamingException ne) {
61             throw new EJBException(ne);
62         } catch (CreateException ce) {
63             throw new EJBException(ce);
64         }
65     }
66
67     public void setAddress(AddressDO addrValue) throws CreateException, NamingException {
68         String street = addrValue.getStreet();
69         String city = addrValue.getCity();
70         String state = addrValue.getState();
71         String zip = addrValue.getZip();
72
73         setAddress(street,city,state,zip);
74     }
75
76
77     public AddressDO getAddress() {
78         
79         AddressLocal addrLocal = this.getHomeAddress();
80         if (addrLocal == null) return null;
81         String street = addrLocal.getStreet();
82         String city = addrLocal.getCity();
83         String state = addrLocal.getState();
84         String zip = addrLocal.getZip();
85         AddressDO addrValue = new AddressDO(street,city,state,zip);
86         return addrValue;
87     }
88
89     public void addPhoneNumber(String number, byte type)
90         throws NamingException, CreateException {
91         InitialContext jndiEnc = new InitialContext( );
92         PhoneHomeLocal phoneHome = (PhoneHomeLocal)(jndiEnc.lookup("java:comp/env/ejb/PhoneHomeLocal"));
93
94         PhoneLocal phone = phoneHome.create(number,type);
95         Collection phoneNumbers = this.getPhoneNumbers( );
96         phoneNumbers.add(phone);
97     }
98
99     public void removePhoneNumber(byte typeToRemove) {
100
101         Collection phoneNumbers = this.getPhoneNumbers( );
102         Iterator iterator = phoneNumbers.iterator();
103
104         while(iterator.hasNext()){
105             PhoneLocal phone = (PhoneLocal)iterator.next();
106             if (phone.getType() == typeToRemove) {
107                 phoneNumbers.remove(phone);
108                 break;
109             }
110
111         }
112     }
113
114     public void updatePhoneNumber(String number, byte typeToUpdate) {
115
116
117         Collection phoneNumbers = this.getPhoneNumbers( );
118         Iterator iterator = phoneNumbers.iterator();
119         while(iterator.hasNext()){
120             PhoneLocal phone = (PhoneLocal)iterator.next();
121             if (phone.getType() == typeToUpdate) {
122                 phone.setNumber(number);
123                 break;
124             }
125         }
126     }
127
128     public Vector getPhoneList() {
129
130         Vector vv = new Vector();
131         Collection phoneNumbers = this.getPhoneNumbers();
132
133         Iterator iterator = phoneNumbers.iterator();
134         while(iterator.hasNext()) {
135             PhoneLocal phone = (PhoneLocal)iterator.next();
136             String ss = "type = '" + phone.getType() + "', number = '" + phone.getNumber() + "'";
137             vv.add(ss);
138         }
139
140         return vv;
141     }
142
143
144     // persistent relationships
145

146     public abstract AddressLocal getHomeAddress();
147     public abstract void setHomeAddress(AddressLocal address);
148
149     public abstract CreditCardLocal getCreditCard();
150     public abstract void setCreditCard(CreditCardLocal card);
151
152     public abstract java.util.Collection getPhoneNumbers( );
153     public abstract void setPhoneNumbers(java.util.Collection phones);
154
155     // abstract accessor methods
156
public abstract Integer getId();
157     public abstract void setId(Integer id);
158  
159     public abstract String getLastName( );
160     public abstract void setLastName(String lname);
161
162     public abstract String getFirstName( );
163     public abstract void setFirstName(String fname);
164
165     public abstract boolean getHasGoodCredit();
166     public abstract void setHasGoodCredit(boolean flag);
167
168     // standard call back methods
169

170     public void setEntityContext(EntityContext ec){}
171     public void unsetEntityContext(){}
172     public void ejbLoad(){}
173     public void ejbStore(){}
174     public void ejbActivate(){}
175     public void ejbPassivate(){}
176     public void ejbRemove() throws javax.ejb.RemoveException {}
177
178 }
179
Popular Tags