KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
19 import java.net.URLEncoder JavaDoc;
20
21 /**
22  * This is a stateless session bean used to get the information about a user.
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_ViewUserInfoBean 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 comment related to a specific user.
37    *
38    * @param userHome an <code>UserHome</code> value
39    * @param userId a user id
40    * @return a string in html format
41    * @since 1.1
42    */

43   public String JavaDoc getComments(UserHome userHome, Integer JavaDoc userId) throws RemoteException JavaDoc
44   {
45     Collection JavaDoc list;
46     StringBuffer JavaDoc html;
47     CommentHome cHome = null;
48     Comment comment = null;
49     User user = null;
50
51     // Try to find the comments corresponding for this user
52

53     try
54     {
55       cHome = (CommentHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Comment"),
56                                                        CommentHome.class);
57     }
58     catch (Exception JavaDoc e)
59     {
60       throw new RemoteException JavaDoc("Cannot lookup Comment: " +e+"<br>");
61     }
62     
63     utx = sessionContext.getUserTransaction();
64
65     try
66     {
67       utx.begin();
68       list = cHome.findByToUser(userId);
69       if (list.isEmpty())
70        html = new StringBuffer JavaDoc("<h3>There is no comment yet for this user.</h3><br>");
71       else
72       {
73         html = new StringBuffer JavaDoc("<br><hr><br><h3>Comments for this user</h3><br>");
74
75         html.append(printCommentHeader());
76         // Display each comment and the name of its author
77
Iterator JavaDoc it = list.iterator();
78         while (it.hasNext())
79         {
80           comment = (Comment)it.next();
81           String JavaDoc userName;
82           try
83           {
84             user = userHome.findByPrimaryKey(new UserPK(comment.getFromUserId()));
85             userName = user.getNickName();
86           }
87           catch (Exception JavaDoc e)
88           {
89             throw new RemoteException JavaDoc("This author does not exist (got exception: " +e+")<br>");
90           }
91           html.append(printComment(userName, comment));
92         }
93         html.append(printCommentFooter());
94       }
95       utx.commit();
96     }
97     catch (Exception JavaDoc e)
98     {
99       try
100       {
101         utx.rollback();
102         throw new RemoteException JavaDoc("Exception getting comment list: " + e +"<br>");
103       }
104       catch (Exception JavaDoc se)
105       {
106         throw new RemoteException JavaDoc("Transaction rollback failed: " + e +"<br>");
107       }
108     }
109     return html.toString();
110   }
111
112
113   /**
114    * Get the information about a user.
115    *
116    * @param userId a user id
117    * @return a string in html format
118    * @since 1.1
119    */

120   public String JavaDoc getUserInfo(Integer JavaDoc userId) throws RemoteException JavaDoc
121   {
122     StringBuffer JavaDoc html = new StringBuffer JavaDoc();
123     UserHome uHome = null;
124     User user = null;
125  
126
127     // Try to find the user corresponding to the userId
128
try
129     {
130       uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"),
131                                                     UserHome.class);
132     }
133     catch (Exception JavaDoc e)
134     {
135       throw new RemoteException JavaDoc("Cannot lookup User: " +e+"<br>");
136     }
137     try
138     {
139       user = uHome.findByPrimaryKey(new UserPK(userId));
140       html.append(user.getHTMLGeneralUserInformation());
141       html.append(getComments(uHome, userId));
142     }
143     catch (Exception JavaDoc e)
144     {
145       throw new RemoteException JavaDoc("Cannot get user information (got exception: " +e+")<br>");
146     }
147     return html.toString();
148   }
149
150   /**
151    * Comment header printed function
152    *
153    * @return a string in html format
154    * @since 1.1
155    */

156   public String JavaDoc printCommentHeader()
157   {
158     return "<DL>\n";
159   }
160
161   /**
162    * Comment printed function
163    *
164    * @param userName the name of the user who is the subject of the comments
165    * @param comment the comment to display
166    * @return a string in html format
167    * @since 1.1
168    */

169   public String JavaDoc printComment(String JavaDoc userName, Comment comment) throws RemoteException JavaDoc
170   {
171     try
172     {
173       return comment.printComment(userName);
174     }
175     catch (RemoteException JavaDoc re)
176     {
177       throw new RemoteException JavaDoc("Unable to print Comment (exception: "+re+")<br>\n");
178     }
179   }
180
181   /**
182    * Comment footer printed function
183    *
184    * @return a string in html format
185    * @since 1.1
186    */

187   public String JavaDoc printCommentFooter()
188   {
189     return "</DL>\n";
190   }
191
192  
193
194  
195
196   // ======================== EJB related methods ============================
197

198   /**
199    * This method is empty for a stateless session bean
200    */

201   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
202   {
203   }
204
205   /** This method is empty for a stateless session bean */
206   public void ejbActivate() throws RemoteException JavaDoc {}
207   /** This method is empty for a stateless session bean */
208   public void ejbPassivate() throws RemoteException JavaDoc {}
209   /** This method is empty for a stateless session bean */
210   public void ejbRemove() throws RemoteException JavaDoc {}
211
212
213   /**
214    * Sets the associated session context. The container calls this method
215    * after the instance creation. This method is called with no transaction context.
216    * We also retrieve the Home interfaces of all RUBiS's beans.
217    *
218    * @param sessionContext - A SessionContext interface for the instance.
219    * @exception RemoteException - Thrown if the instance could not perform the function
220    * requested by the container because of a system-level error.
221    */

222   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
223   {
224     this.sessionContext = sessionContext;
225     if (dataSource == null)
226     {
227       // Finds DataSource from JNDI
228

229       try
230       {
231         initialContext = new InitialContext JavaDoc();
232         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
233       }
234       catch (Exception JavaDoc e)
235       {
236         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
237       }
238     }
239   }
240
241 }
242
Popular Tags