1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.SessionBean ; 5 import javax.ejb.SessionContext ; 6 import javax.ejb.FinderException ; 7 import javax.ejb.ObjectNotFoundException ; 8 import javax.ejb.CreateException ; 9 import javax.ejb.RemoveException ; 10 import javax.ejb.EJBException ; 11 import javax.naming.Context ; 12 import javax.naming.InitialContext ; 13 import javax.rmi.PortableRemoteObject ; 14 import javax.sql.DataSource ; 15 import java.io.Serializable ; 16 import javax.transaction.UserTransaction ; 17 import java.util.Collection ; 18 import java.util.Iterator ; 19 import java.net.URLEncoder ; 20 21 27 28 public class SB_ViewUserInfoBean implements SessionBean 29 { 30 protected SessionContext sessionContext; 31 protected Context initialContext = null; 32 protected DataSource dataSource = null; 33 private UserTransaction utx = null; 34 35 43 public String getComments(UserHome userHome, Integer userId) throws RemoteException 44 { 45 Collection list; 46 StringBuffer html; 47 CommentHome cHome = null; 48 Comment comment = null; 49 User user = null; 50 51 53 try 54 { 55 cHome = (CommentHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Comment"), 56 CommentHome.class); 57 } 58 catch (Exception e) 59 { 60 throw new RemoteException ("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 ("<h3>There is no comment yet for this user.</h3><br>"); 71 else 72 { 73 html = new StringBuffer ("<br><hr><br><h3>Comments for this user</h3><br>"); 74 75 html.append(printCommentHeader()); 76 Iterator it = list.iterator(); 78 while (it.hasNext()) 79 { 80 comment = (Comment)it.next(); 81 String userName; 82 try 83 { 84 user = userHome.findByPrimaryKey(new UserPK(comment.getFromUserId())); 85 userName = user.getNickName(); 86 } 87 catch (Exception e) 88 { 89 throw new RemoteException ("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 e) 98 { 99 try 100 { 101 utx.rollback(); 102 throw new RemoteException ("Exception getting comment list: " + e +"<br>"); 103 } 104 catch (Exception se) 105 { 106 throw new RemoteException ("Transaction rollback failed: " + e +"<br>"); 107 } 108 } 109 return html.toString(); 110 } 111 112 113 120 public String getUserInfo(Integer userId) throws RemoteException 121 { 122 StringBuffer html = new StringBuffer (); 123 UserHome uHome = null; 124 User user = null; 125 126 127 try 129 { 130 uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"), 131 UserHome.class); 132 } 133 catch (Exception e) 134 { 135 throw new RemoteException ("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 e) 144 { 145 throw new RemoteException ("Cannot get user information (got exception: " +e+")<br>"); 146 } 147 return html.toString(); 148 } 149 150 156 public String printCommentHeader() 157 { 158 return "<DL>\n"; 159 } 160 161 169 public String printComment(String userName, Comment comment) throws RemoteException 170 { 171 try 172 { 173 return comment.printComment(userName); 174 } 175 catch (RemoteException re) 176 { 177 throw new RemoteException ("Unable to print Comment (exception: "+re+")<br>\n"); 178 } 179 } 180 181 187 public String printCommentFooter() 188 { 189 return "</DL>\n"; 190 } 191 192 193 194 195 196 198 201 public void ejbCreate() throws CreateException , RemoteException 202 { 203 } 204 205 206 public void ejbActivate() throws RemoteException {} 207 208 public void ejbPassivate() throws RemoteException {} 209 210 public void ejbRemove() throws RemoteException {} 211 212 213 222 public void setSessionContext(SessionContext sessionContext) throws RemoteException 223 { 224 this.sessionContext = sessionContext; 225 if (dataSource == null) 226 { 227 229 try 230 { 231 initialContext = new InitialContext (); 232 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 233 } 234 catch (Exception e) 235 { 236 throw new RemoteException ("Cannot get JNDI InitialContext"); 237 } 238 } 239 } 240 241 } 242 | Popular Tags |