KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servlets > ServletTest2


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 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: ServletTest2.java,v 1.9 2004/04/19 06:39:30 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package servlets;
28
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
33 import java.util.Calendar;
34 import java.util.Collection;
35 import java.util.Date;
36 import java.util.HashSet;
37 import java.util.Iterator;
38 import java.util.Set;
39
40 import javax.naming.Context;
41 import javax.naming.InitialContext;
42 import javax.servlet.ServletException;
43 import javax.servlet.http.HttpServlet;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46 import javax.transaction.UserTransaction;
47
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.CustomerHomeLocal;
53 import com.titan.customer.CustomerLocal;
54 import com.titan.customer.Name;
55 import com.titan.reservation.ReservationHomeLocal;
56 import com.titan.reservation.ReservationLocal;
57 import com.titan.ship.ShipHomeLocal;
58 import com.titan.ship.ShipLocal;
59
60 /**
61  * This servlet is used to test O'Reilly examples
62  * @author JOnAS team
63  */

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

77     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
78
79         boolean ok = true;
80         response.setContentType("text/html");
81         PrintWriter out = response.getWriter();
82         out.println("<html>");
83         out.println("<head>");
84         out.println("<title>");
85         out.println("O'Reilly Examples (Suite)</title>");
86         out.println("<link rel=\"stylesheet\" HREF=\"style.css\" />");
87         out.println("</head>");
88         out.println("<body style=\"background : white; color : black;\">");
89         out.println("<h1>Example Showing Cruise/Ship and Cruise/Reservation Relationships</h1>");
90         Context initialContext = null;
91         try {
92             initialContext = new InitialContext();
93         } catch (Exception e) {
94             out.println("<p>ERROR: Cannot get initial context for JNDI: " + e + "</p>");
95             return;
96         }
97
98         // Connecting to ShipHomeLocal thru JNDI
99
ShipHomeLocal shiphome = null;
100         CruiseHomeLocal cruisehome = null;
101         ReservationHomeLocal reservationhome = null;
102         CustomerHomeLocal customerhome = null;
103         CabinHomeLocal cabinhome = null;
104         UserTransaction tran = null;
105         try {
106             shiphome = (ShipHomeLocal) initialContext.lookup("java:comp/env/ejb/ShipHomeLocal");
107             cruisehome = (CruiseHomeLocal) initialContext.lookup("java:comp/env/ejb/CruiseHomeLocal");
108             reservationhome = (ReservationHomeLocal) initialContext.lookup("java:comp/env/ejb/ReservationHomeLocal");
109             customerhome = (CustomerHomeLocal) initialContext.lookup("java:comp/env/ejb/CustomerHomeLocal");
110             cabinhome = (CabinHomeLocal) initialContext.lookup("java:comp/env/ejb/CabinHomeLocal");
111             tran = (UserTransaction) initialContext.lookup("java:comp/UserTransaction");
112         } catch (Exception e) {
113             out.println("<p>ERROR: Cannot lookup java:comp/env/ejb/XXHomeLocal: " + e + "</p>");
114             return;
115
116         }
117
118         out.println("<a HREF=\"index.html\"><b>Back to Menu</b></a>");
119
120         out.println("<H2>Example Showing Cruise/Ship Relationship (Fig 7-12)</H2>");
121         out.println("<p>(ie Sharing a bean reference in a Many-to-One Relationship)</p>");
122
123         out.println("<H3>Creating Ships</H3>");
124         // Create some Ship beans - manually set key
125
ShipLocal shipA = null;
126         ShipLocal shipB = null;
127         try {
128             shipA = shiphome.create(new Integer(1001), "Ship A", 30000.0);
129             shipB = shiphome.create(new Integer(1002), "Ship B", 40000.0);
130         } catch (Exception e) {
131             out.println("<p>ERROR: Exception caught " + e + "</p>");
132             return;
133         }
134         out.println("<ul>");
135         out.println("<li>id = '" + shipA.getId() + "', name = '" + shipA.getName() + "', tonnage = '"
136                 + shipA.getTonnage() + "'</li>");
137         out.println("<li>id = '" + shipB.getId() + "', name=" + shipB.getName() + "', tonnage = '" + shipB.getTonnage()
138                 + " </li>");
139         out.println("</ul>");
140
141         out.println("<H3>Creating Cruises</H3>");
142         // Create some Cruise beans - automatic key generation by CMP engine
143
// Link 1-3 to Ship A, 4-6 to Ship B
144
CruiseLocal[] cruises = new CruiseLocal[7];
145         try {
146             cruises[1] = cruisehome.create("Cruise 1", shipA);
147             cruises[2] = cruisehome.create("Cruise 2", shipA);
148             cruises[3] = cruisehome.create("Cruise 3", shipA);
149             cruises[4] = cruisehome.create("Cruise 4", shipB);
150             cruises[5] = cruisehome.create("Cruise 5", shipB);
151             cruises[6] = cruisehome.create("Cruise 6", shipB);
152         } catch (Exception e) {
153             out.println("<p>ERROR: Exception caught " + e + "</p>");
154             return;
155         }
156         out.println("<ul>");
157         for (int jj = 1; jj < 7; jj++) {
158             CruiseLocal cc = cruises[jj];
159             out.println("<li>'" + cc.getName() + "' is using '" + cc.getShip().getName() + "'</li>");
160         }
161         out.println("</ul>");
162
163         out.print("<H3>Changing 'Cruise 1' to use same ship as 'Cruise 4'</H3>");
164         ShipLocal newship = cruises[4].getShip();
165         cruises[1].setShip(newship);
166         out.println("<ul>");
167         for (int jj = 1; jj < 7; jj++) {
168             CruiseLocal cc = cruises[jj];
169             out.println("<li>'" + cc.getName() + "' is using '" + cc.getShip().getName() + "'</li>");
170         }
171         out.println("</ul>");
172
173         out.println("<H3>Content of Tables</H3>");
174         try {
175             listCruises(out, cruisehome);
176             listShips(out, shiphome);
177         } catch (Exception e) {
178             out.println("<p>ERROR: exception caught = " + e + " </p>");
179         }
180
181         out.println("<H2>Example Showing Cruise/Reservation Relationship Using set() (Fig 7-14)</H2>");
182         out
183                 .println("<p>(ie Sharing an entire collection in a One-to-Many bidirectional Relationship Using set() )</p>");
184         out.println("<H3>Creating some Cruise beans</H3>");
185         // Create some Cruise beans - leave ship reference empty since we don't
186
// care
187
CruiseLocal cruiseA = null;
188         CruiseLocal cruiseB = null;
189         try {
190             cruiseA = cruisehome.create("Cruise A", null);
191             cruiseB = cruisehome.create("Cruise B", null);
192         } catch (Exception e) {
193             out.println("<p>ERROR: exception caught = " + e + " </p>");
194         }
195         out.println("<ul>");
196         out.print("<li>name = '" + cruiseA.getName() + "'</li>");
197         out.print("<li>name = '" + cruiseB.getName() + "'</li>");
198         out.println("</ul>");
199
200         out.print("<H3>Creating Reservations</H3>");
201         // Create some Reservation beans - automatic key generation by CMP
202
// engine
203
// Link 1-3 to Cruise A, 4-6 to Cruise B
204
ReservationLocal reservations[] = new ReservationLocal[7];
205         Calendar date = Calendar.getInstance();
206         date.set(2002, 10, 1);
207         // Leave the Customers collection null in the create() call,
208
// we don't care about it right now
209
try {
210             reservations[1] = reservationhome.create(cruiseA, null);
211             reservations[1].setDate(date.getTime());
212             reservations[1].setAmountPaid(4000.0);
213             date.add(Calendar.DAY_OF_MONTH, 7);
214
215             reservations[2] = reservationhome.create(cruiseA, null);
216             reservations[2].setDate(date.getTime());
217             reservations[2].setAmountPaid(5000.0);
218             date.add(Calendar.DAY_OF_MONTH, 7);
219
220             reservations[3] = reservationhome.create(cruiseA, null);
221             reservations[3].setDate(date.getTime());
222             reservations[3].setAmountPaid(6000.0);
223             date.add(Calendar.DAY_OF_MONTH, 7);
224
225             reservations[4] = reservationhome.create(cruiseB, null);
226             reservations[4].setDate(date.getTime());
227             reservations[4].setAmountPaid(7000.0);
228             date.add(Calendar.DAY_OF_MONTH, 7);
229
230             reservations[5] = reservationhome.create(cruiseB, null);
231             reservations[5].setDate(date.getTime());
232             reservations[5].setAmountPaid(8000.0);
233             date.add(Calendar.DAY_OF_MONTH, 7);
234
235             reservations[6] = reservationhome.create(cruiseB, null);
236             reservations[6].setDate(date.getTime());
237             reservations[6].setAmountPaid(9000.0);
238             date.add(Calendar.DAY_OF_MONTH, 7);
239
240         } catch (Exception e) {
241             out.println("<p>ERROR: Exception caught " + e + "</p>");
242             return;
243         }
244         out.println("<ul>");
245         DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
246         for (int jj = 1; jj < 7; jj++) {
247             ReservationLocal rr = reservations[jj];
248             CruiseLocal thiscruise = rr.getCruise();
249             String cruisename = (thiscruise != null ? thiscruise.getName() : "No Cruise!");
250             out.println("<li>reservationDate = '" + df.format(rr.getDate()) + "', amountPaid = '" + rr.getAmountPaid()
251                     + "', for '" + cruisename + "'</li> ");
252         }
253         out.println("</ul>");
254
255         out.println("<H3>Testing CruiseB.setReservations(CruiseA.getReservations())</H3>");
256         // show the effect of a simple "setReservations" on a cruise
257
Collection areservations = cruiseA.getReservations();
258         cruiseB.setReservations(areservations);
259         // Report information on the 6 reservations
260
String[] cruisename = new String[7];
261         out.println("<ul>");
262         for (int jj = 1; jj < 7; jj++) {
263             ReservationLocal rr = reservations[jj];
264             CruiseLocal thiscruise = rr.getCruise();
265             cruisename[jj] = (thiscruise != null ? thiscruise.getName() : "No Cruise!");
266             out.print("<li>reservationDate = '" + df.format(rr.getDate()) + "', amount paid = '" + rr.getAmountPaid()
267                     + "', for '" + cruisename[jj] + "'</li>");
268         }
269         out.println("</ul>");
270         if (!cruisename[4].equals("No Cruise!") || !cruisename[5].equals("No Cruise!")
271                 || !cruisename[6].equals("No Cruise!")) {
272             ok = false;
273         }
274
275         out.print("<H3>Content of Tables</H3>");
276         try {
277             listCruises(out, cruisehome);
278             listReservations(out, reservationhome);
279         } catch (Exception e) {
280             out.println("<p>ERROR: exception caught = " + e + " </p>");
281             return;
282         }
283
284         out.println("<H2>Example Showing Cruise/Reservation Relationship Using addAll(..) (Fig 7-15)</H2>");
285         out.print("<p>(ie Using Collection.addAll() in a One-to-Many bidirectional relationship)</p>");
286         // Show how to combine reservations using Collection methods
287
// Operations such as this must be done in a transaction, usually done
288
// within a
289
// stateless session EJB using declarative transactions rather than
290
// manually like this
291

292         date.set(2002, 10, 1);
293         // Remove previous reservation
294
for (int jj = 1; jj < 7; jj++) {
295             try {
296                 reservations[jj].remove();
297             } catch (Exception ex) {
298                 out.println("<p>ERROR: exception caught = " + ex + "</p>");
299             }
300         }
301         // Reinit the reservation as before the previous test
302
try {
303             reservations[1] = reservationhome.create(cruiseA, null);
304             reservations[1].setDate(date.getTime());
305             reservations[1].setAmountPaid(4000.0);
306             date.add(Calendar.DAY_OF_MONTH, 7);
307
308             reservations[2] = reservationhome.create(cruiseA, null);
309             reservations[2].setDate(date.getTime());
310             reservations[2].setAmountPaid(5000.0);
311             date.add(Calendar.DAY_OF_MONTH, 7);
312
313             reservations[3] = reservationhome.create(cruiseA, null);
314             reservations[3].setDate(date.getTime());
315             reservations[3].setAmountPaid(6000.0);
316             date.add(Calendar.DAY_OF_MONTH, 7);
317
318             reservations[4] = reservationhome.create(cruiseB, null);
319             reservations[4].setDate(date.getTime());
320             reservations[4].setAmountPaid(7000.0);
321             date.add(Calendar.DAY_OF_MONTH, 7);
322
323             reservations[5] = reservationhome.create(cruiseB, null);
324             reservations[5].setDate(date.getTime());
325             reservations[5].setAmountPaid(8000.0);
326             date.add(Calendar.DAY_OF_MONTH, 7);
327
328             reservations[6] = reservationhome.create(cruiseB, null);
329             reservations[6].setDate(date.getTime());
330             reservations[6].setAmountPaid(9000.0);
331             date.add(Calendar.DAY_OF_MONTH, 7);
332
333         } catch (Exception e) {
334             out.println("<p>ERROR: Exception caught " + e + "<p>");
335             return;
336         }
337
338         Collection breservations = null;
339         try {
340             tran.begin();
341             areservations = cruiseA.getReservations();
342             breservations = cruiseB.getReservations();
343             breservations.addAll(areservations);
344             tran.commit();
345         } catch (Exception e) {
346             e.printStackTrace();
347             try {
348                 tran.rollback();
349             } catch (Exception ex) {
350                 ex.printStackTrace();
351                 return;
352             }
353             return;
354         }
355         // Report information on the 6 reservations
356
out.println("<ul>");
357         for (int jj = 1; jj < 7; jj++) {
358             ReservationLocal rr = reservations[jj];
359             CruiseLocal thiscruise = rr.getCruise();
360             cruisename[jj] = (thiscruise != null ? thiscruise.getName() : "No Cruise!");
361             out.print("<li>reservationDate = '" + df.format(rr.getDate()) + "', amount paid = '" + rr.getAmountPaid()
362                     + "', for '" + cruisename[jj] + "'</li>");
363         }
364         out.println("</ul>");
365
366         out.print("<H3>Contents of Tables</H3>");
367         try {
368             listCruises(out, cruisehome);
369             listReservations(out, reservationhome);
370         } catch (Exception e) {
371             out.println("<p>ERROR: exception caught = " + e + "</p>");
372             return;
373         }
374
375         out.println("<H2>Example Showing Reservation/Customer Relationships (Fig 7-17)</H2>");
376         out.print("<p>(ie Using Collection.addAll() in a Many-to-Many bidirectional relationship)</p>");
377
378         out.println("<H3>Using 'ShipA' and 'CruiseA'</H3>");
379         cruiseA.setShip(shipA);
380         out.println("<ul>");
381         out.println("<li>cruise.getName() = '" + cruiseA.getName() + "'</li>");
382         out.println("<li>ship.getName() = '" + shipA.getName() + "'</li>");
383         out.println("<li>cruise.getShip().getName() = '" + cruiseA.getShip().getName() + "'</li>");
384         out.println("</ul>");
385
386         out.println("<H3>Creating two sets of customers, one with 1-3, and one with 4-6</H3>");
387         // create two sets of customers, one with customers 1-3 and one with 4-6
388
Set lowcustomers = new HashSet();
389         Set highcustomers = new HashSet();
390         CustomerLocal cust = null;
391         out.println("<ul>");
392         for (int kk = 1; kk < 7; kk++) {
393             try {
394                 cust = customerhome.create(new Integer(kk));
395             } catch (Exception e) {
396                 out.println("<p>ERROR: exception caught = " + e + "</p>");
397             }
398             cust.setName(new Name("Customer " + kk, "mike"));
399             if (kk <= 3) {
400                 lowcustomers.add(cust);
401             } else {
402                 highcustomers.add(cust);
403             }
404             out.print("<li>customer '" + cust.getName().getLastName() + "' created </li>");
405         }
406         out.println("</ul>");
407         // Remove previous reservation
408
for (int jj = 1; jj < 7; jj++) {
409             try {
410                 reservations[jj].remove();
411             } catch (Exception ex) {
412                 out.println("<p>ERROR: exception caught = " + ex + " </p>");
413             }
414         }
415
416         out.print("<H3>Creating Reservations '1' and '2', each with 3 customers</H3>");
417         try {
418             reservations[1] = reservationhome.create(cruiseA, lowcustomers);
419             reservations[1].setDate(date.getTime());
420             reservations[1].setAmountPaid(4000.0);
421             date.add(Calendar.DAY_OF_MONTH, 7);
422
423             reservations[2] = reservationhome.create(cruiseA, highcustomers);
424             reservations[2].setDate(date.getTime());
425             reservations[2].setAmountPaid(5000.0);
426             date.add(Calendar.DAY_OF_MONTH, 7);
427         } catch (Exception e) {
428             out.println("<p>ERROR: Exception caught " + e + "</p>");
429             return;
430         }
431         // report information on the reservations
432
reportReservations(out, reservations, 2, tran);
433
434         out.print("<H3>Performing customers_a.addAll(customers_b) test</H3>");
435         // Finally we can perform the test shown in Figure 7-17
436
// Operations such as this must be done in a transaction, usually done
437
// within a
438
// stateless session EJB using declarative transactions rather than
439
// manually like this
440
try {
441             tran.begin();
442             Set customers_a = reservations[1].getCustomers();
443             Set customers_b = reservations[2].getCustomers();
444             customers_a.addAll(customers_b);
445             tran.commit();
446         } catch (Exception e) {
447             e.printStackTrace();
448             try {
449                 tran.rollback();
450             } catch (Exception ex) {
451                 out.println("<p>ERROR: Exception caught " + ex + "</p>");
452                 return;
453             }
454         }
455         // report information on the reservations
456
reportReservations(out, reservations, 2, tran);
457
458         out.print("<H2>Sharing an entire collection in a Many-to-Many bidirectional relationship (Fig 7-18)</H2>");
459         out.println("<H3>Creating four sets of customers 1-3, 2-4, 3-5, 4-6 </H3>");
460         // create four sets of customers, 1-3, 2-4, 3-5, 4-6
461
Set customers13 = new HashSet();
462         Set customers24 = new HashSet();
463         Set customers35 = new HashSet();
464         Set customers46 = new HashSet();
465         out.println("<ul>");
466         for (int kk = 1; kk < 7; kk++) {
467             try {
468                 cust = customerhome.create(new Integer(kk + 500));
469                 cust.setName(new Name("Customer_1 " + kk, "bill"));
470             } catch (Exception e) {
471                 out.println("<p>ERROR: exception caught = " + e + " </p></ul>");
472                 return;
473             }
474             if (kk <= 3) {
475                 customers13.add(cust);
476             }
477             if (kk >= 2 && kk <= 4) {
478                 customers24.add(cust);
479             }
480             if (kk >= 3 && kk <= 5) {
481                 customers35.add(cust);
482             }
483             if (kk >= 4) {
484                 customers46.add(cust);
485             }
486             out.print("<li>customer '" + cust.getName().getLastName() + "' created</li>");
487         }
488         out.println("</ul>");
489
490         out.print("<H3>Creating Reservations 1-4 using three customers each</H3>");
491         ReservationLocal reservations1[] = new ReservationLocal[5];
492         try {
493             reservations1[1] = reservationhome.create(cruiseA, customers13);
494             reservations1[1].setDate(date.getTime());
495             reservations1[1].setAmountPaid(4000.0);
496             date.add(Calendar.DAY_OF_MONTH, 7);
497
498             reservations1[2] = reservationhome.create(cruiseA, customers24);
499             reservations1[2].setDate(date.getTime());
500             reservations1[2].setAmountPaid(5000.0);
501             date.add(Calendar.DAY_OF_MONTH, 7);
502
503             reservations1[3] = reservationhome.create(cruiseA, customers35);
504             reservations1[3].setDate(date.getTime());
505             reservations1[3].setAmountPaid(6000.0);
506             date.add(Calendar.DAY_OF_MONTH, 7);
507
508             reservations1[4] = reservationhome.create(cruiseA, customers46);
509             reservations1[4].setDate(date.getTime());
510             reservations1[4].setAmountPaid(7000.0);
511             date.add(Calendar.DAY_OF_MONTH, 7);
512         } catch (Exception e) {
513             out.println("<p>ERROR: Exception caught " + e + "</p>");
514             return;
515         }
516         reportReservations(out, reservations1, 4, tran);
517
518         out.print("<H3>Performing reservationD.setCustomers(customersA) (Fig 7-18)</H3>");
519         try {
520             tran.begin();
521             Set customers_a = reservations1[1].getCustomers();
522             reservations1[4].setCustomers(customers_a);
523             tran.commit();
524         } catch (Exception e) {
525             e.printStackTrace();
526             try {
527                 tran.rollback();
528             } catch (Exception ex) {
529                 out.println("<p>ERROR: Exception caught " + ex + "</p>");
530                 return;
531             }
532         }
533         reportReservations(out, reservations1, 4, tran);
534
535         out.print("<H2>Example Showing Reservation/Cabin Relationships (Fig 7-20)</H2>");
536         out.print("<p>(ie Removing beans in a Many-to-Many unidirectional relationship)</p>");
537         out.print("<H3>Creating Cabins four sets of cabins, 1-3, 2-4, 3-5, 4-6</H3>");
538         // create four sets of cabins, 1-3, 2-4, 3-5, 4-6
539
Set cabins13 = new HashSet();
540         Set cabins24 = new HashSet();
541         Set cabins35 = new HashSet();
542         Set cabins46 = new HashSet();
543         CabinLocal cabin = null;
544         out.println("<ul>");
545         for (int kk = 1; kk < 7; kk++) {
546             try {
547                 cabin = cabinhome.create(new Integer(kk));
548             } catch (Exception e) {
549                 out.println("<p>ERROR: exception caught = " + e + " </p></ul>");
550             }
551             cabin.setName("Cabin " + kk);
552             if (kk <= 3) {
553                 cabins13.add(cabin);
554             }
555             if (kk >= 2 && kk <= 4) {
556                 cabins24.add(cabin);
557             }
558             if (kk >= 3 && kk <= 5) {
559                 cabins35.add(cabin);
560             }
561             if (kk >= 4) {
562                 cabins46.add(cabin);
563             }
564             out.print("<li>cabin '" + cabin.getName() + "' created</li>");
565         }
566         out.println("</ul>");
567
568         out.print("<H3>Creating Reservations 1-4 using three cabins each</H2>");
569         ReservationLocal reservations2[] = new ReservationLocal[5];
570         // leave Customers collection null, we dont care about it for this
571
// example
572
try {
573             reservations2[1] = reservationhome.create(cruiseA, null);
574             reservations2[1].setCabins(cabins13);
575             reservations2[1].setDate(date.getTime());
576             reservations2[1].setAmountPaid(4000.0);
577             date.add(Calendar.DAY_OF_MONTH, 7);
578
579             reservations2[2] = reservationhome.create(cruiseA, null);
580             reservations2[2].setCabins(cabins24);
581             reservations2[2].setDate(date.getTime());
582             reservations2[2].setAmountPaid(5000.0);
583             date.add(Calendar.DAY_OF_MONTH, 7);
584
585             reservations2[3] = reservationhome.create(cruiseA, null);
586             reservations2[3].setCabins(cabins35);
587             reservations2[3].setDate(date.getTime());
588             reservations2[3].setAmountPaid(6000.0);
589             date.add(Calendar.DAY_OF_MONTH, 7);
590
591             reservations2[4] = reservationhome.create(cruiseA, null);
592             reservations2[4].setCabins(cabins46);
593             reservations2[4].setDate(date.getTime());
594             reservations2[4].setAmountPaid(7000.0);
595             date.add(Calendar.DAY_OF_MONTH, 7);
596         } catch (Exception e) {
597             out.println("<p>ERROR: Exception caught " + e + "</p>");
598             return;
599         }
600         reportReservations(out, reservations2, 4, tran);
601
602         out.print("<H3>Performing cabins_a collection iterator.remove() test</H3>");
603         // Finally we can perform the test shown in Figure 7-20
604
try {
605             tran.begin();
606             Set cabins_a = reservations2[1].getCabins();
607             Iterator iterator = cabins_a.iterator();
608             out.print("<ul>");
609             while (iterator.hasNext()) {
610                 CabinLocal cc = (CabinLocal) iterator.next();
611                 out.print("<li>Removing '" + cc.getName() + "' from 'cabins_a'" + "</li>");
612                 iterator.remove();
613             }
614             out.print("</ul>");
615             tran.commit();
616         } catch (Exception e) {
617             try {
618                 tran.rollback();
619             } catch (Exception ex) {
620                 out.println("<p>ERROR: Exception caught " + ex + "</p>");
621                 return;
622             }
623         }
624         reportReservations(out, reservations2, 4, tran);
625
626         out.print("<H2>Example Showing Reservation/Cabin Relationships </H2>");
627
628         out.print("<H3>Creating Cabins in ShipA</H3>");
629         CabinLocal cabins[] = new CabinLocal[4];
630         try {
631             cabins[1] = cabinhome.create(new Integer(10));
632             cabins[1].setShip(shipA);
633             cabins[1].setDeckLevel(1);
634             cabins[1].setName("Minnesota Suite");
635             cabins[1].setBedCount(2);
636
637             cabins[2] = cabinhome.create(new Integer(11));
638             cabins[2].setShip(shipA);
639             cabins[2].setDeckLevel(2);
640             cabins[2].setName("California Suite");
641             cabins[2].setBedCount(2);
642
643             cabins[3] = cabinhome.create(new Integer(12));
644             cabins[3].setShip(shipA);
645             cabins[3].setDeckLevel(3);
646             cabins[3].setName("Missouri Suite");
647             cabins[3].setBedCount(2);
648         } catch (Exception e) {
649             out.println("<p>ERROR: Exception caught " + e + "</p>");
650             return;
651         }
652         out.println("<ul>");
653         for (int jj = 1; jj < 4; jj++) {
654             CabinLocal cc = cabins[jj];
655             out.print("<li>cabin '" + cc.getName() + "' is on ship '" + cc.getShip().getName() + "'</li>");
656         }
657         out.println("</ul>");
658
659         out.print("<H3>Creating Reservations</H3>");
660         ReservationLocal reservations3[] = new ReservationLocal[3];
661         try {
662             reservations3[1] = reservationhome.create(cruiseA, null);
663             reservations3[1].setDate(date.getTime());
664             reservations3[1].setAmountPaid(4000.0);
665             date.add(Calendar.DAY_OF_MONTH, 7);
666
667             reservations3[2] = reservationhome.create(cruiseA, null);
668             reservations3[2].setDate(date.getTime());
669             reservations3[2].setAmountPaid(5000.0);
670             date.add(Calendar.DAY_OF_MONTH, 7);
671         } catch (Exception e) {
672             out.println("<p>ERROR: Exception caught " + e + "</p>");
673             return;
674         }
675         reportReservations(out, reservations3, 2, tran);
676
677         out.print("<H3>Creating Links from Reservations to Cabins</H3>");
678         Set cabins1 = new HashSet(2);
679         cabins1.add(cabins[1]);
680         cabins1.add(cabins[2]);
681         Set cabins2 = new HashSet(2);
682         cabins2.add(cabins[2]);
683         cabins2.add(cabins[3]);
684         reservations3[1].setCabins(cabins1);
685         reservations3[2].setCabins(cabins2);
686         reportReservations(out, reservations3, 2, tran);
687
688         out.print("<H3>Testing reservation_b.setCabins(reservation_a.getCabins())</H3>");
689         try {
690             tran.begin();
691             Set cabins_a = reservations3[1].getCabins();
692             reservations3[2].setCabins(cabins_a);
693             tran.commit();
694         } catch (Exception e) {
695             try {
696                 tran.rollback();
697             } catch (Exception ex) {
698                 out.println("<p>ERROR: Exception caught " + ex + "</p>");
699                 return;
700             }
701         }
702         reportReservations(out, reservations3, 2, tran);
703
704         out.print("<H2>Cleaning all tables</H2>");
705         try {
706             Collection clc = customerhome.findAllCustomers();
707             java.util.Iterator iterator = clc.iterator();
708             while (iterator.hasNext()) {
709                 CustomerLocal cl = (CustomerLocal) iterator.next();
710                 cl.remove();
711             }
712             clc = shiphome.findAllShips();
713             iterator = clc.iterator();
714             while (iterator.hasNext()) {
715                 ShipLocal sl = (ShipLocal) iterator.next();
716                 sl.remove();
717             }
718             clc = cruisehome.findAllCruises();
719             iterator = clc.iterator();
720             while (iterator.hasNext()) {
721                 CruiseLocal cl = (CruiseLocal) iterator.next();
722                 cl.remove();
723             }
724
725             clc = reservationhome.findAllReservations();
726             iterator = clc.iterator();
727             while (iterator.hasNext()) {
728                 ReservationLocal rl = (ReservationLocal) iterator.next();
729                 rl.remove();
730             }
731             clc = cabinhome.findAllCabins();
732             iterator = clc.iterator();
733             while (iterator.hasNext()) {
734                 CabinLocal cl = (CabinLocal) iterator.next();
735                 cl.remove();
736             }
737
738         } catch (Exception ex) {
739             out.println("<p>ERROR: during cleaning exception caught = " + ex + "</p>");
740         }
741         if (ok) {
742             out.println("<p align=\"center\"><strong>Servlet is OK.</strong></p>");
743         }
744         out.println("<a HREF=\"index.html\"><b>Back to Menu</b></a>");
745         out.println("</body>");
746         out.println("</html>");
747     }
748
749     private void listCruises(PrintWriter out, CruiseHomeLocal chl) throws Exception {
750         out.println("<p><b>Cruises</b> Table Content:</p>");
751         out.println("<ul>");
752         java.util.Collection clc = chl.findAllCruises();
753         if (clc == null) {
754             out.println("<li>Cruises table is empty</li>");
755         } else {
756             java.util.Iterator iterator = clc.iterator();
757             while (iterator.hasNext()) {
758                 CruiseLocal cl = (CruiseLocal) iterator.next();
759                 String cname = cl.getName();
760                 String sname = (cl.getShip() != null ? cl.getShip().getName() : "No Ship!");
761                 out.println("<li>cruiseName = '" + cname + "', shipName = '" + sname + "'</li>");
762
763             }
764         }
765         out.println("</ul>");
766     }
767
768     private void listShips(PrintWriter out, ShipHomeLocal cchl) throws Exception {
769         out.println("<p><b>Ships</b> Table Content:</p>");
770         out.println("<ul>");
771         java.util.Collection clc = cchl.findAllShips();
772         if (clc == null) {
773             out.println("<li>Ships table is empty </li>");
774         } else {
775             java.util.Iterator iterator = clc.iterator();
776             while (iterator.hasNext()) {
777                 ShipLocal ccl = (ShipLocal) iterator.next();
778                 double tonnage = ccl.getTonnage();
779                 String name = ccl.getName();
780                 out.println("<li>shipName = '" + name + "', tonnage = '" + tonnage + "'</li>");
781             }
782         }
783         out.println("</ul>");
784     }
785
786     private void listReservations(PrintWriter out, ReservationHomeLocal cchl) throws Exception {
787         out.println("<p><b>Reservations</b> Table Content:</p>");
788         out.println("<ul>");
789         java.util.Collection clc = cchl.findAllReservations();
790         if (clc == null) {
791             out.println("<li>Reservations table is empty</li>");
792             out.println("</ul>");
793         } else {
794             java.util.Iterator iterator = clc.iterator();
795             while (iterator.hasNext()) {
796                 ReservationLocal ccl = (ReservationLocal) iterator.next();
797                 Date date = ccl.getDate();
798                 out.println("<li>reservation date = '" + date + "'</li>");
799             }
800             out.println("</ul>");
801         }
802     }
803
804     private void reportReservations(PrintWriter out, ReservationLocal reservations[], int nb, UserTransaction tran) {
805         DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
806         // report information on the reservations
807
out.println("<ul>");
808         for (int jj = 1; jj < nb + 1; jj++) {
809             ReservationLocal rr = reservations[jj];
810             CruiseLocal thiscruise = rr.getCruise();
811             String cruisename = (thiscruise != null ? thiscruise.getName() : "No Cruise!");
812             // Operations such as this must be done in a transaction, usually
813
// done within a
814
// stateless session EJB using declarative transactions rather than
815
// manually like this
816
String customerinfo = "";
817             CustomerLocal cust = null;
818             String cabininfo = "";
819             try {
820                 tran.begin();
821                 Set customerset = rr.getCustomers();
822                 Iterator iterator = customerset.iterator();
823                 while (iterator.hasNext()) {
824                     cust = (CustomerLocal) iterator.next();
825                     customerinfo += cust.getName().getLastName() + " ";
826                 }
827                 Set cabinset = rr.getCabins();
828                 iterator = cabinset.iterator();
829                 while (iterator.hasNext()) {
830                     CabinLocal cabin = (CabinLocal) iterator.next();
831                     cabininfo += cabin.getName() + " ";
832                 }
833                 tran.commit();
834             } catch (Exception e) {
835                 e.printStackTrace();
836                 try {
837                     tran.rollback();
838                 } catch (Exception ex) {
839                     out.println("<p>ERROR: Exception caught " + ex + "</p>");
840                     return;
841                 }
842             }
843             out.print("<li>reservationDate = '" + df.format(rr.getDate()) + "', amountPaid = '" + rr.getAmountPaid()
844                     + "', for '" + cruisename + "' with :</li>");
845             out.print("<ul>");
846             if (!customerinfo.equals("")) {
847                 out.print("<li>customers: " + customerinfo);
848             }
849             if (!cabininfo.equals("")) {
850                 out.print("<li>cabins: " + cabininfo + "</li>");
851             }
852             out.print("</ul>");
853         }
854         out.println("</ul>");
855     }
856
857 }
Popular Tags