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