001 package biz.wedoit4u.databeans;
002
003 import java.io.Serializable;
004 import java.util.ArrayList;
005 import java.util.Collection;
006 import java.util.Iterator;
007 import java.util.Map;
008 import java.util.HashMap;
009 import biz.wedoit4u.CMAException;
010 import biz.wedoit4u.Logger;
011
012 /**
013 * A Java Bean that is used to represent a record in the
014 * <code>categories</code> table. This bean can be used to cache the
015 * contents of a database record, as well as insulate entity beans from
016 * client code. All instance variables are public, except the primary
017 * key column (<code>category_id</code>), which has only a accessor.
018 * All instances of this class are cached in the runtime environment
019 * <code>System.getProperties()</code>, under a property indentified
020 * by the value of {@link #CACHE_NAME}.
021 *
022 * <p>Copyright 2003, Rakesh Vidyadharan</p>
023 *
024 * @author Rakesh Vidyadharan on 4<sup><small>th</small></sup> October 2003
025 * @version $Id: CategoryBean.java,v 1.8 2004/05/26 11:42:30 rakesh Exp $
026 */
027 public class CategoryBean extends Object implements Serializable
028 {
029 /**
030 * An object that is used to <code>synchronise</code> modifications
031 * to the system cache of instances of these beans.
032 */
033 private static final Object lockObject;
034
035 /**
036 * The <code>Map</code> that is used to <code>cache</code> instances
037 * of this bean.
038 */
039 private static final Map cache;
040
041 /**
042 * A constant that denotes the name of the system property at which
043 * all instances of this class are cached.
044 */
045 public static final String CACHE_NAME = "CategoryBeanCache";
046
047 /**
048 * Intialise the <code>Map</code> in which all instances of this
049 * class are cached.
050 */
051 static
052 {
053 lockObject = new Object();
054 cache = new HashMap();
055 System.getProperties().put( CategoryBean.CACHE_NAME, cache );
056 }
057
058 /**
059 * The value in <code>category_id</code> column.
060 */
061 private int categoryId = 0;
062
063 /**
064 * The value in the <code>customer_id</code> column.
065 */
066 public int customerId;
067
068 /**
069 * The value in the <code>menu_name</code> column.
070 */
071 public String menuName;
072
073 /**
074 * The value in the <code>long_name</code> column.
075 */
076 public String longName;
077
078 /**
079 * The value in the <code>brief</code> column.
080 */
081 public String brief;
082
083 /**
084 * The value in the <code>picture</code> column.
085 */
086 public String picture;
087
088 /**
089 * The value of the <code>description</code> column.
090 */
091 public String description;
092
093 /**
094 * The value of the <code>sort_order</code> column.
095 */
096 public int sortOrder;
097
098 /**
099 * Default constructor. Cannot be directly instantiated.
100 */
101 private CategoryBean() {}
102
103 /**
104 * Create a new <code>category</code> record with all the column
105 * values. When creating a temporary instace (for client code)
106 * specify a value of <code>0</code> for the <code>customerId</code>
107 * parameter. Creating a new instance of the class also adds the
108 * new instance to the system cache, unless the categoryId value
109 * is <code>0</code>.
110 *
111 * @param categoryId - The <code>category_id</code> value.
112 * @param customerId - The <code>customer_id</code> value.
113 * @param menuName - The value for the <code>menu_name</code>
114 * column.
115 * @param longName - The value for the <code>long_name</code>
116 * column.
117 * @param brief - The value for the <code>brief</code> column.
118 * @param picture - The value for the <code>picture</code>
119 * column.
120 * @param description - The value for the <code>description
121 * </code> column.
122 * @param sortOrder - The value for the <code>sort_order</code>
123 * column.
124 * @throws CMAException - If an attempt is made to create an instance
125 * of the object that already exists, as identified by the
126 * customerTypeId value.
127 */
128 public static final CategoryBean create( int categoryId,
129 int customerId, String menuName, String longName, String brief,
130 String picture, String description, int sortOrder )
131 throws CMAException
132 {
133 Integer idObject = new Integer( categoryId );
134 if ( cache.containsKey( idObject ) )
135 {
136 throw new CMAException( "An instance of CategoryBean with primary key value " + categoryId + " already exists." );
137 }
138
139 try
140 {
141 categoryId = CategoryHelper.insert( categoryId, customerId, menuName, longName, brief, picture, description, sortOrder );
142 }
143 catch ( Exception ex )
144 {
145 throw new CMAException( "Exception creating new database record with category_id " + categoryId + ".", ex );
146 }
147
148 return CategoryBean.getInstance( categoryId );
149 }
150
151 /**
152 * Write back all the values in the bean fields to the database.
153 * All the columns in the <code>categories</code> table are
154 * updated with the values in the bean fields.
155 *
156 * @throws CMAException - If errors are encountered while writing
157 * the values to the database.
158 */
159 public void save() throws CMAException
160 {
161 try
162 {
163 CategoryHelper.update( this );
164 }
165 catch ( Exception ex )
166 {
167 throw new CMAException( "Error while updating values in categories for category_id " + categoryId + ".", ex );
168 }
169 }
170
171 /**
172 * Delete the specified instance of this class from the system cache
173 * as well as from the database.
174 *
175 * @param cb - The bean instance that is to be removed.
176 * @throws CMAException - If errors are encountered while removing
177 * the associated record in the database.
178 */
179 public static final void delete( CategoryBean cb ) throws CMAException
180 {
181 Integer idObject = new Integer( cb.getCategoryId() );
182 if ( ! cache.containsKey( idObject ) )
183 {
184 throw new CMAException( "The specified instance of the bean does not exist." );
185 }
186
187 cache.remove( idObject );
188
189 try
190 {
191 CategoryHelper.delete( cb.getCategoryId() );
192 }
193 catch ( Exception ex )
194 {
195 throw new CMAException( "Exception deleting database record with category_id " + cb.getCategoryId() + " in CategoryBean.delete.", ex );
196 }
197 }
198
199 /**
200 * Fetch the instance of the bean identified by the <code>primary
201 * key</code> value specified.
202 *
203 * @param categoryId - The <code>category_id</code> value that
204 * uniquely identifies an instance of this object.
205 * @return CategoryBean - The appropriate bean instance.
206 * @throws CMAException - If the bean instance is not available in
207 * the system cache.
208 */
209 public static final CategoryBean getInstance( int categoryId )
210 throws CMAException
211 {
212 Integer idObject = new Integer( categoryId );
213 if ( ! cache.containsKey( idObject ) )
214 {
215 try
216 {
217 CategoryBean cb = new CategoryBean();
218 cb.categoryId = categoryId;
219 CategoryHelper.select( cb );
220 synchronized ( lockObject )
221 {
222 cache.put( idObject, cb );
223 }
224 return cb;
225 }
226 catch ( Exception ex )
227 {
228 throw new CMAException( "Unable to find instance of CategoryBean with primary key value " + categoryId + ".", ex );
229 }
230 }
231 else
232 {
233 return ( (CategoryBean) cache.get( idObject ) );
234 }
235 }
236
237 /**
238 * Return a <code>Collection</code> of bean instances that represent
239 * all the records in the <code>categories</code> table.
240 *
241 * @return Collection - The collection of java bean instances.
242 * @throws CMAException - If errors are encountered while fetching
243 * all the bean instances.
244 */
245 public static final Collection findAll() throws CMAException
246 {
247 Collection collection = new ArrayList();
248 try
249 {
250 Collection list = CategoryHelper.findAll();
251 return getCollection( list );
252 }
253 catch ( Exception ex )
254 {
255 throw new CMAException( "Error fetching all the categories records in CategoryBean.findAll.", ex );
256 }
257 }
258
259 /**
260 * Return a <code>Collection</code> of bean instances that represent
261 * all the records in the <code>categories</code> table that are
262 * associated with the specified <code>customer_id</code>.
263 *
264 * @return Collection - The collection of java bean instances.
265 * @throws CMAException - If errors are encountered while fetching
266 * all the bean instances.
267 */
268 public static final Collection findByCustomer( int customerId )
269 throws CMAException
270 {
271 try
272 {
273 Collection list = CategoryHelper.findByCustomer( customerId );
274 return getCollection( list );
275 }
276 catch ( Exception ex )
277 {
278 throw new CMAException( "Error fetching the categories records in CategoryBean.findByCustomer for customer_id " + customerId + ".", ex );
279 }
280 }
281
282 /**
283 * Return a <code>Collection</code> of bean instances from the
284 * specified <code>Collection</code> of primary key values.
285 *
286 * @param list - The Collection of primary key values.
287 * @throws CMAException - If errors are encountered while fetching
288 * the bean instances.
289 */
290 private static final Collection getCollection( Collection list )
291 throws CMAException
292 {
293 Collection collection = new ArrayList();
294
295 for ( Iterator iterator = list.iterator(); iterator.hasNext(); )
296 {
297 int categoryId = ( (Integer) iterator.next() ).intValue();
298 collection.add( CategoryBean.getInstance( categoryId ) );
299 }
300
301 return collection;
302 }
303
304 /**
305 * Add the specified bean instance to the system cache. This method
306 * follows the general contract provided by the <code>Map</code>
307 * interface in that, if an entry with the same key already exists,
308 * then that entry is replaced, or a new one created. This method
309 * uses the {@link #lockObject} to <code>synchronise</code>
310 * modifications to the <code>Map</code> cache.
311 *
312 * @param ctb - The instance that is being added to
313 * the system cache.
314 */
315 public static final void setInstance( CategoryBean ctb )
316 {
317 synchronized( lockObject )
318 {
319 Integer idObject = new Integer( ctb.getCategoryId() );
320 cache.put( idObject, ctb );
321 }
322 }
323
324 /**
325 * Returns the value in <code>category_id</code> column.
326 *
327 * @return int - The value/reference of/to categoryId.
328 */
329 public final int getCategoryId() { return categoryId; }
330 }