001: package org.objectweb.jonas.jtests.beans.ejbql;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Date;
006: import java.util.Iterator;
007: import java.util.Set;
008:
009: import javax.ejb.CreateException;
010: import javax.ejb.EntityContext;
011: import javax.ejb.FinderException;
012: import javax.ejb.RemoveException;
013: import javax.naming.InitialContext;
014:
015: public abstract class ReservationBean implements javax.ejb.EntityBean {
016:
017: private SequenceSessionLocalHome seqHome = null;
018:
019: private SequenceSessionLocal seqLocal = null;
020:
021: private CustomerHomeLocal customerhl = null;
022:
023: private CruiseHomeLocal cruisehl = null;
024:
025: private CabinHomeLocal cabinhl = null;
026:
027: public Object ejbCreate(Integer cruiseId, Collection customers)
028: throws CreateException {
029: int id = seqLocal.getNextNumberInSequence("Reservation");
030: setId(new Integer(id));
031: return null;
032: }
033:
034: public void ejbPostCreate(Integer cruiseId, Collection customers) {
035:
036: try {
037: CruiseLocal cruise = cruisehl.findByPrimaryKey(cruiseId);
038: setCruise(cruise);
039: } catch (FinderException e) {
040: System.out.println("Cruise not find :" + cruiseId);
041: }
042:
043: Collection myCustomers = this .getCustomers();
044:
045: ArrayList al;
046: if (customers == null)
047: al = new ArrayList();
048: else {
049: if (customers.size() == -1)
050: al = new ArrayList();
051: else {
052: al = new ArrayList(customers.size());
053: for (Iterator it = customers.iterator(); it.hasNext();) {
054: Integer customerid = new Integer("-1");
055: try {
056: customerid = (Integer) it.next();
057: CustomerLocal customer = customerhl
058: .findByPrimaryKey(customerid);
059: al.add(customer);
060: } catch (FinderException e) {
061: System.out.println("Customer not found "
062: + customerid);
063: }
064: }
065: }
066: }
067: myCustomers.addAll(al);
068: }
069:
070: // relationship fields
071: public void setAllCabins(Set cabins) {
072:
073: ArrayList al;
074: if (cabins == null)
075: al = new ArrayList();
076: else {
077: if (cabins.size() == -1)
078: al = new ArrayList();
079: else {
080: al = new ArrayList(cabins.size());
081: for (Iterator it = cabins.iterator(); it.hasNext();) {
082: Integer cabinid = new Integer("-1");
083: try {
084: cabinid = (Integer) it.next();
085: CabinLocal cabin = cabinhl
086: .findByPrimaryKey(cabinid);
087: al.add(cabin);
088: } catch (FinderException e) {
089: System.out
090: .println("Cabin not found " + cabinid);
091: }
092: }
093: }
094: }
095: this .getCabins().addAll(al);
096: }
097:
098: // persistent fields getter and setter
099: public abstract CruiseLocal getCruise();
100:
101: public abstract void setCruise(CruiseLocal cruise);
102:
103: public abstract Set getCabins();
104:
105: public abstract void setCabins(Set cabins);
106:
107: public abstract Set getCustomers();
108:
109: public abstract void setCustomers(Set customers);
110:
111: public abstract Integer getId();
112:
113: public abstract void setId(Integer id);
114:
115: public abstract Date getDate();
116:
117: public abstract void setDate(Date date);
118:
119: public abstract double getAmountPaid();
120:
121: public abstract void setAmountPaid(double amount);
122:
123: // abstract ejbSelect() methods
124: public abstract long ejbSelectCountOfReservations()
125: throws FinderException;
126:
127: public abstract double ejbSelectMinAmountOfReservations()
128: throws FinderException;
129:
130: public abstract double ejbSelectMinAmountForCruise(String cName)
131: throws FinderException;
132:
133: public abstract Collection ejbSelectAmountsForCruise(String cName)
134: throws FinderException;
135:
136: public abstract double ejbSelectMaxAmountOfReservations()
137: throws FinderException;
138:
139: public abstract long ejbSelectCountOfReservationsForCustomer(
140: CustomerLocal c) throws FinderException;
141:
142: public abstract long ejbSelectAmountOfReservationsForCustomer(
143: CustomerLocal c) throws FinderException;
144:
145: // Public Home method required to test the private ejbSelectXXXX method
146: public long ejbHomeGetCountOfReservations() throws FinderException {
147: return this .ejbSelectCountOfReservations();
148: }
149:
150: public double ejbHomeGetMinAmountOfReservations()
151: throws FinderException {
152: return this .ejbSelectMinAmountOfReservations();
153: }
154:
155: public double ejbHomeGetMinAmountForCruise(String cName)
156: throws FinderException {
157: return this .ejbSelectMinAmountForCruise(cName);
158: }
159:
160: public double ejbHomeGetMaxAmountOfReservations()
161: throws FinderException {
162: return this .ejbSelectMaxAmountOfReservations();
163: }
164:
165: public Collection ejbHomeGetAmountsForCruise(String cName)
166: throws FinderException {
167: return this .ejbSelectAmountsForCruise(cName);
168: }
169:
170: public long ejbHomeGetCountOfReservationsForCustomer(Integer custId)
171: throws FinderException {
172: return this .ejbSelectCountOfReservationsForCustomer(customerhl
173: .findByPrimaryKey(custId));
174: }
175:
176: public double ejbHomeGetAmountOfReservationsForCustomer(
177: Integer custId) throws FinderException {
178: return this .ejbSelectAmountOfReservationsForCustomer(customerhl
179: .findByPrimaryKey(custId));
180: }
181:
182: // standard call back methods
183: public void setEntityContext(EntityContext ec) {
184: try {
185: InitialContext cntx = new InitialContext();
186: seqHome = (SequenceSessionLocalHome) cntx
187: .lookup("java:comp/env/ejb/SequenceSessionLocalHome");
188: customerhl = (CustomerHomeLocal) cntx
189: .lookup("java:comp/env/ejb/CustomerLocalHome");
190: cruisehl = (CruiseHomeLocal) cntx
191: .lookup("java:comp/env/ejb/CruiseLocalHome");
192: cabinhl = (CabinHomeLocal) cntx
193: .lookup("java:comp/env/ejb/CabinLocalHome");
194: seqLocal = seqHome.create();
195: } catch (Exception e) {
196: throw new javax.ejb.EJBException(e);
197: }
198: }
199:
200: public void unsetEntityContext() {
201: }
202:
203: public void ejbLoad() {
204: }
205:
206: public void ejbStore() {
207: }
208:
209: public void ejbActivate() {
210: }
211:
212: public void ejbPassivate() {
213: }
214:
215: public void ejbRemove() throws RemoveException {
216: }
217:
218: }
|