Source Code Cross Referenced for ServletTest2.java in  » J2EE » JOnAS-4.8.6 » servlets » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » servlets 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999-2004 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: ServletTest2.java 4618 2004-04-19 06:39:30Z benoitf $
024:         * --------------------------------------------------------------------------
025:         */package servlets;
026:
027:        import java.io.IOException;
028:        import java.io.PrintWriter;
029:        import java.text.DateFormat;
030:        import java.text.SimpleDateFormat;
031:        import java.util.Calendar;
032:        import java.util.Collection;
033:        import java.util.Date;
034:        import java.util.HashSet;
035:        import java.util.Iterator;
036:        import java.util.Set;
037:
038:        import javax.naming.Context;
039:        import javax.naming.InitialContext;
040:        import javax.servlet.ServletException;
041:        import javax.servlet.http.HttpServlet;
042:        import javax.servlet.http.HttpServletRequest;
043:        import javax.servlet.http.HttpServletResponse;
044:        import javax.transaction.UserTransaction;
045:
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.CustomerHomeLocal;
051:        import com.titan.customer.CustomerLocal;
052:        import com.titan.customer.Name;
053:        import com.titan.reservation.ReservationHomeLocal;
054:        import com.titan.reservation.ReservationLocal;
055:        import com.titan.ship.ShipHomeLocal;
056:        import com.titan.ship.ShipLocal;
057:
058:        /**
059:         * This servlet is used to test O'Reilly examples
060:         * @author JOnAS team
061:         */
062:        public class ServletTest2 extends HttpServlet {
063:
064:            /**
065:             * Called by the server (via the service method) to allow a servlet to
066:             * handle a GET request.
067:             * @param request an HttpServletRequest object that contains the request the
068:             *        client has made of the servlet
069:             * @param response an HttpServletResponse object that contains the response
070:             *        the servlet sends to the client
071:             * @throws IOException if an input or output error is detected when the
072:             *         servlet handles the GET request
073:             * @throws ServletException if the request for the GET could not be handled
074:             */
075:            public void doGet(HttpServletRequest request,
076:                    HttpServletResponse response) throws IOException,
077:                    ServletException {
078:
079:                boolean ok = true;
080:                response.setContentType("text/html");
081:                PrintWriter out = response.getWriter();
082:                out.println("<html>");
083:                out.println("<head>");
084:                out.println("<title>");
085:                out.println("O'Reilly Examples (Suite)</title>");
086:                out.println("<link rel=\"stylesheet\" href=\"style.css\" />");
087:                out.println("</head>");
088:                out
089:                        .println("<body style=\"background : white; color : black;\">");
090:                out
091:                        .println("<h1>Example Showing Cruise/Ship and Cruise/Reservation Relationships</h1>");
092:                Context initialContext = null;
093:                try {
094:                    initialContext = new InitialContext();
095:                } catch (Exception e) {
096:                    out
097:                            .println("<p>ERROR: Cannot get initial context for JNDI: "
098:                                    + e + "</p>");
099:                    return;
100:                }
101:
102:                // Connecting to ShipHomeLocal thru JNDI
103:                ShipHomeLocal shiphome = null;
104:                CruiseHomeLocal cruisehome = null;
105:                ReservationHomeLocal reservationhome = null;
106:                CustomerHomeLocal customerhome = null;
107:                CabinHomeLocal cabinhome = null;
108:                UserTransaction tran = null;
109:                try {
110:                    shiphome = (ShipHomeLocal) initialContext
111:                            .lookup("java:comp/env/ejb/ShipHomeLocal");
112:                    cruisehome = (CruiseHomeLocal) initialContext
113:                            .lookup("java:comp/env/ejb/CruiseHomeLocal");
114:                    reservationhome = (ReservationHomeLocal) initialContext
115:                            .lookup("java:comp/env/ejb/ReservationHomeLocal");
116:                    customerhome = (CustomerHomeLocal) initialContext
117:                            .lookup("java:comp/env/ejb/CustomerHomeLocal");
118:                    cabinhome = (CabinHomeLocal) initialContext
119:                            .lookup("java:comp/env/ejb/CabinHomeLocal");
120:                    tran = (UserTransaction) initialContext
121:                            .lookup("java:comp/UserTransaction");
122:                } catch (Exception e) {
123:                    out
124:                            .println("<p>ERROR: Cannot lookup java:comp/env/ejb/XXHomeLocal: "
125:                                    + e + "</p>");
126:                    return;
127:
128:                }
129:
130:                out.println("<a href=\"index.html\"><b>Back to Menu</b></a>");
131:
132:                out
133:                        .println("<H2>Example Showing Cruise/Ship Relationship (Fig 7-12)</H2>");
134:                out
135:                        .println("<p>(ie Sharing a bean reference in a Many-to-One Relationship)</p>");
136:
137:                out.println("<H3>Creating Ships</H3>");
138:                // Create some Ship beans - manually set key
139:                ShipLocal shipA = null;
140:                ShipLocal shipB = null;
141:                try {
142:                    shipA = shiphome.create(new Integer(1001), "Ship A",
143:                            30000.0);
144:                    shipB = shiphome.create(new Integer(1002), "Ship B",
145:                            40000.0);
146:                } catch (Exception e) {
147:                    out.println("<p>ERROR: Exception caught " + e + "</p>");
148:                    return;
149:                }
150:                out.println("<ul>");
151:                out.println("<li>id = '" + shipA.getId() + "', name = '"
152:                        + shipA.getName() + "', tonnage = '"
153:                        + shipA.getTonnage() + "'</li>");
154:                out.println("<li>id = '" + shipB.getId() + "', name="
155:                        + shipB.getName() + "', tonnage = '"
156:                        + shipB.getTonnage() + " </li>");
157:                out.println("</ul>");
158:
159:                out.println("<H3>Creating Cruises</H3>");
160:                // Create some Cruise beans - automatic key generation by CMP engine
161:                // Link 1-3 to Ship A, 4-6 to Ship B
162:                CruiseLocal[] cruises = new CruiseLocal[7];
163:                try {
164:                    cruises[1] = cruisehome.create("Cruise 1", shipA);
165:                    cruises[2] = cruisehome.create("Cruise 2", shipA);
166:                    cruises[3] = cruisehome.create("Cruise 3", shipA);
167:                    cruises[4] = cruisehome.create("Cruise 4", shipB);
168:                    cruises[5] = cruisehome.create("Cruise 5", shipB);
169:                    cruises[6] = cruisehome.create("Cruise 6", shipB);
170:                } catch (Exception e) {
171:                    out.println("<p>ERROR: Exception caught " + e + "</p>");
172:                    return;
173:                }
174:                out.println("<ul>");
175:                for (int jj = 1; jj < 7; jj++) {
176:                    CruiseLocal cc = cruises[jj];
177:                    out.println("<li>'" + cc.getName() + "' is using '"
178:                            + cc.getShip().getName() + "'</li>");
179:                }
180:                out.println("</ul>");
181:
182:                out
183:                        .print("<H3>Changing 'Cruise 1' to use same ship as 'Cruise 4'</H3>");
184:                ShipLocal newship = cruises[4].getShip();
185:                cruises[1].setShip(newship);
186:                out.println("<ul>");
187:                for (int jj = 1; jj < 7; jj++) {
188:                    CruiseLocal cc = cruises[jj];
189:                    out.println("<li>'" + cc.getName() + "' is using '"
190:                            + cc.getShip().getName() + "'</li>");
191:                }
192:                out.println("</ul>");
193:
194:                out.println("<H3>Content of Tables</H3>");
195:                try {
196:                    listCruises(out, cruisehome);
197:                    listShips(out, shiphome);
198:                } catch (Exception e) {
199:                    out.println("<p>ERROR: exception caught = " + e + " </p>");
200:                }
201:
202:                out
203:                        .println("<H2>Example Showing Cruise/Reservation Relationship Using set() (Fig 7-14)</H2>");
204:                out
205:                        .println("<p>(ie Sharing an entire collection in a One-to-Many bidirectional Relationship Using set() )</p>");
206:                out.println("<H3>Creating some Cruise beans</H3>");
207:                // Create some Cruise beans - leave ship reference empty since we don't
208:                // care
209:                CruiseLocal cruiseA = null;
210:                CruiseLocal cruiseB = null;
211:                try {
212:                    cruiseA = cruisehome.create("Cruise A", null);
213:                    cruiseB = cruisehome.create("Cruise B", null);
214:                } catch (Exception e) {
215:                    out.println("<p>ERROR: exception caught = " + e + " </p>");
216:                }
217:                out.println("<ul>");
218:                out.print("<li>name = '" + cruiseA.getName() + "'</li>");
219:                out.print("<li>name = '" + cruiseB.getName() + "'</li>");
220:                out.println("</ul>");
221:
222:                out.print("<H3>Creating Reservations</H3>");
223:                // Create some Reservation beans - automatic key generation by CMP
224:                // engine
225:                // Link 1-3 to Cruise A, 4-6 to Cruise B
226:                ReservationLocal reservations[] = new ReservationLocal[7];
227:                Calendar date = Calendar.getInstance();
228:                date.set(2002, 10, 1);
229:                // Leave the Customers collection null in the create() call,
230:                // we don't care about it right now
231:                try {
232:                    reservations[1] = reservationhome.create(cruiseA, null);
233:                    reservations[1].setDate(date.getTime());
234:                    reservations[1].setAmountPaid(4000.0);
235:                    date.add(Calendar.DAY_OF_MONTH, 7);
236:
237:                    reservations[2] = reservationhome.create(cruiseA, null);
238:                    reservations[2].setDate(date.getTime());
239:                    reservations[2].setAmountPaid(5000.0);
240:                    date.add(Calendar.DAY_OF_MONTH, 7);
241:
242:                    reservations[3] = reservationhome.create(cruiseA, null);
243:                    reservations[3].setDate(date.getTime());
244:                    reservations[3].setAmountPaid(6000.0);
245:                    date.add(Calendar.DAY_OF_MONTH, 7);
246:
247:                    reservations[4] = reservationhome.create(cruiseB, null);
248:                    reservations[4].setDate(date.getTime());
249:                    reservations[4].setAmountPaid(7000.0);
250:                    date.add(Calendar.DAY_OF_MONTH, 7);
251:
252:                    reservations[5] = reservationhome.create(cruiseB, null);
253:                    reservations[5].setDate(date.getTime());
254:                    reservations[5].setAmountPaid(8000.0);
255:                    date.add(Calendar.DAY_OF_MONTH, 7);
256:
257:                    reservations[6] = reservationhome.create(cruiseB, null);
258:                    reservations[6].setDate(date.getTime());
259:                    reservations[6].setAmountPaid(9000.0);
260:                    date.add(Calendar.DAY_OF_MONTH, 7);
261:
262:                } catch (Exception e) {
263:                    out.println("<p>ERROR: Exception caught " + e + "</p>");
264:                    return;
265:                }
266:                out.println("<ul>");
267:                DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
268:                for (int jj = 1; jj < 7; jj++) {
269:                    ReservationLocal rr = reservations[jj];
270:                    CruiseLocal this cruise = rr.getCruise();
271:                    String cruisename = (this cruise != null ? this cruise
272:                            .getName() : "No Cruise!");
273:                    out.println("<li>reservationDate = '"
274:                            + df.format(rr.getDate()) + "', amountPaid = '"
275:                            + rr.getAmountPaid() + "', for '" + cruisename
276:                            + "'</li> ");
277:                }
278:                out.println("</ul>");
279:
280:                out
281:                        .println("<H3>Testing CruiseB.setReservations(CruiseA.getReservations())</H3>");
282:                // show the effect of a simple "setReservations" on a cruise
283:                Collection areservations = cruiseA.getReservations();
284:                cruiseB.setReservations(areservations);
285:                // Report information on the 6 reservations
286:                String[] cruisename = new String[7];
287:                out.println("<ul>");
288:                for (int jj = 1; jj < 7; jj++) {
289:                    ReservationLocal rr = reservations[jj];
290:                    CruiseLocal this cruise = rr.getCruise();
291:                    cruisename[jj] = (this cruise != null ? this cruise.getName()
292:                            : "No Cruise!");
293:                    out.print("<li>reservationDate = '"
294:                            + df.format(rr.getDate()) + "', amount paid = '"
295:                            + rr.getAmountPaid() + "', for '" + cruisename[jj]
296:                            + "'</li>");
297:                }
298:                out.println("</ul>");
299:                if (!cruisename[4].equals("No Cruise!")
300:                        || !cruisename[5].equals("No Cruise!")
301:                        || !cruisename[6].equals("No Cruise!")) {
302:                    ok = false;
303:                }
304:
305:                out.print("<H3>Content of Tables</H3>");
306:                try {
307:                    listCruises(out, cruisehome);
308:                    listReservations(out, reservationhome);
309:                } catch (Exception e) {
310:                    out.println("<p>ERROR: exception caught = " + e + " </p>");
311:                    return;
312:                }
313:
314:                out
315:                        .println("<H2>Example Showing Cruise/Reservation Relationship Using addAll(..) (Fig 7-15)</H2>");
316:                out
317:                        .print("<p>(ie Using Collection.addAll() in a One-to-Many bidirectional relationship)</p>");
318:                // Show how to combine reservations using Collection methods
319:                // Operations such as this must be done in a transaction, usually done
320:                // within a
321:                // stateless session EJB using declarative transactions rather than
322:                // manually like this
323:
324:                date.set(2002, 10, 1);
325:                // Remove previous reservation
326:                for (int jj = 1; jj < 7; jj++) {
327:                    try {
328:                        reservations[jj].remove();
329:                    } catch (Exception ex) {
330:                        out.println("<p>ERROR: exception caught = " + ex
331:                                + "</p>");
332:                    }
333:                }
334:                // Reinit the reservation as before the previous test
335:                try {
336:                    reservations[1] = reservationhome.create(cruiseA, null);
337:                    reservations[1].setDate(date.getTime());
338:                    reservations[1].setAmountPaid(4000.0);
339:                    date.add(Calendar.DAY_OF_MONTH, 7);
340:
341:                    reservations[2] = reservationhome.create(cruiseA, null);
342:                    reservations[2].setDate(date.getTime());
343:                    reservations[2].setAmountPaid(5000.0);
344:                    date.add(Calendar.DAY_OF_MONTH, 7);
345:
346:                    reservations[3] = reservationhome.create(cruiseA, null);
347:                    reservations[3].setDate(date.getTime());
348:                    reservations[3].setAmountPaid(6000.0);
349:                    date.add(Calendar.DAY_OF_MONTH, 7);
350:
351:                    reservations[4] = reservationhome.create(cruiseB, null);
352:                    reservations[4].setDate(date.getTime());
353:                    reservations[4].setAmountPaid(7000.0);
354:                    date.add(Calendar.DAY_OF_MONTH, 7);
355:
356:                    reservations[5] = reservationhome.create(cruiseB, null);
357:                    reservations[5].setDate(date.getTime());
358:                    reservations[5].setAmountPaid(8000.0);
359:                    date.add(Calendar.DAY_OF_MONTH, 7);
360:
361:                    reservations[6] = reservationhome.create(cruiseB, null);
362:                    reservations[6].setDate(date.getTime());
363:                    reservations[6].setAmountPaid(9000.0);
364:                    date.add(Calendar.DAY_OF_MONTH, 7);
365:
366:                } catch (Exception e) {
367:                    out.println("<p>ERROR: Exception caught " + e + "<p>");
368:                    return;
369:                }
370:
371:                Collection breservations = null;
372:                try {
373:                    tran.begin();
374:                    areservations = cruiseA.getReservations();
375:                    breservations = cruiseB.getReservations();
376:                    breservations.addAll(areservations);
377:                    tran.commit();
378:                } catch (Exception e) {
379:                    e.printStackTrace();
380:                    try {
381:                        tran.rollback();
382:                    } catch (Exception ex) {
383:                        ex.printStackTrace();
384:                        return;
385:                    }
386:                    return;
387:                }
388:                // Report information on the 6 reservations
389:                out.println("<ul>");
390:                for (int jj = 1; jj < 7; jj++) {
391:                    ReservationLocal rr = reservations[jj];
392:                    CruiseLocal this cruise = rr.getCruise();
393:                    cruisename[jj] = (this cruise != null ? this cruise.getName()
394:                            : "No Cruise!");
395:                    out.print("<li>reservationDate = '"
396:                            + df.format(rr.getDate()) + "', amount paid = '"
397:                            + rr.getAmountPaid() + "', for '" + cruisename[jj]
398:                            + "'</li>");
399:                }
400:                out.println("</ul>");
401:
402:                out.print("<H3>Contents of Tables</H3>");
403:                try {
404:                    listCruises(out, cruisehome);
405:                    listReservations(out, reservationhome);
406:                } catch (Exception e) {
407:                    out.println("<p>ERROR: exception caught = " + e + "</p>");
408:                    return;
409:                }
410:
411:                out
412:                        .println("<H2>Example Showing Reservation/Customer Relationships (Fig 7-17)</H2>");
413:                out
414:                        .print("<p>(ie Using Collection.addAll() in a Many-to-Many bidirectional relationship)</p>");
415:
416:                out.println("<H3>Using 'ShipA' and 'CruiseA'</H3>");
417:                cruiseA.setShip(shipA);
418:                out.println("<ul>");
419:                out.println("<li>cruise.getName() = '" + cruiseA.getName()
420:                        + "'</li>");
421:                out.println("<li>ship.getName() = '" + shipA.getName()
422:                        + "'</li>");
423:                out.println("<li>cruise.getShip().getName() = '"
424:                        + cruiseA.getShip().getName() + "'</li>");
425:                out.println("</ul>");
426:
427:                out
428:                        .println("<H3>Creating two sets of customers, one with 1-3, and one with 4-6</H3>");
429:                // create two sets of customers, one with customers 1-3 and one with 4-6
430:                Set lowcustomers = new HashSet();
431:                Set highcustomers = new HashSet();
432:                CustomerLocal cust = null;
433:                out.println("<ul>");
434:                for (int kk = 1; kk < 7; kk++) {
435:                    try {
436:                        cust = customerhome.create(new Integer(kk));
437:                    } catch (Exception e) {
438:                        out.println("<p>ERROR: exception caught = " + e
439:                                + "</p>");
440:                    }
441:                    cust.setName(new Name("Customer " + kk, "mike"));
442:                    if (kk <= 3) {
443:                        lowcustomers.add(cust);
444:                    } else {
445:                        highcustomers.add(cust);
446:                    }
447:                    out.print("<li>customer '" + cust.getName().getLastName()
448:                            + "' created </li>");
449:                }
450:                out.println("</ul>");
451:                // Remove previous reservation
452:                for (int jj = 1; jj < 7; jj++) {
453:                    try {
454:                        reservations[jj].remove();
455:                    } catch (Exception ex) {
456:                        out.println("<p>ERROR: exception caught = " + ex
457:                                + " </p>");
458:                    }
459:                }
460:
461:                out
462:                        .print("<H3>Creating Reservations '1' and '2', each with 3 customers</H3>");
463:                try {
464:                    reservations[1] = reservationhome.create(cruiseA,
465:                            lowcustomers);
466:                    reservations[1].setDate(date.getTime());
467:                    reservations[1].setAmountPaid(4000.0);
468:                    date.add(Calendar.DAY_OF_MONTH, 7);
469:
470:                    reservations[2] = reservationhome.create(cruiseA,
471:                            highcustomers);
472:                    reservations[2].setDate(date.getTime());
473:                    reservations[2].setAmountPaid(5000.0);
474:                    date.add(Calendar.DAY_OF_MONTH, 7);
475:                } catch (Exception e) {
476:                    out.println("<p>ERROR: Exception caught " + e + "</p>");
477:                    return;
478:                }
479:                // report information on the reservations
480:                reportReservations(out, reservations, 2, tran);
481:
482:                out
483:                        .print("<H3>Performing customers_a.addAll(customers_b) test</H3>");
484:                // Finally we can perform the test shown in Figure 7-17
485:                // Operations such as this must be done in a transaction, usually done
486:                // within a
487:                // stateless session EJB using declarative transactions rather than
488:                // manually like this
489:                try {
490:                    tran.begin();
491:                    Set customers_a = reservations[1].getCustomers();
492:                    Set customers_b = reservations[2].getCustomers();
493:                    customers_a.addAll(customers_b);
494:                    tran.commit();
495:                } catch (Exception e) {
496:                    e.printStackTrace();
497:                    try {
498:                        tran.rollback();
499:                    } catch (Exception ex) {
500:                        out
501:                                .println("<p>ERROR: Exception caught " + ex
502:                                        + "</p>");
503:                        return;
504:                    }
505:                }
506:                // report information on the reservations
507:                reportReservations(out, reservations, 2, tran);
508:
509:                out
510:                        .print("<H2>Sharing an entire collection in a Many-to-Many bidirectional relationship (Fig 7-18)</H2>");
511:                out
512:                        .println("<H3>Creating four sets of customers 1-3, 2-4, 3-5, 4-6 </H3>");
513:                // create four sets of customers, 1-3, 2-4, 3-5, 4-6
514:                Set customers13 = new HashSet();
515:                Set customers24 = new HashSet();
516:                Set customers35 = new HashSet();
517:                Set customers46 = new HashSet();
518:                out.println("<ul>");
519:                for (int kk = 1; kk < 7; kk++) {
520:                    try {
521:                        cust = customerhome.create(new Integer(kk + 500));
522:                        cust.setName(new Name("Customer_1 " + kk, "bill"));
523:                    } catch (Exception e) {
524:                        out.println("<p>ERROR: exception caught = " + e
525:                                + " </p></ul>");
526:                        return;
527:                    }
528:                    if (kk <= 3) {
529:                        customers13.add(cust);
530:                    }
531:                    if (kk >= 2 && kk <= 4) {
532:                        customers24.add(cust);
533:                    }
534:                    if (kk >= 3 && kk <= 5) {
535:                        customers35.add(cust);
536:                    }
537:                    if (kk >= 4) {
538:                        customers46.add(cust);
539:                    }
540:                    out.print("<li>customer '" + cust.getName().getLastName()
541:                            + "' created</li>");
542:                }
543:                out.println("</ul>");
544:
545:                out
546:                        .print("<H3>Creating Reservations 1-4 using three customers each</H3>");
547:                ReservationLocal reservations1[] = new ReservationLocal[5];
548:                try {
549:                    reservations1[1] = reservationhome.create(cruiseA,
550:                            customers13);
551:                    reservations1[1].setDate(date.getTime());
552:                    reservations1[1].setAmountPaid(4000.0);
553:                    date.add(Calendar.DAY_OF_MONTH, 7);
554:
555:                    reservations1[2] = reservationhome.create(cruiseA,
556:                            customers24);
557:                    reservations1[2].setDate(date.getTime());
558:                    reservations1[2].setAmountPaid(5000.0);
559:                    date.add(Calendar.DAY_OF_MONTH, 7);
560:
561:                    reservations1[3] = reservationhome.create(cruiseA,
562:                            customers35);
563:                    reservations1[3].setDate(date.getTime());
564:                    reservations1[3].setAmountPaid(6000.0);
565:                    date.add(Calendar.DAY_OF_MONTH, 7);
566:
567:                    reservations1[4] = reservationhome.create(cruiseA,
568:                            customers46);
569:                    reservations1[4].setDate(date.getTime());
570:                    reservations1[4].setAmountPaid(7000.0);
571:                    date.add(Calendar.DAY_OF_MONTH, 7);
572:                } catch (Exception e) {
573:                    out.println("<p>ERROR: Exception caught " + e + "</p>");
574:                    return;
575:                }
576:                reportReservations(out, reservations1, 4, tran);
577:
578:                out
579:                        .print("<H3>Performing reservationD.setCustomers(customersA) (Fig 7-18)</H3>");
580:                try {
581:                    tran.begin();
582:                    Set customers_a = reservations1[1].getCustomers();
583:                    reservations1[4].setCustomers(customers_a);
584:                    tran.commit();
585:                } catch (Exception e) {
586:                    e.printStackTrace();
587:                    try {
588:                        tran.rollback();
589:                    } catch (Exception ex) {
590:                        out
591:                                .println("<p>ERROR: Exception caught " + ex
592:                                        + "</p>");
593:                        return;
594:                    }
595:                }
596:                reportReservations(out, reservations1, 4, tran);
597:
598:                out
599:                        .print("<H2>Example Showing Reservation/Cabin Relationships (Fig 7-20)</H2>");
600:                out
601:                        .print("<p>(ie Removing beans in a Many-to-Many unidirectional relationship)</p>");
602:                out
603:                        .print("<H3>Creating Cabins four sets of cabins, 1-3, 2-4, 3-5, 4-6</H3>");
604:                // create four sets of cabins, 1-3, 2-4, 3-5, 4-6
605:                Set cabins13 = new HashSet();
606:                Set cabins24 = new HashSet();
607:                Set cabins35 = new HashSet();
608:                Set cabins46 = new HashSet();
609:                CabinLocal cabin = null;
610:                out.println("<ul>");
611:                for (int kk = 1; kk < 7; kk++) {
612:                    try {
613:                        cabin = cabinhome.create(new Integer(kk));
614:                    } catch (Exception e) {
615:                        out.println("<p>ERROR: exception caught = " + e
616:                                + " </p></ul>");
617:                    }
618:                    cabin.setName("Cabin " + kk);
619:                    if (kk <= 3) {
620:                        cabins13.add(cabin);
621:                    }
622:                    if (kk >= 2 && kk <= 4) {
623:                        cabins24.add(cabin);
624:                    }
625:                    if (kk >= 3 && kk <= 5) {
626:                        cabins35.add(cabin);
627:                    }
628:                    if (kk >= 4) {
629:                        cabins46.add(cabin);
630:                    }
631:                    out.print("<li>cabin '" + cabin.getName()
632:                            + "' created</li>");
633:                }
634:                out.println("</ul>");
635:
636:                out
637:                        .print("<H3>Creating Reservations 1-4 using three cabins each</H2>");
638:                ReservationLocal reservations2[] = new ReservationLocal[5];
639:                // leave Customers collection null, we dont care about it for this
640:                // example
641:                try {
642:                    reservations2[1] = reservationhome.create(cruiseA, null);
643:                    reservations2[1].setCabins(cabins13);
644:                    reservations2[1].setDate(date.getTime());
645:                    reservations2[1].setAmountPaid(4000.0);
646:                    date.add(Calendar.DAY_OF_MONTH, 7);
647:
648:                    reservations2[2] = reservationhome.create(cruiseA, null);
649:                    reservations2[2].setCabins(cabins24);
650:                    reservations2[2].setDate(date.getTime());
651:                    reservations2[2].setAmountPaid(5000.0);
652:                    date.add(Calendar.DAY_OF_MONTH, 7);
653:
654:                    reservations2[3] = reservationhome.create(cruiseA, null);
655:                    reservations2[3].setCabins(cabins35);
656:                    reservations2[3].setDate(date.getTime());
657:                    reservations2[3].setAmountPaid(6000.0);
658:                    date.add(Calendar.DAY_OF_MONTH, 7);
659:
660:                    reservations2[4] = reservationhome.create(cruiseA, null);
661:                    reservations2[4].setCabins(cabins46);
662:                    reservations2[4].setDate(date.getTime());
663:                    reservations2[4].setAmountPaid(7000.0);
664:                    date.add(Calendar.DAY_OF_MONTH, 7);
665:                } catch (Exception e) {
666:                    out.println("<p>ERROR: Exception caught " + e + "</p>");
667:                    return;
668:                }
669:                reportReservations(out, reservations2, 4, tran);
670:
671:                out
672:                        .print("<H3>Performing cabins_a collection iterator.remove() test</H3>");
673:                // Finally we can perform the test shown in Figure 7-20
674:                try {
675:                    tran.begin();
676:                    Set cabins_a = reservations2[1].getCabins();
677:                    Iterator iterator = cabins_a.iterator();
678:                    out.print("<ul>");
679:                    while (iterator.hasNext()) {
680:                        CabinLocal cc = (CabinLocal) iterator.next();
681:                        out.print("<li>Removing '" + cc.getName()
682:                                + "' from 'cabins_a'" + "</li>");
683:                        iterator.remove();
684:                    }
685:                    out.print("</ul>");
686:                    tran.commit();
687:                } catch (Exception e) {
688:                    try {
689:                        tran.rollback();
690:                    } catch (Exception ex) {
691:                        out
692:                                .println("<p>ERROR: Exception caught " + ex
693:                                        + "</p>");
694:                        return;
695:                    }
696:                }
697:                reportReservations(out, reservations2, 4, tran);
698:
699:                out
700:                        .print("<H2>Example Showing Reservation/Cabin Relationships </H2>");
701:
702:                out.print("<H3>Creating Cabins in ShipA</H3>");
703:                CabinLocal cabins[] = new CabinLocal[4];
704:                try {
705:                    cabins[1] = cabinhome.create(new Integer(10));
706:                    cabins[1].setShip(shipA);
707:                    cabins[1].setDeckLevel(1);
708:                    cabins[1].setName("Minnesota Suite");
709:                    cabins[1].setBedCount(2);
710:
711:                    cabins[2] = cabinhome.create(new Integer(11));
712:                    cabins[2].setShip(shipA);
713:                    cabins[2].setDeckLevel(2);
714:                    cabins[2].setName("California Suite");
715:                    cabins[2].setBedCount(2);
716:
717:                    cabins[3] = cabinhome.create(new Integer(12));
718:                    cabins[3].setShip(shipA);
719:                    cabins[3].setDeckLevel(3);
720:                    cabins[3].setName("Missouri Suite");
721:                    cabins[3].setBedCount(2);
722:                } catch (Exception e) {
723:                    out.println("<p>ERROR: Exception caught " + e + "</p>");
724:                    return;
725:                }
726:                out.println("<ul>");
727:                for (int jj = 1; jj < 4; jj++) {
728:                    CabinLocal cc = cabins[jj];
729:                    out.print("<li>cabin '" + cc.getName() + "' is on ship '"
730:                            + cc.getShip().getName() + "'</li>");
731:                }
732:                out.println("</ul>");
733:
734:                out.print("<H3>Creating Reservations</H3>");
735:                ReservationLocal reservations3[] = new ReservationLocal[3];
736:                try {
737:                    reservations3[1] = reservationhome.create(cruiseA, null);
738:                    reservations3[1].setDate(date.getTime());
739:                    reservations3[1].setAmountPaid(4000.0);
740:                    date.add(Calendar.DAY_OF_MONTH, 7);
741:
742:                    reservations3[2] = reservationhome.create(cruiseA, null);
743:                    reservations3[2].setDate(date.getTime());
744:                    reservations3[2].setAmountPaid(5000.0);
745:                    date.add(Calendar.DAY_OF_MONTH, 7);
746:                } catch (Exception e) {
747:                    out.println("<p>ERROR: Exception caught " + e + "</p>");
748:                    return;
749:                }
750:                reportReservations(out, reservations3, 2, tran);
751:
752:                out
753:                        .print("<H3>Creating Links from Reservations to Cabins</H3>");
754:                Set cabins1 = new HashSet(2);
755:                cabins1.add(cabins[1]);
756:                cabins1.add(cabins[2]);
757:                Set cabins2 = new HashSet(2);
758:                cabins2.add(cabins[2]);
759:                cabins2.add(cabins[3]);
760:                reservations3[1].setCabins(cabins1);
761:                reservations3[2].setCabins(cabins2);
762:                reportReservations(out, reservations3, 2, tran);
763:
764:                out
765:                        .print("<H3>Testing reservation_b.setCabins(reservation_a.getCabins())</H3>");
766:                try {
767:                    tran.begin();
768:                    Set cabins_a = reservations3[1].getCabins();
769:                    reservations3[2].setCabins(cabins_a);
770:                    tran.commit();
771:                } catch (Exception e) {
772:                    try {
773:                        tran.rollback();
774:                    } catch (Exception ex) {
775:                        out
776:                                .println("<p>ERROR: Exception caught " + ex
777:                                        + "</p>");
778:                        return;
779:                    }
780:                }
781:                reportReservations(out, reservations3, 2, tran);
782:
783:                out.print("<H2>Cleaning all tables</H2>");
784:                try {
785:                    Collection clc = customerhome.findAllCustomers();
786:                    java.util.Iterator iterator = clc.iterator();
787:                    while (iterator.hasNext()) {
788:                        CustomerLocal cl = (CustomerLocal) iterator.next();
789:                        cl.remove();
790:                    }
791:                    clc = shiphome.findAllShips();
792:                    iterator = clc.iterator();
793:                    while (iterator.hasNext()) {
794:                        ShipLocal sl = (ShipLocal) iterator.next();
795:                        sl.remove();
796:                    }
797:                    clc = cruisehome.findAllCruises();
798:                    iterator = clc.iterator();
799:                    while (iterator.hasNext()) {
800:                        CruiseLocal cl = (CruiseLocal) iterator.next();
801:                        cl.remove();
802:                    }
803:
804:                    clc = reservationhome.findAllReservations();
805:                    iterator = clc.iterator();
806:                    while (iterator.hasNext()) {
807:                        ReservationLocal rl = (ReservationLocal) iterator
808:                                .next();
809:                        rl.remove();
810:                    }
811:                    clc = cabinhome.findAllCabins();
812:                    iterator = clc.iterator();
813:                    while (iterator.hasNext()) {
814:                        CabinLocal cl = (CabinLocal) iterator.next();
815:                        cl.remove();
816:                    }
817:
818:                } catch (Exception ex) {
819:                    out.println("<p>ERROR: during cleaning exception caught = "
820:                            + ex + "</p>");
821:                }
822:                if (ok) {
823:                    out
824:                            .println("<p align=\"center\"><strong>Servlet is OK.</strong></p>");
825:                }
826:                out.println("<a href=\"index.html\"><b>Back to Menu</b></a>");
827:                out.println("</body>");
828:                out.println("</html>");
829:            }
830:
831:            private void listCruises(PrintWriter out, CruiseHomeLocal chl)
832:                    throws Exception {
833:                out.println("<p><b>Cruises</b> Table Content:</p>");
834:                out.println("<ul>");
835:                java.util.Collection clc = chl.findAllCruises();
836:                if (clc == null) {
837:                    out.println("<li>Cruises table is empty</li>");
838:                } else {
839:                    java.util.Iterator iterator = clc.iterator();
840:                    while (iterator.hasNext()) {
841:                        CruiseLocal cl = (CruiseLocal) iterator.next();
842:                        String cname = cl.getName();
843:                        String sname = (cl.getShip() != null ? cl.getShip()
844:                                .getName() : "No Ship!");
845:                        out.println("<li>cruiseName = '" + cname
846:                                + "', shipName = '" + sname + "'</li>");
847:
848:                    }
849:                }
850:                out.println("</ul>");
851:            }
852:
853:            private void listShips(PrintWriter out, ShipHomeLocal cchl)
854:                    throws Exception {
855:                out.println("<p><b>Ships</b> Table Content:</p>");
856:                out.println("<ul>");
857:                java.util.Collection clc = cchl.findAllShips();
858:                if (clc == null) {
859:                    out.println("<li>Ships table is empty </li>");
860:                } else {
861:                    java.util.Iterator iterator = clc.iterator();
862:                    while (iterator.hasNext()) {
863:                        ShipLocal ccl = (ShipLocal) iterator.next();
864:                        double tonnage = ccl.getTonnage();
865:                        String name = ccl.getName();
866:                        out.println("<li>shipName = '" + name
867:                                + "', tonnage = '" + tonnage + "'</li>");
868:                    }
869:                }
870:                out.println("</ul>");
871:            }
872:
873:            private void listReservations(PrintWriter out,
874:                    ReservationHomeLocal cchl) throws Exception {
875:                out.println("<p><b>Reservations</b> Table Content:</p>");
876:                out.println("<ul>");
877:                java.util.Collection clc = cchl.findAllReservations();
878:                if (clc == null) {
879:                    out.println("<li>Reservations table is empty</li>");
880:                    out.println("</ul>");
881:                } else {
882:                    java.util.Iterator iterator = clc.iterator();
883:                    while (iterator.hasNext()) {
884:                        ReservationLocal ccl = (ReservationLocal) iterator
885:                                .next();
886:                        Date date = ccl.getDate();
887:                        out.println("<li>reservation date = '" + date
888:                                + "'</li>");
889:                    }
890:                    out.println("</ul>");
891:                }
892:            }
893:
894:            private void reportReservations(PrintWriter out,
895:                    ReservationLocal reservations[], int nb,
896:                    UserTransaction tran) {
897:                DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
898:                // report information on the reservations
899:                out.println("<ul>");
900:                for (int jj = 1; jj < nb + 1; jj++) {
901:                    ReservationLocal rr = reservations[jj];
902:                    CruiseLocal this cruise = rr.getCruise();
903:                    String cruisename = (this cruise != null ? this cruise
904:                            .getName() : "No Cruise!");
905:                    // Operations such as this must be done in a transaction, usually
906:                    // done within a
907:                    // stateless session EJB using declarative transactions rather than
908:                    // manually like this
909:                    String customerinfo = "";
910:                    CustomerLocal cust = null;
911:                    String cabininfo = "";
912:                    try {
913:                        tran.begin();
914:                        Set customerset = rr.getCustomers();
915:                        Iterator iterator = customerset.iterator();
916:                        while (iterator.hasNext()) {
917:                            cust = (CustomerLocal) iterator.next();
918:                            customerinfo += cust.getName().getLastName() + " ";
919:                        }
920:                        Set cabinset = rr.getCabins();
921:                        iterator = cabinset.iterator();
922:                        while (iterator.hasNext()) {
923:                            CabinLocal cabin = (CabinLocal) iterator.next();
924:                            cabininfo += cabin.getName() + " ";
925:                        }
926:                        tran.commit();
927:                    } catch (Exception e) {
928:                        e.printStackTrace();
929:                        try {
930:                            tran.rollback();
931:                        } catch (Exception ex) {
932:                            out.println("<p>ERROR: Exception caught " + ex
933:                                    + "</p>");
934:                            return;
935:                        }
936:                    }
937:                    out.print("<li>reservationDate = '"
938:                            + df.format(rr.getDate()) + "', amountPaid = '"
939:                            + rr.getAmountPaid() + "', for '" + cruisename
940:                            + "' with :</li>");
941:                    out.print("<ul>");
942:                    if (!customerinfo.equals("")) {
943:                        out.print("<li>customers: " + customerinfo);
944:                    }
945:                    if (!cabininfo.equals("")) {
946:                        out.print("<li>cabins: " + cabininfo + "</li>");
947:                    }
948:                    out.print("</ul>");
949:                }
950:                out.println("</ul>");
951:            }
952:
953:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.