Class AuditListener<I extends Comparable<I> & Serializable>

  • 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 Audit for usage instructions.

    Author:
    Bauke Scholtz
    See Also:
    Audit
    • Constructor Detail

      • AuditListener

        public AuditListener()
    • 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.