001: package com.titan.customer;
002:
003: import com.titan.phone.*;
004: import com.titan.address.AddressLocal;
005: import com.titan.address.AddressHomeLocal;
006:
007: import javax.naming.InitialContext;
008: import javax.ejb.EJBException;
009: import javax.ejb.EntityContext;
010: import javax.ejb.CreateException;
011: import javax.naming.NamingException;
012: import java.util.Date;
013: import java.util.Collection;
014: import java.util.Iterator;
015: import java.util.Vector;
016:
017: public abstract class CustomerBean implements javax.ejb.EntityBean {
018:
019: public Integer ejbCreate(Integer id)
020: throws javax.ejb.CreateException {
021: this .setId(id);
022: return null;
023: }
024:
025: public void ejbPostCreate(Integer id) {
026: }
027:
028: // business methods
029:
030: public Name getName() {
031: Name name = new Name(getLastName(), getFirstName());
032: return name;
033: }
034:
035: public void setName(Name name) {
036: setLastName(name.getLastName());
037: setFirstName(name.getFirstName());
038: }
039:
040: public void setAddress(String street, String city, String state,
041: String zip) throws EJBException {
042:
043: AddressLocal addr = this .getHomeAddress();
044:
045: try {
046:
047: if (addr == null) {
048: // Customer doesn't have an address yet. Create a new one.
049: InitialContext cntx = new InitialContext();
050: AddressHomeLocal addrHome = (AddressHomeLocal) cntx
051: .lookup("java:comp/env/ejb/AddressHomeLocal");
052: addr = addrHome.createAddress(street, city, state, zip);
053: this .setHomeAddress(addr);
054: } else {
055: // Customer already has an address. Change its fields
056: addr.setStreet(street);
057: addr.setCity(city);
058: addr.setState(state);
059: addr.setZip(zip);
060: }
061: } catch (NamingException ne) {
062: throw new EJBException(ne);
063: } catch (CreateException ce) {
064: throw new EJBException(ce);
065: }
066: }
067:
068: public void setAddress(AddressDO addrValue) throws CreateException,
069: NamingException {
070: String street = addrValue.getStreet();
071: String city = addrValue.getCity();
072: String state = addrValue.getState();
073: String zip = addrValue.getZip();
074:
075: setAddress(street, city, state, zip);
076: }
077:
078: public AddressDO getAddress() {
079:
080: AddressLocal addrLocal = this .getHomeAddress();
081: if (addrLocal == null)
082: return null;
083: String street = addrLocal.getStreet();
084: String city = addrLocal.getCity();
085: String state = addrLocal.getState();
086: String zip = addrLocal.getZip();
087: AddressDO addrValue = new AddressDO(street, city, state, zip);
088: return addrValue;
089: }
090:
091: public void addPhoneNumber(String number, byte type)
092: throws NamingException, CreateException {
093: InitialContext jndiEnc = new InitialContext();
094: PhoneHomeLocal phoneHome = (PhoneHomeLocal) (jndiEnc
095: .lookup("java:comp/env/ejb/PhoneHomeLocal"));
096:
097: PhoneLocal phone = phoneHome.create(number, type);
098: Collection phoneNumbers = this .getPhoneNumbers();
099: phoneNumbers.add(phone);
100: }
101:
102: public void removePhoneNumber(byte typeToRemove) {
103:
104: Collection phoneNumbers = this .getPhoneNumbers();
105: Iterator iterator = phoneNumbers.iterator();
106:
107: while (iterator.hasNext()) {
108: PhoneLocal phone = (PhoneLocal) iterator.next();
109: if (phone.getType() == typeToRemove) {
110: phoneNumbers.remove(phone);
111: break;
112: }
113:
114: }
115: }
116:
117: public void updatePhoneNumber(String number, byte typeToUpdate) {
118:
119: Collection phoneNumbers = this .getPhoneNumbers();
120: Iterator iterator = phoneNumbers.iterator();
121: while (iterator.hasNext()) {
122: PhoneLocal phone = (PhoneLocal) iterator.next();
123: if (phone.getType() == typeToUpdate) {
124: phone.setNumber(number);
125: break;
126: }
127: }
128: }
129:
130: public Vector getPhoneList() {
131:
132: Vector vv = new Vector();
133: Collection phoneNumbers = this .getPhoneNumbers();
134:
135: Iterator iterator = phoneNumbers.iterator();
136: while (iterator.hasNext()) {
137: PhoneLocal phone = (PhoneLocal) iterator.next();
138: String ss = "type = '" + phone.getType() + "', number = '"
139: + phone.getNumber() + "'";
140: vv.add(ss);
141: }
142:
143: return vv;
144: }
145:
146: // persistent relationships
147:
148: public abstract AddressLocal getHomeAddress();
149:
150: public abstract void setHomeAddress(AddressLocal address);
151:
152: public abstract CreditCardLocal getCreditCard();
153:
154: public abstract void setCreditCard(CreditCardLocal card);
155:
156: public abstract java.util.Collection getPhoneNumbers();
157:
158: public abstract void setPhoneNumbers(java.util.Collection phones);
159:
160: public abstract java.util.Collection getReservations();
161:
162: public abstract void setReservations(
163: java.util.Collection reservations);
164:
165: // abstract accessor methods
166: public abstract Integer getId();
167:
168: public abstract void setId(Integer id);
169:
170: public abstract String getLastName();
171:
172: public abstract void setLastName(String lname);
173:
174: public abstract String getFirstName();
175:
176: public abstract void setFirstName(String fname);
177:
178: public abstract boolean getHasGoodCredit();
179:
180: public abstract void setHasGoodCredit(boolean flag);
181:
182: // standard call back methods
183:
184: public void setEntityContext(EntityContext ec) {
185: }
186:
187: public void unsetEntityContext() {
188: }
189:
190: public void ejbLoad() {
191: }
192:
193: public void ejbStore() {
194: }
195:
196: public void ejbActivate() {
197: }
198:
199: public void ejbPassivate() {
200: }
201:
202: public void ejbRemove() throws javax.ejb.RemoveException {
203: }
204:
205: }
|