KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > MDB_AboutMe


1 package edu.rice.rubis.beans;
2
3 import java.rmi.RemoteException JavaDoc;
4 import javax.ejb.MessageDrivenBean JavaDoc;
5 import javax.ejb.MessageDrivenContext JavaDoc;
6 import javax.ejb.EJBException JavaDoc;
7 import javax.jms.*;
8 import javax.naming.Context JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.rmi.PortableRemoteObject JavaDoc;
11 import javax.sql.DataSource JavaDoc;
12 import java.sql.Connection JavaDoc;
13 import java.sql.PreparedStatement JavaDoc;
14 import java.sql.ResultSet JavaDoc;
15 import java.sql.SQLException JavaDoc;
16 import java.io.Serializable JavaDoc;
17 import javax.transaction.UserTransaction JavaDoc;
18 import java.net.URLEncoder JavaDoc;
19
20 /**
21  * This is a stateless session bean used to give to a user the information about himself.
22  *
23  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
24  * @version 1.1
25  */

26
27 public class MDB_AboutMe implements MessageDrivenBean JavaDoc, MessageListener
28 {
29   private DataSource JavaDoc dataSource;
30   private MessageDrivenContext JavaDoc messageDrivenContext;
31   private TopicConnectionFactory connectionFactory;
32   private TopicConnection connection;
33   private Topic topic;
34   private TopicSession session;
35   private TopicPublisher replier;
36   private Context JavaDoc initialContext = null;
37
38
39   public MDB_AboutMe()
40   {
41
42   }
43
44   public void onMessage(Message message)
45   {
46     try
47     {
48       MapMessage request = (MapMessage)message;
49       String JavaDoc correlationID = request.getJMSCorrelationID();
50       String JavaDoc username = request.getString("username");
51       String JavaDoc password = request.getString("password");
52         // Retrieve the connection factory
53
connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName);
54       // get the post comment form
55
String JavaDoc html = getAboutMe(username, password);
56       // send the reply
57
TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo();
58       if (temporaryTopic != null)
59       {
60         // create a connection
61
connection = connectionFactory.createTopicConnection();
62         // create a session: no transaction, auto ack
63
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
64         TextMessage reply = session.createTextMessage();
65         reply.setJMSCorrelationID(correlationID);
66         reply.setText(html);
67
68         replier = session.createPublisher(null); // unidentified publisher
69
connection.start();
70         replier.publish(temporaryTopic, reply);
71         replier.close();
72         session.close();
73         connection.stop();
74         connection.close();
75       }
76     }
77     catch (Exception JavaDoc e)
78     {
79       throw new EJBException JavaDoc("Message traitment failed for MDB_AboutMe: " +e);
80     }
81   }
82
83   /**
84    * Authenticate the user and get the information about the user.
85    *
86    * @return a string in html format
87    * @since 1.1
88    */

89   public String JavaDoc getAboutMe(String JavaDoc username, String JavaDoc password) throws RemoteException JavaDoc
90   {
91        // UserTransaction utx = null;
92
PreparedStatement JavaDoc stmt = null;
93     ResultSet JavaDoc rs = null;
94     Connection JavaDoc conn = null;
95     int userId = -1;
96     StringBuffer JavaDoc html = new StringBuffer JavaDoc();
97
98     // Authenticate the user
99
if ((username != null && !username.equals("")) || (password != null && !password.equals("")))
100       {
101         TopicConnection authConnection;
102         TopicSession authSession;
103         Topic authTopic;
104         try
105         {
106           // create a connection
107
authConnection = connectionFactory.createTopicConnection();
108           // lookup the destination
109
authTopic = (Topic) initialContext.lookup(BeanConfig.PrefixTopicName+"topicAuth");
110           // create a session
111
authSession = authConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
112
}
113         catch (Exception JavaDoc e)
114         {
115           throw new EJBException JavaDoc("Cannot connect to message bean MDB_Auth : " +e+"<br>");
116         }
117         try
118         {
119           // create a requestor to receive the reply
120
TopicRequestor requestor = new TopicRequestor(authSession, authTopic);
121           // create a message
122
MapMessage m = authSession.createMapMessage();
123           // set parameters
124
m.setStringProperty("nickname", username);
125           m.setStringProperty("password", password);
126           m.setJMSCorrelationID("auth");
127           // send the message and receive the reply
128
authConnection.start(); // allows message to be delivered (default is connection stopped)
129
MapMessage authReply = (MapMessage)requestor.request(m);
130           authConnection.stop();
131           // read the reply
132
userId = authReply.getInt("userId");
133           // close connection and session
134
requestor.close(); // also close the session
135
authConnection.close();
136         }
137         catch (Exception JavaDoc e)
138         {
139           throw new EJBException JavaDoc("user authentication failed: " +e+"<br>");
140         }
141         if (userId == -1)
142         {
143            html.append("You don't have an account on RUBiS!<br>You have to register first.<br>");
144            return html.toString();
145         }
146       }
147     // Try to find the user corresponding to the userId
148
String JavaDoc firstname = null, lastname = null, nickname = null, email = null, date = null;
149     int rating = 0;
150     try
151     {
152       conn = dataSource.getConnection();
153       stmt = conn.prepareStatement("SELECT * FROM users WHERE id=?");
154       stmt.setInt(1, userId);
155       rs = stmt.executeQuery();
156     }
157     catch (Exception JavaDoc e)
158     {
159       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
160       try { conn.close(); } catch (Exception JavaDoc ignore) {}
161       throw new RemoteException JavaDoc("Failed to execute Query for user: " +e);
162     }
163     try
164     {
165       if (rs.first())
166       {
167         firstname = rs.getString("firstname");
168         lastname = rs.getString("lastname");
169         nickname = rs.getString("nickname");
170         email = rs.getString("email");
171         date = rs.getString("creation_date");
172         rating = rs.getInt("rating");
173       }
174       stmt.close();
175       conn.close();
176       html.append("<h2>Information about "+nickname+"<br></h2>");
177       html.append("Real life name : "+firstname+" "+lastname+"<br>");
178       html.append("Email address : "+email+"<br>");
179       html.append("User since : "+date+"<br>");
180       html.append("Current rating : <b>"+rating+"</b><br>");
181     }
182     catch (Exception JavaDoc e)
183     {
184       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
185       try { conn.close(); } catch (Exception JavaDoc ignore) {}
186       throw new RemoteException JavaDoc("This user does not exist (got exception: " +e+")<br>");
187     }
188
189     try
190     {
191        // utx = messageDrivenContext.getUserTransaction();
192
// utx.begin();
193
conn = dataSource.getConnection();
194       html.append(listItem(userId, conn));
195       html.append(listBoughtItems(userId, conn));
196       html.append(listWonItems(userId, conn));
197       html.append(listBids(userId, username, password, conn));
198       html.append(listComments(userId, conn));
199       conn.close();
200       // utx.commit();
201
}
202     catch (Exception JavaDoc e)
203     {
204       try { conn.close(); } catch (Exception JavaDoc ignore) {}
205        // try { utx.rollback(); } catch (Exception ignore) {}
206
throw new RemoteException JavaDoc("Cannot get information about items and bids: " +e+"<br>");
207     }
208     return html.toString();
209   }
210                    
211   /** List items the user is currently selling and sold in the past 30 days */
212   public String JavaDoc listItem(int userId, Connection JavaDoc conn) throws RemoteException JavaDoc
213   {
214     StringBuffer JavaDoc sell = new StringBuffer JavaDoc();
215     ResultSet JavaDoc currentSellings = null;
216     ResultSet JavaDoc pastSellings = null;
217     PreparedStatement JavaDoc pastStmt = null;
218     PreparedStatement JavaDoc currentStmt = null;
219
220     String JavaDoc itemName, endDate, startDate;
221     float currentPrice=0, initialPrice=0, buyNow=0, reservePrice=0;
222     int quantity=0, itemId=-1;
223
224     // Retrieve ItemHome to get the names of the items the user sold
225
try
226     {
227       currentStmt = conn.prepareStatement("SELECT * FROM items WHERE items.seller=? AND items.end_date>=NOW()");
228       currentStmt.setInt(1, userId);
229       currentSellings = currentStmt.executeQuery();
230     }
231     catch (Exception JavaDoc e)
232     {
233       try { currentStmt.close(); } catch (Exception JavaDoc ignore) {}
234       try { conn.close(); } catch (Exception JavaDoc ignore) {}
235       throw new RemoteException JavaDoc("Exception getting current sellings list: " +e+"<br>");
236     }
237     try
238     {
239       pastStmt = conn.prepareStatement("SELECT * FROM old_items WHERE old_items.seller=? AND TO_DAYS(NOW()) - TO_DAYS(old_items.end_date) < 30");
240       pastStmt.setInt(1, userId);
241       pastSellings = pastStmt.executeQuery();
242     }
243     catch (Exception JavaDoc e)
244     {
245       try { pastStmt.close(); } catch (Exception JavaDoc ignore) {}
246       try { conn.close(); } catch (Exception JavaDoc ignore) {}
247       throw new RemoteException JavaDoc("Exception getting past sellings list: " +e+"<br>");
248     }
249     try
250     {
251       if (currentSellings.first())
252       {
253         // display current sellings
254
sell.append(printSellHeader("Items you are currently selling."));
255         do
256         {
257           // Get the name of the items
258
try
259           {
260             itemId = currentSellings.getInt("id");
261             itemName = currentSellings.getString("name");
262             endDate = currentSellings.getString("end_date");
263             startDate = currentSellings.getString("start_date");
264             initialPrice = currentSellings.getFloat("initial_price");
265             reservePrice = currentSellings.getFloat("reserve_price");
266             buyNow = currentSellings.getFloat("buy_now");
267             quantity = currentSellings.getInt("quantity");
268
269             currentPrice = currentSellings.getFloat("max_bid");
270             if (currentPrice <initialPrice)
271               currentPrice = initialPrice;
272
273           }
274           catch (Exception JavaDoc e)
275           {
276             try { currentStmt.close(); } catch (Exception JavaDoc ignore) {}
277             try { conn.close(); } catch (Exception JavaDoc ignore) {}
278             throw new RemoteException JavaDoc("Exception getting item: " + e +"<br>");
279           }
280           // display information about the item
281
sell.append(printSell(itemId, itemName, initialPrice, currentPrice, quantity, reservePrice, buyNow, startDate, endDate));
282         }
283         while (currentSellings.next());
284         currentStmt.close();
285         sell.append(printItemFooter());
286       }
287       else
288       {
289         currentStmt.close();
290         sell.append("<br>");
291         sell.append(printHTMLHighlighted("<h3>You are currently selling no item.</h3>"));
292       }
293     }
294     catch (Exception JavaDoc e)
295     {
296       try { currentStmt.close(); } catch (Exception JavaDoc ignore) {}
297       try { conn.close(); } catch (Exception JavaDoc ignore) {}
298       throw new RemoteException JavaDoc("Exception getting current items in sell: " + e +"<br>");
299     }
300     try
301     {
302       if (pastSellings.first())
303       {
304         // display past sellings
305
sell.append("<br>");
306         sell.append(printSellHeader("Items you sold in the last 30 days."));
307         do
308         {
309           // Get the name of the items
310
try
311           {
312             itemId = pastSellings.getInt("id");
313             itemName = pastSellings.getString("name");
314             endDate = pastSellings.getString("end_date");
315             startDate = pastSellings.getString("start_date");
316             initialPrice = pastSellings.getFloat("initial_price");
317             reservePrice = pastSellings.getFloat("reserve_price");
318             buyNow = pastSellings.getFloat("buy_now");
319             quantity = pastSellings.getInt("quantity");
320
321             currentPrice = pastSellings.getFloat("max_bid");
322             if (currentPrice <initialPrice)
323               currentPrice = initialPrice;
324           }
325           catch (Exception JavaDoc e)
326           {
327             try { pastStmt.close(); } catch (Exception JavaDoc ignore) {}
328             try { conn.close(); } catch (Exception JavaDoc ignore) {}
329             throw new RemoteException JavaDoc("Exception getting sold item: " + e +"<br>");
330           }
331           // display information about the item
332
sell.append(printSell(itemId, itemName, initialPrice, currentPrice, quantity, reservePrice, buyNow, startDate, endDate));
333         }
334         while (pastSellings.next());
335         pastStmt.close();
336         sell.append(printItemFooter());
337       }
338       else
339       {
340         sell.append(printHTMLHighlighted("<br><h3>You didn't sell any item in the past 30 days.</h3>"));
341         pastStmt.close();
342         return sell.toString();
343       }
344     }
345     catch (Exception JavaDoc e)
346     {
347       try { pastStmt.close(); } catch (Exception JavaDoc ignore) {}
348       try { conn.close(); } catch (Exception JavaDoc ignore) {}
349       throw new RemoteException JavaDoc("Exception getting sold items: " + e +"<br>");
350     }
351     return sell.toString();
352   }
353
354   /** List items the user bought in the last 30 days*/
355   public String JavaDoc listBoughtItems(int userId, Connection JavaDoc conn) throws RemoteException JavaDoc
356   {
357     ResultSet JavaDoc buy = null;
358     PreparedStatement JavaDoc stmt = null;
359     String JavaDoc itemName = null, sellerName = null;
360     int quantity=0, sellerId=-1, itemId=-1;
361     float buyNow=0;
362     StringBuffer JavaDoc html = new StringBuffer JavaDoc();
363
364     // Get the list of items the user bought
365
try
366     {
367       stmt = conn.prepareStatement("SELECT * FROM buy_now WHERE buy_now.buyer_id=? AND TO_DAYS(NOW()) - TO_DAYS(buy_now.date)<=30");
368       stmt.setInt(1, userId);
369       buy = stmt.executeQuery();
370       if (!buy.first())
371       {
372         stmt.close();
373         return printHTMLHighlighted("<br><h3>You didn't buy any item in the last 30 days.</h3><br>");
374       }
375     }
376     catch (Exception JavaDoc e)
377     {
378       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
379       try { conn.close(); } catch (Exception JavaDoc ignore) {}
380       throw new RemoteException JavaDoc("Exception getting bought items list: " +e+"<br>");
381     }
382     html.append(printUserBoughtItemHeader());
383     PreparedStatement JavaDoc itemStmt = null;
384     PreparedStatement JavaDoc sellerStmt = null;
385     try
386     {
387       itemStmt = conn.prepareStatement("SELECT * FROM items WHERE id=?");
388       sellerStmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
389       ResultSet JavaDoc itemRS = null;
390       ResultSet JavaDoc sellerResult = null;
391       do
392       {
393     itemId = buy.getInt("item_id");
394     quantity = buy.getInt("qty");
395         // Get the name of the items
396

397         itemStmt.setInt(1, itemId);
398         itemRS = itemStmt.executeQuery();
399         if (itemRS.first())
400         {
401           itemName = itemRS.getString("name");
402           sellerId = itemRS.getInt("seller");
403           buyNow = itemRS.getFloat("buy_now");
404         }
405
406         sellerStmt.setInt(1, sellerId);
407         sellerResult = sellerStmt.executeQuery();
408         // Get the seller's name
409
if (sellerResult.first())
410           sellerName = sellerResult.getString("nickname");
411
412         // display information about the item
413
html.append(printUserBoughtItem(itemId, itemName, quantity, buyNow, sellerId, sellerName));
414       }
415       while (buy.next());
416       stmt.close();
417       itemStmt.close();
418       sellerStmt.close();
419     }
420     catch (Exception JavaDoc e)
421     {
422       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
423       try { itemStmt.close(); } catch (Exception JavaDoc ignore) {}
424       try { sellerStmt.close(); } catch (Exception JavaDoc ignore) {}
425       throw new RemoteException JavaDoc("Exception getting bought items: " +e+"<br>");
426     }
427     html.append(printItemFooter());
428     return html.toString();
429
430   }
431
432   /** List items the user won in the last 30 days*/
433   public String JavaDoc listWonItems(int userId, Connection JavaDoc conn) throws RemoteException JavaDoc
434   {
435     int sellerId=-1, itemId=-1;
436     float currentPrice=0, initialPrice=0;
437     String JavaDoc itemName = null, sellerName = null;
438     ResultSet JavaDoc won = null;
439     PreparedStatement JavaDoc stmt = null;
440     StringBuffer JavaDoc html;
441
442     // Get the list of the user's won items
443
try
444     {
445       stmt = conn.prepareStatement("SELECT item_id FROM bids, old_items WHERE bids.user_id=? AND bids.item_id=old_items.id AND TO_DAYS(NOW()) - TO_DAYS(old_items.end_date) < 30 GROUP BY item_id");
446       stmt.setInt(1, userId);
447       won = stmt.executeQuery();
448       if (!won.first())
449       {
450         stmt.close();
451         return printHTMLHighlighted("<br><h3>You didn't win any item in the last 30 days.</h3><br>");
452       }
453     }
454     catch (Exception JavaDoc e)
455     {
456       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
457       try { conn.close(); } catch (Exception JavaDoc ignore) {}
458       throw new RemoteException JavaDoc("Exception getting won items list: " +e+"<br>");
459     }
460     html = new StringBuffer JavaDoc(printUserWonItemHeader());
461
462     PreparedStatement JavaDoc itemStmt = null;
463     PreparedStatement JavaDoc sellerStmt = null;
464     try
465     {
466       itemStmt = conn.prepareStatement("SELECT * FROM old_items WHERE id=?");
467       sellerStmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
468       ResultSet JavaDoc itemRS = null;
469       ResultSet JavaDoc sellerResult = null;
470       do
471       {
472     itemId = won.getInt("item_id");
473         // Get the name of the items
474
itemStmt.setInt(1, itemId);
475         itemRS = itemStmt.executeQuery();
476         if (itemRS.first())
477         {
478           itemName = itemRS.getString("name");
479           sellerId = itemRS.getInt("seller");
480           initialPrice = itemRS.getFloat("initial_price");
481
482           currentPrice = itemRS.getFloat("max_bid");
483           if (currentPrice <initialPrice)
484             currentPrice = initialPrice;
485         }
486
487         sellerStmt.setInt(1, sellerId);
488         sellerResult = sellerStmt.executeQuery();
489         // Get the seller's name
490
if (sellerResult.first())
491           sellerName = sellerResult.getString("nickname");
492
493         // display information about the item
494
html.append(printUserWonItem(itemId, itemName, currentPrice, sellerId, sellerName));
495       }
496       while (won.next());
497       stmt.close();
498       itemStmt.close();
499       sellerStmt.close();
500     }
501     catch (Exception JavaDoc e)
502     {
503       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
504       try { itemStmt.close(); } catch (Exception JavaDoc ignore) {}
505       try { sellerStmt.close(); } catch (Exception JavaDoc ignore) {}
506       try { conn.close(); } catch (Exception JavaDoc ignore) {}
507       throw new RemoteException JavaDoc("Exception getting won items: " +e+"<br>");
508     }
509     html.append(printItemFooter());
510     return html.toString();
511   }
512
513
514   /** List comments about the user */
515   public String JavaDoc listComments(int userId, Connection JavaDoc conn) throws RemoteException JavaDoc
516   {
517     ResultSet JavaDoc rs = null;
518     PreparedStatement JavaDoc stmt = null;
519     String JavaDoc date = null, comment = null;
520     int authorId = -1;
521     StringBuffer JavaDoc html = null;
522
523     // Try to find the comment corresponding to the user
524
try
525     {
526       stmt = conn.prepareStatement("SELECT * FROM comments WHERE to_user_id=?");
527       stmt.setInt(1, userId);
528       rs = stmt.executeQuery();
529       if (!rs.first())
530       {
531         stmt.close();
532         return printHTMLHighlighted(("<br><h3>There is no comment yet for this user.</h3><br>"));
533       }
534       else
535         html = new StringBuffer JavaDoc(printHTMLHighlighted("<h3>Comments for this user</h3><br>"));
536     }
537     catch (Exception JavaDoc e)
538     {
539       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
540       try { conn.close(); } catch (Exception JavaDoc ignore) {}
541       throw new RemoteException JavaDoc("Failed to execute Query for list of comments: " +e);
542     }
543     html.append(printCommentHeader());
544     PreparedStatement JavaDoc authorStmt = null;
545     try
546     {
547     // Display each comment and the name of its author
548
authorStmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
549       ResultSet JavaDoc authorRS = null;
550       do
551       {
552         comment = rs.getString("comment");
553         date = rs.getString("date");
554         authorId = rs.getInt("from_user_id");
555
556         String JavaDoc authorName = "none";
557         try
558         {
559           authorStmt.setInt(1, authorId);
560           authorRS = authorStmt.executeQuery();
561           if (authorRS.first())
562             authorName = authorRS.getString("nickname");
563         }
564         catch (Exception JavaDoc e)
565         {
566           throw new RemoteException JavaDoc("Failed to execute Query for the comment author: " +e);
567         }
568         html.append(printComment(authorName, date, comment, authorId));
569       }
570       while (rs.next());
571       authorStmt.close();
572       stmt.close();
573     }
574     catch (Exception JavaDoc e)
575     {
576       try { authorStmt.close(); } catch (Exception JavaDoc ignore) {}
577       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
578       try { conn.close(); } catch (Exception JavaDoc ignore) {}
579       throw new RemoteException JavaDoc("Failed to get comments list: " +e);
580     }
581     html.append(printCommentFooter());
582     return html.toString();
583   }
584
585   /** List items the user put a bid on in the last 30 days*/
586   public String JavaDoc listBids(int userId, String JavaDoc username, String JavaDoc password, Connection JavaDoc conn) throws RemoteException JavaDoc
587   {
588     float currentPrice = 0, initialPrice = 0, maxBid = 0;
589     String JavaDoc itemName = null, sellerName = null, startDate = null, endDate = null;
590     int sellerId = -1, quantity = 0, itemId = -1;
591     ResultSet JavaDoc bid = null;
592     PreparedStatement JavaDoc stmt = null;
593     StringBuffer JavaDoc html = null;
594
595     // Get the list of the user's last bids
596
try
597     {
598       stmt = conn.prepareStatement("SELECT item_id, bids.max_bid FROM bids, items WHERE bids.user_id=? AND bids.item_id=items.id AND items.end_date>=NOW() GROUP BY item_id");
599       stmt.setInt(1, userId);
600       bid = stmt.executeQuery();
601       if (!bid.first())
602       {
603         stmt.close();
604         return printHTMLHighlighted("<h3>You didn't put any bid.</h3>");
605       }
606     }
607     catch (Exception JavaDoc e)
608     {
609       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
610       try { conn.close(); } catch (Exception JavaDoc ignore) {}
611       throw new RemoteException JavaDoc("Exception getting bids list: " +e+"<br>");
612     }
613     html = new StringBuffer JavaDoc(printUserBidsHeader());
614
615     PreparedStatement JavaDoc itemStmt = null;
616     PreparedStatement JavaDoc sellerStmt = null;
617     try
618     {
619       itemStmt = conn.prepareStatement("SELECT * FROM items WHERE id=?");
620       sellerStmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
621       ResultSet JavaDoc rs = null;
622       ResultSet JavaDoc sellerResult = null;
623       do
624       {
625     itemId = bid.getInt("item_id");
626     maxBid = bid.getFloat("max_bid"); // maximum bid
627

628         itemStmt.setInt(1, itemId);
629         rs = itemStmt.executeQuery();
630
631         // Get the name of the items
632
if (rs.first())
633         {
634           itemName = rs.getString("name");
635           initialPrice = rs.getFloat("initial_price");
636           quantity = rs.getInt("quantity");
637           startDate = rs.getString("start_date");
638           endDate = rs.getString("end_date");
639           sellerId = rs.getInt("seller");
640
641           currentPrice = rs.getFloat("max_bid"); // currentPrice
642
if (currentPrice <initialPrice)
643             currentPrice = initialPrice;
644         }
645
646         sellerStmt.setInt(1, sellerId);
647         sellerResult = sellerStmt.executeQuery();
648         // Get the seller's name
649
if (sellerResult.first())
650           sellerName = sellerResult.getString("nickname");
651
652         // display information about user's bids
653
html.append(printItemUserHasBidOn(itemId, itemName, initialPrice, maxBid, currentPrice, quantity, startDate, endDate, sellerId, sellerName, username, password));
654       }
655       while(bid.next());
656       stmt.close();
657       itemStmt.close();
658       sellerStmt.close();
659     }
660     catch (Exception JavaDoc e)
661     {
662       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
663       try { itemStmt.close(); } catch (Exception JavaDoc ignore) {}
664       try { sellerStmt.close(); } catch (Exception JavaDoc ignore) {}
665       try { conn.close(); } catch (Exception JavaDoc ignore) {}
666       throw new RemoteException JavaDoc("Exception getting items the user has bid on: " + e +"<br>");
667     }
668     html.append(printItemFooter());
669     return html.toString();
670   }
671
672   /**
673    * user's bought items list header printed function
674    *
675    * @return a string in html format
676    * @since 1.1
677    */

678   public String JavaDoc printUserBoughtItemHeader()
679   {
680     return "<br>"+
681       printHTMLHighlighted("<p><h3>Items you bouhgt in the past 30 days.</h3>\n")+
682       "<TABLE border=\"1\" summary=\"List of items\">\n"+
683       "<THEAD>\n"+
684       "<TR><TH>Designation<TH>Quantity<TH>Price you bought it<TH>Seller"+
685       "<TBODY>\n";
686   }
687
688   /**
689    * user's won items list header printed function
690    *
691    * @return a string in html format
692    * @since 1.1
693    */

694   public String JavaDoc printUserWonItemHeader()
695   {
696     return "<br>"+
697       printHTMLHighlighted("<p><h3>Items you won in the past 30 days.</h3>\n")+
698       "<TABLE border=\"1\" summary=\"List of items\">\n"+
699       "<THEAD>\n"+
700       "<TR><TH>Designation<TH>Price you bought it<TH>Seller"+
701       "<TBODY>\n";
702   }
703
704   /**
705    * user's bids list header printed function
706    *
707    * @return a string in html format
708    * @since 1.1
709    */

710   public String JavaDoc printUserBidsHeader()
711   {
712     return "<br>"+
713       printHTMLHighlighted("<p><h3>Items you have bid on.</h3>\n")+
714       "<TABLE border=\"1\" summary=\"Items You've bid on\">\n"+
715       "<THEAD>\n"+
716       "<TR><TH>Designation<TH>Initial Price<TH>Current price<TH>Your max bid<TH>Quantity"+
717       "<TH>Start Date<TH>End Date<TH>Seller<TH>Put a new bid\n"+
718       "<TBODY>\n";
719   }
720
721
722   /**
723    * Display item information for the AboutMe servlet
724    *
725    * @return a <code>String</code> containing HTML code
726    * @exception RemoteException if an error occurs
727    * @since 1.0
728    */

729   public String JavaDoc printUserBoughtItem(int id, String JavaDoc name, int qty, float buyNow, int sellerId, String JavaDoc sellerName) throws RemoteException JavaDoc
730   {
731     return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+"</a>\n"+
732       "<TD>"+qty+"\n"+"<TD>"+buyNow+"\n"+
733       "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+sellerName+"</a>\n";
734   }
735
736   /**
737    * Display item information for the AboutMe servlet
738    *
739    * @return a <code>String</code> containing HTML code (Warning last link must be completed by servlet)
740    * @exception RemoteException if an error occurs
741    * @since 1.0
742    */

743   public String JavaDoc printItemUserHasBidOn(int id, String JavaDoc name, float initialPrice, float maxBid, float currentPrice, int quantity, String JavaDoc startDate, String JavaDoc endDate, int sellerId, String JavaDoc sellerName, String JavaDoc username, String JavaDoc password) throws RemoteException JavaDoc
744   {
745     StringBuffer JavaDoc html = new StringBuffer JavaDoc("<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+
746       "<TD>"+initialPrice+"<TD>"+currentPrice+"<TD>"+maxBid+"<TD>"+quantity+"<TD>"+startDate+"<TD>"+endDate+
747       "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+sellerName+
748       "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutBid?itemId="+id);
749     html.append("&nickname="+URLEncoder.encode(username)+"&password="+URLEncoder.encode(password)+"\"><IMG SRC=\""+BeanConfig.context+"/bid_now.jpg\" height=22 width=90></a>\n");
750     return html.toString();
751   }
752
753
754   /**
755    * Display item information as an HTML table row
756    *
757    * @return a <code>String</code> containing HTML code
758    * @exception RemoteException if an error occurs
759    * @since 1.0
760    */

761   public String JavaDoc printSell(int id, String JavaDoc name, float initialPrice, float maxBid, int quantity, float reservePrice, float buyNow, String JavaDoc startDate, String JavaDoc endDate) throws RemoteException JavaDoc
762   {
763     return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+
764       "<TD>"+initialPrice+"<TD>"+maxBid+"<TD>"+quantity+"<TD>"+reservePrice+"<TD>"+buyNow+"<TD>"+startDate+"<TD>"+endDate+"\n";
765   }
766  
767   /**
768    * Display item information for the AboutMe servlet
769    *
770    * @return a <code>String</code> containing HTML code
771    * @exception RemoteException if an error occurs
772    * @since 1.0
773    */

774   public String JavaDoc printUserWonItem(int id, String JavaDoc name, float maxBid, int sellerId, String JavaDoc sellerName) throws RemoteException JavaDoc
775   {
776     return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+"</a>\n"+
777       "<TD>"+maxBid+"\n"+
778       "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+sellerName+"</a>\n";
779   }
780
781   /**
782    * user's sellings header printed function
783    *
784    * @return a string in html format
785    * @since 1.1
786    */

787   public String JavaDoc printSellHeader(String JavaDoc title)
788   {
789     return printHTMLHighlighted("<p><h3>"+title+"</h3>\n")+
790       "<TABLE border=\"1\" summary=\"List of items\">\n"+
791       "<THEAD>\n"+
792       "<TR><TH>Designation<TH>Initial Price<TH>Current price<TH>Quantity<TH>ReservePrice<TH>Buy Now"+
793       "<TH>Start Date<TH>End Date\n"+
794       "<TBODY>\n";
795   }
796
797
798   /**
799    * Item footer printed function
800    *
801    * @return a string in html format
802    * @since 1.1
803    */

804   public String JavaDoc printItemFooter()
805   {
806     return "</TABLE>\n";
807   }
808
809   /**
810    * Comment header printed function
811    *
812    * @return a string in html format
813    * @since 1.1
814    */

815   public String JavaDoc printCommentHeader()
816   {
817     return "<DL>\n";
818   }
819
820   /**
821    * Display comment information as an HTML table row
822    *
823    * @return a <code>String</code> containing HTML code
824    * @exception RemoteException if an error occurs
825    * @since 1.0
826    */

827   public String JavaDoc printComment(String JavaDoc userName, String JavaDoc date, String JavaDoc comment, int fromUserId) throws RemoteException JavaDoc
828   {
829     return "<DT><b><BIG><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+fromUserId+"\">"+userName+"</a></BIG></b>"+
830       " wrote the "+date+"<DD><i>"+comment+"</i><p>\n";
831   }
832
833   /**
834    * Comment footer printed function
835    *
836    * @return a string in html format
837    * @since 1.1
838    */

839   public String JavaDoc printCommentFooter()
840   {
841     return "</DL>\n";
842   }
843
844   /**
845    * Construct a html highlighted string.
846    * @param msg the message to display
847    * @return a string in html format
848    * @since 1.1
849    */

850   public String JavaDoc printHTMLHighlighted(String JavaDoc msg)
851   {
852     return "<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>" + msg + "</B></FONT></TD></TR>\n</TABLE><p>\n";
853 }
854
855   // ======================== EJB related methods ============================
856

857   /**
858    * Set the associated context. The container call this method
859    * after the instance creation.
860    * The enterprise Bean instance should store the reference to the context
861    * object in an instance variable.
862    * This method is called with no transaction context.
863    *
864    * @param MessageDrivenContext A MessageDrivenContext interface for the instance.
865    * @throws EJBException Thrown by the method to indicate a failure caused by
866    * a system-level error.
867    */

868   public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
869   {
870     messageDrivenContext = ctx;
871     if (dataSource == null)
872     {
873       // Finds DataSource from JNDI
874
try
875       {
876         initialContext = new InitialContext JavaDoc();
877         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
878       }
879       catch (Exception JavaDoc e)
880       {
881         throw new EJBException JavaDoc("Cannot get JNDI InitialContext");
882       }
883     }
884   }
885
886   /**
887    * The Message driven bean must define an ejbCreate methods with no args.
888    *
889    */

890   public void ejbCreate()
891   {
892
893   }
894  
895   /**
896    * A container invokes this method before it ends the life of the message-driven object.
897    * This happens when a container decides to terminate the message-driven object.
898    *
899    * This method is called with no transaction context.
900    *
901    * @throws EJBException Thrown by the method to indicate a failure caused by
902    * a system-level error.
903    */

904   public void ejbRemove() {}
905
906
907 }
908
Popular Tags