Skip to content

Commit

Permalink
chore: split audit
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Oct 11, 2024
1 parent 213bf85 commit 810224b
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 76 deletions.
3 changes: 3 additions & 0 deletions auditing-event-listener/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Auditing

> This module just define
5 changes: 5 additions & 0 deletions auditing-event-listener/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies {
api project(":auditing")
api "org.hibernate:hibernate-core:${hibernateVersion}"
api "jakarta.enterprise:jakarta.enterprise.cdi-api:${jakartaCdiApiVersion}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.qwlabs.auditing;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.hibernate.event.spi.PreInsertEvent;
import org.hibernate.event.spi.PreInsertEventListener;
import org.hibernate.event.spi.PreUpdateEvent;
import org.hibernate.event.spi.PreUpdateEventListener;
import org.hibernate.event.spi.PreUpsertEvent;
import org.hibernate.event.spi.PreUpsertEventListener;

import java.time.Clock;
import java.time.OffsetDateTime;
import java.util.Objects;

@ApplicationScoped
public class AuditedEventListener implements PreUpdateEventListener, PreUpsertEventListener, PreInsertEventListener {
private final AuditorIdResolver auditorIdResolver;

@Inject
public AuditedEventListener(Instance<AuditorIdResolver> auditorIdResolvers) {
this.auditorIdResolver = DefaultAuditorIdResolver.orDefault(auditorIdResolvers);
}

@Override
public boolean onPreUpsert(PreUpsertEvent preUpsertEvent) {
handle(preUpsertEvent.getEntity(), true);
return false;
}

@Override
public boolean onPreInsert(PreInsertEvent preInsertEvent) {
handle(preInsertEvent.getEntity(), false);
return false;
}

@Override
public boolean onPreUpdate(PreUpdateEvent preUpdateEvent) {
handle(preUpdateEvent.getEntity(), true);
return false;
}

private void handle(Object entity, boolean force) {
if (!(entity instanceof AuditedEntity audited)) {
return;
}
if (force || Objects.isNull(audited.getUpdatedAt())) {
audited.setUpdatedAt(OffsetDateTime.now(Clock.systemUTC()));
}
if (force || Objects.isNull(audited.getUpdatedBy())) {
audited.setUpdatedBy(auditorIdResolver.resolve(entity).orElse(null));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.qwlabs.auditing;

import java.util.Optional;

public interface AuditorIdResolver {
Optional<String> resolve(Object entity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.qwlabs.auditing;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.hibernate.event.spi.PreInsertEvent;
import org.hibernate.event.spi.PreInsertEventListener;
import org.hibernate.event.spi.PreUpsertEvent;
import org.hibernate.event.spi.PreUpsertEventListener;

import java.time.Clock;
import java.time.OffsetDateTime;
import java.util.Optional;

@ApplicationScoped
public class CreatedAuditedEventListener implements PreUpsertEventListener, PreInsertEventListener {
private final AuditorIdResolver auditorIdResolver;

@Inject
public CreatedAuditedEventListener(Instance<AuditorIdResolver> auditorIdResolvers) {
this.auditorIdResolver = DefaultAuditorIdResolver.orDefault(auditorIdResolvers);
}

@Override
public boolean onPreUpsert(PreUpsertEvent preUpsertEvent) {
handle(preUpsertEvent.getEntity());
return false;
}

@Override
public boolean onPreInsert(PreInsertEvent preInsertEvent) {
handle(preInsertEvent.getEntity());
return false;
}

private void handle(Object entity) {
if (!(entity instanceof CreatedAuditedEntity created)) {
return;
}
created.setCreatedAt(Optional.ofNullable(created.getCreatedAt())
.orElseGet(() -> OffsetDateTime.now(Clock.systemUTC())));
created.setCreatedBy(Optional.ofNullable(created.getCreatedBy())
.orElseGet(() -> auditorIdResolver.resolve(created).orElse(null)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.qwlabs.auditing;

import jakarta.enterprise.inject.Instance;

import java.util.Optional;

public class DefaultAuditorIdResolver implements AuditorIdResolver {
private static final AuditorIdResolver INSTANCE = new DefaultAuditorIdResolver();

@Override
public Optional<String> resolve(Object entity) {
return Optional.empty();
}

protected static AuditorIdResolver orDefault(Instance<AuditorIdResolver> auditorIdResolvers) {
return auditorIdResolvers.stream().findFirst().orElse(INSTANCE);
}
}
5 changes: 5 additions & 0 deletions auditing-event-listener/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd"
bean-discovery-mode="all">
</beans>
Empty file.
Empty file.
1 change: 0 additions & 1 deletion auditing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ apply from: "${rootProject.rootDir}/gradle/hibernate.gradle"

dependencies {
api "jakarta.persistence:jakarta.persistence-api:${jakartaPersistenceApiVersion}"
api "jakarta.enterprise:jakarta.enterprise.cdi-api:${jakartaCdiApiVersion}"
}
2 changes: 0 additions & 2 deletions auditing/src/main/java/com/qwlabs/auditing/AuditedEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.qwlabs.auditing;

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -10,7 +9,6 @@
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingListener.class)
public class AuditedEntity extends CreatedAuditedEntity {
private OffsetDateTime updatedAt;
private String updatedBy;
Expand Down
63 changes: 0 additions & 63 deletions auditing/src/main/java/com/qwlabs/auditing/AuditingListener.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.qwlabs.auditing;

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -10,7 +9,6 @@
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingListener.class)
public class CreatedAuditedEntity {
private OffsetDateTime createdAt;
private String createdBy;
Expand Down
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ pluginManagement {
rootProject.name = 'doraemon'

include 'lang'
include 'auditing'
include 'auditing-event-listener'
include 'basic-tree'
include 'mptt-tree'
include 'exceptions'
include 'panache'
include 'jakarta-json'
include 'jakarta-data'
include 'jackson'
include 'auditing'

include 'validator'
include 'excel'
include 'jpa'
Expand Down

0 comments on commit 810224b

Please sign in to comment.