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.sql.Connection ; 16 import java.sql.PreparedStatement ; 17 import java.sql.ResultSet ; 18 import java.sql.SQLException ; 19 import java.io.Serializable ; 20 import javax.transaction.UserTransaction ; 21 22 28 29 public class SB_RegisterItemBean implements SessionBean 30 { 31 protected SessionContext sessionContext; 32 protected Context initialContext = null; 33 protected DataSource dataSource = null; 34 private UserTransaction utx = null; 35 36 52 public String createItem(String name, String description, float initialPrice, int quantity, float reservePrice, float buyNow, String startDate, String endDate, int userId, int categoryId) throws RemoteException 53 { 54 String html; 55 int itemId = -1; 56 Connection conn = null; 57 PreparedStatement stmt = null; 58 59 utx = sessionContext.getUserTransaction(); 60 try 62 { 63 utx.begin(); 64 try 65 { 66 conn = dataSource.getConnection(); 67 stmt = conn.prepareStatement("INSERT INTO items VALUES (NULL, \""+name+ 68 "\", \""+description+"\", \""+initialPrice+"\", \""+ 69 quantity+"\", \""+reservePrice+"\", \""+buyNow+ 70 "\", 0, 0, \""+startDate+"\", \""+endDate+"\", \""+userId+ 71 "\", "+ categoryId+")"); 72 stmt.executeUpdate(); 73 stmt.close(); 74 } 75 catch (Exception e) 76 { 77 try { stmt.close(); } catch (Exception ignore) {} 78 try { conn.close(); } catch (Exception ignore) {} 79 throw new RemoteException ("Failed to create the item: " +e); 80 } 81 try 83 { 84 stmt = conn.prepareStatement("SELECT id FROM items WHERE name=?"); 85 stmt.setString(1, name); 86 ResultSet irs = stmt.executeQuery(); 87 if (!irs.first()) 88 { 89 try { stmt.close(); } catch (Exception ignore) {} 90 try { conn.close(); } catch (Exception ignore) {} 91 throw new RemoteException ("This item does not exist in the database."); 92 } 93 itemId = irs.getInt("id"); 94 95 html = "<TR><TD>Item id<TD>"+itemId+"\n"; 96 } 97 catch (Exception e) 98 { 99 try { stmt.close(); } catch (Exception ignore) {} 100 try { conn.close(); } catch (Exception ignore) {} 101 throw new RemoteException ("Failed to retrieve the item id: " +e); 102 } 103 if (stmt != null) stmt.close(); 104 if (conn != null) conn.close(); 105 utx.commit(); 106 } 107 catch (Exception e) 108 { 109 try { stmt.close(); } catch (Exception ignore) {} 110 try { conn.close(); } catch (Exception ignore) {} 111 try 112 { 113 utx.rollback(); 114 throw new RemoteException ("Item registration failed (got exception: " +e+")<br>"); 115 } 116 catch (Exception se) 117 { 118 throw new RemoteException ("Transaction rollback failed: " + e +"<br>"); 119 } 120 } 121 return html; 122 } 123 124 125 127 130 public void ejbCreate() throws CreateException , RemoteException 131 { 132 } 133 134 135 public void ejbActivate() throws RemoteException {} 136 137 public void ejbPassivate() throws RemoteException {} 138 139 public void ejbRemove() throws RemoteException {} 140 141 142 151 public void setSessionContext(SessionContext sessionContext) throws RemoteException 152 { 153 this.sessionContext = sessionContext; 154 if (dataSource == null) 155 { 156 158 try 159 { 160 initialContext = new InitialContext (); 161 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 162 } 163 catch (Exception e) 164 { 165 throw new RemoteException ("Cannot get JNDI InitialContext"); 166 } 167 } 168 } 169 170 } 171 | Popular Tags |