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