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.Date;
007    import java.util.Iterator;
008    import java.util.Map;
009    import java.util.HashMap;
010    import biz.wedoit4u.CMAException;
011    import biz.wedoit4u.Logger;
012    
013    /**
014     * A java bean that represents a record in the <code>customers</code> 
015     * table.  This bean is used to cache the values read 
016     * from the database, and to make modifications you need to the bean
017     * fields, and then send it back to the database for updating the
018     * associated record.  All instances of this class are cached into a
019     * <code>Map</code> object that is stored in the 
020     * <code>System.getProperties()</code> object under a property with
021     * name {@link #CACHE_NAME}.  All instances variables are public except
022     * the <code>primary key</code> column <code>customer_id</code>.
023     *
024     * <p>Copyright 2003, Rakesh Vidyadharan</p>
025     *
026     * @author Rakesh Vidyadharan on 9<sup><small>th</small></sup> September 2003
027     * @version $Id: CustomerBean.java,v 1.10 2004/05/26 11:42:31 rakesh Exp $
028     */
029    public class CustomerBean 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 = "CustomerBeanCache";
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( CustomerBean.CACHE_NAME, cache );
058      }
059    
060      /**
061       * The value in the <code>customer_id</code> column.
062       */
063      private int customerId;
064    
065      /**
066       * The value in the <code>username</code> column.
067       */
068      public String username;
069    
070      /**
071       * The value in the <code>password</code> column.
072       */
073      public String password;
074    
075      /**
076       * The value in the <code>email</code> column.
077       */
078      public String email;
079    
080      /**
081       * The value in the <code>domain</code> column.
082       */
083      public String domain;
084    
085      /**
086       * The value in the <code>active</code> column.
087       */
088      public char active;
089    
090      /**
091       * The value in the <code>creation_date</code> column.
092       */
093      public Date creationDate = new Date();
094    
095      /**
096       * The value in the <code>activation_date</code> column.
097       */
098      public Date activationDate = new Date();
099    
100      /**
101       * The value in the <code>expiration_date</code> column.
102       */
103      public Date expirationDate = new Date();
104    
105      /**
106       * The value in the <code>customer_type_id</code> column.
107       */
108      public int customerTypeId;
109    
110      /**
111       * Default constructor.  Cannot be directly instantiated.
112       */
113      private CustomerBean() {}
114    
115      /**
116       * Create a new <code>customers</code> record with the values 
117       * specified for all the columns in the table.  If you wish
118       * to use the database sequence to generate the customer_id value,
119       * specify a value of <code>0</code> for the customer_id.
120       * Creating a new instance will also add the new instance of the
121       * system cache, unless the customerId value specified is
122       * <code>0</code>.
123       *
124       * @param customerId - The <code>customer_id</code> value.
125       * @param username - The <code>username</code> value.
126       * @param password - The <code>password</code> value.
127       * @param email - The <code>email</code> value.  This
128       *   may be <code>null</code>.
129       * @param domain - The <code>domain</code> value.  This
130       *   may be <code>null</code>.
131       * @param active - The <code>active</code> value.
132       * @param activationDate - The <code>activation_date</code> 
133       *   value.
134       * @param expirationDate - The <code>expiration_date</code> 
135       *   value.
136       * @param customerTypeId - The <code>customer_type_id</code>  
137       *   value.
138       * @throws CMAException - If an attempt is made to create an instance
139       *   of the object that already exists, as identified by the
140       *   customerTypeId value.
141       */
142      public static final CustomerBean create( int customerId,
143          String username, String password, String email, String domain, 
144          char active, Date activationDate, Date expirationDate, 
145          int customerTypeId ) throws CMAException
146      {
147        Integer idObject = new Integer( customerId );
148        if ( cache.containsKey( idObject ) )
149        {
150          throw new CMAException( "An instance of CustomerBean with primary key value " + customerId + " already exists." );
151        }
152    
153        try
154        {
155          customerId = CustomerHelper.insert( customerId, username, password, email, domain, active, activationDate, expirationDate, customerTypeId );
156        }
157        catch ( Exception ex )
158        {
159          throw new CMAException( "Exception creating new database record with customer_id " + customerId + " in CustomerBean.create.", ex );
160        }
161    
162        return CustomerBean.getInstance( customerId );
163      }
164    
165      /**
166       * Delete the specified instance of this class from the system cache
167       * as well as from the database.
168       *
169       * @param cb - The bean instance that is to be removed.
170       * @throws CMAException - If errors are encountered while removing
171       *   the associated record in the database.
172       */
173      public static final void delete( CustomerBean cb ) throws CMAException
174      {
175        Integer idObject = new Integer( cb.getCustomerId() );
176        if ( ! cache.containsKey( idObject ) )
177        {
178          throw new CMAException( "The specified instance of the bean does not exist." );
179        }
180    
181        cache.remove( idObject );
182    
183        try
184        {
185          CustomerHelper.delete( cb.getCustomerId() );
186        }
187        catch ( Exception ex )
188        {
189          throw new CMAException( "Exception deleting database record with customer_id " + cb.getCustomerId() + " in CustomerBean.delete.", ex );
190        }
191      }
192    
193      /**
194       * Write back all the values in the bean fields to the database.
195       * All the columns in the <code>customers</code> table are
196       * updated with the values in the bean fields.
197       *
198       * @throws CMAException - If errors are encountered while writing
199       *   the values to the database.
200       */
201      public void save() throws CMAException
202      {
203        try
204        {
205          CustomerHelper.update( this );
206        }
207        catch ( Exception ex )
208        {
209          throw new CMAException( "Error while updating values in customers for customer_id " + customerId + ".", ex );
210        }
211      }
212    
213      /**
214       * Fetch the instance of the bean from the system cache indentified
215       * by the primary key column {@link #customerId}.
216       *
217       * @param customerId - The primary key value based upon which
218       *   to find the appropriate bean instance.
219       * @throws CMAException - If the appropriate bean instance is not
220       *   found.
221       */
222      public static final CustomerBean getInstance( int customerId )
223        throws CMAException
224      {
225        Integer idObject = new Integer( customerId );
226        if ( ! cache.containsKey( idObject ) )
227        {
228          try
229          {
230            CustomerBean cb = new CustomerBean();
231            cb.customerId = customerId;
232            CustomerHelper.select( cb );
233            synchronized ( lockObject ) 
234            {
235              cache.put( idObject, cb );
236            }
237            return cb;
238          }
239          catch ( Exception ex )
240          {
241            throw new CMAException( "Unable to find instance of CustomerBean with primary key value " + customerId + ".", ex );
242          }
243        }
244        else
245        {
246          return ( (CustomerBean) cache.get( idObject ) );
247        }
248      }
249    
250      /**
251       * Return a <code>Collection</code> of bean instances that represent
252       * all the records in the <code>customers</code> table.
253       *
254       * @return Collection - The collection of java bean instances.
255       * @throws CMAException - If errors are encountered while fetching
256       *   all the bean instances.
257       */
258      public static final Collection findAll() throws CMAException
259      {
260        Collection collection = new ArrayList();
261        try
262        {
263          Collection list = CustomerHelper.findAll();
264          return getCollection( list );
265        }
266        catch ( Exception ex )
267        {
268          throw new CMAException( "Error fetching all the customers records.", ex );
269        }
270      }
271    
272      /**
273       * Return a <code>Collection</code> of bean instances that represent
274       * all the records in the <code>customers</code> table that are
275       * associated with the specified <code>customer_type_id</code>.
276       *
277       * @param customerTypeId - The <code>customer_type_id</code> 
278       *   foreign key.
279       * @return Collection - The collection of java bean instances.
280       * @throws CMAException - If errors are encountered while fetching
281       *   all the bean instances.
282       */
283      public static final Collection findByCustomerType( 
284          int customerTypeId ) throws CMAException
285      {
286        try
287        {
288          Collection list = CustomerHelper.findByCustomerType( customerTypeId );
289          return getCollection( list );
290        }
291        catch ( Exception ex )
292        {
293          throw new CMAException( "Error fetching the customers records in CustomerBean.findByCustomerType for customer_type_id " + customerTypeId + ".", ex );
294        }
295      }
296    
297      /**
298       * Return a <code>Collection</code> of bean instances from the
299       * specified <code>Collection</code> of primary key values.
300       *
301       * @param list - The Collection of primary key values.
302       * @throws CMAException - If errors are encountered while fetching
303       *   the bean instances.
304       */
305      private static final Collection getCollection( Collection list )
306        throws CMAException
307      {
308        Collection collection = new ArrayList();
309    
310        for ( Iterator iterator = list.iterator(); iterator.hasNext(); )
311        {
312          int customerId = ( (Integer) iterator.next() ).intValue();
313          collection.add( CustomerBean.getInstance( customerId ) );
314        }
315    
316        return collection;
317      }
318    
319      /**
320       * Return the bean instance identified by the {@link #username}
321       * and {@link #password} values specified.
322       *
323       * @param username - The <code>username</code> value.
324       * @param password - The <code>password</code> value.
325       * @throws CMAException - If errors are encountered while fetching
326       *   the bean instance.
327       */
328      public static final CustomerBean getInstance( String username,
329          String password ) throws CMAException
330      {
331        try
332        {
333          return getInstance( CustomerHelper.findByUsernameAndPassword( username, password ) );
334        }
335        catch ( Throwable t )
336        {
337          throw new CMAException( "Error finding CustomerBean instance for username " + username + " and password " + password + ".", t );
338        }
339      }
340    
341      /**
342       * Add the specified bean instance to the system cache.  This method
343       * follows the general contract provided by the <code>Map</code>
344       * interface in that, if an entry with the same key already exists,
345       * then that entry is replaced, or a new one created.  This method
346       * uses the {@link #lockObject} to <code>synchronise</code> 
347       * modifications to the <code>Map</code> cache.
348       *
349       * @param ctb - The instance that is being added to
350       *   the system cache.
351       */
352      public static final void setInstance( CustomerBean ctb )
353      {
354        synchronized( lockObject )
355        {
356          Integer idObject = new Integer( ctb.getCustomerId() );
357          cache.put( idObject, ctb );
358        }
359      }
360      
361      /**
362       * Returns {@link #customerId}.
363       *
364       * @return int - The value/reference of/to customerId.
365       */
366      public final int getCustomerId()
367      {
368        return customerId;
369      }
370    }