1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.model.dao;
17
18 import java.io.Serializable;
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.Collection;
21
22 import net.sf.oness.common.model.auditing.Auditable;
23 import net.sf.oness.common.model.bo.AuditableBusinessObject;
24 import net.sf.oness.common.model.security.SecurityHelper;
25 import net.sf.oness.common.model.temporal.DateRange;
26 import net.sf.oness.common.model.util.CollectionCloneConverter;
27
28 import org.apache.commons.beanutils.BeanUtils;
29 import org.apache.commons.beanutils.ConvertUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.springframework.dao.InvalidDataAccessApiUsageException;
33 import org.springframework.dao.OptimisticLockingFailureException;
34
35 /***
36 * Class with helper methods to implement an auditing dao
37 *
38 * @author Carlos Sanchez
39 * @version $Revision: 1.14 $
40 */
41 public class AuditingDaoHelper {
42
43 private static Log log = LogFactory.getLog(AuditingDaoHelper.class);
44
45 static {
46 ConvertUtils.register(new CollectionCloneConverter(), Collection.class);
47 }
48
49 /***
50 * Set creation date, code and createdBy before saving
51 *
52 * @see net.sf.oness.common.model.dao.AuditableDao#create(net.sf.oness.common.model.bo.Auditable)
53 */
54 public static AuditableBusinessObject create(AuditableDao dao,
55 AuditableBusinessObject value) {
56 value.setTransactionTime(DateRange.startingNow());
57 value.setCreatedBy(SecurityHelper.getUserName());
58 AuditableBusinessObject newValue = dao.create(value);
59 newValue.setCode(new Long(newValue.getId().longValue()));
60 dao.update(newValue);
61 return newValue;
62 }
63
64 /***
65 * Save a new object with createdBy and curent date as creation date. Update
66 * deletion date and deletedBy in the old value.
67 *
68 * @return the value returned by {@link AuditableDao#create(Auditable)}
69 * @throws OptimisticLockingFailureException
70 * if the value has been already updated or deleted
71 *
72 * @see net.sf.oness.common.model.dao.AuditableDao#update(net.sf.oness.common.model.bo.Auditable)
73 */
74 public static AuditableBusinessObject update(AuditableDao dao,
75 AuditableBusinessObject value) {
76 Serializable id = value.getId();
77
78 AuditableBusinessObject oldValue = dao.findById(id);
79 DateRange range = oldValue.getTransactionTime();
80 if (!range.isOpen())
81 throw new OptimisticLockingFailureException("Value with id " + id
82 + " has been already updated or deleted");
83 range.endNow();
84 oldValue.setDeletedBy(SecurityHelper.getUserName());
85 dao.update(oldValue);
86
87 AuditableBusinessObject newValue = null;
88 try {
89 newValue = (AuditableBusinessObject) BeanUtils.cloneBean(value);
90 } catch (IllegalAccessException e) {
91 throw new InvalidDataAccessApiUsageException(e.getMessage(), e);
92 } catch (InvocationTargetException e) {
93 throw new InvalidDataAccessApiUsageException(e.getMessage(), e);
94 } catch (InstantiationException e) {
95 throw new InvalidDataAccessApiUsageException(e.getMessage(), e);
96 } catch (NoSuchMethodException e) {
97 throw new InvalidDataAccessApiUsageException(e.getMessage(), e);
98 }
99 newValue.setCode(new Long(oldValue.getCode().longValue()));
100 newValue.setTransactionTime(DateRange.startingOn(range.getEnd()));
101 newValue.setCreatedBy(SecurityHelper.getUserName());
102 newValue.setDeletedBy(null);
103 return dao.create(newValue);
104 }
105
106 /***
107 * Set deletion date and deletedBy
108 *
109 * @throws org.springframework.dao.OptimisticLockingFailureException
110 * if the value has been already updated or deleted
111 * @see net.sf.oness.common.model.dao.AuditableDao#delete(java.io.Serializable)
112 */
113 public static void delete(AuditableDao dao, Serializable id) {
114 AuditableBusinessObject oldValue = dao.findById(id);
115
116 DateRange range = oldValue.getTransactionTime();
117 if (!range.isOpen())
118 throw new OptimisticLockingFailureException("Value with id " + id
119 + " has been already updated or deleted");
120
121 range.endNow();
122 oldValue.setDeletedBy(SecurityHelper.getUserName());
123 dao.update(oldValue);
124 }
125
126 }