001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s):
022: * --------------------------------------------------------------------------
023: * $Id: ServletTest3.java 7140 2005-07-28 13:35:39Z benoitf $
024: * --------------------------------------------------------------------------
025: */package servlets;
026:
027: import java.io.IOException;
028: import java.io.PrintWriter;
029: import java.util.ArrayList;
030: import java.util.Calendar;
031: import java.util.Collection;
032: import java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.Set;
035: import java.util.Vector;
036:
037: import javax.naming.Context;
038: import javax.naming.InitialContext;
039: import javax.servlet.ServletException;
040: import javax.servlet.http.HttpServlet;
041: import javax.servlet.http.HttpServletRequest;
042: import javax.servlet.http.HttpServletResponse;
043:
044: import com.titan.address.AddressHomeLocal;
045: import com.titan.address.AddressLocal;
046: import com.titan.cabin.CabinHomeLocal;
047: import com.titan.cabin.CabinLocal;
048: import com.titan.cruise.CruiseHomeLocal;
049: import com.titan.cruise.CruiseLocal;
050: import com.titan.customer.CreditCardHomeLocal;
051: import com.titan.customer.CreditCardLocal;
052: import com.titan.customer.CustomerHomeLocal;
053: import com.titan.customer.CustomerLocal;
054: import com.titan.customer.Name;
055: import com.titan.phone.PhoneHomeLocal;
056: import com.titan.reservation.ReservationHomeLocal;
057: import com.titan.reservation.ReservationLocal;
058: import com.titan.ship.ShipHomeLocal;
059: import com.titan.ship.ShipLocal;
060:
061: /**
062: * This servlet is used to test O'Reilly examples
063: * @author JOnAS team
064: */
065: public class ServletTest3 extends HttpServlet {
066:
067: /**
068: * Called by the server (via the service method) to allow a servlet to
069: * handle a GET request.
070: * @param request an HttpServletRequest object that contains the request the
071: * client has made of the servlet
072: * @param response an HttpServletResponse object that contains the response
073: * the servlet sends to the client
074: * @throws IOException if an input or output error is detected when the
075: * servlet handles the GET request
076: * @throws ServletException if the request for the GET could not be handled
077: */
078: public void doGet(HttpServletRequest request,
079: HttpServletResponse response) throws IOException,
080: ServletException {
081:
082: boolean ok = true;
083: response.setContentType("text/html");
084: PrintWriter out = response.getWriter();
085:
086: out.println("<html>");
087: out.println("<head>");
088: out.println("<title>");
089: out.println("O'Reilly Examples</title>");
090: out.println("<link rel=\"stylesheet\" href=\"style.css\" />");
091: out.println("</head>");
092: out
093: .println("<body style=\"background : white; color : black;\">");
094: out.println("<h1>EJB QL Examples from Chapter 8</h1>");
095:
096: Context initialContext = null;
097: try {
098: initialContext = new InitialContext();
099: } catch (Exception e) {
100: out.print("<p>ERROR: Cannot get initial context for JNDI: "
101: + e + "</p>");
102: return;
103: }
104:
105: out.println("<a href=\"index.html\"><b>Back to Menu</b></a>");
106:
107: // Connecting to CustomerHomeLocal and CreditCardHomeLocal thru JNDI
108: CustomerHomeLocal customerhome = null;
109: CreditCardHomeLocal cardhome = null;
110: AddressHomeLocal addresshome = null;
111: ShipHomeLocal shiphome = null;
112: CruiseHomeLocal cruisehome = null;
113: ReservationHomeLocal reservationhome = null;
114: CabinHomeLocal cabinhome = null;
115: PhoneHomeLocal phonehome = null;
116: try {
117: customerhome = (CustomerHomeLocal) initialContext
118: .lookup("java:comp/env/ejb/CustomerHomeLocal");
119: cardhome = (CreditCardHomeLocal) initialContext
120: .lookup("java:comp/env/ejb/CreditCardHomeLocal");
121: addresshome = (AddressHomeLocal) initialContext
122: .lookup("java:comp/env/ejb/AddressHomeLocal");
123: shiphome = (ShipHomeLocal) initialContext
124: .lookup("java:comp/env/ejb/ShipHomeLocal");
125: cruisehome = (CruiseHomeLocal) initialContext
126: .lookup("java:comp/env/ejb/CruiseHomeLocal");
127: reservationhome = (ReservationHomeLocal) initialContext
128: .lookup("java:comp/env/ejb/ReservationHomeLocal");
129: cabinhome = (CabinHomeLocal) initialContext
130: .lookup("java:comp/env/ejb/CabinHomeLocal");
131: phonehome = (PhoneHomeLocal) initialContext
132: .lookup("java:comp/env/ejb/PhoneHomeLocal");
133: } catch (Exception e) {
134: out
135: .println("<p>ERROR: Cannot lookup java:comp/env/ejb/XXHomeLocal: "
136: + e + "</p>");
137: return;
138: }
139:
140: String cities[] = new String[6];
141: cities[0] = "Minneapolis";
142: cities[1] = "St. Paul";
143: cities[2] = "Rochester";
144: cities[3] = "Winona";
145: cities[4] = "Wayzata";
146: cities[5] = "Eagan";
147:
148: out.println("<h2>Example showing Sample EJB-QL</h2>");
149:
150: out.print("<H3>Creating a Ship and Cruise</H3>");
151: ShipLocal shipA = null;
152: CruiseLocal cruiseA = null;
153: try {
154: shipA = shiphome.create(new Integer(10772), "Ship A",
155: 30000.0);
156: cruiseA = cruisehome.create("Cruise A", shipA);
157: } catch (Exception ex) {
158: ok = false;
159: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
160: }
161: out.println("<ul>");
162: out.println("<li>cruise.getName() = '" + cruiseA.getName()
163: + "'</li>");
164: out.println("<li>ship.getName() = '" + shipA.getName()
165: + "'</li>");
166: out.println("<li>cruise.getShip().getName() = '"
167: + cruiseA.getShip().getName() + "'</li>");
168: out.println("</ul>");
169:
170: out
171: .print("<H3>Creating Ship Beans with Various Tonnage Values</H3>");
172: out.print("<ul>");
173: for (int jj = 1; jj <= 10; jj++) {
174: try {
175: ShipLocal ship = shiphome.create(new Integer(jj),
176: "Ship " + jj, 30000.0 + (10000.0 * jj));
177: printShip(out, ship);
178: } catch (Exception ex) {
179: ok = false;
180: out
181: .print("<p>ERROR: Exception caught : " + ex
182: + "</p>");
183: }
184:
185: }
186: out.print("</ul>");
187:
188: out.print("<H3>Finding Ships with Exactly 100K Tonnage</H3>");
189: out
190: .print("<p><code>SELECT OBJECT(s) FROM JE2_Ship AS s WHERE s.tonnage = ?1</code></p>");
191: Collection ships100k = null;
192: try {
193: ships100k = shiphome.findByTonnage(new Double(100000.0));
194: } catch (Exception ex) {
195: ok = false;
196: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
197: }
198: out.print("<p><i>Number of ships = " + ships100k.size()
199: + " <br>Expected = 1 ships</i></p>");
200: if (ships100k.size() != 1) {
201: ok = false;
202: }
203: Iterator iterator = ships100k.iterator();
204: out.print("<ul>");
205: while (iterator.hasNext()) {
206: ShipLocal ship = (ShipLocal) (iterator.next());
207: printShip(out, ship);
208: }
209: out.print("</ul>");
210:
211: out
212: .print("<H3>Finding Ships with Tonnage between 50K and 110K</H3>");
213: out
214: .print("<p><code>SELECT OBJECT(s) FROM JE2_Ship AS s WHERE s.tonnage BETWEEN ?1 AND ?2</code></p>");
215: Collection ships50110k = null;
216: try {
217: ships50110k = shiphome.findByTonnage(new Double(50000.0),
218: new Double(110000.0));
219: } catch (Exception ex) {
220: ok = false;
221: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
222: }
223: out.print("<p><i>Number of ships = " + ships50110k.size()
224: + "<br>Expected = 7 ships</i></p>");
225: if (ships50110k.size() != 7) {
226: ok = false;
227: }
228: iterator = ships50110k.iterator();
229: out.print("<ul>");
230: while (iterator.hasNext()) {
231: ShipLocal ship = (ShipLocal) (iterator.next());
232: printShip(out, ship);
233: }
234: out.print("</ul>");
235:
236: // creating cruise for future use
237: cruiseA = null;
238: CruiseLocal cruiseB = null;
239: try {
240: ShipLocal ship1 = shiphome.findByPrimaryKey(new Integer(1));
241: cruiseA = cruisehome.create("Alaska Cruise", ship1);
242: cruiseB = cruisehome.create("Bohemian Cruise", ship1);
243: } catch (Exception ex) {
244: ok = false;
245: out.print("<p>ERROR: Exception caught : " + ex + "<p>");
246: }
247:
248: out.println("<h3>Creating Customers</h3>");
249: for (int kk = 80; kk <= 99; kk++) {
250: CustomerLocal customer = null;
251: AddressLocal addr = null;
252: try {
253: customer = customerhome.create(new Integer(kk));
254: customer.setName(new Name("Smith" + kk, "John"));
255: customer.addPhoneNumber("612-555-12" + kk, (byte) 1);
256: addr = addresshome.createAddress("10" + kk
257: + " Elm Street", cities[(kk - 80) % 6],
258: (kk % 2 == 0 ? "MN" : "CA"), "5540"
259: + (kk % 5 + 1));
260: } catch (Exception e) {
261: ok = false;
262: out
263: .println("<p>ERROR: exception caught = " + e
264: + "<p>");
265: }
266: customer.setHomeAddress(addr);
267: customer.setHasGoodCredit((kk % 4 == 0));
268: //printCustomer(out, customer, addr);
269: }
270:
271: // Creating Customers 1-6, each with 2 reservations for 2 cabins
272: Calendar date = Calendar.getInstance();
273: date.set(2002, 10, 1);
274: try {
275:
276: for (int kk = 201; kk < 207; kk++) {
277: Collection customers = new ArrayList();
278: CustomerLocal cust = customerhome
279: .create(new Integer(kk));
280: cust.setName(new Name("Customer " + kk, "Mike"));
281: cust.setHasGoodCredit((kk % 2 == 0)); // odds are bums
282: AddressLocal addr = addresshome.createAddress("50" + kk
283: + " Main Street", "Minneapolis", "MN", "5510"
284: + kk);
285: cust.setHomeAddress(addr);
286: //out.print("<li> Customer name=
287: // "+cust.getName().getLastName()+"</li>");
288: customers.add(cust); // put this single customer in the
289: // collection
290:
291: //printCustomer(out, cust,addr);
292:
293: Collection reservations = new ArrayList();
294:
295: for (int jj = 0; jj < 2; jj++) {
296:
297: ReservationLocal reservation = reservationhome
298: .create(cruiseA, customers);
299: reservation.setDate(date.getTime());
300: reservation.setAmountPaid(1000 * kk + 100 * jj
301: + 2000);
302: date.add(Calendar.DAY_OF_MONTH, 7);
303:
304: Set cabins = new HashSet();
305: CabinLocal cabin = cabinhome.create(new Integer(
306: 1000 + kk * 100 + jj));
307: cabin.setDeckLevel(kk - 200);
308: cabin.setName("Cabin " + kk + "0" + jj + "1");
309:
310: cabins.add(cabin);
311: cabin = cabinhome.create(new Integer(1000 + kk
312: * 100 + 10 + jj));
313: cabin.setDeckLevel(kk - 200);
314: cabin.setName("Cabin " + kk + "0" + jj + "2");
315:
316: cabins.add(cabin);
317:
318: reservation.setCabins(cabins); // this reservation has 2
319: // cabins
320: //out.print("<ul>");
321: //printReservation(out, reservation);
322: //out.print("</ul>");
323: }
324: }
325: } catch (Exception ex) {
326: ok = false;
327: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
328: }
329:
330: String fnames[] = new String[5];
331: fnames[0] = "John";
332: fnames[1] = "Paul";
333: fnames[2] = "Ringo";
334: fnames[3] = "Joe";
335: fnames[4] = "Roger";
336:
337: String[] lnames = new String[3];
338: lnames[0] = "Smith";
339: lnames[1] = "Johnson";
340: lnames[2] = "Star";
341:
342: // Creating Customers 50-69
343: try {
344: for (int kk = 50; kk <= 69; kk++) {
345: CustomerLocal customer = customerhome
346: .create(new Integer(kk));
347: customer.setName(new Name(lnames[(kk - 50) % 3],
348: fnames[(kk - 50) % 5]));
349: customer.addPhoneNumber("612-555-12" + kk, (byte) 1);
350: AddressLocal addr = addresshome.createAddress("10" + kk
351: + " Elm Street", cities[(kk - 50) % 6],
352: (kk % 2 == 0 ? "MN" : "CA"), "5540"
353: + (kk % 5 + 1));
354: customer.setHomeAddress(addr);
355: customer.setHasGoodCredit((kk % 4 == 0));
356:
357: //printCustomer(out, customer, addr);
358:
359: // Some customers will have reservations already on one of the
360: // two cruises..
361: if (kk % 3 != 0) {
362: Collection customers = new ArrayList();
363: customers.add(customer); // put this single customer in the
364: // collection
365: ReservationLocal reservation = reservationhome
366: .create((kk % 3 == 1 ? cruiseA : cruiseB),
367: customers);
368: reservation.setDate(date.getTime());
369: reservation.setAmountPaid(10 * kk + 2000);
370: //out.print("<ul>");
371: //printReservation(out, reservation);
372: //out.print("</ul>");
373:
374: date.add(Calendar.DAY_OF_MONTH, 7);
375: }
376: }
377: } catch (Exception ex) {
378: ok = false;
379: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
380: }
381:
382: // Creating Customers 100-109
383: try {
384: for (int kk = 100; kk <= 109; kk++) {
385: CustomerLocal customer = customerhome
386: .create(new Integer(kk));
387: customer.setName(new Name("Lennon" + kk, "Paul"));
388: customer.addPhoneNumber("666-543-12" + kk, (byte) 1);
389: AddressLocal addr = addresshome.createAddress("10" + kk
390: + " Abbey Road", cities[(kk - 100) % 6],
391: (kk % 2 == 0 ? "FL" : "WA"), "5540"
392: + (kk % 5 + 1));
393:
394: customer.setHomeAddress(addr);
395: customer.setHasGoodCredit((kk % 4 == 0));
396:
397: //printCustomer(out, customer, addr);
398: }
399: } catch (Exception ex) {
400: ok = false;
401: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
402: }
403:
404: try {
405: listCustomers(out, customerhome);
406: } catch (Exception e) {
407: ok = false;
408: out.println("<p>ERROR: exception caught = " + e + "</p>");
409: }
410:
411: out
412: .print("<H3>Finding Customer having name 'John Smith85'</H3>");
413: out
414: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName = ?1 AND c.firstName = ?2</code></p>");
415: CustomerLocal customer85 = null;
416: try {
417: customer85 = customerhome
418: .findByExactName("Smith85", "John");
419: } catch (Exception ex) {
420: ok = false;
421: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
422: }
423: AddressLocal addr85 = customer85.getHomeAddress();
424: printCustomer(out, customer85, addr85);
425:
426: out.print("<H3>Finding Customer 'Smith90'</H3>");
427: out
428: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName = 'Smith90' </code></p>");
429: CustomerLocal customer90 = null;
430: try {
431: customer90 = customerhome.findSmith90();
432: } catch (Exception ex) {
433: ok = false;
434: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
435: }
436:
437: AddressLocal addr90 = customer90.getHomeAddress();
438: printCustomer(out, customer90, addr90);
439:
440: out.print("<H3>Finding Customers having GoodCredit</H3>");
441: out
442: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.hasGoodCredit = TRUE</code></p>");
443: Collection mplscustomers = null;
444: try {
445: mplscustomers = customerhome.findByGoodCredit();
446: } catch (Exception ex) {
447: ok = false;
448: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
449: }
450:
451: iterator = mplscustomers.iterator();
452: out.print("<p><i>Number of customers = " + mplscustomers.size()
453: + "<br>Expected = 16 customers</i></p>");
454: if (mplscustomers.size() != 16) {
455: ok = false;
456: }
457: while (iterator.hasNext()) {
458: CustomerLocal customer = (CustomerLocal) (iterator.next());
459: AddressLocal addr = customer.getHomeAddress();
460: printCustomer(out, customer, addr);
461: }
462:
463: out
464: .print("<H3>Finding Customers having City = 'Minneapolis' and State = 'MN'</H3>");
465: out
466: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.homeAddress.city = ?1 AND c.homeAddress.state = ?2</code></p>");
467: try {
468: mplscustomers = customerhome
469: .findByCity("Minneapolis", "MN");
470: } catch (Exception ex) {
471: ok = false;
472: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
473: }
474: out.print("<p><i>Number of customers = " + mplscustomers.size()
475: + "<br>Expected = 14 customers</i></p>");
476: if (mplscustomers.size() != 14) {
477: ok = false;
478: }
479: iterator = mplscustomers.iterator();
480: while (iterator.hasNext()) {
481: CustomerLocal customer = (CustomerLocal) (iterator.next());
482: AddressLocal addr = customer.getHomeAddress();
483: printCustomer(out, customer, addr);
484: }
485:
486: out.print("<H3>Cabins Table Content</H3>");
487: try {
488: listCabins(out, cabinhome);
489: } catch (Exception ex) {
490: ok = false;
491: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
492: }
493:
494: out
495: .print("<H3>Retrieve a collection of all cabins on deck '3'</H3>");
496: out
497: .print("<p><code>SELECT OBJECT(c) FROM Cabin AS c WHERE c.deckLevel = ?1</code></p>");
498: Collection cabins = null;
499: try {
500: cabins = cabinhome.findAllOnDeckLevel(new Integer(3));
501: out.print("<p><i>Number of cabins = " + cabins.size()
502: + "<br>Expected = 4 cabins</i></p>");
503: Iterator iter = cabins.iterator();
504: out.print("<ul>");
505: while (iter.hasNext()) {
506: CabinLocal cabin = (CabinLocal) (iter.next());
507: out.print("<li>cabin '" + cabin.getName()
508: + "' on deck '" + cabin.getDeckLevel()
509: + "'</li>");
510: }
511: out.print("</ul>");
512: } catch (Exception ex) {
513: ok = false;
514: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
515: }
516:
517: try {
518: out
519: .print("<H3>Finding Customer having a name exactly matching 'Joe Star'</H3>");
520: out
521: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName = ?1 AND c.firstName = ?2</code></p>");
522: CustomerLocal customer = customerhome.findByExactName(
523: "Star", "Joe");
524: AddressLocal addr = customer.getHomeAddress();
525: printCustomer(out, customer, addr);
526: Collection customers = null;
527:
528: out
529: .print("<H3>Finding Customers having a name like 'Jo S' (no wildcards)</H3>");
530: out
531: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName LIKE ?1 AND c.firstName LIKE ?2</code></p>");
532:
533: customers = customerhome.findByName("S", "Jo");
534: iterator = customers.iterator();
535: while (iterator.hasNext()) {
536: customer = (CustomerLocal) (iterator.next());
537: addr = customer.getHomeAddress();
538: printCustomer(out, customer, addr);
539: }
540: out.print("<p><i>Number of customers = " + customers.size()
541: + "<br>Expected = 0 customers</i></p>");
542: if (customers.size() != 0) {
543: ok = false;
544: }
545:
546: out
547: .print("<H3>Finding Customers having a name like 'Jo% S%' (with wildcards)</H3>");
548: out
549: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName LIKE ?1 AND c.firstName LIKE ?2</code></p>");
550: customers = customerhome.findByName("S%", "Jo%");
551: iterator = customers.iterator();
552: out.print("<p><i>Number of customers = " + customers.size()
553: + "<br>Expected = 26 customers</i></p>");
554: if (customers.size() != 26) {
555: ok = false;
556: }
557: while (iterator.hasNext()) {
558: customer = (CustomerLocal) (iterator.next());
559: addr = customer.getHomeAddress();
560: printCustomer(out, customer, addr);
561: }
562:
563: out
564: .print("<H2>Finding Customers having a name like 'Jo% S%' and living in 'MN'</H2>");
565: out
566: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c <br>WHERE c.lastName LIKE ?1 AND c.firstName LIKE ?2 AND c.homeAddress.state = ?3</code></p>");
567: customers = customerhome.findByNameAndState("S%", "Jo%",
568: "MN");
569: iterator = customers.iterator();
570: out.print("<p><i>Number of customers = " + customers.size()
571: + "<br>Expected = 13 customers</i></p>");
572: if (customers.size() != 13) {
573: ok = false;
574: }
575: while (iterator.hasNext()) {
576: customer = (CustomerLocal) (iterator.next());
577: addr = customer.getHomeAddress();
578: printCustomer(out, customer, addr);
579: }
580:
581: out
582: .print("<H2>Finding Customers Living in Warm Climates</H2>");
583: out
584: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.homeAddress.state IN ('FL','TX','AZ','CA')</code></p>");
585: customers = customerhome.findInHotStates();
586: out.print("<p><i>Number of customers = " + customers.size()
587: + "<br>Expected = 25 customers</i></p>");
588: if (customers.size() != 25) {
589: ok = false;
590: }
591: iterator = customers.iterator();
592: while (iterator.hasNext()) {
593: customer = (CustomerLocal) (iterator.next());
594: addr = customer.getHomeAddress();
595: printCustomer(out, customer, addr);
596: }
597:
598: out.print("<H2>Finding Customers With No Reservation</H2>");
599: out
600: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.reservations IS EMPTY</code></p>");
601: customers = customerhome.findWithNoReservations();
602: out.print("<p><i>Number of customers = " + customers.size()
603: + "<br>Expected = 37 customers</i></p>");
604: if (customers.size() != 37) {
605: ok = false;
606: }
607: iterator = customers.iterator();
608: while (iterator.hasNext()) {
609: customer = (CustomerLocal) (iterator.next());
610: addr = customer.getHomeAddress();
611: printCustomer(out, customer, addr);
612: }
613:
614: out.print("<H2>Finding Customers On Alaska Cruise</H2>");
615: out
616: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS cust, Cruise AS cr, IN (cr.reservations) AS res <br>WHERE cr = ?1 AND cust MEMBER OF res.customers</code></p>");
617: CruiseLocal crA = cruisehome.findByName("Alaska Cruise");
618: customers = customerhome.findOnCruise(cruiseA);
619: out.print("<p><i>Number of customers = " + customers.size()
620: + "<br>Expected = 12 customers</i></p>");
621: if (customers.size() != 12) {
622: ok = false;
623: }
624: iterator = customers.iterator();
625: while (iterator.hasNext()) {
626: customer = (CustomerLocal) (iterator.next());
627: addr = customer.getHomeAddress();
628: printCustomer(out, customer, addr);
629: }
630:
631: out
632: .print("<H2>Select the zip codes of the state 'FL'</H2>");
633: out
634: .print("<p><code>SELECT a.zip FROM JE2_Address AS a WHERE a.state = ?1</code></p>");
635: Collection zipCodes = null;
636: zipCodes = addresshome.selectZipCodes("FL");
637: out
638: .print("<p><i>Number of zip codes = "
639: + zipCodes.size()
640: + "<br>Expected = 5 zip codes (from 55401 to 55405)</i></p>");
641: if (zipCodes.size() != 5) {
642: ok = false;
643: }
644: Iterator iZipCodes = zipCodes.iterator();
645: out.println("<ul>");
646: while (iZipCodes.hasNext()) {
647: out.print("<li>zip code = '" + iZipCodes.next()
648: + "'</li>");
649: }
650: out.println("</ul>");
651:
652: out
653: .print("<H2>Select the customer which have the address 'addr85' </H2>");
654: out
655: .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.homeAddress = ?1</code></p>");
656: CustomerLocal cust = null;
657: cust = addresshome.selectCustomer(addr85);
658: addr = cust.getHomeAddress();
659: printCustomer(out, cust, addr);
660:
661: } catch (Exception ex) {
662: ok = false;
663: out.print("<p>ERROR: Exception caught : " + ex + "</p>");
664: }
665: out.print("<H2>Cleaning all tables</H2>");
666:
667: try {
668: Collection clc = customerhome.findAllCustomers();
669: iterator = clc.iterator();
670: while (iterator.hasNext()) {
671: CustomerLocal cl = (CustomerLocal) iterator.next();
672: cl.remove();
673: }
674: clc = shiphome.findAllShips();
675: iterator = clc.iterator();
676: while (iterator.hasNext()) {
677: ShipLocal sl = (ShipLocal) iterator.next();
678: sl.remove();
679: }
680: clc = cruisehome.findAllCruises();
681: iterator = clc.iterator();
682: while (iterator.hasNext()) {
683: CruiseLocal cl = (CruiseLocal) iterator.next();
684: cl.remove();
685: }
686: clc = reservationhome.findAllReservations();
687: iterator = clc.iterator();
688: while (iterator.hasNext()) {
689: ReservationLocal rl = (ReservationLocal) iterator
690: .next();
691: rl.remove();
692: }
693: clc = cabinhome.findAllCabins();
694: iterator = clc.iterator();
695: while (iterator.hasNext()) {
696: CabinLocal cl = (CabinLocal) iterator.next();
697: cl.remove();
698: }
699: } catch (Exception ex) {
700: ok = false;
701: out.println("<p>ERROR: during cleaning exception caught = "
702: + ex + "</p>");
703: }
704:
705: if (ok) {
706: out
707: .println("<p align=\"center\"><strong>Servlet is OK.</strong></p>");
708: }
709: out.println("<a href=\"index.html\"><b>Back to Menu</b></a>");
710: out.println("</body>");
711: out.println("</html>");
712:
713: }
714:
715: private void listCabins(PrintWriter out, CabinHomeLocal chl)
716: throws Exception {
717: out.println("<p><b>Cabins</b> Table Content:</p>");
718: out.println("<ul>");
719: Collection clc = chl.findAllCabins();
720: if (clc == null) {
721: out.println("<li>Cabins table is empty</li>");
722: } else {
723: Iterator iterator = clc.iterator();
724: while (iterator.hasNext()) {
725: CabinLocal cl = (CabinLocal) iterator.next();
726: String name = cl.getName();
727: int deck = cl.getDeckLevel();
728: int bd = cl.getBedCount();
729: out.println("<li>cabinName = '" + name
730: + "', deckLevel = '" + deck + "', bedCount = '"
731: + bd + "'</li>");
732: }
733: }
734: out.println("</ul>");
735: }
736:
737: private void listCustomers(PrintWriter out, CustomerHomeLocal chl)
738: throws Exception {
739: out.println("<p><b>Customers</b> Table Content:</p>");
740: out.println("<ul>");
741: java.util.Collection clc = chl.findAllCustomers();
742: if (clc == null) {
743: out.println("<li>Customers table is empty</li>");
744: } else {
745: java.util.Iterator iterator = clc.iterator();
746: while (iterator.hasNext()) {
747: CustomerLocal cl = (CustomerLocal) iterator.next();
748: String name = cl.getName().getLastName();
749: String fname = cl.getName().getFirstName();
750: String number = " No Card! ";
751: boolean gc = cl.getHasGoodCredit();
752: if (cl.getCreditCard() != null) {
753: number = cl.getCreditCard().getNumber();
754: }
755: out.print("<li>firstName = '" + fname
756: + "', lastName = '" + name + "'");
757: out.println("<ul>");
758: out.print("<li>creditCardNumber = '" + number
759: + "', goodCredit = '" + gc + "'</li>");
760: if (cl.getHomeAddress() != null) {
761: String city = cl.getHomeAddress().getCity();
762: String state = cl.getHomeAddress().getState();
763: out.print("<li>city> = '" + city + "', state = '"
764: + state + "'</li>");
765: }
766: out.println("</li>");
767: out.println("</ul>");
768: }
769: }
770: out.println("</ul>");
771: }
772:
773: private void listCreditcards(PrintWriter out,
774: CreditCardHomeLocal cchl) throws Exception {
775: out.println("<p><b>CreditCards</b> Table Content:</p>");
776: out.println("<ul>");
777: java.util.Collection clc = cchl.findAllCreditCards();
778: if (clc == null) {
779: out.println("<li>CreditCards table is empty</li>");
780: } else {
781: java.util.Iterator iterator = clc.iterator();
782: while (iterator.hasNext()) {
783: CreditCardLocal ccl = (CreditCardLocal) iterator.next();
784: String number = ccl.getNumber();
785: String name = ccl.getNameOnCard();
786: out.println("<li>creditCardNumber = '" + number
787: + "', nameOnCard '= '" + name + "'</li>");
788: }
789: }
790: out.println("</ul>");
791: }
792:
793: private void listAddress(PrintWriter out, AddressHomeLocal cchl)
794: throws Exception {
795: out.println("<p><b>Addresses</b> Table Content:</p>");
796: out.println("<ul>");
797: java.util.Collection clc = cchl.findAllAddress();
798: if (clc == null) {
799: out.println("<li>Addresses table is empty</li>");
800: } else {
801: java.util.Iterator iterator = clc.iterator();
802: while (iterator.hasNext()) {
803: AddressLocal al = (AddressLocal) iterator.next();
804: String city = al.getCity();
805: String street = al.getStreet();
806: out.println("<li>adressCity = '" + city
807: + "', street = '" + street + "'</li>");
808: }
809: }
810: out.println("</ul>");
811: }
812:
813: private void listPhones(PrintWriter out, Vector vv) {
814: out.println("<ul>");
815: for (int jj = 0; jj < vv.size(); jj++) {
816: String ss = (String) (vv.get(jj));
817: out.println("<li> " + ss + "</li>");
818: }
819: out.println("</ul>");
820: }
821:
822: private void printShip(PrintWriter out, ShipLocal ship) {
823: out.print("<li> shipId = '" + ship.getId() + "', name = '"
824: + ship.getName() + "', tonnage = '" + ship.getTonnage()
825: + "'</li>");
826: }
827:
828: private void printCustomer(PrintWriter out, CustomerLocal customer,
829: AddressLocal addr) {
830: out.print("<ul>");
831: out.print("<li>firstName = '"
832: + customer.getName().getFirstName() + "', lastName = '"
833: + customer.getName().getLastName()
834: + "', goodCredit = '" + customer.getHasGoodCredit()
835: + "'</li>");
836: out.print("<li>street = '" + addr.getStreet() + "', city = '"
837: + addr.getCity() + "', state = '" + addr.getState()
838: + "', zip = '" + addr.getZip() + "'</li>");
839: out.print("</ul>");
840: }
841:
842: private void printReservation(PrintWriter out,
843: ReservationLocal reservation) {
844: out.print("<ul>");
845: String cru = "no Cruise!";
846: if (reservation.getCruise() != null) {
847: cru = reservation.getCruise().getName();
848: }
849: out.print("<li>reservationDate: '" + reservation.getDate()
850: + "' on '" + cru + "' amountPaid: '"
851: + reservation.getAmountPaid() + "'</li>");
852: Set cabinset = reservation.getCabins();
853: String customerinfo = "";
854: CustomerLocal cust = null;
855: String cabininfo = "";
856: Set customerset = reservation.getCustomers();
857: Iterator iterator = customerset.iterator();
858: while (iterator.hasNext()) {
859: cust = (CustomerLocal) iterator.next();
860: customerinfo += cust.getName().getLastName() + " ";
861: }
862: iterator = cabinset.iterator();
863: while (iterator.hasNext()) {
864: CabinLocal cabin = (CabinLocal) iterator.next();
865: cabininfo += cabin.getName() + " ";
866: }
867: out.print("<ul>");
868: if (!customerinfo.equals("")) {
869: out.print("<li>customers: " + customerinfo);
870: }
871: if (!cabininfo.equals("")) {
872: out.print("<li>cabins: " + cabininfo + "</li>");
873: }
874: out.print("</ul>");
875: out.print("</ul>");
876: }
877:
878: }
|