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>partners</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>partner_id</code>.
023 *
024 * <p>Copyright 2004, Rakesh Vidyadharan</p>
025 *
026 * @author Rakesh Vidyadharan on 1<sup><small>st</small></sup> January 2004
027 * @version $Id: PartnerBean.java,v 1.5 2004/05/26 11:42:32 rakesh Exp $
028 */
029 public class PartnerBean 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 = "PartnerBeanCache";
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( PartnerBean.CACHE_NAME, cache );
058 }
059
060 /**
061 * The value in the <code>partner_id</code> column.
062 */
063 private int partnerId;
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>active</code> column.
077 */
078 public char active;
079
080 /**
081 * The value in the <code>creation_date</code> column.
082 */
083 public Date creationDate = new Date();
084
085 /**
086 * The value in the <code>expiration_date</code> column.
087 */
088 public Date expirationDate = new Date();
089
090 /**
091 * The value in the <code>partner_name</code> column.
092 */
093 public String partnerName;
094
095 /**
096 * The value in the <code>description</code> column.
097 */
098 public String description;
099
100 /**
101 * The value in the <code>email</code> column.
102 */
103 public String email;
104
105 /**
106 * The value in the <code>telephone</code> column.
107 */
108 public String telephone;
109
110 /**
111 * The value in the <code>mobile_phone</code> column.
112 */
113 public String mobilePhone;
114
115 /**
116 * Default constructor. Cannot be directly instantiated.
117 */
118 private PartnerBean() {}
119
120 /**
121 * Create a new <code>partners</code> record with the values
122 * specified for all the columns in the table. If you wish
123 * to use the database sequence to generate the partner_id value,
124 * specify a value of <code>0</code> for the partner_id.
125 * Creating a new instance will also add the new instance of the
126 * system cache, unless the partnerId value specified is
127 * <code>0</code>.
128 *
129 * @param partnerId - The <code>partner_id</code> value.
130 * @param username - The <code>username</code> value.
131 * @param password - The <code>password</code> value.
132 * @param active - The <code>active</code> value.
133 * @param expirationDate - The <code>expiration_date</code>
134 * value.
135 * @param partnerName - The <code>partner_name</code> value.
136 * @param description - The <code>description</code> value.
137 * @param email - The <code>email</code> value.
138 * @param telephone - The <code>telephone</code> value.
139 * @param mobilePhone - The <code>mobilePhone</code> value.
140 * @throws CMAException - If an attempt is made to create an instance
141 * of the object that already exists, as identified by the
142 * partnerId value.
143 */
144 public static final PartnerBean create( int partnerId,
145 String username, String password, char active,
146 Date expirationDate, String partnerName, String description,
147 String email, String telephone, String mobilePhone )
148 throws CMAException
149 {
150 Integer idObject = new Integer( partnerId );
151 if ( cache.containsKey( idObject ) )
152 {
153 throw new CMAException( "An instance of PartnerBean with primary key value " + partnerId + " already exists." );
154 }
155
156 try
157 {
158 partnerId = PartnerHelper.insert( partnerId, username, password, active, expirationDate, partnerName, description, email, telephone, mobilePhone );
159 }
160 catch ( Exception ex )
161 {
162 throw new CMAException( "Exception creating new database record with partner_id " + partnerId + " in PartnerBean.create.", ex );
163 }
164
165 return PartnerBean.getInstance( partnerId );
166 }
167
168 /**
169 * Write back all the values in the bean fields to the database.
170 * All the columns in the <code>partners</code> table are
171 * updated with the values in the bean fields.
172 *
173 * @throws CMAException - If errors are encountered while writing
174 * the values to the database.
175 */
176 public void save() throws CMAException
177 {
178 try
179 {
180 PartnerHelper.update( this );
181 }
182 catch ( Exception ex )
183 {
184 throw new CMAException( "Error while updating values in partners for partner_id " + partnerId + ".", ex );
185 }
186 }
187
188 /**
189 * Delete the specified instance of this class from the system cache
190 * as well as from the database.
191 *
192 * @param pb - The bean instance that is to be removed.
193 * @throws CMAException - If errors are encountered while removing
194 * the associated record in the database.
195 */
196 public static final void delete( PartnerBean pb ) throws CMAException
197 {
198 Integer idObject = new Integer( pb.getPartnerId() );
199 if ( ! cache.containsKey( idObject ) )
200 {
201 throw new CMAException( "The specified instance of the bean does not exist." );
202 }
203
204 cache.remove( idObject );
205
206 try
207 {
208 PartnerHelper.delete( pb.getPartnerId() );
209 }
210 catch ( Exception ex )
211 {
212 throw new CMAException( "Exception deleting database record with partner_id " + pb.getPartnerId() + " in PartnerBean.delete.", ex );
213 }
214 }
215
216 /**
217 * Fetch the instance of the bean from the system cache indentified
218 * by the primary key column {@link #partnerId}.
219 *
220 * @param partnerId - The primary key value based upon which
221 * to find the appropriate bean instance.
222 * @throws CMAException - If the appropriate bean instance is not
223 * found.
224 */
225 public static final PartnerBean getInstance( int partnerId )
226 throws CMAException
227 {
228 Integer idObject = new Integer( partnerId );
229 if ( cache.containsKey( idObject ) )
230 {
231 return ( (PartnerBean) cache.get( idObject ) );
232 }
233 else
234 {
235 try
236 {
237 PartnerBean pb = new PartnerBean();
238 pb.partnerId = partnerId;
239 PartnerHelper.select( pb );
240 synchronized ( lockObject )
241 {
242 cache.put( idObject, pb );
243 }
244 return pb;
245 }
246 catch ( Exception ex )
247 {
248 throw new CMAException( "Unable to find instance of PartnerBean with primary key value " + partnerId + ".", ex );
249 }
250 }
251 }
252
253 /**
254 * Return a <code>Collection</code> of bean instances that represent
255 * all the records in the <code>partners</code> table.
256 *
257 * @return Collection - The collection of java bean instances.
258 * @throws CMAException - If errors are encountered while fetching
259 * all the bean instances.
260 */
261 public static final Collection findAll() throws CMAException
262 {
263 Collection collection = new ArrayList();
264 try
265 {
266 Collection list = PartnerHelper.findAll();
267
268 for ( Iterator iterator = list.iterator(); iterator.hasNext(); )
269 {
270 int partnerId = ( (Integer) iterator.next() ).intValue();
271 collection.add( PartnerBean.getInstance( partnerId ) );
272 }
273
274 return collection;
275 }
276 catch ( Exception ex )
277 {
278 throw new CMAException( "Error fetching all the partners records.", ex );
279 }
280 }
281
282 /**
283 * Return the bean instance identified by the {@link #username}
284 * and {@link #password} values specified.
285 *
286 * @param username - The <code>username</code> value.
287 * @param password - The <code>password</code> value.
288 * @throws CMAException - If errors are encountered while fetching
289 * the bean instance.
290 */
291 public static final PartnerBean getInstance( String username,
292 String password ) throws CMAException
293 {
294 try
295 {
296 return getInstance( PartnerHelper.findByUsernameAndPassword( username, password ) );
297 }
298 catch ( Throwable t )
299 {
300 throw new CMAException( "Error finding PartnerBean instance for username " + username + " and password " + password + ".", t );
301 }
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( PartnerBean ctb )
316 {
317 synchronized( lockObject )
318 {
319 Integer idObject = new Integer( ctb.getPartnerId() );
320 cache.put( idObject, ctb );
321 }
322 }
323
324 /**
325 * Returns {@link #partnerId}.
326 *
327 * @return int - The value/reference of/to partnerId.
328 */
329 public final int getPartnerId()
330 {
331 return partnerId;
332 }
333 }