DATAJPA-68 - Refactored AuditingEntityListener.
Separated touch(…) methods for updating and creation as some persistence providers might hand in entities with IDs already assigned into a @PrePersist method and thus the isNew() check will fail.
This commit is contained in:
@@ -106,13 +106,31 @@ public class AuditingEntityListener<T> implements InitializingBean {
|
||||
|
||||
/**
|
||||
* Sets modification and creation date and auditor on the target object in
|
||||
* case it implements {@link Auditable}.
|
||||
* case it implements {@link Auditable} on persist events.
|
||||
*
|
||||
* @param target
|
||||
*/
|
||||
@PrePersist
|
||||
public void touchForCreate(Object target) {
|
||||
|
||||
touch(target, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets modification and creation date and auditor on the target object in
|
||||
* case it implements {@link Auditable} on update events.
|
||||
*
|
||||
* @param target
|
||||
*/
|
||||
@PreUpdate
|
||||
public void touch(Object target) {
|
||||
public void touchForUpdate(Object target) {
|
||||
|
||||
touch(target, false);
|
||||
}
|
||||
|
||||
|
||||
private void touch(Object target, boolean isNew) {
|
||||
|
||||
if (!(target instanceof Auditable)) {
|
||||
return;
|
||||
@@ -121,8 +139,8 @@ public class AuditingEntityListener<T> implements InitializingBean {
|
||||
@SuppressWarnings("unchecked")
|
||||
Auditable<T, ?> auditable = (Auditable<T, ?>) target;
|
||||
|
||||
T auditor = touchAuditor(auditable);
|
||||
DateTime now = dateTimeForNow ? touchDate(auditable) : null;
|
||||
T auditor = touchAuditor(auditable, isNew);
|
||||
DateTime now = dateTimeForNow ? touchDate(auditable, isNew) : null;
|
||||
|
||||
Object defaultedNow = now == null ? "not set" : now;
|
||||
Object defaultedAuditor = auditor == null ? "unknown" : auditor;
|
||||
@@ -139,7 +157,7 @@ public class AuditingEntityListener<T> implements InitializingBean {
|
||||
* @param auditable
|
||||
* @return
|
||||
*/
|
||||
private T touchAuditor(final Auditable<T, ?> auditable) {
|
||||
private T touchAuditor(final Auditable<T, ?> auditable, boolean isNew) {
|
||||
|
||||
if (null == auditorAware) {
|
||||
return null;
|
||||
@@ -147,7 +165,7 @@ public class AuditingEntityListener<T> implements InitializingBean {
|
||||
|
||||
T auditor = auditorAware.getCurrentAuditor();
|
||||
|
||||
if (auditable.isNew()) {
|
||||
if (isNew) {
|
||||
|
||||
auditable.setCreatedBy(auditor);
|
||||
|
||||
@@ -169,11 +187,11 @@ public class AuditingEntityListener<T> implements InitializingBean {
|
||||
* @param auditable
|
||||
* @return
|
||||
*/
|
||||
private DateTime touchDate(final Auditable<T, ?> auditable) {
|
||||
private DateTime touchDate(final Auditable<T, ?> auditable, boolean isNew) {
|
||||
|
||||
DateTime now = new DateTime();
|
||||
|
||||
if (auditable.isNew()) {
|
||||
if (isNew) {
|
||||
auditable.setCreatedDate(now);
|
||||
|
||||
if (!modifyOnCreation) {
|
||||
|
||||
@@ -60,7 +60,7 @@ public class AuditingEntityListenerUnitTests {
|
||||
@Test
|
||||
public void doesNotSetAuditorIfNotConfigured() {
|
||||
|
||||
listener.touch(user);
|
||||
listener.touchForCreate(user);
|
||||
|
||||
assertNotNull(user.getCreatedDate());
|
||||
assertNotNull(user.getLastModifiedDate());
|
||||
@@ -79,7 +79,7 @@ public class AuditingEntityListenerUnitTests {
|
||||
|
||||
listener.setAuditorAware(auditorAware);
|
||||
|
||||
listener.touch(user);
|
||||
listener.touchForCreate(user);
|
||||
|
||||
assertNotNull(user.getCreatedDate());
|
||||
assertNotNull(user.getLastModifiedDate());
|
||||
@@ -100,7 +100,7 @@ public class AuditingEntityListenerUnitTests {
|
||||
|
||||
listener.setAuditorAware(auditorAware);
|
||||
listener.setModifyOnCreation(false);
|
||||
listener.touch(user);
|
||||
listener.touchForCreate(user);
|
||||
|
||||
assertNotNull(user.getCreatedDate());
|
||||
assertNotNull(user.getCreatedBy());
|
||||
@@ -122,7 +122,7 @@ public class AuditingEntityListenerUnitTests {
|
||||
user = new AuditableUser(1L);
|
||||
|
||||
listener.setAuditorAware(auditorAware);
|
||||
listener.touch(user);
|
||||
listener.touchForUpdate(user);
|
||||
|
||||
assertNull(user.getCreatedBy());
|
||||
assertNull(user.getCreatedDate());
|
||||
@@ -139,7 +139,7 @@ public class AuditingEntityListenerUnitTests {
|
||||
|
||||
listener.setDateTimeForNow(false);
|
||||
listener.setAuditorAware(auditorAware);
|
||||
listener.touch(user);
|
||||
listener.touchForCreate(user);
|
||||
|
||||
assertNotNull(user.getCreatedBy());
|
||||
assertNull(user.getCreatedDate());
|
||||
|
||||
Reference in New Issue
Block a user