KickJava   Java API By Example, From Geeks To Geeks.

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


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

28
29 public class SB_AboutMeBean implements SessionBean JavaDoc
30 {
31   protected SessionContext JavaDoc sessionContext;
32   protected Context JavaDoc initialContext = null;
33   protected DataSource JavaDoc dataSource = null;
34   //private UserTransaction utx = null;
35

36
37   /**
38    * Authenticate the user and get the information about the user.
39    *
40    * @return a string in html format
41    * @since 1.1
42    */

43   public String JavaDoc getAboutMe(String JavaDoc username, String JavaDoc password) throws RemoteException JavaDoc
44   {
45     int uid = -1;
46     Integer JavaDoc userId;
47     StringBuffer JavaDoc html = new StringBuffer JavaDoc();
48
49     // Authenticate the user
50
SB_AuthHome authHome = null;
51     SB_Auth auth = null;
52     try
53     {
54       authHome = (SB_AuthHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/SB_Auth"), SB_AuthHome.class);
55       auth = authHome.create();
56     }
57     catch (Exception JavaDoc e)
58     {
59       throw new RemoteException JavaDoc("Cannot lookup SB_Auth: " +e);
60     }
61     try
62     {
63       uid = auth.authenticate(username, password);
64     }
65     catch (Exception JavaDoc e)
66     {
67       throw new RemoteException JavaDoc("Authentication failed: " +e);
68     }
69     if (uid == -1)
70     {
71       return "You don't have an account on RUBiS!<br>You have to register first.<br>";
72     }
73     // Try to find the user corresponding to the userId
74
UserHome uHome;
75     try
76     {
77       userId = new Integer JavaDoc (uid);
78       uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"), UserHome.class);
79     }
80     catch (Exception JavaDoc e)
81     {
82       throw new RemoteException JavaDoc("Cannot lookup User: " +e+"<br>");
83     }
84     try
85     {
86       User u = uHome.findByPrimaryKey(new UserPK(userId));
87
88       html.append(u.getHTMLGeneralUserInformation());
89     }
90     catch (Exception JavaDoc e)
91     {
92       throw new RemoteException JavaDoc("This user does not exist (got exception: " +e+")<br>");
93     }
94
95     // Try to find the comments corresponding for this user
96
CommentHome cHome;
97     try
98     {
99       cHome = (CommentHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Comment"), CommentHome.class);
100     }
101     catch (Exception JavaDoc e)
102     {
103       throw new RemoteException JavaDoc("Cannot lookup Comment: " +e+"<br>");
104     }
105     // Retrieve ItemHome
106
ItemHome iHome;
107     try
108     {
109       iHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"), ItemHome.class);
110     }
111     catch (Exception JavaDoc e)
112     {
113       throw new RemoteException JavaDoc("Cannot lookup item: " +e+"<br>");
114     }
115     // Connecting to Query Home thru JNDI
116
QueryHome qHome;
117     try
118     {
119       qHome = (QueryHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Query"), QueryHome.class);
120     }
121     catch (Exception JavaDoc e)
122     {
123       throw new RemoteException JavaDoc("Cannot lookup Query: " +e+"<br>");
124     }
125     try
126     {
127       html.append(listItem(userId, iHome));
128       html.append(listBoughtItems(userId, iHome));
129       html.append(listWonItems(userId, iHome, qHome));
130       html.append(listBids(userId, username, password, iHome, qHome));
131       html.append(listComments(cHome, userId, uHome));
132         
133     }
134     catch (Exception JavaDoc e)
135     {
136       throw new RemoteException JavaDoc("Cannot lookup Query: " +e+"<br>");
137     }
138     return html.toString();
139   }
140                    
141   /** List items the user is currently selling and sold in the past 30 days */
142   public String JavaDoc listItem(Integer JavaDoc userId, ItemHome iHome) throws RemoteException JavaDoc
143   {
144     Item item;
145     StringBuffer JavaDoc sell = new StringBuffer JavaDoc();
146     Collection JavaDoc currentItemList, pastItemList;
147
148     try
149     {
150       currentItemList = iHome.findUserCurrentSellings(userId);
151       pastItemList = iHome.findUserPastSellings(userId);
152     }
153     catch (Exception JavaDoc e)
154     {
155       throw new RemoteException JavaDoc("Exception getting item list: " +e+"<br>");
156     }
157
158     if ((currentItemList == null) || (currentItemList.isEmpty()))
159     {
160       sell.append("<br>");
161       sell.append(printHTMLHighlighted("<h3>You are currently selling no item.</h3>"));
162     }
163     else
164     {
165       // display current sellings
166
try
167       {
168         sell.append(printSellHeader("Items you are currently selling."));
169       
170         Iterator JavaDoc it = currentItemList.iterator();
171         while (it.hasNext())
172         {
173           // Get the name of the items
174
item = (Item)it.next();
175           // display information about the item
176
sell.append(item.printSell());
177         }
178         sell.append(printItemFooter());
179       }
180       catch (Exception JavaDoc e)
181       {
182         throw new RemoteException JavaDoc("Exception getting item: " + e +"<br>");
183       }
184     }
185     
186     if ((pastItemList == null) || (pastItemList.isEmpty()))
187     {
188       sell.append("<br><h3>You didn't sell any item.</h3>");
189       return sell.toString();
190     }
191     // display past sellings
192
sell.append("<br>");
193     sell.append(printSellHeader("Items you sold in the last 30 days."));
194     try
195     {
196       Iterator JavaDoc it = pastItemList.iterator();
197       while (it.hasNext())
198       {
199         // Get the name of the items
200
item = (Item)it.next();
201         // display information about the item
202
sell.append(item.printSell());
203       }
204       sell.append(printItemFooter());
205     }
206     catch (Exception JavaDoc e)
207     {
208       throw new RemoteException JavaDoc("Exception getting item: " + e +"<br>");
209     }
210     
211     return sell.toString();
212   }
213
214   /** List items the user bought in the last 30 days*/
215   public String JavaDoc listBoughtItems(Integer JavaDoc userId, ItemHome iHome) throws RemoteException JavaDoc
216   {
217     BuyNowHome buyHome;
218     BuyNow buy;
219     Item item;
220     Collection JavaDoc buyList=null;
221     int quantity;
222     StringBuffer JavaDoc html = new StringBuffer JavaDoc();
223
224     // Get the list of items the user bought
225
try
226     {
227       buyHome = (BuyNowHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/BuyNow"),
228                                                         BuyNowHome.class);
229     }
230     catch (Exception JavaDoc e)
231     {
232       throw new RemoteException JavaDoc("Cannot lookup BuyNow: " +e+"<br>");
233     }
234     try
235     {
236       buyList = buyHome.findUserBuyNow(userId);
237     }
238     catch (Exception JavaDoc e)
239     {
240       throw new RemoteException JavaDoc("Exception getting item list (buy now): " +e+"<br>");
241     }
242     if ((buyList == null) || (buyList.isEmpty()))
243     {
244       return "<br><h3>You didn't buy any item in the last 30 days.</h3><br>";
245     }
246     html.append(printUserBoughtItemHeader());
247     
248     Iterator JavaDoc it = buyList.iterator();
249     while (it.hasNext())
250     {
251       // Get the name of the items
252
try
253       {
254         buy = (BuyNow)it.next();
255         quantity = buy.getQuantity();
256       }
257       catch (Exception JavaDoc e)
258       {
259         throw new RemoteException JavaDoc("Exception getting buyNow quantity: " + e +"<br>");
260       }
261       try
262       {
263         item = iHome.findByPrimaryKey(new ItemPK(buy.getItemId()));
264         // display information about the item
265
html.append(item.printUserBoughtItem(quantity));
266       }
267       catch (Exception JavaDoc e)
268       {
269         throw new RemoteException JavaDoc("Exception getting item: " + e +"<br>");
270       }
271     }
272     html.append(printItemFooter());
273     return html.toString();
274
275   }
276
277   /** List items the user won in the last 30 days*/
278   public String JavaDoc listWonItems(Integer JavaDoc userId, ItemHome iHome, QueryHome qHome) throws RemoteException JavaDoc
279   {
280     Enumeration JavaDoc wonList=null;
281     Query q;
282     Item item;
283     float price;
284     String JavaDoc name;
285     StringBuffer JavaDoc html;
286
287     // Get the list of the user's won items
288
try
289     {
290       q = qHome.create();
291       wonList = q.getUserWonItems(userId).elements();
292     }
293     catch (Exception JavaDoc e)
294     {
295       throw new RemoteException JavaDoc("Exception getting won items list: " +e+"<br>");
296     }
297
298     if ((wonList == null) || (!wonList.hasMoreElements()))
299     {
300       return "<br><h3>You didn't win any item in the last 30 days.</h3><br>";
301     }
302     html = new StringBuffer JavaDoc(printUserWonItemHeader());
303
304     while (wonList.hasMoreElements())
305     {
306       // Get the name of the items
307
try
308       {
309         item = iHome.findByPrimaryKey((ItemPK)wonList.nextElement());
310         // display information about the item
311
html.append(item.printUserWonItem());
312       }
313       catch (Exception JavaDoc e)
314       {
315         throw new RemoteException JavaDoc("Exception getting item: " + e +"<br>");
316       }
317     }
318     html.append(printItemFooter());
319     return html.toString();
320   }
321
322
323   /** List comments about the user */
324   public String JavaDoc listComments(CommentHome home, Integer JavaDoc userId, UserHome uHome) throws RemoteException JavaDoc
325   {
326     Collection JavaDoc list;
327     Comment comment;
328     StringBuffer JavaDoc html;
329     try
330     {
331       list = home.findByToUser(userId);
332       html = new StringBuffer JavaDoc("<br>");
333       if (list.isEmpty())
334         html.append(printHTMLHighlighted("<h3>There is no comment yet for this user.</h3>"));
335       else
336         html.append(printHTMLHighlighted("<h3>Comments for this user</h3>"));
337       html.append("<br>");
338       html.append(printCommentHeader());
339       // Display each comment and the name of its author
340
Iterator JavaDoc it = list.iterator();
341       while (it.hasNext())
342       {
343         comment = (Comment)it.next();
344         String JavaDoc userName;
345         try
346         {
347           User u = uHome.findByPrimaryKey(new UserPK(comment.getFromUserId()));
348           userName = u.getNickName();
349           html.append(printComment(userName, comment));
350         }
351         catch (Exception JavaDoc e)
352         {
353           throw new RemoteException JavaDoc("This author does not exist (got exception: " +e+")<br>");
354         }
355       }
356       html.append(printCommentFooter());
357     }
358     catch (Exception JavaDoc e)
359     {
360       throw new RemoteException JavaDoc("Exception getting comment list: " + e +"<br>");
361     }
362     return html.toString();
363   }
364
365   /** List items the user put a bid on in the last 30 days*/
366   public String JavaDoc listBids(Integer JavaDoc userId, String JavaDoc username, String JavaDoc password, ItemHome iHome, QueryHome qHome) throws RemoteException JavaDoc
367   {
368     Enumeration JavaDoc bidList=null;
369     Query q;
370     BidHome bidHome;
371     Bid bid;
372     Item item;
373     float price;
374     String JavaDoc name;
375     StringBuffer JavaDoc html;
376
377     // Get the list of the user's last bids
378
try
379     {
380       q = qHome.create();
381       bidList = q.getUserBids(userId).elements();
382     }
383     catch (Exception JavaDoc e)
384     {
385       throw new RemoteException JavaDoc("Exception getting bids list: " +e+"<br>");
386     }
387     if ((bidList == null) || (!bidList.hasMoreElements()))
388     {
389       return printHTMLHighlighted("<h3>You didn't put any bid.</h3>");
390     }
391
392     // Lookup bid home interface
393
try
394     {
395       bidHome = (BidHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Bid"),
396                                                      BidHome.class);
397     }
398     catch (Exception JavaDoc e)
399     {
400       throw new RemoteException JavaDoc("Cannot lookup Bid: " +e+"<br>");
401     }
402
403     html = new StringBuffer JavaDoc(printUserBidsHeader());
404
405     while (bidList.hasMoreElements())
406     {
407       // Get the amount of the last bids
408
try
409       {
410         bid = bidHome.findByPrimaryKey((BidPK)bidList.nextElement());
411       }
412       catch (Exception JavaDoc e)
413       {
414         throw new RemoteException JavaDoc("Exception getting bid: " + e +"<br>");
415       }
416
417       // Get the name of the items
418
try
419       {
420         item = iHome.findByPrimaryKey(new ItemPK(bid.getItemId()));
421         html.append(printItemUserHasBidOn(bid, item, username, password));
422       }
423       catch (Exception JavaDoc e)
424       {
425         throw new RemoteException JavaDoc("Exception getting item: " + e +"<br>");
426       }
427       // display information about user's bids
428
}
429     html.append(printItemFooter());
430     return html.toString();
431   }
432
433   /**
434    * user's bought items list header printed function
435    *
436    * @return a string in html format
437    * @since 1.1
438    */

439   public String JavaDoc printUserBoughtItemHeader()
440   {
441     return "<br>"+
442       printHTMLHighlighted("<p><h3>Items you bouhgt in the past 30 days.</h3>\n")+
443       "<TABLE border=\"1\" summary=\"List of items\">\n"+
444       "<THEAD>\n"+
445       "<TR><TH>Designation<TH>Quantity<TH>Price you bought it<TH>Seller"+
446       "<TBODY>\n";
447   }
448
449   /**
450    * user's won items list header printed function
451    *
452    * @return a string in html format
453    * @since 1.1
454    */

455   public String JavaDoc printUserWonItemHeader()
456   {
457     return "<br>"+
458       printHTMLHighlighted("<p><h3>Items you won in the past 30 days.</h3>\n")+
459       "<TABLE border=\"1\" summary=\"List of items\">\n"+
460       "<THEAD>\n"+
461       "<TR><TH>Designation<TH>Price you bought it<TH>Seller"+
462       "<TBODY>\n";
463   }
464
465   /**
466    * user's bids list header printed function
467    *
468    * @return a string in html format
469    * @since 1.1
470    */

471   public String JavaDoc printUserBidsHeader()
472   {
473     return "<br>"+
474       printHTMLHighlighted("<p><h3>Items you have bid on.</h3>\n")+
475       "<TABLE border=\"1\" summary=\"Items You've bid on\">\n"+
476       "<THEAD>\n"+
477       "<TR><TH>Designation<TH>Initial Price<TH>Current price<TH>Your max bid<TH>Quantity"+
478       "<TH>Start Date<TH>End Date<TH>Seller<TH>Put a new bid\n"+
479       "<TBODY>\n";
480   }
481
482
483   /**
484    * items list printed function
485    *
486    * @return a string in html format
487    * @since 1.1
488    */

489   public String JavaDoc printItemUserHasBidOn(Bid bid, Item item, String JavaDoc username, String JavaDoc password) throws RemoteException JavaDoc
490   {
491     try
492     {
493       return item.printItemUserHasBidOn(bid.getMaxBid())+"&nickname="+URLEncoder.encode(username)+"&password="+URLEncoder.encode(password)+"\"><IMG SRC=\"/EJB_HTML/bid_now.jpg\" height=22 width=90></a>\n";
494     }
495     catch (RemoteException JavaDoc re)
496     {
497       throw new RemoteException JavaDoc("Unable to print Item (exception: "+re+")<br>\n");
498     }
499   }
500
501
502   /**
503    * user's sellings header printed function
504    *
505    * @return a string in html format
506    * @since 1.1
507    */

508   public String JavaDoc printSellHeader(String JavaDoc title)
509   {
510     return printHTMLHighlighted("<p><h3>"+title+"</h3>\n")+
511       "<TABLE border=\"1\" summary=\"List of items\">\n"+
512       "<THEAD>\n"+
513       "<TR><TH>Designation<TH>Initial Price<TH>Current price<TH>Quantity<TH>ReservePrice<TH>Buy Now"+
514       "<TH>Start Date<TH>End Date\n"+
515       "<TBODY>\n";
516   }
517
518
519   /**
520    * Item footer printed function
521    *
522    * @return a string in html format
523    * @since 1.1
524    */

525   public String JavaDoc printItemFooter()
526   {
527     return "</TABLE>\n";
528   }
529
530   /**
531    * Comment header printed function
532    *
533    * @return a string in html format
534    * @since 1.1
535    */

536   public String JavaDoc printCommentHeader()
537   {
538     return "<DL>\n";
539   }
540
541   /**
542    * Comment printed function
543    *
544    * @param userName the name of the user who is the subject of the comments
545    * @param comment the comment to display
546    * @return a string in html format
547    * @since 1.1
548    */

549   public String JavaDoc printComment(String JavaDoc userName, Comment comment) throws RemoteException JavaDoc
550   {
551     try
552     {
553       return comment.printComment(userName);
554     }
555     catch (RemoteException JavaDoc re)
556     {
557       throw new RemoteException JavaDoc("Unable to print Comment (exception: "+re+")<br>\n");
558     }
559   }
560
561   /**
562    * Comment footer printed function
563    *
564    * @return a string in html format
565    * @since 1.1
566    */

567   public String JavaDoc printCommentFooter()
568   {
569     return "</DL>\n";
570   }
571
572   /**
573    * Construct a html highlighted string.
574    * @param msg the message to display
575    * @return a string in html format
576    * @since 1.1
577    */

578   public String JavaDoc printHTMLHighlighted(String JavaDoc msg)
579   {
580     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";
581 }
582
583
584 // ======================== EJB related methods ============================
585

586 /**
587  * This method is empty for a stateless session bean
588  */

589 public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
590 {
591 }
592
593 /** This method is empty for a stateless session bean */
594 public void ejbActivate() throws RemoteException JavaDoc {}
595 /** This method is empty for a stateless session bean */
596 public void ejbPassivate() throws RemoteException JavaDoc {}
597 /** This method is empty for a stateless session bean */
598 public void ejbRemove() throws RemoteException JavaDoc {}
599
600
601 /**
602  * Sets the associated session context. The container calls this method
603  * after the instance creation. This method is called with no transaction context.
604  * We also retrieve the Home interfaces of all RUBiS's beans.
605  *
606  * @param sessionContext - A SessionContext interface for the instance.
607  * @exception RemoteException - Thrown if the instance could not perform the function
608  * requested by the container because of a system-level error.
609  */

610 public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
611 {
612   this.sessionContext = sessionContext;
613 if (dataSource == null)
614 {
615     // Finds DataSource from JNDI
616

617     try
618     {
619       initialContext = new InitialContext JavaDoc();
620       dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
621     }
622     catch (Exception JavaDoc e)
623     {
624       throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
625     }
626   }
627 }
628
629 }
630
Popular Tags