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_StoreBuyNowBean implements SessionBean 26 { 27 protected SessionContext sessionContext; 28 protected Context initialContext = null; 29 protected DataSource dataSource = null; 30 private UserTransaction utx = null; 31 32 40 public void createBuyNow(Integer itemId, Integer userId, int qty) throws RemoteException 41 { 42 utx = sessionContext.getUserTransaction(); 43 Item item; 45 try 46 { 47 utx.begin(); 48 ItemHome itemHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"), 49 ItemHome.class); 50 item = itemHome.findByPrimaryKey(new ItemPK(itemId)); 51 item.setQuantity(item.getQuantity() - qty); 52 if (item.getQuantity() == 0) 53 item.setEndDate(TimeManagement.currentDateToString()); 54 } 55 catch (Exception e) 56 { 57 try 58 { 59 utx.rollback(); 60 throw new RemoteException ("Cannot update Item: " +e+"<br>"); 61 } 62 catch (Exception se) 63 { 64 throw new RemoteException ("Transaction rollback failed: " + e +"<br>"); 65 } 66 } 67 try 68 { 69 BuyNowHome bHome = (BuyNowHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/BuyNow"), 70 BuyNowHome.class); 71 edu.rice.rubis.beans.BuyNow b = bHome.create(userId, itemId, qty); 72 utx.commit(); 73 } 74 catch (Exception e) 75 { 76 try 77 { 78 utx.rollback(); 79 throw new RemoteException ("Error while storing the comment (got exception: " +e+")<br>"); 80 } 81 catch (Exception se) 82 { 83 throw new RemoteException ("Transaction rollback failed: " + e +"<br>"); 84 } 85 } 86 87 } 88 89 90 91 93 96 public void ejbCreate() throws CreateException , RemoteException 97 { 98 } 99 100 101 public void ejbActivate() throws RemoteException {} 102 103 public void ejbPassivate() throws RemoteException {} 104 105 public void ejbRemove() throws RemoteException {} 106 107 108 117 public void setSessionContext(SessionContext sessionContext) throws RemoteException 118 { 119 this.sessionContext = sessionContext; 120 if (dataSource == null) 121 { 122 124 try 125 { 126 initialContext = new InitialContext (); 127 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 128 } 129 catch (Exception e) 130 { 131 throw new RemoteException ("Cannot get JNDI InitialContext"); 132 } 133 } 134 } 135 136 } 137 | Popular Tags |