View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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 }