-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pipinet
committed
Oct 11, 2024
1 parent
213bf85
commit 810224b
Showing
15 changed files
with
143 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Auditing | ||
|
||
> This module just define |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
56 changes: 56 additions & 0 deletions
56
auditing-event-listener/src/main/java/com/qwlabs/auditing/AuditedEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
auditing-event-listener/src/main/java/com/qwlabs/auditing/AuditorIdResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
46 changes: 46 additions & 0 deletions
46
auditing-event-listener/src/main/java/com/qwlabs/auditing/CreatedAuditedEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
auditing-event-listener/src/main/java/com/qwlabs/auditing/DefaultAuditorIdResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
5
auditing-event-listener/src/main/resources/META-INF/beans.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
auditing/src/main/java/com/qwlabs/auditing/AuditingListener.java
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
auditing/src/main/java/com/qwlabs/auditing/AuditorIdProvider.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters