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 18 24 25 public class SB_PutCommentBean implements SessionBean 26 { 27 protected SessionContext sessionContext; 28 protected Context initialContext = null; 29 protected DataSource dataSource = null; 30 32 33 39 public String getCommentForm(Integer itemId, Integer toId, String username, String password) throws RemoteException 40 { 41 int userId = -1; 42 String html = ""; 43 UserHome uHome; 44 ItemHome iHome; 45 46 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 e) 57 { 58 throw new RemoteException ("Cannot lookup SB_Auth: " +e); 59 } 60 try 61 { 62 userId = auth.authenticate(username, password); 63 } 64 catch (Exception e) 65 { 66 throw new RemoteException ("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 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 e) 83 { 84 throw new RemoteException ("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 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 e) 103 { 104 throw new RemoteException ("Cannot build comment form: " +e); 105 } 106 return html; 107 } 108 109 110 111 112 113 114 116 119 public void ejbCreate() throws CreateException , RemoteException 120 { 121 } 122 123 124 public void ejbActivate() throws RemoteException {} 125 126 public void ejbPassivate() throws RemoteException {} 127 128 public void ejbRemove() throws RemoteException {} 129 130 131 140 public void setSessionContext(SessionContext sessionContext) throws RemoteException 141 { 142 this.sessionContext = sessionContext; 143 if (dataSource == null) 144 { 145 147 try 148 { 149 initialContext = new InitialContext (); 150 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 151 } 152 catch (Exception e) 153 { 154 throw new RemoteException ("Cannot get JNDI InitialContext"); 155 } 156 } 157 } 158 159 } 160 | Popular Tags |