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_styles
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>style_class_id</code>.
022 *
023 * <p>Copyright 2003, Rakesh Vidyadharan</p>
024 *
025 * @author Rakesh Vidyadharan on 16<sup><small>th</small></sup> September 2003
026 * @version $Id: CustomerStyleBean.java,v 1.2 2004/05/26 11:42:31 rakesh Exp $
027 */
028 public class CustomerStyleBean implements Serializable
029 {
030 /**
031 * An object that is used to <code>synchronise</code> modifications
032 * to the system cache of instances of these beans.
033 */
034 private static final Object lockObject;
035
036 /**
037 * The <code>Map</code> that is used to <code>cache</code> instances
038 * of this bean.
039 */
040 private static final Map cache;
041
042 /**
043 * A constant that denotes the name of the system property at which
044 * all instances of this class are cached.
045 */
046 public static final String CACHE_NAME = "CustomerStyleBeanCache";
047
048 /**
049 * Intialise the <code>Map</code> in which all instances of this
050 * class are cached.
051 */
052 static
053 {
054 lockObject = new Object();
055 cache = new HashMap();
056 System.getProperties().put( CustomerStyleBean.CACHE_NAME, cache );
057 }
058
059 /**
060 * The value in the <code>style_class_id</code> column. This column
061 * is part of the compound primary key for this table.
062 */
063 private int styleClassId;
064
065 /**
066 * The value in the <code>customer_id</code> column. This column is
067 * part of the compound primary key for this table.
068 */
069 private int customerId;
070
071 /**
072 * The value in the <code>color</code> column.
073 */
074 public String color;
075
076 /**
077 * The value in the <code>font_family</code> column.
078 */
079 public String fontFamily;
080
081 /**
082 * The value in the <code>font_weight</code> column.
083 */
084 public String fontWeight;
085
086 /**
087 * The value in the <code>font_size</code> column.
088 */
089 public String fontSize;
090
091 /**
092 * The value in the <code>background_color</code> column.
093 */
094 public String backgroundColor;
095
096 /**
097 * The value in the <code>text_align</code> column.
098 */
099 public String textAlign;
100
101 /**
102 * The value in the <code>vertical_align</code> column.
103 */
104 public String verticalAlign;
105
106 /**
107 * The value in the <code>width</code> column.
108 */
109 public String width;
110
111 /**
112 * The value in the <code>height</code> column.
113 */
114 public String height;
115
116 /**
117 * The value in the <code>border_style</code> column.
118 */
119 public String borderStyle;
120
121 /**
122 * The value in the <code>border_width</code> column.
123 */
124 public String borderWidth;
125
126 /**
127 * The value in the <code>padding_top</code> column.
128 */
129 public int paddingTop;
130
131 /**
132 * The value in the <code>padding_left</code> column.
133 */
134 public int paddingLeft;
135
136 /**
137 * The value in the <code>padding_right</code> column.
138 */
139 public int paddingRight;
140
141 /**
142 * The value in the <code>padding_bottom</code> column.
143 */
144 public int paddingBottom;
145
146 /**
147 * The value in the <code>text_decoration</code> column.
148 */
149 public String textDecoration;
150
151 /**
152 * The default constructor. Cannot be directly instantiated.
153 */
154 private CustomerStyleBean() {}
155
156 /**
157 * Create a new instance of the bean that may be used to create
158 * a corresponding record in the <code>customer_styles</code> table.
159 *
160 * <p><b>Note:</b> The compound primary key components {@link
161 * #styleClassId} and {@link #customerId} are both <code>foreign key
162 * </code> references to the <code>style_classes.style_class_id</code>
163 * and <code>customers.customer_id</code> columns.</p>
164 *
165 * @param styleClassId - The primary key value to use.
166 * @param customerId - The primary key value to use.
167 * @param color - The <code>color</code> style attribute.
168 * @param fontFamily - The <code>font-family</code> style
169 * attribute.
170 * @param fontWeight - The <code>font-weight</code> style
171 * attribute.
172 * @param fontSize - The <code>font-size</code> style
173 * attribute.
174 * @param backgroundColor - The <code>background-color</code>
175 * style attribute.
176 * @param textAlign - The <code>text-align</code> style
177 * attribute.
178 * @param verticalAlign - The <code>vertical-align</code> style
179 * attribute.
180 * @param width - The <code>width</code> style attribute.
181 * @param height - The <code>height</code> style attribute.
182 * @param borderStyle - The <code>border-style</code> style
183 * attribute.
184 * @param borderWidth - The <code>border-width</code> style
185 * attribute.
186 * @param paddingTop - The <code>padding-top</code> style
187 * attribute.
188 * @param paddingLeft - The <code>padding-left</code> style
189 * attribute.
190 * @param paddingRight - The <code>padding-right</code> style
191 * attribute.
192 * @param paddingBottom - The <code>padding-bottom</code> style
193 * attribute.
194 * @param textDecoration - The <code>text-decoration</code>
195 * style attribute.
196 * @throws CMAException - If an attempt is made to create an instance
197 * of the object that already exists, as identified by the
198 * styleClassId value.
199 */
200 public static final CustomerStyleBean create( int styleClassId,
201 int customerId, String color, String fontFamily,
202 String fontWeight, String fontSize, String backgroundColor,
203 String textAlign, String verticalAlign, String width,
204 String height, String borderStyle, String borderWidth,
205 int paddingTop, int paddingLeft, int paddingRight,
206 int paddingBottom, String textDecoration )
207 throws CMAException
208 {
209 CustomerStylesPK idObject = new CustomerStylesPK( styleClassId, customerId );
210 if ( cache.containsKey( idObject ) )
211 {
212 throw new CMAException( "An instance of CustomerStyleBean with primary key value " + idObject + " already exists." );
213 }
214
215 try
216 {
217 CustomerStyleHelper.insert( styleClassId, customerId, color, fontFamily, fontWeight, fontSize, backgroundColor, textAlign, verticalAlign, width, height, borderStyle, borderWidth, paddingTop, paddingLeft, paddingRight, paddingBottom, textDecoration );
218 }
219 catch ( Exception ex )
220 {
221 throw new CMAException( "Exception creating new database record with style_class_id " + styleClassId + " and customer_id " + customerId + ".", ex );
222 }
223
224 return CustomerStyleBean.getInstance( idObject );
225 }
226
227 /**
228 * Delete the specified instance of this class from the system cache
229 * as well as from the database.
230 *
231 * @see biz.wedoit4u.databeans.CustomerStyleHelper#delete( int, int )
232 * @param sab - The bean instance that is to be
233 * removed.
234 * @throws CMAException - If errors are encountered while removing
235 * the associated record in the database.
236 */
237 public static final void delete( CustomerStyleBean sab )
238 throws CMAException
239 {
240 CustomerStylesPK idObject = new CustomerStylesPK( sab.getStyleClassId(), sab.getCustomerId() );
241 if ( ! cache.containsKey( idObject ) )
242 {
243 throw new CMAException( "The specified instance of the bean does not exist." );
244 }
245
246 cache.remove( idObject );
247
248 try
249 {
250 CustomerStyleHelper.delete( sab.getStyleClassId(), sab.getCustomerId() );
251 }
252 catch ( Exception ex )
253 {
254 throw new CMAException( "Exception deleting database record with style_class_id " + sab.getStyleClassId() + " and customer_id " + sab.getCustomerId() + " in CustomerStyleBean.delete.", ex );
255 }
256 }
257
258 /**
259 * Write back all the values in the bean fields to the database.
260 * All the columns in the <code>customer_styles</code> table are
261 * updated with the values in the bean fields.
262 *
263 * @throws CMAException - If errors are encountered while writing
264 * the values to the database.
265 */
266 public void save() throws CMAException
267 {
268 try
269 {
270 CustomerStyleHelper.update( this );
271 }
272 catch ( Exception ex )
273 {
274 throw new CMAException( "Error while updating values in customer_styles for style_class_id " + styleClassId + ".", ex );
275 }
276 }
277
278 /**
279 * Return a <code>Collection</code> of bean instances that represent
280 * all the records in the <code>customer_styles</code> table.
281 *
282 * @return Collection - The collection of java bean instances.
283 * @throws CMAException - If errors are encountered while fetching
284 * all the bean instances.
285 */
286 public static final Collection findAll() throws CMAException
287 {
288 Collection collection = new ArrayList();
289 try
290 {
291 Collection list = CustomerStyleHelper.findAll();
292
293 for ( Iterator iterator = list.iterator(); iterator.hasNext(); )
294 {
295 CustomerStylesPK pk = (CustomerStylesPK) iterator.next();
296 collection.add( CustomerStyleBean.getInstance( pk ) );
297 }
298 }
299 catch ( Exception ex )
300 {
301 throw new CMAException( "Error fetching all the customer_styles records.", ex );
302 }
303
304 return collection;
305 }
306
307 /**
308 * Return a <code>Collection</code> of bean instances that represent
309 * all the records in the <code>customer_styles</code> table that
310 * are associated with the specified <code>customer_id</code>.
311 *
312 * @param customerId - The <code>customer_id</code> for which
313 * all associated records are to be fetched.
314 * @return Collection - The collection of java bean instances.
315 * @throws CMAException - If errors are encountered while fetching
316 * all the bean instances.
317 */
318 public static final Collection findByCustomer( int customerId )
319 throws CMAException
320 {
321 Collection collection = new ArrayList();
322 try
323 {
324 Collection list = CustomerStyleHelper.findByCustomer( customerId );
325
326 for ( Iterator iterator = list.iterator(); iterator.hasNext(); )
327 {
328 CustomerStylesPK pk = (CustomerStylesPK) iterator.next();
329 collection.add( CustomerStyleBean.getInstance( pk ) );
330 }
331 }
332 catch ( Exception ex )
333 {
334 throw new CMAException( "Error fetching all the customer_styles records in CustomerStyleBean.findByCustomer for customer_id " + customerId + ".", ex );
335 }
336
337 return collection;
338 }
339
340 /**
341 * Fetch the instance of the bean identified by the <code>primary
342 * key</code> value specified. If the bean instance does not exist
343 * in the cache, try to fetch it from the database.
344 *
345 * @param pk - The object representing the
346 * compound primary key columns for this table.
347 * @return CustomerStyleBean - The appropriate bean instance.
348 * @throws CMAException - If the specified bean instance is not
349 * available in the system cache.
350 */
351 public static final CustomerStyleBean getInstance(
352 CustomerStylesPK pk ) throws CMAException
353 {
354 if ( ! cache.containsKey( pk ) )
355 {
356 try
357 {
358 CustomerStyleBean sab = new CustomerStyleBean();
359 sab.styleClassId = pk.styleClassId;
360 sab.customerId = pk.customerId;
361 CustomerStyleHelper.select( sab );
362 synchronized ( lockObject )
363 {
364 cache.put( pk, sab );
365 }
366 return sab;
367 }
368 catch ( Exception ex )
369 {
370 throw new CMAException( "Unable to find instance of CustomerStyleBean with primary key value " + pk.toString() + ".", ex );
371 }
372 }
373 else
374 {
375 return ( (CustomerStyleBean) cache.get( pk ) );
376 }
377 }
378
379 /**
380 * Add the specified bean instance to the system cache. This method
381 * follows the general contract provided by the <code>Map</code>
382 * interface in that, if an entry with the same key already exists,
383 * then that entry is replaced, or a new one created. This method
384 * uses the {@link #lockObject} to <code>synchronise</code>
385 * modifications to the <code>Map</code> cache.
386 *
387 * @param sab - The instance that is being added to
388 * the system cache.
389 */
390 public static final void setInstance( CustomerStyleBean sab )
391 {
392 synchronized( lockObject )
393 {
394 Integer idObject = new Integer( sab.getStyleClassId() );
395 cache.put( idObject, sab );
396 }
397 }
398
399 /**
400 * Returns {@link #styleClassId}.
401 *
402 * @return int - The value/reference of/to styleClassId.
403 */
404 public final int getStyleClassId()
405 {
406 return styleClassId;
407 }
408
409 /**
410 * Returns {@link #customerId}.
411 *
412 * @return int - The value/reference of/to customerId.
413 */
414 public final int getCustomerId()
415 {
416 return customerId;
417 }
418 }