Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/bean/AuditedExtension.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/bean/AuditedExtension.java,v 1.5 2003/09/30 15:13:09 joe Exp $
2    * $Revision: 1.5 $
3    * $Date: 2003/09/30 15:13:09 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.bean;
31  
32  import com.RuntimeCollective.webapps.BeanUtils;
33  import com.RuntimeCollective.webapps.EntityBeanStore;
34  import com.RuntimeCollective.webapps.RuntimeDataSource;
35  import com.RuntimeCollective.webapps.RuntimeParameters;
36  import com.RuntimeCollective.webapps.bean.EntityBean;
37  import com.RuntimeCollective.webapps.bean.User;
38  
39  import java.sql.SQLException;
40  import java.util.ArrayList;
41  import java.util.Date;
42  import java.util.List;
43  
44  /**
45   * This Extension can be used to collect creation/lastModified user/date on
46   * any EntityBean.
47   * <p>
48   * To set audit data, simply user markAsCreated and markAsEdited.
49   * <br>
50   * To view the data, getFor() the extension, and use its properties.
51   *
52   * @version $Id: AuditedExtension.java,v 1.5 2003/09/30 15:13:09 joe Exp $
53   */
54  public class AuditedExtension implements EntityBean {
55  
56      private static final String SELECT_ID = "select id from "; 
57      private static final String WHERE_BEAN_ID = " where bean_id = ";
58      private static final String DELETE_FROM = "delete from ";
59      private static final String WHERE_ID = " where id = ";
60      private static final String SELECT_DATA = "select bean_id, creation_user, creation_date, last_mod_user, last_mod_date from "; 
61      private static final String UPDATE = "update "; 
62  
63      private static final String ESC = "'";
64      private static final String EMPTY_STRING = "";
65      private static final String SPACE = " ";
66  
67      public static final String DATABASE_TABLE = "webapps_auditedext";
68      private static final String FIELD_BEAN_ID = "bean_id";
69      private static final String FIELD_CREATION_USER = "creation_user";
70      private static final String FIELD_CREATION_DATE = "creation_date";
71      private static final String FIELD_LAST_MODIFIED_USER = "last_mod_user";
72      private static final String FIELD_LAST_MODIFIED_DATE = "last_mod_date";
73  
74  
75      // -------------------- EntityBean implementation -------------------------
76      
77      /** This bean's id */
78      protected int id;    
79  
80      /**
81       * Set the unique id of this bean instance.
82       */
83      public void setId(int id) {
84          this.id = id;
85      }
86  
87      /**
88       * Get the unique id of this bean instance.
89       */
90      public int getId() {
91          return id;
92      }
93  
94      /**
95       * Save this bean in the database.
96       */
97      public void save() {
98  
99          try {
100 
101             // beanId shouldn't be null, really
102             Integer beanInt = (beanId == EntityBean.NULL_ID) ? null : new Integer(beanId);
103             Integer creationUserInt = (creationUserId == EntityBean.NULL_ID) ? null : new Integer(creationUserId);
104             Integer lastModifiedUserInt = (lastModifiedUserId == EntityBean.NULL_ID) ? null : new Integer(lastModifiedUserId);
105             RuntimeDataSource.save(id, DATABASE_TABLE,
106                                    new String[] { FIELD_BEAN_ID,
107                                                   FIELD_CREATION_USER,
108                                                   FIELD_CREATION_DATE,
109                                                   FIELD_LAST_MODIFIED_USER,
110                                                   FIELD_LAST_MODIFIED_DATE},
111                                    new Object[] { beanInt,
112                                                   creationUserInt,
113                                                   creationDate,
114                                                   lastModifiedUserInt,
115                                                   lastModifiedDate }
116                                    );
117             
118         } catch (SQLException e) {
119             RuntimeParameters.logError(this, "Could not save.", e);
120             e.printStackTrace();
121         }
122     }
123 
124     /**
125      * Delete this bean from the database.
126      */
127     public void delete() {
128         try {
129             RuntimeDataSource.update(DELETE_FROM+DATABASE_TABLE+WHERE_ID+id);
130 
131         } catch (SQLException e) {
132             RuntimeParameters.logError(this, "Could not delete.", e);
133             e.printStackTrace();
134         }
135     }
136 
137 
138     // -------------------- AuditedExtension specific -------------------------
139 
140     /**
141      * Constructs a new blank bean with a unique id.
142      */
143     public AuditedExtension() throws SQLException {
144         setId(RuntimeDataSource.nextId());
145     }
146 
147     /**
148      * Gets a bean from the RuntimeDataSource, given an id.
149      *
150      * @param id id of the Moderated bean
151      * @exception SQLException thrown if no bean with such an id exits
152      */    
153     public AuditedExtension(int id) throws SQLException {
154         Object[] results = RuntimeDataSource.queryRow(SELECT_DATA+DATABASE_TABLE+WHERE_ID+id);
155         if (results.length != 5) {
156             throw new SQLException("Cannot load AuditedExtension with id="+id+" : "
157                                    +results.length+" fields found in "+DATABASE_TABLE+".");
158         }
159 
160         this.id = id;
161 
162         if (results[0] != null)
163             beanId = Integer.parseInt(results[0].toString());
164         else
165             beanId = EntityBean.NULL_ID;
166 
167         if (results[1] != null)
168             creationUserId = Integer.parseInt(results[1].toString());
169         else
170             creationUserId = EntityBean.NULL_ID;
171 
172         if (results[2] != null)
173             creationDate = RuntimeDataSource.toDate(results[2].toString());
174         else
175             creationDate = null;
176 
177         if (results[3] != null)
178             lastModifiedUserId = Integer.parseInt(results[3].toString());
179         else
180             lastModifiedUserId = EntityBean.NULL_ID;
181 
182         if (results[4] != null)
183             lastModifiedDate = RuntimeDataSource.toDate(results[4].toString());
184         else
185             lastModifiedDate = null;
186 
187     }
188 
189 
190     /** The id of the EntityBean this AuditedExtension is for. */
191     protected int beanId = EntityBean.NULL_ID;
192 
193     /** Gets the bean. */
194     public EntityBean getEntityBean() {
195         return (EntityBean) RuntimeParameters.getStore().get(EntityBean.class.getName(), beanId);
196     }
197 
198     /** Sets the bean. */
199     public void setEntityBean(EntityBean bean) {
200         if (bean == null)
201             this.beanId = EntityBean.NULL_ID;
202         else
203             this.beanId = bean.getId();
204     }
205     
206 
207     /** The creationUser. */
208     protected int creationUserId = EntityBean.NULL_ID;
209 
210     /** Set the creationUser. */
211     public void setCreationUser(User creationUser) {
212         if (creationUser != null) {
213             creationUserId = creationUser.getId();
214         } else {
215             creationUserId = EntityBean.NULL_ID;
216         }
217     }
218 
219     /** Get the creationUser. */
220     public User getCreationUser() {
221         if (creationUserId != EntityBean.NULL_ID) {
222             return (User) RuntimeParameters.getStore().get(User.class.getName(), creationUserId);
223         } else {
224             return null;
225         }
226     }
227 
228     /** The creation date bean */
229     protected Date creationDate;
230 
231     /** Set the creation date. */
232     public void setCreationDate(Date date) {
233         creationDate = date;
234     }
235 
236     /** Get the creation date. */
237     public Date getCreationDate() {
238         return creationDate;
239     }
240 
241     /** The lastModifiedUser */
242     protected int lastModifiedUserId = EntityBean.NULL_ID;
243 
244     /** Set the lastModifiedUser. */
245     public void setLastModifiedUser(User user) {
246         if (user != null)
247             lastModifiedUserId = user.getId();
248         else
249             lastModifiedUserId = EntityBean.NULL_ID;
250     }
251 
252     /** Get the lastModifiedUser. */
253     public User getLastModifiedUser() {
254         if (lastModifiedUserId != EntityBean.NULL_ID)
255             return (User) RuntimeParameters.getStore().get(User.class.getName(), lastModifiedUserId);
256         else
257             return null;
258     }
259 
260     /** The last modified date. */
261     protected Date lastModifiedDate;
262 
263     /** Set the last modified date bean */
264     public void setLastModifiedDate(Date date) {
265         lastModifiedDate = date;
266     }
267 
268     /** Get the last modified date. */
269     public Date getLastModifiedDate() {
270         return lastModifiedDate;
271     }
272 
273 
274     public String toString() {
275         return AuditedExtension.class.getName() + " " + id + " ( " + super.toString() + " )";
276     }
277 
278 
279 
280     // -------------------- AuditedExtension statics -------------------------        
281     
282     /**
283      * Gets the AuditedExtension for a particular bean.
284      */
285     public static AuditedExtension getFor(EntityBean bean) {
286 
287         if (bean == null) {
288             return null;
289         }
290 
291         try {
292             int[] ids = RuntimeDataSource.queryInts(SELECT_ID+DATABASE_TABLE+WHERE_BEAN_ID+bean.getId());
293             if (ids.length > 0) {
294                 return ((AuditedExtension) RuntimeParameters.getStore().get(AuditedExtension.class.getName(), ids[0]));
295             }
296         } catch (SQLException e) {
297             RuntimeParameters.logError("AuditedExtension", "Could not getAllFor.", e);
298             e.printStackTrace();
299         }
300 
301         return null;
302     }
303 
304     /**
305      * Gets the AuditedExtension for a particular bean. Create (and save) it if necessary.
306      */
307     public static AuditedExtension getOrCreateFor(EntityBean bean) {
308 
309         if (bean == null) {
310             return null;
311         }
312 
313         AuditedExtension modExt = AuditedExtension.getFor(bean);
314         if (modExt != null) {
315             return modExt;
316         }
317 
318         // ok we'll need to create it
319         modExt = (AuditedExtension)RuntimeParameters.getStore().create(AuditedExtension.class.getName());
320         modExt.setEntityBean(bean);
321         
322         // not saving it - caller is responsible!
323         return modExt;
324     }
325     
326     /**
327      * Get or create an AuditedExtension for the bean, sets the Creation data,
328      * and save the extension. Assumes the bean has been saved at least once.
329      */
330     public static void markAsCreated(EntityBean bean, User user) {
331         markAsCreated(bean, user, true);
332     }
333 
334     /**
335      * Get an AuditedExtension for the bean, sets the Creation data,
336      * and save the extension. Assumes the bean has been saved at least once.
337      * If createIfNecessary == false and the bean has no AuditedExtension, nothing happens.
338      */
339     public static void markAsCreated(EntityBean bean, User user, boolean createIfNecessary) {
340         if (bean == null) {
341             return;
342         }
343 
344         AuditedExtension ext = getFor(bean);
345         if ((ext == null) && (createIfNecessary)) {
346             ext = getOrCreateFor(bean);
347         }
348         if (ext != null) {
349             ext.setCreationUser(user);
350             ext.setCreationDate(new Date());
351             RuntimeParameters.getStore().save(ext);
352         }
353     }
354 
355     /**
356      * Get or create an AuditedExtension for the bean, sets the LastModified data,
357      * and save the extension. Assumes the bean has been saved at least once.
358      */
359     public static void markAsEdited(EntityBean bean, User user) {
360         markAsEdited(bean, user, true);
361     }
362 
363     /**
364      * Get an AuditedExtension for the bean, sets the LastModified data,
365      * and save the extension. Assumes the bean has been saved at least once.
366      * If createIfNecessary == false and the bean has no AuditedExtension, nothing happens.
367      */
368     public static void markAsEdited(EntityBean bean, User user, boolean createIfNecessary) {
369         if (bean == null) {
370             return;
371         }
372 
373         AuditedExtension ext = getFor(bean);
374         if ((ext == null) && (createIfNecessary)) {
375             ext = getOrCreateFor(bean);
376         }
377         if (ext != null) {
378             ext.setLastModifiedUser(user);
379             ext.setLastModifiedDate(new Date());
380             RuntimeParameters.getStore().save(ext);
381         }
382     }
383 }
384 
385 
386