Adjust how entity draft auditing works.

This commit is contained in:
Greg Burd 2017-09-12 15:51:49 -04:00
parent 58b29ad181
commit 83ef8d7b0c
5 changed files with 32 additions and 17 deletions

View file

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.helenus</groupId>
<artifactId>helenus-core</artifactId>
<version>2.0.27-SNAPSHOT</version>
<version>2.0.28-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helenus</name>

View file

@ -9,23 +9,31 @@ import java.util.Date;
public abstract class AbstractAuditedEntityDraft<E> extends AbstractEntityDraft<E> {
public AbstractAuditedEntityDraft(MapExportable entity, AuditProvider auditProvider) {
public AbstractAuditedEntityDraft(MapExportable entity) {
super(entity);
Date in = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());
Date now = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
String who = auditProvider == null ? "unknown" : auditProvider.operatorName();
String who = getCurrentAuditor();
if (entity == null) {
set("createdBy", who);
if (who != null) {
set("createdBy", who);
}
set("createdAt", now);
}
set("modifiedBy", who);
if (who != null) {
set("modifiedBy", who);
}
set("modifiedAt", now);
}
protected String getCurrentAuditor() {
return null;
}
public Date createdAt() {
return (Date) get("createdAt", Date.class);
}

View file

@ -1,10 +0,0 @@
package net.helenus.core;
public interface AuditProvider {
/**
* What to record in the database row as the name of the agent causing the mutation.
* @return a string name that indicates the identity of the operator mutating the data at this time.
*/
public String operatorName();
}

View file

@ -576,4 +576,19 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
+ p.getEntity().getMappingInterface());
}
}
@Override
public E sync(UnitOfWork uow) {
E result = super.sync(uow);
if (draft != null) {
String key = getStatementCacheKey();
if (key != null) {
Set<Object> set = new HashSet<Object>(1);
set.add(result);
uow.getCache().put(key, set);
}
}
return result;
}
}

View file

@ -24,18 +24,20 @@ public interface Inventory {
// Entity/Draft pattern-enabling methods:
Draft(UUID id) {
super(null, null);
super(null);
// Primary Key:
set("id", id);
}
Draft(Inventory inventory) {
super((MapExportable) inventory, null);
super((MapExportable) inventory);
}
public Class<Inventory> getEntityClass() { return Inventory.class; }
protected String getCurrentAuditor() { return "unknown"; }
// Immutable properties:
public UUID id() {
return this.<UUID>get("id", UUID.class);