Package org.omnifaces.persistence.audit
Class AuditListener<I extends Comparable<I> & Serializable>
- java.lang.Object
-
- org.omnifaces.persistence.audit.AuditListener<I>
-
- Type Parameters:
I- The generic ID type. This is used to associate auditable properties with a specific entity.
public abstract class AuditListener<I extends Comparable<I> & Serializable> extends Object
See
Auditfor usage instructions.- Author:
- Bauke Scholtz
- See Also:
Audit
-
-
Constructor Summary
Constructors Constructor Description AuditListener()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidafterUpdate(BaseEntity<I> entity)voidbeforeUpdate(BaseEntity<I> entity)protected static <T> Tinject(Class<T> type)Work around for CDI inject not working in JPA EntityListener.protected abstract voidsaveAuditedChange(BaseEntity<I> entity, PropertyDescriptor property, Object oldValue, Object newValue)Example implementation:
-
-
-
Method Detail
-
beforeUpdate
public void beforeUpdate(BaseEntity<I> entity)
-
afterUpdate
public void afterUpdate(BaseEntity<I> entity)
-
saveAuditedChange
protected abstract void saveAuditedChange(BaseEntity<I> entity, PropertyDescriptor property, Object oldValue, Object newValue)
Example implementation:
YourAuditedChange yourAuditedChange = new YourAuditedChange(); yourAuditedChange.setTimestamp(Instant.now()); yourAuditedChange.setUser(activeUser); yourAuditedChange.setEntityName(entityManager.getMetamodel().entity(entity.getClass()).getName()); yourAuditedChange.setEntityId(entity.getId()); yourAuditedChange.setPropertyName(property.getName()); yourAuditedChange.setOldValue(oldValue != null ? oldValue.toString() : null); yourAuditedChange.setNewValue(newValue != null ? newValue.toString() : null); inject(YourAuditedChangeService.class).persist(yourAuditedChange);
- Parameters:
entity- The parent entity.property- The audited property.oldValue- The old value.newValue- The new value.
-
inject
protected static <T> T inject(Class<T> type)
Work around for CDI inject not working in JPA EntityListener. Usage:
YourAuditedChangeService service = inject(YourAuditedChangeService.class);
- Type Parameters:
T- The generic CDI managed bean type.- Parameters:
type- The CDI managed bean type.- Returns:
- The CDI managed instance.
-
-