KickJava   Java API By Example, From Geeks To Geeks.

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


1 package edu.rice.rubis.beans;
2
3 import java.net.URLEncoder JavaDoc;
4 import java.rmi.*;
5 import javax.ejb.*;
6 import javax.rmi.PortableRemoteObject JavaDoc;
7 import javax.naming.InitialContext JavaDoc;
8
9 /**
10  * CategoryBean is an entity bean with "container managed persistence".
11  * The state of an instance is stored into a relational database.
12  * The following table should exist:<p>
13  * <pre>
14  * CREATE TABLE categories (
15  * id INTEGER UNSIGNED NOT NULL UNIQUE,
16  * name VARCHAR(50),
17  * PRIMARY KEY(id)
18  * );
19  * </pre>
20  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
21  * @version 1.0
22  */

23
24 public class CategoryBean implements EntityBean
25 {
26   private EntityContext entityContext;
27   private transient boolean isDirty; // used for the isModified function
28

29   /* Class member variables */
30
31   public Integer JavaDoc id;
32   public String JavaDoc name;
33
34
35   /**
36    * Get category's id.
37    *
38    * @return category id
39    * @exception RemoteException if an error occurs
40    */

41   public Integer JavaDoc getId() throws RemoteException
42   {
43     return id;
44   }
45
46   /**
47    * Get the category name.
48    *
49    * @return category name
50    * @exception RemoteException if an error occurs
51    */

52   public String JavaDoc getName() throws RemoteException
53   {
54     return name;
55   }
56
57   /**
58    * Set category's name
59    *
60    * @param newName category name
61    * @exception RemoteException if an error occurs
62    */

63   public void setName(String JavaDoc newName) throws RemoteException
64   {
65     name = newName;
66     isDirty = true; // the bean content has been modified
67
}
68
69
70   /**
71    * This method is used to create a new Category Bean. Note that the category
72    * id is automatically generated by the database (AUTO_INCREMENT) on the
73    * primary key.
74    *
75    * @param categoryName Category name
76    *
77    * @return pk primary key set to null
78    * @exception CreateException if an error occurs
79    * @exception RemoteException if an error occurs
80    * @exception RemoveException if an error occurs
81    */

82   public CategoryPK ejbCreate(String JavaDoc categoryName) throws CreateException, RemoteException, RemoveException
83   {
84       // Connecting to IDManager Home interface thru JNDI
85
IDManagerHome home = null;
86       IDManager idManager = null;
87       
88       try
89       {
90         InitialContext JavaDoc initialContext = new InitialContext JavaDoc();
91         home = (IDManagerHome)PortableRemoteObject.narrow(initialContext.lookup(
92                "java:comp/env/ejb/IDManager"), IDManagerHome.class);
93       }
94       catch (Exception JavaDoc e)
95       {
96         throw new RemoteException("Cannot lookup IDManager: " +e);
97       }
98      try
99       {
100         IDManagerPK idPK = new IDManagerPK();
101         idManager = home.findByPrimaryKey(idPK);
102         id = idManager.getNextCategoryID();
103         name = categoryName;
104       }
105       catch (Exception JavaDoc e)
106       {
107         throw new RemoteException("Cannot create category: " +e);
108       }
109       return null;
110   }
111
112
113   /** This method just set an internal flag to
114       reload the id generated by the DB */

115   public void ejbPostCreate(String JavaDoc categoryName)
116   {
117     isDirty = true; // the id has to be reloaded from the DB
118
}
119
120   /** Persistence is managed by the container and the bean
121       becomes up to date */

122   public void ejbLoad() throws RemoteException
123   {
124     isDirty = false;
125   }
126
127   /** Persistence is managed by the container and the bean
128       becomes up to date */

129   public void ejbStore() throws RemoteException
130   {
131     isDirty = false;
132   }
133
134   /** This method is empty because persistence is managed by the container */
135   public void ejbActivate() throws RemoteException {}
136   /** This method is empty because persistence is managed by the container */
137   public void ejbPassivate() throws RemoteException {}
138   /** This method is empty because persistence is managed by the container */
139   public void ejbRemove() throws RemoteException, RemoveException {}
140
141   /**
142    * Sets the associated entity context. The container invokes this method
143    * on an instance after the instance has been created.
144    *
145    * This method is called in an unspecified transaction context.
146    *
147    * @param context - An EntityContext interface for the instance. The instance should
148    * store the reference to the context in an instance variable.
149    * @exception EJBException Thrown by the method to indicate a failure
150    * caused by a system-level error.
151    * @exception RemoteException - This exception is defined in the method signature
152    * to provide backward compatibility for enterprise beans
153    * written for the EJB 1.0 specification.
154    * Enterprise beans written for the EJB 1.1 and
155    * higher specification should throw the javax.ejb.EJBException
156    * instead of this exception.
157    */

158   public void setEntityContext(EntityContext context) throws RemoteException
159   {
160     entityContext = context;
161   }
162
163   /**
164    * Unsets the associated entity context. The container calls this method
165    * before removing the instance. This is the last method that the container
166    * invokes on the instance. The Java garbage collector will eventually invoke
167    * the finalize() method on the instance.
168    *
169    * This method is called in an unspecified transaction context.
170    *
171    * @exception EJBException Thrown by the method to indicate a failure
172    * caused by a system-level error.
173    * @exception RemoteException - This exception is defined in the method signature
174    * to provide backward compatibility for enterprise beans
175    * written for the EJB 1.0 specification.
176    * Enterprise beans written for the EJB 1.1 and
177    * higher specification should throw the javax.ejb.EJBException
178    * instead of this exception.
179    */

180   public void unsetEntityContext() throws RemoteException
181   {
182     entityContext = null;
183   }
184
185   /**
186    * Display category information for the BrowseCategories servlet
187    *
188    * @return a <code>String</code> containing HTML code
189    * @exception RemoteException if an error occurs
190    * @since 1.0
191    */

192   public String JavaDoc printCategory() throws RemoteException
193   {
194     return "<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByCategory?category="+id+
195                   "&categoryName="+URLEncoder.encode(name)+"\">"+name+"</a><br>\n";
196   }
197
198   /**
199    * Display category information for the BrowseCategories servlet
200    *
201    * @return a <code>String</code> containing HTML code
202    * @exception RemoteException if an error occurs
203    * @since 1.0
204    */

205   public String JavaDoc printCategoryByRegion(int regionId) throws RemoteException
206   {
207     return "<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+id+
208       "&categoryName="+URLEncoder.encode(name)+"&region="+regionId+"\">"+name+"</a><br>\n";
209   }
210
211
212   /**
213    * Returns true if the beans has been modified.
214    * It prevents the EJB server from reloading a bean
215    * that has not been modified.
216    *
217    * @return a <code>boolean</code> value
218    */

219   public boolean isModified()
220   {
221     return isDirty;
222   }
223
224
225   /**
226    * Display category information for the BrowseCategories servlet
227    *
228    * @return a <code>String</code> containing HTML code
229    * @exception RemoteException if an error occurs
230    * @since 1.0
231    */

232   public String JavaDoc printCategoryToSellItem(int userId) throws RemoteException
233   {
234     return "<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.SellItemForm?category="+id+"&user="+userId+"\">"+name+"</a><br>\n";
235   }
236 }
237
Popular Tags