KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 /**
19  * This is a stateless session bean used to get the information to build the html form
20  * used to put a comment on a user.
21  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
22  * @version 1.1
23  */

24
25 public class SB_PutCommentBean implements SessionBean JavaDoc
26 {
27   protected SessionContext JavaDoc sessionContext;
28   protected Context JavaDoc initialContext = null;
29   protected DataSource JavaDoc dataSource = null;
30 // private UserTransaction utx = null;
31

32
33   /**
34    * Authenticate the user and get the information to build the html form.
35    *
36    * @return a string in html format
37    * @since 1.1
38    */

39   public String JavaDoc getCommentForm(Integer JavaDoc itemId, Integer JavaDoc toId, String JavaDoc username, String JavaDoc password) throws RemoteException JavaDoc
40   {
41     int userId = -1;
42     String JavaDoc html = "";
43     UserHome uHome;
44     ItemHome iHome;
45
46     // Authenticate the user who want to comment
47
if ((username != null && !username.equals("")) || (password != null && !password.equals("")))
48       {
49         SB_AuthHome authHome = null;
50         SB_Auth auth = null;
51         try
52         {
53           authHome = (SB_AuthHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/SB_Auth"), SB_AuthHome.class);
54           auth = authHome.create();
55         }
56         catch (Exception JavaDoc e)
57         {
58           throw new RemoteException JavaDoc("Cannot lookup SB_Auth: " +e);
59         }
60         try
61         {
62           userId = auth.authenticate(username, password);
63         }
64         catch (Exception JavaDoc e)
65         {
66           throw new RemoteException JavaDoc("Authentication failed: " +e);
67         }
68         if (userId == -1)
69         {
70            html = (" You don't have an account on RUBiS!<br>You have to register first.<br>");
71            return html;
72         }
73       }
74     // Try to find the user corresponding to the 'to' ID
75
try
76     {
77       uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"),
78                                                     UserHome.class);
79       iHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"),
80                                                     ItemHome.class);
81     }
82     catch (Exception JavaDoc e)
83     {
84       throw new RemoteException JavaDoc("Cannot lookup User or Item: " +e+"<br>");
85     }
86     try
87     {
88       User to = uHome.findByPrimaryKey(new UserPK(toId));
89       Item item = iHome.findByPrimaryKey(new ItemPK(itemId));
90       String JavaDoc toName = to.getNickName();
91
92       html = "<center><h2>Give feedback about your experience with "+toName+"</h2><br>\n" +
93         "<form action=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.StoreComment\" method=POST>\n"+
94         "<input type=hidden name=to value="+toId.intValue()+">\n"+
95         "<input type=hidden name=from value="+userId+">\n"+
96         "<input type=hidden name=itemId value="+itemId.intValue()+">\n"+
97         "<center><table>\n"+
98         "<tr><td><b>From</b><td>"+username+"\n"+
99         "<tr><td><b>To</b><td>"+toName+"\n"+
100         "<tr><td><b>About item</b><td>"+item.getName()+"\n";
101     }
102     catch (Exception JavaDoc e)
103     {
104       throw new RemoteException JavaDoc("Cannot build comment form: " +e);
105     }
106     return html;
107   }
108                    
109
110
111
112
113
114   // ======================== EJB related methods ============================
115

116   /**
117    * This method is empty for a stateless session bean
118    */

119   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
120   {
121   }
122
123   /** This method is empty for a stateless session bean */
124   public void ejbActivate() throws RemoteException JavaDoc {}
125   /** This method is empty for a stateless session bean */
126   public void ejbPassivate() throws RemoteException JavaDoc {}
127   /** This method is empty for a stateless session bean */
128   public void ejbRemove() throws RemoteException JavaDoc {}
129
130
131   /**
132    * Sets the associated session context. The container calls this method
133    * after the instance creation. This method is called with no transaction context.
134    * We also retrieve the Home interfaces of all RUBiS's beans.
135    *
136    * @param sessionContext - A SessionContext interface for the instance.
137    * @exception RemoteException - Thrown if the instance could not perform the function
138    * requested by the container because of a system-level error.
139    */

140   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
141   {
142     this.sessionContext = sessionContext;
143     if (dataSource == null)
144     {
145       // Finds DataSource from JNDI
146

147       try
148       {
149         initialContext = new InitialContext JavaDoc();
150         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
151       }
152       catch (Exception JavaDoc e)
153       {
154         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
155       }
156     }
157   }
158
159 }
160
Popular Tags