KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
16 import java.sql.PreparedStatement JavaDoc;
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.SQLException JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import javax.transaction.UserTransaction JavaDoc;
21
22 /**
23  * This is a stateless session bean used to register a new item.
24  *
25  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
26  * @version 1.1
27  */

28
29 public class SB_RegisterItemBean implements SessionBean JavaDoc
30 {
31   protected SessionContext JavaDoc sessionContext;
32   protected Context JavaDoc initialContext = null;
33   protected DataSource JavaDoc dataSource = null;
34   private UserTransaction JavaDoc utx = null;
35
36   /**
37    * Create a new item.
38    *
39    * @param name name of the item
40    * @param description item's description
41    * @param initialPrice item's initial price
42    * @param quantity number of items
43    * @param reservePrice item's reserve price
44    * @param buyNow item's price to buy it now
45    * @param startDate auction's start date
46    * @param endDate auction's end date
47    * @param userId seller id
48    * @param catagoryId category id
49    * @return a string in html format
50    * @since 1.1
51    */

52   public String JavaDoc createItem(String JavaDoc name, String JavaDoc description, float initialPrice, int quantity, float reservePrice, float buyNow, String JavaDoc startDate, String JavaDoc endDate, int userId, int categoryId) throws RemoteException JavaDoc
53   {
54     String JavaDoc html;
55     int itemId = -1;
56     Connection JavaDoc conn = null;
57     PreparedStatement JavaDoc stmt = null;
58
59     utx = sessionContext.getUserTransaction();
60     // Try to create a new item
61
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 JavaDoc e)
76       {
77         try { stmt.close(); } catch (Exception JavaDoc ignore) {}
78         try { conn.close(); } catch (Exception JavaDoc ignore) {}
79         throw new RemoteException JavaDoc("Failed to create the item: " +e);
80       }
81       // To test if the item was correctly added in the database
82
try
83       {
84         stmt = conn.prepareStatement("SELECT id FROM items WHERE name=?");
85         stmt.setString(1, name);
86         ResultSet JavaDoc irs = stmt.executeQuery();
87         if (!irs.first())
88         {
89           try { stmt.close(); } catch (Exception JavaDoc ignore) {}
90           try { conn.close(); } catch (Exception JavaDoc ignore) {}
91           throw new RemoteException JavaDoc("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 JavaDoc e)
98       {
99         try { stmt.close(); } catch (Exception JavaDoc ignore) {}
100         try { conn.close(); } catch (Exception JavaDoc ignore) {}
101         throw new RemoteException JavaDoc("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 JavaDoc e)
108     {
109       try { stmt.close(); } catch (Exception JavaDoc ignore) {}
110       try { conn.close(); } catch (Exception JavaDoc ignore) {}
111       try
112       {
113         utx.rollback();
114         throw new RemoteException JavaDoc("Item registration failed (got exception: " +e+")<br>");
115       }
116       catch (Exception JavaDoc se)
117       {
118         throw new RemoteException JavaDoc("Transaction rollback failed: " + e +"<br>");
119       }
120     }
121     return html;
122   }
123   
124
125   // ======================== EJB related methods ============================
126

127   /**
128    * This method is empty for a stateless session bean
129    */

130   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
131   {
132   }
133
134   /** This method is empty for a stateless session bean */
135   public void ejbActivate() throws RemoteException JavaDoc {}
136   /** This method is empty for a stateless session bean */
137   public void ejbPassivate() throws RemoteException JavaDoc {}
138   /** This method is empty for a stateless session bean */
139   public void ejbRemove() throws RemoteException JavaDoc {}
140
141
142   /**
143    * Sets the associated session context. The container calls this method
144    * after the instance creation. This method is called with no transaction context.
145    * We also retrieve the Home interfaces of all RUBiS's beans.
146    *
147    * @param sessionContext - A SessionContext interface for the instance.
148    * @exception RemoteException - Thrown if the instance could not perform the function
149    * requested by the container because of a system-level error.
150    */

151   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
152   {
153     this.sessionContext = sessionContext;
154     if (dataSource == null)
155     {
156       // Finds DataSource from JNDI
157

158       try
159       {
160         initialContext = new InitialContext JavaDoc();
161         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
162       }
163       catch (Exception JavaDoc e)
164       {
165         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
166       }
167     }
168   }
169
170 }
171
Popular Tags