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: ServletTest.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.util.Calendar;
030: import java.util.Vector;
031:
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.servlet.ServletException;
035: import javax.servlet.http.HttpServlet;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import com.titan.address.AddressHomeLocal;
040: import com.titan.address.AddressLocal;
041: import com.titan.customer.CreditCardHomeLocal;
042: import com.titan.customer.CreditCardLocal;
043: import com.titan.customer.CustomerHomeLocal;
044: import com.titan.customer.CustomerLocal;
045: import com.titan.customer.Name;
046: import com.titan.phone.PhoneLocal;
047:
048: /**
049: * This servlet is used to test O'Reilly examples
050: * @author JOnAS team
051: */
052: public class ServletTest extends HttpServlet {
053:
054: /**
055: * Called by the server (via the service method) to allow a servlet to
056: * handle a GET request.
057: * @param request an HttpServletRequest object that contains the request
058: * the client has made of the servlet
059: * @param response an HttpServletResponse object that contains the
060: * response the servlet sends to the client
061: * @throws IOException if an input or output error is detected when the
062: * servlet handles the GET request
063: * @throws ServletException if the request for the GET could not be handled
064: */
065: public void doGet(HttpServletRequest request,
066: HttpServletResponse response) throws IOException,
067: ServletException {
068:
069: boolean ok = true;
070: response.setContentType("text/html");
071: PrintWriter out = response.getWriter();
072:
073: out.println("<html>");
074: out.println("<head>");
075: out.println("<title>");
076: out.println("O'Reilly Examples</title>");
077: out.println("<link rel=\"stylesheet\" href=\"style.css\" />");
078: out.println("</head>");
079: out
080: .println("<body style=\"background : white; color : black;\">");
081: out.println("<h1>Examples 1:1 Relationships</h1>");
082:
083: Context initialContext = null;
084: try {
085: initialContext = new InitialContext();
086: } catch (Exception e) {
087: out
088: .println("<p>ERROR: Cannot get initial context for JNDI: "
089: + e + "</p>");
090: return;
091: }
092:
093: out.println("<a href=\"index.html\"><b>Back to Menu</b></a>");
094:
095: out
096: .println("<h2>Example showing CreditCard/Customer relationship</h2>");
097:
098: out
099: .println("<p>(ie Setting reference in a one-to-one bidirectional relationship)</p>");
100:
101: // Connecting to CustomerHomeLocal and CreditCardHomeLocal thru JNDI
102: CustomerHomeLocal customerhome = null;
103: CreditCardHomeLocal cardhome = null;
104: AddressHomeLocal addresshome = null;
105:
106: try {
107: customerhome = (CustomerHomeLocal) initialContext
108: .lookup("java:comp/env/ejb/CustomerHomeLocal");
109: cardhome = (CreditCardHomeLocal) initialContext
110: .lookup("java:comp/env/ejb/CreditCardHomeLocal");
111: addresshome = (AddressHomeLocal) initialContext
112: .lookup("java:comp/env/ejb/AddressHomeLocal");
113: } catch (Exception e) {
114: out
115: .println("<p>ERROR: Cannot lookup java:comp/env/ejb/XXHomeLocal: "
116: + e + "</p>");
117: return;
118: }
119:
120: out.println("<h3>Creating Customer 'Smith'.</h3>");
121: Integer primaryKey = new Integer(71);
122: CustomerLocal customer = null;
123: try {
124: customer = customerhome.create(primaryKey);
125: } catch (javax.ejb.CreateException ex) {
126: out.println("<p>Cannot create customer: " + ex + "</p>");
127: return;
128: }
129: customer.setName(new Name("Smith", "John"));
130:
131: out.println("<h3>Creating CreditCard.</h3>");
132: // set Credit Card info
133: Calendar now = Calendar.getInstance();
134: CreditCardLocal card = null;
135: try {
136: card = cardhome.create(now.getTime(), "370000000000001",
137: "John Smith", "O'Reilly");
138: } catch (javax.ejb.CreateException ex) {
139: out.println("<p>ERROR: Cannot create creditcard : " + ex
140: + " </p>");
141: return;
142: }
143:
144: out.println("<H3>Linking CreditCard and Customer.</H3>");
145: customer.setCreditCard(card);
146:
147: out
148: .println("<H3>Testing both directions on relationship:</H3>");
149: String cardname = customer.getCreditCard().getNameOnCard();
150: out.println("customer.getCreditCard().getNameOnCard() = '"
151: + cardname + "'<br>");
152: Name name = card.getCustomer().getName();
153: String custfullname = name.getFirstName() + " "
154: + name.getLastName();
155: out.println("card.getCustomer().getName() = '" + custfullname
156: + "'<br>");
157: if (!cardname.equals(custfullname)) {
158: out.println("<p>ERROR: The name must be the same</p>");
159: ok = false;
160: }
161: out.println("<H3>Content of Tables:</H3>");
162: try {
163: listCustomers(out, customerhome);
164: listCreditcards(out, cardhome);
165: } catch (Exception e) {
166: out.println("<p>ERROR: exception caught = " + e + "</p>");
167: }
168:
169: out
170: .println("<H3>Unlink the beans using CreditCard, test Customer side:</H3>");
171: card.setCustomer(null);
172: CreditCardLocal newcardref = customer.getCreditCard();
173: if (newcardref == null) {
174: out
175: .print("<p>Card is properly unlinked from customer bean</p>");
176: } else {
177: out
178: .print("<p>ERROR: Whoops, customer still thinks it has a card !</p>");
179: ok = false;
180: }
181: // relink to avoid side effect on further test
182: card.setCustomer(customer);
183:
184: out
185: .println("<h2>Example Showing Customer/Address Relationship</h2>");
186: out
187: .println("<p>(ie Setting reference in a one-to-one unidirectional relationship)</p>");
188:
189: // Find Customer 71
190: primaryKey = new Integer(71);
191: try {
192: customer = customerhome.findByPrimaryKey(primaryKey);
193: } catch (Exception e) {
194: out.println("<p>ERROR: Exception : " + e + "</p>");
195: return;
196: }
197:
198: AddressLocal addr = customer.getHomeAddress();
199: if (addr == null) {
200: out
201: .println("<h3>Address reference is NULL, Creating one and setting in Customer:</h3>");
202: try {
203: addr = addresshome.createAddress(
204: "333 North Washington", "Minneapolis", "MN",
205: "55401");
206: } catch (Exception e) {
207: out.println("<p>ERROR: Exception : " + e + "</p>");
208: return;
209: }
210: customer.setHomeAddress(addr);
211: }
212: out.println("<p>Address Info: " + addr.getStreet() + ", "
213: + addr.getCity() + ", " + addr.getState() + ", "
214: + addr.getZip() + "</p>");
215: // Modifying Address
216:
217: out
218: .println("<H3>Modifying Address through address reference:</H3>");
219: addr.setStreet("445 East Lake Street");
220: addr.setCity("Wayzata");
221: addr.setState("MN");
222: addr.setZip("55432");
223: out.println("<p>Address Info: " + addr.getStreet() + ", "
224: + addr.getCity() + ", " + addr.getState() + ", "
225: + addr.getZip() + "</p>");
226:
227: out
228: .print("<H3>Creating New Address and calling setHomeAddress():</H3>");
229: AddressLocal addrold = addr;
230: try {
231: addr = addresshome.createAddress("700 Main Street",
232: "St. Paul", "MN", "55302");
233: } catch (Exception e) {
234: out.println("<p>ERROR: Exception : " + e + "</p>");
235: return;
236: }
237: out.println("<p>Address Info: " + addr.getStreet() + ", "
238: + addr.getCity() + ", " + addr.getState() + ", "
239: + addr.getZip() + "</p>");
240: customer.setHomeAddress(addr);
241:
242: // Note: Original Address remains in database, orphaned by setHomeAddress call..
243:
244: out
245: .print("<H3>Retrieving Address reference from Customer via getHomeAddress():</H3>");
246: addr = customer.getHomeAddress();
247: out.print("<p>Address Info: " + addr.getStreet() + ", "
248: + addr.getCity() + ", " + addr.getState() + ", "
249: + addr.getZip() + "</p>");
250:
251: out.print("<H3>Content of Tables:</H3>");
252: try {
253: listCustomers(out, customerhome);
254: listAddress(out, addresshome);
255: } catch (Exception e) {
256: out.println("<p>ERROR: exception caught = " + e + "</p>");
257: return;
258: }
259:
260: out
261: .println("<h2>Example Showing Customer/Phone Relationship</h2>");
262:
263: // Display current phone numbers and types
264: out.println("<h3>Starting content of phone list:</h3>");
265:
266: Vector vv = customer.getPhoneList();
267: listPhones(out, vv);
268:
269: // add a new phone number
270: out
271: .println("<h3>Adding a new type '1' phone number to Customer 'John Smith'.</h3>");
272: try {
273: customer.addPhoneNumber("612-555-1212", (byte) 1);
274: } catch (Exception e) {
275: out.println("<p>ERROR: Exception : " + e + "</p>");
276: return;
277: }
278:
279: out
280: .println("<h3>New content of phone list for customer 'John Smith':</h3>");
281: vv = customer.getPhoneList();
282: listPhones(out, vv);
283:
284: // add a new phone number
285: out
286: .println("<h3>Adding a new type '2' phone number to Customer 'John Smith'.</h3>");
287: try {
288: customer.addPhoneNumber("800-333-3333", (byte) 2);
289: } catch (Exception e) {
290: out.println("<p>ERROR: Exception : " + e + "</p>");
291: return;
292: }
293: out
294: .println("<h3>New content of phone list for customer 'John Smith':</h3>");
295: vv = customer.getPhoneList();
296: listPhones(out, vv);
297:
298: // update a phone number
299: out.println("<h3>Updating type '1' phone numbers.</h3>");
300: try {
301: customer.updatePhoneNumber("763-555-1212", (byte) 1);
302: } catch (Exception e) {
303: out.println("<p>ERROR: Exception : " + e + "</p>");
304: return;
305: }
306:
307: out
308: .println("<h3>New content of phone list for customer 'John Smith':</h3>");
309: vv = customer.getPhoneList();
310: listPhones(out, vv);
311:
312: // delete a phone number
313: out
314: .print("<h3>Removing type '1' phone numbers from this Customer.</h3>");
315: customer.removePhoneNumber((byte) 1);
316:
317: out.println("<h3>Final content of phone list:</h3>");
318: vv = customer.getPhoneList();
319: listPhones(out, vv);
320: // Note that the phone is still in the database,
321: // but it is no longer related to this customer bean
322:
323: // Remove newly created beans for clean
324: out
325: .println("<H2>Remove newly created beans for cleaning.</H2>");
326: try {
327: customer.remove();
328: addrold.remove();
329: } catch (Exception e) {
330: out.println("<p>Exception : " + e + "</p>");
331: return;
332: }
333:
334: if (ok) {
335: out
336: .println("<p align=\"center\"><strong>Servlet is OK.</strong></p>");
337: }
338:
339: out.println("<a href=\"index.html\"><b>Back to Menu</b></a>");
340: out.println("</body>");
341: out.println("</html>");
342: }
343:
344: private void listCustomers(PrintWriter out, CustomerHomeLocal chl)
345: throws Exception {
346: out.println("<p><b>Customers</b> Table Content:</p>");
347: out.println("<ul>");
348: java.util.Collection clc = chl.findAllCustomers();
349: if (clc == null) {
350: out.println("<li>Customers table is empty</li>");
351: } else {
352: java.util.Iterator iterator = clc.iterator();
353: while (iterator.hasNext()) {
354: CustomerLocal cl = (CustomerLocal) iterator.next();
355: String name = cl.getName().getLastName();
356: String number = cl.getCreditCard().getNumber();
357: out.print("<li>customerLastName = '" + name
358: + "', creditCardNumber = '" + number);
359: if (cl.getHomeAddress() != null) {
360: String city = cl.getHomeAddress().getCity();
361: out.print("', city = '" + city);
362: }
363: out.println("'</li>");
364: java.util.Collection phoneNumbers = cl
365: .getPhoneNumbers();
366: java.util.Iterator phiterator = phoneNumbers.iterator();
367: out.println("<ul>");
368: while (phiterator.hasNext()) {
369: PhoneLocal phone = (PhoneLocal) phiterator.next();
370: out.println("<li>phoneType = '" + phone.getType()
371: + "', phoneNumber = '" + phone.getNumber()
372: + "'</li>");
373: }
374: out.println("</ul>");
375:
376: }
377: }
378: out.println("</ul>");
379: }
380:
381: private void listCreditcards(PrintWriter out,
382: CreditCardHomeLocal cchl) throws Exception {
383: out.println("<p><b>Creditcards</b> Table Content:</p>");
384: out.println("<ul>");
385: java.util.Collection clc = cchl.findAllCreditCards();
386: if (clc == null) {
387: out.println("<li>CreditCards table is empty</li>");
388: } else {
389: java.util.Iterator iterator = clc.iterator();
390: while (iterator.hasNext()) {
391: CreditCardLocal ccl = (CreditCardLocal) iterator.next();
392: String number = ccl.getNumber();
393: String name = ccl.getNameOnCard();
394: out.println("<li>creditCardNumber = '" + number
395: + "', nameOnCard = '" + name + "'</li>");
396: }
397: }
398: out.println("</ul>");
399: }
400:
401: private void listAddress(PrintWriter out, AddressHomeLocal cchl)
402: throws Exception {
403: out.println("<p><b>Addresses</b> Table Content:</p>");
404: out.println("<ul>");
405: java.util.Collection clc = cchl.findAllAddress();
406: if (clc == null) {
407: out.println("<li>Addresses table is empty</li>");
408: } else {
409: java.util.Iterator iterator = clc.iterator();
410: while (iterator.hasNext()) {
411: AddressLocal al = (AddressLocal) iterator.next();
412: String city = al.getCity();
413: String street = al.getStreet();
414: out.println("<li>city = '" + city + "', street = '"
415: + street + "'</li>");
416: }
417: }
418: out.println("</ul>");
419: }
420:
421: private void listPhones(PrintWriter out, Vector vv) {
422: out.println("<ul>");
423: if (vv.size() == 0) {
424: out.println("<li>phones list is empty</li>");
425: } else {
426: for (int jj = 0; jj < vv.size(); jj++) {
427: String ss = (String) (vv.get(jj));
428: out.println("<li>" + ss + "</li>");
429: }
430: }
431: out.println("</ul>");
432: }
433:
434: }
|