KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.net.URLEncoder JavaDoc;
20
21 /**
22  * This is a stateless session bean used to get the information about an item.
23  *
24  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
25  * @version 1.1
26  */

27
28 public class SB_ViewItemBean implements SessionBean JavaDoc
29 {
30   protected SessionContext JavaDoc sessionContext;
31   protected Context JavaDoc initialContext = null;
32   protected DataSource JavaDoc dataSource = null;
33   private UserTransaction JavaDoc utx = null;
34
35   /**
36    * Get the full description of an item and the bidding option if userId>0.
37    *
38    * @param item an <code>Item</code> value
39    * @param userId an authenticated user id
40    */

41   public String JavaDoc getItemDescription(Integer JavaDoc itemId, int userId) throws RemoteException JavaDoc
42   {
43     StringBuffer JavaDoc html = new StringBuffer JavaDoc();
44     ItemHome iHome = null;
45     Item item = null;
46
47     try
48     {
49       iHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"), ItemHome.class);
50       item = iHome.findByPrimaryKey(new ItemPK(itemId));
51     }
52     catch (Exception JavaDoc e)
53     {
54       throw new RemoteException JavaDoc("Cannot lookup Item: " +e);
55     }
56     try
57     {
58       float buyNow = 0;
59       int nbOfBids = item.getNbOfBids();
60       int qty = item.getQuantity();
61       String JavaDoc firstBid;
62       float maxBid = item.getMaxBid();
63       String JavaDoc itemName = item.getName();
64
65       if (maxBid == 0)
66       {
67         firstBid = "none";
68         maxBid = item.getInitialPrice();
69         buyNow = item.getBuyNow();
70       }
71       else
72       {
73         if (qty > 1)
74         {
75           BidHome bHome = null;
76           QueryHome qHome = null;
77           Query query = null;
78           try
79           { // Connecting to Query Home thru JNDI
80
qHome = (QueryHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Query"), QueryHome.class);
81             query = qHome.create();
82           }
83           catch (Exception JavaDoc e)
84           {
85             throw new RemoteException JavaDoc("Cannot lookup Query: " +e);
86           }
87           try
88           {
89             // Connecting to Bid Home thru JNDI
90
bHome = (BidHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Bid"), BidHome.class);
91           }
92           catch (Exception JavaDoc e)
93           {
94             throw new RemoteException JavaDoc("Cannot lookup Bid: " +e);
95           }
96           try
97           {
98             /* Get the qty max first bids and parse bids in this order
99                until qty is reached. The bid that reaches qty is the
100                current minimum bid. */

101             Enumeration JavaDoc list = query.getItemQtyMaxBid(qty, itemId).elements();
102             Bid bid;
103             int numberOfItems = 0;
104             while (list.hasMoreElements())
105             {
106               bid = bHome.findByPrimaryKey((BidPK)list.nextElement());
107               numberOfItems += bid.getQuantity();
108               if (numberOfItems >= qty)
109               {
110                 maxBid = bid.getBid();
111                 break;
112               }
113             }
114           }
115           catch (Exception JavaDoc e)
116           {
117             throw new RemoteException JavaDoc("Problem while computing current bid: "+e+"<br>");
118           }
119         }
120
121         Float JavaDoc foo = new Float JavaDoc(maxBid);
122         firstBid = foo.toString();
123       }
124      
125       if (userId>0)
126       {
127         //header = printHTMLheader("RUBiS: Bidding\n");
128
html.append(printHTMLHighlighted("You are ready to bid on: "+itemName));
129       }
130       else
131       {
132         //header = printHTMLheader("RUBiS: Viewing "+itemName+"\n");
133
html.append(printHTMLHighlighted(itemName));
134       }
135       html.append("<TABLE>\n"+
136                   "<TR><TD>Currently<TD><b><BIG>"+maxBid+"</BIG></b>\n");
137       // Check if the reservePrice has been met (if any)
138
float reservePrice = item.getReservePrice();
139       Integer JavaDoc sellerId = item.getSellerId();
140       if (reservePrice > 0)
141       { // Has the reserve price been met ?
142
if (maxBid >= reservePrice)
143           html.append("(The reserve price has been met)\n");
144         else
145           html.append("(The reserve price has NOT been met)\n");
146       }
147       html.append("<TR><TD>Quantity<TD><b><BIG>"+qty+"</BIG></b>\n"+
148                   "<TR><TD>First bid<TD><b><BIG>"+firstBid+"</BIG></b>\n"+
149                   "<TR><TD># of bids<TD><b><BIG>"+nbOfBids+"</BIG></b> (<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewBidHistory?itemId="+itemId+"\">bid history</a>)\n"+
150                   "<TR><TD>Seller<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+item.getSellerNickname()+"</a> (<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutCommentAuth?to="+sellerId+"&itemId="+itemId+"\">Leave a comment on this user</a>)\n"+
151                   "<TR><TD>Started<TD>"+item.getStartDate()+"\n"+
152                   "<TR><TD>Ends<TD>"+item.getEndDate()+"\n"+
153                   "</TABLE>");
154       // Can the user buy this item now ?
155
if (buyNow > 0)
156         html.append("<p><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.BuyNowAuth?itemId="+itemId+"\">"+
157                     "<IMG SRC=\""+BeanConfig.context+"/buy_it_now.jpg\" height=22 width=150></a>"+
158                     " <BIG><b>You can buy this item right now for only $"+buyNow+"</b></BIG><br><p>\n");
159
160       if (userId<=0)
161       {
162         html.append("<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutBidAuth?itemId="+itemId+"\"><IMG SRC=\"/EJB_HTML/bid_now.jpg\" height=22 width=90> on this item</a>\n");
163       }
164
165       html.append(printHTMLHighlighted("Item description"));
166       html.append(item.getDescription());
167       html.append("<br><p>\n");
168
169       if (userId>0)
170       {
171         html.append(printHTMLHighlighted("Bidding"));
172         float minBid = maxBid+1;
173         html.append("<form action=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.StoreBid\" method=POST>\n"+
174                   "<input type=hidden name=minBid value="+minBid+">\n"+
175                   "<input type=hidden name=userId value="+userId+">\n"+
176                   "<input type=hidden name=itemId value="+itemId+">\n"+
177                   "<input type=hidden name=maxQty value="+qty+">\n"+
178                   "<center><table>\n"+
179                   "<tr><td>Your bid (minimum bid is "+minBid+"):</td>\n"+
180                   "<td><input type=text size=10 name=bid></td></tr>\n"+
181                   "<tr><td>Your maximum bid:</td>\n"+
182                   "<td><input type=text size=10 name=maxBid></td></tr>\n");
183         if (qty > 1)
184           html.append("<tr><td>Quantity:</td>\n"+
185                     "<td><input type=text size=5 name=qty></td></tr>\n");
186         else
187           html.append("<input type=hidden name=qty value=1>\n");
188         html.append("</table><p><input type=submit value=\"Bid now!\"></center><p>\n");
189       }
190     }
191     catch (RemoteException JavaDoc re)
192     {
193       throw new RemoteException JavaDoc("Unable to print Item description (exception: "+re+")<br>\n");
194     }
195     return html.toString();
196   }
197
198   /**
199    * Construct a html highlighted string.
200    * @param msg the message to display
201    * @return a string in html format
202    * @since 1.1
203    */

204   public String JavaDoc printHTMLHighlighted(String JavaDoc msg)
205   {
206     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";
207   }
208
209
210   // ======================== EJB related methods ============================
211

212   /**
213    * This method is empty for a stateless session bean
214    */

215   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
216   {
217   }
218
219   /** This method is empty for a stateless session bean */
220   public void ejbActivate() throws RemoteException JavaDoc {}
221   /** This method is empty for a stateless session bean */
222   public void ejbPassivate() throws RemoteException JavaDoc {}
223   /** This method is empty for a stateless session bean */
224   public void ejbRemove() throws RemoteException JavaDoc {}
225
226
227   /**
228    * Sets the associated session context. The container calls this method
229    * after the instance creation. This method is called with no transaction context.
230    * We also retrieve the Home interfaces of all RUBiS's beans.
231    *
232    * @param sessionContext - A SessionContext interface for the instance.
233    * @exception RemoteException - Thrown if the instance could not perform the function
234    * requested by the container because of a system-level error.
235    */

236   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
237   {
238     this.sessionContext = sessionContext;
239     if (dataSource == null)
240     {
241       // Finds DataSource from JNDI
242

243       try
244       {
245         initialContext = new InitialContext JavaDoc();
246         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
247       }
248       catch (Exception JavaDoc e)
249       {
250         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
251       }
252     }
253   }
254
255 }
256
Popular Tags