001: package org.objectweb.jonas.jtests.beans.ejbql;
002:
003: import javax.ejb.FinderException;
004: import javax.ejb.EJBException;
005: import javax.ejb.EntityContext;
006: import javax.ejb.CreateException;
007: import javax.naming.InitialContext;
008: import javax.naming.NamingException;
009:
010: import java.util.ArrayList;
011: import java.util.Collection;
012: import java.util.Date;
013: import java.util.Iterator;
014: import java.util.Set;
015: import java.util.Vector;
016: import java.rmi.RemoteException;
017:
018: public abstract class CustomerBean implements javax.ejb.EntityBean {
019:
020: private CruiseHomeLocal cruiseHomeLocal = null;
021:
022: private CustomerHomeLocal customerHomeLocal = null;
023:
024: private CreditCardHomeLocal creditCardHomeLocal = null;
025:
026: private CreditCompanyHomeLocal creditCompanyHomeLocal = null;
027:
028: private AddressHomeLocal addressHomeLocal = null;
029:
030: public Integer ejbCreate(Integer id)
031: throws javax.ejb.CreateException {
032: this .setId(id);
033: return null;
034: }
035:
036: public void ejbPostCreate(Integer id) {
037: }
038:
039: // business methods
040:
041: public Name getName() {
042: Name name = new Name(getLastName(), getFirstName());
043: return name;
044: }
045:
046: public void setName(Name name) {
047: setLastName(name.getLastName());
048: setFirstName(name.getFirstName());
049: }
050:
051: public void setAddress(String street, String city, String state,
052: String zip) throws EJBException {
053:
054: AddressLocal addr = this .getHomeAddress();
055:
056: try {
057:
058: if (addr == null) {
059: // Customer doesn't have an address yet. Create a new one.
060: InitialContext cntx = new InitialContext();
061: AddressHomeLocal addrHome = (AddressHomeLocal) cntx
062: .lookup("java:comp/env/ejb/AddressHomeLocal");
063: addr = addrHome.createAddress(street, city, state, zip);
064: this .setHomeAddress(addr);
065: } else {
066: // Customer already has an address. Change its fields
067: addr.setStreet(street);
068: addr.setCity(city);
069: addr.setState(state);
070: addr.setZip(zip);
071: }
072: } catch (NamingException ne) {
073: throw new EJBException(ne);
074: } catch (CreateException ce) {
075: throw new EJBException(ce);
076: }
077: }
078:
079: public void setAddress(AddressDO addrValue) throws CreateException,
080: NamingException {
081: String street = addrValue.getStreet();
082: String city = addrValue.getCity();
083: String state = addrValue.getState();
084: String zip = addrValue.getZip();
085:
086: setAddress(street, city, state, zip);
087: }
088:
089: public AddressDO getAddress() {
090:
091: AddressLocal addrLocal = this .getHomeAddress();
092: if (addrLocal == null) {
093: return null;
094: }
095: String street = addrLocal.getStreet();
096: String city = addrLocal.getCity();
097: String state = addrLocal.getState();
098: String zip = addrLocal.getZip();
099: AddressDO addrValue = new AddressDO(street, city, state, zip);
100: return addrValue;
101: }
102:
103: public void addPhoneNumber(String number, byte type)
104: throws NamingException, CreateException, RemoteException {
105: InitialContext jndiEnc = new InitialContext();
106: PhoneHomeLocal phoneHome = (PhoneHomeLocal) (jndiEnc
107: .lookup("java:comp/env/ejb/PhoneHomeLocal"));
108:
109: PhoneLocal phone = phoneHome.create(number, type);
110: Collection phoneNumbers = this .getPhoneNumbers();
111: phoneNumbers.add(phone);
112: }
113:
114: public void removePhoneNumber(byte typeToRemove) {
115:
116: Collection phoneNumbers = this .getPhoneNumbers();
117: Iterator iterator = phoneNumbers.iterator();
118:
119: while (iterator.hasNext()) {
120: PhoneLocal phone = (PhoneLocal) iterator.next();
121: if (phone.getType() == typeToRemove) {
122: phoneNumbers.remove(phone);
123: break;
124: }
125:
126: }
127: }
128:
129: public void updatePhoneNumber(String number, byte typeToUpdate) {
130:
131: Collection phoneNumbers = this .getPhoneNumbers();
132: Iterator iterator = phoneNumbers.iterator();
133: while (iterator.hasNext()) {
134: PhoneLocal phone = (PhoneLocal) iterator.next();
135: if (phone.getType() == typeToUpdate) {
136: phone.setNumber(number);
137: break;
138: }
139: }
140: }
141:
142: public Vector getPhoneList() {
143:
144: Vector vv = new Vector();
145: Collection phoneNumbers = this .getPhoneNumbers();
146:
147: Iterator iterator = phoneNumbers.iterator();
148: while (iterator.hasNext()) {
149: PhoneLocal phone = (PhoneLocal) iterator.next();
150: String ss = "Type=" + phone.getType() + " Number="
151: + phone.getNumber();
152: vv.add(ss);
153: }
154:
155: return vv;
156: }
157:
158: public Collection ejbHomeCallFindOnCruise(Integer cruiseId)
159: throws FinderException {
160:
161: ArrayList customers_id = new ArrayList();
162: CruiseLocal crloc = null;
163: if (cruiseId != null) {
164: crloc = cruiseHomeLocal.findByPrimaryKey(cruiseId);
165: }
166: Collection customers = customerHomeLocal.findOnCruise(crloc);
167: Iterator customer = customers.iterator();
168: while (customer.hasNext()) {
169: CustomerLocal customer_local = (CustomerLocal) customer
170: .next();
171: customers_id.add(customer_local.getId());
172: }
173: return customers_id;
174: }
175:
176: public Collection ejbHomeCallFindByAddressLocal(Integer add)
177: throws FinderException {
178: ArrayList customers_id = new ArrayList();
179: AddressLocal addloc = null;
180: if (add != null) {
181: addloc = addressHomeLocal.findByPrimaryKey(add);
182: }
183: Collection customers = customerHomeLocal
184: .findByAddressLocal(addloc);
185: Iterator customer = customers.iterator();
186: while (customer.hasNext()) {
187: CustomerLocal customer_local = (CustomerLocal) customer
188: .next();
189: customers_id.add(customer_local.getId());
190: }
191: return customers_id;
192: }
193:
194: public Collection ejbHomeCallFindByParameterIsNull(Integer cc)
195: throws FinderException {
196: ArrayList customers_id = new ArrayList();
197: CreditCardLocal ccloc = null;
198: if (cc != null) {
199: ccloc = creditCardHomeLocal.findByPrimaryKey(cc);
200: }
201: Collection customers = customerHomeLocal
202: .findByParameterIsNull(ccloc);
203: Iterator customer = customers.iterator();
204: while (customer.hasNext()) {
205: CustomerLocal customer_local = (CustomerLocal) customer
206: .next();
207: customers_id.add(customer_local.getId());
208: }
209: return customers_id;
210: }
211:
212: public void setCreditCard(Date expCreditcard,
213: String numbCreditcard, String nameCreditcard,
214: String orgCreditcard, String nameCreditCompany,
215: String streetAddress, String cityAddress,
216: String stateAddress, String zipAddress) {
217:
218: // Create the CreditCompany, the Address and the CreditCard
219: // Associate the Address to the CreditCompany, the CreditCompany to the
220: // CreditCard and the CreditCard to the current customer
221: try {
222: CreditCardLocal creditcard = creditCardHomeLocal.create(
223: expCreditcard, numbCreditcard, nameCreditcard,
224: orgCreditcard);
225: this .setCreditCard(creditcard);
226: CreditCompanyLocal creditcompany = creditCompanyHomeLocal
227: .create(nameCreditCompany);
228: creditcard.setCreditCompany(creditcompany);
229: AddressLocal address = addressHomeLocal.createAddress(
230: streetAddress, cityAddress, stateAddress,
231: zipAddress);
232: creditcompany.setAddress(address);
233: } catch (CreateException e) {
234: System.out
235: .println("Probleme dans creation du CreditCompany/Address/CrediCard");
236: e.printStackTrace(System.out);
237: }
238:
239: }
240:
241: // persistent relationships
242:
243: public abstract AddressLocal getHomeAddress();
244:
245: public abstract void setHomeAddress(AddressLocal address);
246:
247: public abstract CreditCardLocal getCreditCard();
248:
249: public abstract void setCreditCard(CreditCardLocal card);
250:
251: public abstract java.util.Collection getPhoneNumbers();
252:
253: public abstract void setPhoneNumbers(java.util.Collection phones);
254:
255: // abstract accessor methods
256: public abstract Integer getId();
257:
258: public abstract void setId(Integer id);
259:
260: public abstract String getLastName();
261:
262: public abstract void setLastName(String lname);
263:
264: public abstract String getFirstName();
265:
266: public abstract void setFirstName(String fname);
267:
268: public abstract boolean getHasGoodCredit();
269:
270: public abstract void setHasGoodCredit(boolean flag);
271:
272: // abstract ejbSelect() methods
273: public abstract String ejbSelectAddrCity(Integer id)
274: throws FinderException;
275:
276: public abstract String ejbSelectLastName(Integer id)
277: throws FinderException;
278:
279: public abstract Set ejbSelectFirstName() throws FinderException;
280:
281: public abstract Collection ejbSelectDistinctFirstName()
282: throws FinderException;
283:
284: public abstract AddressLocal ejbSelectAddr(Integer id)
285: throws FinderException;
286:
287: public abstract Collection ejbSelectAllCreditCardAddr()
288: throws FinderException;
289:
290: public abstract Collection ejbSelectRogerAddr()
291: throws FinderException;
292:
293: public abstract long ejbSelectCountCreditCard()
294: throws FinderException;
295:
296: public abstract Collection ejbSelectCreditCardNumbers()
297: throws FinderException;
298:
299: public abstract long ejbSelectCountOfCustomersWithId1(Integer id)
300: throws FinderException;
301:
302: public abstract long ejbSelectCountOfCustomersWithId2(int id)
303: throws FinderException;
304:
305: public abstract long ejbSelectCountOfCustomersRingo()
306: throws FinderException;
307:
308: //
309: // Public Home method required to test the private ejbSelectXXXX method
310: public String ejbHomeSelectAddrCity(Integer id)
311: throws FinderException {
312: return this .ejbSelectAddrCity(id);
313: }
314:
315: public String ejbHomeSelectLastName(Integer id)
316: throws FinderException {
317: return this .ejbSelectLastName(id);
318: }
319:
320: public Set ejbHomeSelectFirstName() throws FinderException {
321: return this .ejbSelectFirstName();
322: }
323:
324: public Collection ejbHomeSelectDistinctFirstName()
325: throws FinderException {
326: return this .ejbSelectDistinctFirstName();
327: }
328:
329: public Integer ejbHomeSelectAddr(Integer id) throws FinderException {
330: AddressLocal address = this .ejbSelectAddr(id);
331: return address.getId();
332: }
333:
334: public Collection ejbHomeSelectAllCreditCardAddr()
335: throws FinderException {
336: Collection addresses = this .ejbSelectAllCreditCardAddr();
337: ArrayList addresses_id = new ArrayList();
338: Iterator iAddr = addresses.iterator();
339: while (iAddr.hasNext()) {
340: AddressLocal address = (AddressLocal) iAddr.next();
341: addresses_id.add(address.getId());
342: }
343: return addresses_id;
344: }
345:
346: public Collection ejbHomeSelectRogerAddr() throws FinderException {
347: Collection addresses = this .ejbSelectRogerAddr();
348: ArrayList addresses_id = new ArrayList();
349: Iterator iAddr = addresses.iterator();
350: while (iAddr.hasNext()) {
351: AddressLocal address = (AddressLocal) iAddr.next();
352: addresses_id.add(address.getId());
353: }
354: return addresses_id;
355: }
356:
357: public long ejbHomeGetCountCreditCard() throws FinderException {
358: return this .ejbSelectCountCreditCard();
359: }
360:
361: public Collection ejbHomeGetCreditCardNumbers()
362: throws FinderException {
363: return this .ejbSelectCreditCardNumbers();
364: }
365:
366: public long ejbHomeGetCountCustomersWithId1(Integer id)
367: throws FinderException {
368: return this .ejbSelectCountOfCustomersWithId1(id);
369: }
370:
371: public long ejbHomeGetCountCustomersWithId2(int id)
372: throws FinderException {
373: return this .ejbSelectCountOfCustomersWithId2(id);
374: }
375:
376: public long ejbHomeGetCountCustomersRingo() throws FinderException {
377: return this .ejbSelectCountOfCustomersRingo();
378: }
379:
380: // standard call back methods
381:
382: public void setEntityContext(EntityContext ec) {
383: try {
384: InitialContext cntx = new InitialContext();
385: cruiseHomeLocal = (CruiseHomeLocal) cntx
386: .lookup("java:comp/env/ejb/CruiseHomeLocal");
387: customerHomeLocal = (CustomerHomeLocal) cntx
388: .lookup("java:comp/env/ejb/CustomerHomeLocal");
389: creditCardHomeLocal = (CreditCardHomeLocal) cntx
390: .lookup("java:comp/env/ejb/CreditCardHomeLocal");
391: creditCompanyHomeLocal = (CreditCompanyHomeLocal) cntx
392: .lookup("java:comp/env/ejb/CreditCompanyHomeLocal");
393: addressHomeLocal = (AddressHomeLocal) cntx
394: .lookup("java:comp/env/ejb/AddressHomeLocal");
395: } catch (Exception e) {
396: throw new javax.ejb.EJBException(e);
397: }
398: }
399:
400: public void unsetEntityContext() {
401: }
402:
403: public void ejbLoad() {
404: }
405:
406: public void ejbStore() {
407: }
408:
409: public void ejbActivate() {
410: }
411:
412: public void ejbPassivate() {
413: }
414:
415: public void ejbRemove() throws javax.ejb.RemoveException {
416: }
417:
418: }
|