KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servlets > ServletTest3


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s):
22  * --------------------------------------------------------------------------
23  * $Id: ServletTest3.java,v 1.17 2005/07/28 13:35:39 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package servlets;
28
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.util.ArrayList;
32 import java.util.Calendar;
33 import java.util.Collection;
34 import java.util.HashSet;
35 import java.util.Iterator;
36 import java.util.Set;
37 import java.util.Vector;
38
39 import javax.naming.Context;
40 import javax.naming.InitialContext;
41 import javax.servlet.ServletException;
42 import javax.servlet.http.HttpServlet;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import com.titan.address.AddressHomeLocal;
47 import com.titan.address.AddressLocal;
48 import com.titan.cabin.CabinHomeLocal;
49 import com.titan.cabin.CabinLocal;
50 import com.titan.cruise.CruiseHomeLocal;
51 import com.titan.cruise.CruiseLocal;
52 import com.titan.customer.CreditCardHomeLocal;
53 import com.titan.customer.CreditCardLocal;
54 import com.titan.customer.CustomerHomeLocal;
55 import com.titan.customer.CustomerLocal;
56 import com.titan.customer.Name;
57 import com.titan.phone.PhoneHomeLocal;
58 import com.titan.reservation.ReservationHomeLocal;
59 import com.titan.reservation.ReservationLocal;
60 import com.titan.ship.ShipHomeLocal;
61 import com.titan.ship.ShipLocal;
62
63 /**
64  * This servlet is used to test O'Reilly examples
65  * @author JOnAS team
66  */

67 public class ServletTest3 extends HttpServlet {
68
69     /**
70      * Called by the server (via the service method) to allow a servlet to
71      * handle a GET request.
72      * @param request an HttpServletRequest object that contains the request the
73      * client has made of the servlet
74      * @param response an HttpServletResponse object that contains the response
75      * the servlet sends to the client
76      * @throws IOException if an input or output error is detected when the
77      * servlet handles the GET request
78      * @throws ServletException if the request for the GET could not be handled
79      */

80     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
81
82         boolean ok = true;
83         response.setContentType("text/html");
84         PrintWriter out = response.getWriter();
85
86         out.println("<html>");
87         out.println("<head>");
88         out.println("<title>");
89         out.println("O'Reilly Examples</title>");
90         out.println("<link rel=\"stylesheet\" HREF=\"style.css\" />");
91         out.println("</head>");
92         out.println("<body style=\"background : white; color : black;\">");
93         out.println("<h1>EJB QL Examples from Chapter 8</h1>");
94
95         Context initialContext = null;
96         try {
97             initialContext = new InitialContext();
98         } catch (Exception e) {
99             out.print("<p>ERROR: Cannot get initial context for JNDI: " + e + "</p>");
100             return;
101         }
102
103         out.println("<a HREF=\"index.html\"><b>Back to Menu</b></a>");
104
105         // Connecting to CustomerHomeLocal and CreditCardHomeLocal thru JNDI
106
CustomerHomeLocal customerhome = null;
107         CreditCardHomeLocal cardhome = null;
108         AddressHomeLocal addresshome = null;
109         ShipHomeLocal shiphome = null;
110         CruiseHomeLocal cruisehome = null;
111         ReservationHomeLocal reservationhome = null;
112         CabinHomeLocal cabinhome = null;
113         PhoneHomeLocal phonehome = null;
114         try {
115             customerhome = (CustomerHomeLocal) initialContext.lookup("java:comp/env/ejb/CustomerHomeLocal");
116             cardhome = (CreditCardHomeLocal) initialContext.lookup("java:comp/env/ejb/CreditCardHomeLocal");
117             addresshome = (AddressHomeLocal) initialContext.lookup("java:comp/env/ejb/AddressHomeLocal");
118             shiphome = (ShipHomeLocal) initialContext.lookup("java:comp/env/ejb/ShipHomeLocal");
119             cruisehome = (CruiseHomeLocal) initialContext.lookup("java:comp/env/ejb/CruiseHomeLocal");
120             reservationhome = (ReservationHomeLocal) initialContext.lookup("java:comp/env/ejb/ReservationHomeLocal");
121             cabinhome = (CabinHomeLocal) initialContext.lookup("java:comp/env/ejb/CabinHomeLocal");
122             phonehome = (PhoneHomeLocal) initialContext.lookup("java:comp/env/ejb/PhoneHomeLocal");
123         } catch (Exception e) {
124             out.println("<p>ERROR: Cannot lookup java:comp/env/ejb/XXHomeLocal: " + e + "</p>");
125             return;
126         }
127
128         String cities[] = new String[6];
129         cities[0] = "Minneapolis";
130         cities[1] = "St. Paul";
131         cities[2] = "Rochester";
132         cities[3] = "Winona";
133         cities[4] = "Wayzata";
134         cities[5] = "Eagan";
135
136         out.println("<h2>Example showing Sample EJB-QL</h2>");
137
138         out.print("<H3>Creating a Ship and Cruise</H3>");
139         ShipLocal shipA = null;
140         CruiseLocal cruiseA = null;
141         try {
142             shipA = shiphome.create(new Integer(10772), "Ship A", 30000.0);
143             cruiseA = cruisehome.create("Cruise A", shipA);
144         } catch (Exception ex) {
145             ok = false;
146             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
147         }
148         out.println("<ul>");
149         out.println("<li>cruise.getName() = '" + cruiseA.getName() + "'</li>");
150         out.println("<li>ship.getName() = '" + shipA.getName() + "'</li>");
151         out.println("<li>cruise.getShip().getName() = '" + cruiseA.getShip().getName() + "'</li>");
152         out.println("</ul>");
153
154         out.print("<H3>Creating Ship Beans with Various Tonnage Values</H3>");
155         out.print("<ul>");
156         for (int jj = 1; jj <= 10; jj++) {
157             try {
158                 ShipLocal ship = shiphome.create(new Integer(jj), "Ship " + jj, 30000.0 + (10000.0 * jj));
159                 printShip(out, ship);
160             } catch (Exception ex) {
161                 ok = false;
162                 out.print("<p>ERROR: Exception caught : " + ex + "</p>");
163             }
164
165         }
166         out.print("</ul>");
167
168         out.print("<H3>Finding Ships with Exactly 100K Tonnage</H3>");
169         out.print("<p><code>SELECT OBJECT(s) FROM JE2_Ship AS s WHERE s.tonnage = ?1</code></p>");
170         Collection ships100k = null;
171         try {
172             ships100k = shiphome.findByTonnage(new Double(100000.0));
173         } catch (Exception ex) {
174             ok = false;
175             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
176         }
177         out.print("<p><i>Number of ships = " + ships100k.size() + " <br>Expected = 1 ships</i></p>");
178         if (ships100k.size() != 1) {
179             ok = false;
180         }
181         Iterator iterator = ships100k.iterator();
182         out.print("<ul>");
183         while (iterator.hasNext()) {
184             ShipLocal ship = (ShipLocal) (iterator.next());
185             printShip(out, ship);
186         }
187         out.print("</ul>");
188
189         out.print("<H3>Finding Ships with Tonnage between 50K and 110K</H3>");
190         out.print("<p><code>SELECT OBJECT(s) FROM JE2_Ship AS s WHERE s.tonnage BETWEEN ?1 AND ?2</code></p>");
191         Collection ships50110k = null;
192         try {
193             ships50110k = shiphome.findByTonnage(new Double(50000.0), new Double(110000.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 = " + ships50110k.size() + "<br>Expected = 7 ships</i></p>");
199         if (ships50110k.size() != 7) {
200             ok = false;
201         }
202         iterator = ships50110k.iterator();
203         out.print("<ul>");
204         while (iterator.hasNext()) {
205             ShipLocal ship = (ShipLocal) (iterator.next());
206             printShip(out, ship);
207         }
208         out.print("</ul>");
209
210         // creating cruise for future use
211
cruiseA = null;
212         CruiseLocal cruiseB = null;
213         try {
214             ShipLocal ship1 = shiphome.findByPrimaryKey(new Integer(1));
215             cruiseA = cruisehome.create("Alaska Cruise", ship1);
216             cruiseB = cruisehome.create("Bohemian Cruise", ship1);
217         } catch (Exception ex) {
218             ok = false;
219             out.print("<p>ERROR: Exception caught : " + ex + "<p>");
220         }
221
222         out.println("<h3>Creating Customers</h3>");
223         for (int kk = 80; kk <= 99; kk++) {
224             CustomerLocal customer = null;
225             AddressLocal addr = null;
226             try {
227                 customer = customerhome.create(new Integer(kk));
228                 customer.setName(new Name("Smith" + kk, "John"));
229                 customer.addPhoneNumber("612-555-12" + kk, (byte) 1);
230                 addr = addresshome.createAddress("10" + kk + " Elm Street", cities[(kk - 80) % 6], (kk % 2 == 0 ? "MN"
231                         : "CA"), "5540" + (kk % 5 + 1));
232             } catch (Exception e) {
233                 ok = false;
234                 out.println("<p>ERROR: exception caught = " + e + "<p>");
235             }
236             customer.setHomeAddress(addr);
237             customer.setHasGoodCredit((kk % 4 == 0));
238             //printCustomer(out, customer, addr);
239
}
240
241         // Creating Customers 1-6, each with 2 reservations for 2 cabins
242
Calendar date = Calendar.getInstance();
243         date.set(2002, 10, 1);
244         try {
245
246             for (int kk = 201; kk < 207; kk++) {
247                 Collection customers = new ArrayList();
248                 CustomerLocal cust = customerhome.create(new Integer(kk));
249                 cust.setName(new Name("Customer " + kk, "Mike"));
250                 cust.setHasGoodCredit((kk % 2 == 0)); // odds are bums
251
AddressLocal addr = addresshome.createAddress("50" + kk + " Main Street", "Minneapolis", "MN", "5510"
252                         + kk);
253                 cust.setHomeAddress(addr);
254                 //out.print("<li> Customer name=
255
// "+cust.getName().getLastName()+"</li>");
256
customers.add(cust); // put this single customer in the
257
// collection
258

259                 //printCustomer(out, cust,addr);
260

261                 Collection reservations = new ArrayList();
262
263                 for (int jj = 0; jj < 2; jj++) {
264
265                     ReservationLocal reservation = reservationhome.create(cruiseA, customers);
266                     reservation.setDate(date.getTime());
267                     reservation.setAmountPaid(1000 * kk + 100 * jj + 2000);
268                     date.add(Calendar.DAY_OF_MONTH, 7);
269
270                     Set cabins = new HashSet();
271                     CabinLocal cabin = cabinhome.create(new Integer(1000 + kk * 100 + jj));
272                     cabin.setDeckLevel(kk - 200);
273                     cabin.setName("Cabin " + kk + "0" + jj + "1");
274
275                     cabins.add(cabin);
276                     cabin = cabinhome.create(new Integer(1000 + kk * 100 + 10 + jj));
277                     cabin.setDeckLevel(kk - 200);
278                     cabin.setName("Cabin " + kk + "0" + jj + "2");
279
280                     cabins.add(cabin);
281
282                     reservation.setCabins(cabins); // this reservation has 2
283
// cabins
284
//out.print("<ul>");
285
//printReservation(out, reservation);
286
//out.print("</ul>");
287
}
288             }
289         } catch (Exception ex) {
290             ok = false;
291             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
292         }
293
294         String fnames[] = new String[5];
295         fnames[0] = "John";
296         fnames[1] = "Paul";
297         fnames[2] = "Ringo";
298         fnames[3] = "Joe";
299         fnames[4] = "Roger";
300
301         String[] lnames = new String[3];
302         lnames[0] = "Smith";
303         lnames[1] = "Johnson";
304         lnames[2] = "Star";
305
306         // Creating Customers 50-69
307
try {
308             for (int kk = 50; kk <= 69; kk++) {
309                 CustomerLocal customer = customerhome.create(new Integer(kk));
310                 customer.setName(new Name(lnames[(kk - 50) % 3], fnames[(kk - 50) % 5]));
311                 customer.addPhoneNumber("612-555-12" + kk, (byte) 1);
312                 AddressLocal addr = addresshome.createAddress("10" + kk + " Elm Street", cities[(kk - 50) % 6],
313                         (kk % 2 == 0 ? "MN" : "CA"), "5540" + (kk % 5 + 1));
314                 customer.setHomeAddress(addr);
315                 customer.setHasGoodCredit((kk % 4 == 0));
316
317                 //printCustomer(out, customer, addr);
318

319                 // Some customers will have reservations already on one of the
320
// two cruises..
321
if (kk % 3 != 0) {
322                     Collection customers = new ArrayList();
323                     customers.add(customer); // put this single customer in the
324
// collection
325
ReservationLocal reservation = reservationhome.create((kk % 3 == 1 ? cruiseA : cruiseB), customers);
326                     reservation.setDate(date.getTime());
327                     reservation.setAmountPaid(10 * kk + 2000);
328                     //out.print("<ul>");
329
//printReservation(out, reservation);
330
//out.print("</ul>");
331

332                     date.add(Calendar.DAY_OF_MONTH, 7);
333                 }
334             }
335         } catch (Exception ex) {
336             ok = false;
337             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
338         }
339
340         // Creating Customers 100-109
341
try {
342             for (int kk = 100; kk <= 109; kk++) {
343                 CustomerLocal customer = customerhome.create(new Integer(kk));
344                 customer.setName(new Name("Lennon" + kk, "Paul"));
345                 customer.addPhoneNumber("666-543-12" + kk, (byte) 1);
346                 AddressLocal addr = addresshome.createAddress("10" + kk + " Abbey Road", cities[(kk - 100) % 6],
347                         (kk % 2 == 0 ? "FL" : "WA"), "5540" + (kk % 5 + 1));
348
349                 customer.setHomeAddress(addr);
350                 customer.setHasGoodCredit((kk % 4 == 0));
351
352                 //printCustomer(out, customer, addr);
353
}
354         } catch (Exception ex) {
355             ok = false;
356             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
357         }
358
359         try {
360             listCustomers(out, customerhome);
361         } catch (Exception e) {
362             ok = false;
363             out.println("<p>ERROR: exception caught = " + e + "</p>");
364         }
365
366         out.print("<H3>Finding Customer having name 'John Smith85'</H3>");
367         out
368                 .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName = ?1 AND c.firstName = ?2</code></p>");
369         CustomerLocal customer85 = null;
370         try {
371             customer85 = customerhome.findByExactName("Smith85", "John");
372         } catch (Exception ex) {
373             ok = false;
374             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
375         }
376         AddressLocal addr85 = customer85.getHomeAddress();
377         printCustomer(out, customer85, addr85);
378
379         out.print("<H3>Finding Customer 'Smith90'</H3>");
380         out.print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName = 'Smith90' </code></p>");
381         CustomerLocal customer90 = null;
382         try {
383             customer90 = customerhome.findSmith90();
384         } catch (Exception ex) {
385             ok = false;
386             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
387         }
388
389         AddressLocal addr90 = customer90.getHomeAddress();
390         printCustomer(out, customer90, addr90);
391
392         out.print("<H3>Finding Customers having GoodCredit</H3>");
393         out.print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.hasGoodCredit = TRUE</code></p>");
394         Collection mplscustomers = null;
395         try {
396             mplscustomers = customerhome.findByGoodCredit();
397         } catch (Exception ex) {
398             ok = false;
399             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
400         }
401
402         iterator = mplscustomers.iterator();
403         out.print("<p><i>Number of customers = " + mplscustomers.size() + "<br>Expected = 16 customers</i></p>");
404         if (mplscustomers.size() != 16) {
405             ok = false;
406         }
407         while (iterator.hasNext()) {
408             CustomerLocal customer = (CustomerLocal) (iterator.next());
409             AddressLocal addr = customer.getHomeAddress();
410             printCustomer(out, customer, addr);
411         }
412
413         out.print("<H3>Finding Customers having City = 'Minneapolis' and State = 'MN'</H3>");
414         out
415                 .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.homeAddress.city = ?1 AND c.homeAddress.state = ?2</code></p>");
416         try {
417             mplscustomers = customerhome.findByCity("Minneapolis", "MN");
418         } catch (Exception ex) {
419             ok = false;
420             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
421         }
422         out.print("<p><i>Number of customers = " + mplscustomers.size() + "<br>Expected = 14 customers</i></p>");
423         if (mplscustomers.size() != 14) {
424             ok = false;
425         }
426         iterator = mplscustomers.iterator();
427         while (iterator.hasNext()) {
428             CustomerLocal customer = (CustomerLocal) (iterator.next());
429             AddressLocal addr = customer.getHomeAddress();
430             printCustomer(out, customer, addr);
431         }
432
433         out.print("<H3>Cabins Table Content</H3>");
434         try {
435             listCabins(out, cabinhome);
436         } catch (Exception ex) {
437             ok = false;
438             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
439         }
440
441         out.print("<H3>Retrieve a collection of all cabins on deck '3'</H3>");
442         out.print("<p><code>SELECT OBJECT(c) FROM Cabin AS c WHERE c.deckLevel = ?1</code></p>");
443         Collection cabins = null;
444         try {
445             cabins = cabinhome.findAllOnDeckLevel(new Integer(3));
446             out.print("<p><i>Number of cabins = " + cabins.size() + "<br>Expected = 4 cabins</i></p>");
447             Iterator iter = cabins.iterator();
448             out.print("<ul>");
449             while (iter.hasNext()) {
450                 CabinLocal cabin = (CabinLocal) (iter.next());
451                 out.print("<li>cabin '" + cabin.getName() + "' on deck '" + cabin.getDeckLevel() + "'</li>");
452             }
453             out.print("</ul>");
454         } catch (Exception ex) {
455             ok = false;
456             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
457         }
458
459         try {
460             out.print("<H3>Finding Customer having a name exactly matching 'Joe Star'</H3>");
461             out
462                     .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName = ?1 AND c.firstName = ?2</code></p>");
463             CustomerLocal customer = customerhome.findByExactName("Star", "Joe");
464             AddressLocal addr = customer.getHomeAddress();
465             printCustomer(out, customer, addr);
466             Collection customers = null;
467
468             out.print("<H3>Finding Customers having a name like 'Jo S' (no wildcards)</H3>");
469             out
470                     .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName LIKE ?1 AND c.firstName LIKE ?2</code></p>");
471
472             customers = customerhome.findByName("S", "Jo");
473             iterator = customers.iterator();
474             while (iterator.hasNext()) {
475                 customer = (CustomerLocal) (iterator.next());
476                 addr = customer.getHomeAddress();
477                 printCustomer(out, customer, addr);
478             }
479             out.print("<p><i>Number of customers = " + customers.size() + "<br>Expected = 0 customers</i></p>");
480             if (customers.size() != 0) {
481                 ok = false;
482             }
483
484             out.print("<H3>Finding Customers having a name like 'Jo% S%' (with wildcards)</H3>");
485             out
486                     .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.lastName LIKE ?1 AND c.firstName LIKE ?2</code></p>");
487             customers = customerhome.findByName("S%", "Jo%");
488             iterator = customers.iterator();
489             out.print("<p><i>Number of customers = " + customers.size() + "<br>Expected = 26 customers</i></p>");
490             if (customers.size() != 26) {
491                 ok = false;
492             }
493             while (iterator.hasNext()) {
494                 customer = (CustomerLocal) (iterator.next());
495                 addr = customer.getHomeAddress();
496                 printCustomer(out, customer, addr);
497             }
498
499             out.print("<H2>Finding Customers having a name like 'Jo% S%' and living in 'MN'</H2>");
500             out
501                     .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>");
502             customers = customerhome.findByNameAndState("S%", "Jo%", "MN");
503             iterator = customers.iterator();
504             out.print("<p><i>Number of customers = " + customers.size() + "<br>Expected = 13 customers</i></p>");
505             if (customers.size() != 13) {
506                 ok = false;
507             }
508             while (iterator.hasNext()) {
509                 customer = (CustomerLocal) (iterator.next());
510                 addr = customer.getHomeAddress();
511                 printCustomer(out, customer, addr);
512             }
513
514             out.print("<H2>Finding Customers Living in Warm Climates</H2>");
515             out
516                     .print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.homeAddress.state IN ('FL','TX','AZ','CA')</code></p>");
517             customers = customerhome.findInHotStates();
518             out.print("<p><i>Number of customers = " + customers.size() + "<br>Expected = 25 customers</i></p>");
519             if (customers.size() != 25) {
520                 ok = false;
521             }
522             iterator = customers.iterator();
523             while (iterator.hasNext()) {
524                 customer = (CustomerLocal) (iterator.next());
525                 addr = customer.getHomeAddress();
526                 printCustomer(out, customer, addr);
527             }
528
529             out.print("<H2>Finding Customers With No Reservation</H2>");
530             out.print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.reservations IS EMPTY</code></p>");
531             customers = customerhome.findWithNoReservations();
532             out.print("<p><i>Number of customers = " + customers.size() + "<br>Expected = 37 customers</i></p>");
533             if (customers.size() != 37) {
534                 ok = false;
535             }
536             iterator = customers.iterator();
537             while (iterator.hasNext()) {
538                 customer = (CustomerLocal) (iterator.next());
539                 addr = customer.getHomeAddress();
540                 printCustomer(out, customer, addr);
541             }
542
543             out.print("<H2>Finding Customers On Alaska Cruise</H2>");
544             out
545                     .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>");
546             CruiseLocal crA = cruisehome.findByName("Alaska Cruise");
547             customers = customerhome.findOnCruise(cruiseA);
548             out.print("<p><i>Number of customers = " + customers.size() + "<br>Expected = 12 customers</i></p>");
549             if (customers.size() != 12) {
550                 ok = false;
551             }
552             iterator = customers.iterator();
553             while (iterator.hasNext()) {
554                 customer = (CustomerLocal) (iterator.next());
555                 addr = customer.getHomeAddress();
556                 printCustomer(out, customer, addr);
557             }
558
559             out.print("<H2>Select the zip codes of the state 'FL'</H2>");
560             out.print("<p><code>SELECT a.zip FROM JE2_Address AS a WHERE a.state = ?1</code></p>");
561             Collection zipCodes = null;
562             zipCodes = addresshome.selectZipCodes("FL");
563             out.print("<p><i>Number of zip codes = " + zipCodes.size()
564                     + "<br>Expected = 5 zip codes (from 55401 to 55405)</i></p>");
565             if (zipCodes.size() != 5) {
566                 ok = false;
567             }
568             Iterator iZipCodes = zipCodes.iterator();
569             out.println("<ul>");
570             while (iZipCodes.hasNext()) {
571                 out.print("<li>zip code = '" + iZipCodes.next() + "'</li>");
572             }
573             out.println("</ul>");
574
575             out.print("<H2>Select the customer which have the address 'addr85' </H2>");
576             out.print("<p><code>SELECT OBJECT(c) FROM JE2_Customer AS c WHERE c.homeAddress = ?1</code></p>");
577             CustomerLocal cust = null;
578             cust = addresshome.selectCustomer(addr85);
579             addr = cust.getHomeAddress();
580             printCustomer(out, cust, addr);
581
582         } catch (Exception ex) {
583             ok = false;
584             out.print("<p>ERROR: Exception caught : " + ex + "</p>");
585         }
586         out.print("<H2>Cleaning all tables</H2>");
587
588         try {
589             Collection clc = customerhome.findAllCustomers();
590             iterator = clc.iterator();
591             while (iterator.hasNext()) {
592                 CustomerLocal cl = (CustomerLocal) iterator.next();
593                 cl.remove();
594             }
595             clc = shiphome.findAllShips();
596             iterator = clc.iterator();
597             while (iterator.hasNext()) {
598                 ShipLocal sl = (ShipLocal) iterator.next();
599                 sl.remove();
600             }
601             clc = cruisehome.findAllCruises();
602             iterator = clc.iterator();
603             while (iterator.hasNext()) {
604                 CruiseLocal cl = (CruiseLocal) iterator.next();
605                 cl.remove();
606             }
607             clc = reservationhome.findAllReservations();
608             iterator = clc.iterator();
609             while (iterator.hasNext()) {
610                 ReservationLocal rl = (ReservationLocal) iterator.next();
611                 rl.remove();
612             }
613             clc = cabinhome.findAllCabins();
614             iterator = clc.iterator();
615             while (iterator.hasNext()) {
616                 CabinLocal cl = (CabinLocal) iterator.next();
617                 cl.remove();
618             }
619         } catch (Exception ex) {
620             ok = false;
621             out.println("<p>ERROR: during cleaning exception caught = " + ex + "</p>");
622         }
623
624         if (ok) {
625             out.println("<p align=\"center\"><strong>Servlet is OK.</strong></p>");
626         }
627         out.println("<a HREF=\"index.html\"><b>Back to Menu</b></a>");
628         out.println("</body>");
629         out.println("</html>");
630
631     }
632
633     private void listCabins(PrintWriter out, CabinHomeLocal chl) throws Exception {
634         out.println("<p><b>Cabins</b> Table Content:</p>");
635         out.println("<ul>");
636         Collection clc = chl.findAllCabins();
637         if (clc == null) {
638             out.println("<li>Cabins table is empty</li>");
639         } else {
640             Iterator iterator = clc.iterator();
641             while (iterator.hasNext()) {
642                 CabinLocal cl = (CabinLocal) iterator.next();
643                 String name = cl.getName();
644                 int deck = cl.getDeckLevel();
645                 int bd = cl.getBedCount();
646                 out.println("<li>cabinName = '" + name + "', deckLevel = '" + deck + "', bedCount = '" + bd + "'</li>");
647             }
648         }
649         out.println("</ul>");
650     }
651
652     private void listCustomers(PrintWriter out, CustomerHomeLocal chl) throws Exception {
653         out.println("<p><b>Customers</b> Table Content:</p>");
654         out.println("<ul>");
655         java.util.Collection clc = chl.findAllCustomers();
656         if (clc == null) {
657             out.println("<li>Customers table is empty</li>");
658         } else {
659             java.util.Iterator iterator = clc.iterator();
660             while (iterator.hasNext()) {
661                 CustomerLocal cl = (CustomerLocal) iterator.next();
662                 String name = cl.getName().getLastName();
663                 String fname = cl.getName().getFirstName();
664                 String number = " No Card! ";
665                 boolean gc = cl.getHasGoodCredit();
666                 if (cl.getCreditCard() != null) {
667                     number = cl.getCreditCard().getNumber();
668                 }
669                 out.print("<li>firstName = '" + fname + "', lastName = '" + name + "'");
670                 out.println("<ul>");
671                 out.print("<li>creditCardNumber = '" + number + "', goodCredit = '" + gc + "'</li>");
672                 if (cl.getHomeAddress() != null) {
673                     String city = cl.getHomeAddress().getCity();
674                     String state = cl.getHomeAddress().getState();
675                     out.print("<li>city> = '" + city + "', state = '" + state + "'</li>");
676                 }
677                 out.println("</li>");
678                 out.println("</ul>");
679             }
680         }
681         out.println("</ul>");
682     }
683
684     private void listCreditcards(PrintWriter out, CreditCardHomeLocal cchl) throws Exception {
685         out.println("<p><b>CreditCards</b> Table Content:</p>");
686         out.println("<ul>");
687         java.util.Collection clc = cchl.findAllCreditCards();
688         if (clc == null) {
689             out.println("<li>CreditCards table is empty</li>");
690         } else {
691             java.util.Iterator iterator = clc.iterator();
692             while (iterator.hasNext()) {
693                 CreditCardLocal ccl = (CreditCardLocal) iterator.next();
694                 String number = ccl.getNumber();
695                 String name = ccl.getNameOnCard();
696                 out.println("<li>creditCardNumber = '" + number + "', nameOnCard '= '" + name + "'</li>");
697             }
698         }
699         out.println("</ul>");
700     }
701
702     private void listAddress(PrintWriter out, AddressHomeLocal cchl) throws Exception {
703         out.println("<p><b>Addresses</b> Table Content:</p>");
704         out.println("<ul>");
705         java.util.Collection clc = cchl.findAllAddress();
706         if (clc == null) {
707             out.println("<li>Addresses table is empty</li>");
708         } else {
709             java.util.Iterator iterator = clc.iterator();
710             while (iterator.hasNext()) {
711                 AddressLocal al = (AddressLocal) iterator.next();
712                 String city = al.getCity();
713                 String street = al.getStreet();
714                 out.println("<li>adressCity = '" + city + "', street = '" + street + "'</li>");
715             }
716         }
717         out.println("</ul>");
718     }
719
720     private void listPhones(PrintWriter out, Vector vv) {
721         out.println("<ul>");
722         for (int jj = 0; jj < vv.size(); jj++) {
723             String ss = (String) (vv.get(jj));
724             out.println("<li> " + ss + "</li>");
725         }
726         out.println("</ul>");
727     }
728
729     private void printShip(PrintWriter out, ShipLocal ship) {
730         out.print("<li> shipId = '" + ship.getId() + "', name = '" + ship.getName() + "', tonnage = '"
731                 + ship.getTonnage() + "'</li>");
732     }
733
734     private void printCustomer(PrintWriter out, CustomerLocal customer, AddressLocal addr) {
735         out.print("<ul>");
736         out.print("<li>firstName = '" + customer.getName().getFirstName() + "', lastName = '"
737                 + customer.getName().getLastName() + "', goodCredit = '" + customer.getHasGoodCredit() + "'</li>");
738         out.print("<li>street = '" + addr.getStreet() + "', city = '" + addr.getCity() + "', state = '"
739                 + addr.getState() + "', zip = '" + addr.getZip() + "'</li>");
740         out.print("</ul>");
741     }
742
743     private void printReservation(PrintWriter out, ReservationLocal reservation) {
744         out.print("<ul>");
745         String cru = "no Cruise!";
746         if (reservation.getCruise() != null) {
747             cru = reservation.getCruise().getName();
748         }
749         out.print("<li>reservationDate: '" + reservation.getDate() + "' on '" + cru + "' amountPaid: '"
750                 + reservation.getAmountPaid() + "'</li>");
751         Set cabinset = reservation.getCabins();
752         String customerinfo = "";
753         CustomerLocal cust = null;
754         String cabininfo = "";
755         Set customerset = reservation.getCustomers();
756         Iterator iterator = customerset.iterator();
757         while (iterator.hasNext()) {
758             cust = (CustomerLocal) iterator.next();
759             customerinfo += cust.getName().getLastName() + " ";
760         }
761         iterator = cabinset.iterator();
762         while (iterator.hasNext()) {
763             CabinLocal cabin = (CabinLocal) iterator.next();
764             cabininfo += cabin.getName() + " ";
765         }
766         out.print("<ul>");
767         if (!customerinfo.equals("")) {
768             out.print("<li>customers: " + customerinfo);
769         }
770         if (!cabininfo.equals("")) {
771             out.print("<li>cabins: " + cabininfo + "</li>");
772         }
773         out.print("</ul>");
774         out.print("</ul>");
775     }
776
777 }
Popular Tags