All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.genesys.blocks.auditlog.component.AuditTrailInterceptor Maven / Gradle / Ivy

/*
 * Copyright 2018 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.blocks.auditlog.component;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.Format;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.auditlog.annotations.HideAuditValue;
import org.genesys.blocks.auditlog.annotations.NotAudited;
import org.genesys.blocks.auditlog.model.AuditAction;
import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.model.TransactionAuditLog;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.EntityId;
import org.hibernate.CallbackException;
import org.hibernate.EmptyInterceptor;
import org.hibernate.Transaction;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.resource.transaction.spi.TransactionStatus;
import org.hibernate.type.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ResolvableType;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

/**
 * Record changed data using {@link AuditLog} entries.
 *
 * @author Matija Obreza
 */
@Component
public class AuditTrailInterceptor extends EmptyInterceptor implements InitializingBean {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1881637304461659508L;

	/** The Constant LOG. */
	private static final Logger LOG = LoggerFactory.getLogger(AuditTrailInterceptor.class);

	/** The Constant DEFAULT_IGNORED_PROPERTIES. */
	private static final Set DEFAULT_IGNORED_PROPERTIES = Stream.of("serialVersionUID", "id", "createdDate", "lastModifiedDate", "version", "lastModifiedBy")
		.collect(Collectors.toSet());

	/** The ignored properties. */
	private Set ignoredProperties = new HashSet<>(DEFAULT_IGNORED_PROPERTIES);

	/** The ignored properties of audited entities. */
	private final Map, Set> ignoredClassFields;

	/** The secured properties of audited entities. */
	private final Map, Set> securedClassFields;

	/** The audited classes. */
	private Set> auditedClasses = new HashSet<>();

	/** The included classes. */
	// Two caches
	private final Set> ignoredClasses, includedClasses;

	/** The audit trail service. */
	@Autowired
	private AuditTrailService auditTrailService;

	/** The entity manager. */
	@PersistenceContext
	private EntityManager entityManager;

	/** The date format. */
	private final String dateFormat = "dd-MMM-yyyy";

	/** The time format. */
	private final String timeFormat = "HH:mm:ss";

	/** The date time format. */
	private final String dateTimeFormat = "dd-MMM-yyyy HH:mm:ss";

	/** The date formatter. */
	private Format dateFormatter;

	/** The date time formatter. */
	private Format dateTimeFormatter;

	/** The time formatter. */
	private Format timeFormatter;


	/** Place to store audit logs before storing them to db */
	private static final ThreadLocal>> auditLogStack = new ThreadLocal>>() {
		@Override
		protected Stack> initialValue() {
			return new Stack>();
		};
	};

	/**
	 * Instantiates a new audit trail interceptor.
	 */
	public AuditTrailInterceptor() {
		LOG.info("Enabling {}", getClass().getName());
		// make synchronized local caches
		ignoredClasses = Collections.synchronizedSet(new HashSet<>());
		includedClasses = Collections.synchronizedSet(new HashSet<>());
		ignoredClassFields = Collections.synchronizedMap(new HashMap<>());
		securedClassFields = Collections.synchronizedMap(new HashMap<>());
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		assert ignoredProperties != null;
		assert auditTrailService != null;

		// Make them unmodifiable
		ignoredProperties = Collections.unmodifiableSet(ignoredProperties);
		auditedClasses = Collections.unmodifiableSet(auditedClasses);

		dateFormatter = FastDateFormat.getInstance(dateFormat);
		dateTimeFormatter = FastDateFormat.getInstance(dateTimeFormat);
		timeFormatter = FastDateFormat.getInstance(timeFormat);
	}

	/**
	 * Explicitly set the list of classes that should be audited. Note that any
	 * class with {@link Audited} annotation will be included, even if not on this
	 * list.
	 *
	 * @param auditedClasses entity classes to audit
	 * @see Audited
	 */
	public void setAuditedClasses(final Set> auditedClasses) {
		this.auditedClasses = auditedClasses;
	}

	/**
	 * Gets the audited classes.
	 *
	 * @return the audited classes
	 */
	public Set> getAuditedClasses() {
		return auditedClasses;
	}

	/**
	 * Set the list of properties to ignore on all entities (e.g. "password").
	 * Defaults to {@link #DEFAULT_IGNORED_PROPERTIES}. Note that you can explicitly
	 * exclude fields by annotating them with @NotAudited annotation
	 * (see {@link NotAudited}).
	 *
	 * @param ignoredProperties entity property names to exclude from audit trail
	 * @see NotAudited
	 */
	public void setIgnoredProperties(final Set ignoredProperties) {
		this.ignoredProperties = ignoredProperties;
	}

	/**
	 * Gets the ignored properties.
	 *
	 * @return the ignored properties
	 */
	public Set getIgnoredProperties() {
		return ignoredProperties;
	}

	/**
	 * Sets the audit trail service.
	 *
	 * @param auditTrailService the new audit trail service
	 */
	public void setAuditTrailService(final AuditTrailService auditTrailService) {
		this.auditTrailService = auditTrailService;
	}

	/**
	 * Gets the audit trail service.
	 *
	 * @return the audit trail service
	 */
	public AuditTrailService getAuditTrailService() {
		return auditTrailService;
	}

	/*
	 * (non-Javadoc)
	 * @see org.hibernate.EmptyInterceptor#onFlushDirty(java.lang.Object,
	 * java.io.Serializable, java.lang.Object[], java.lang.Object[],
	 * java.lang.String[], org.hibernate.type.Type[])
	 */
	/* We add more stuff to the transaction if that fails we're still good! */
	@Override
	public boolean onFlushDirty(final Object entity, final Serializable id, final Object[] currentState, final Object[] previousState, final String[] propertyNames,
			final Type[] types) {
		final Class entityClass = entity.getClass();
		if (LOG.isTraceEnabled()) {
			LOG.trace("Inspecting Entity.class={} id={}", entityClass, id);
		}

		if (!isAudited(entityClass)) {
			return false;
		}

		final Set entityIgnoredFields = ignoredClassFields.get(entityClass);

		// Identify changed values
		for (int i = 0; i < previousState.length; i++) {
			final String propertyName = propertyNames[i];
			final Object prev = previousState[i];
			final Object curr = currentState[i];

			if (ignoredProperties.contains(propertyName) || (entityIgnoredFields != null && entityIgnoredFields.contains(propertyName))) {
				LOG.trace("{} property in {} is not audited.", propertyName, entityClass.getSimpleName());
				continue;
			}

			if (((prev != null) && !prev.equals(curr)) || ((prev == null) && (curr != null))) {
				LOG.trace("prop={} prev={} curr={} type={}", propertyName, prev, curr, types[i].getReturnedClass());

				if (isPrimitiveType(types[i].getReturnedClass())) {
					final String currentValue = formatValue(curr, types[i], entityClass, propertyName);
					final String previousValue = formatValue(prev, types[i], entityClass, propertyName);
					// Notice cast to Long here!
					recordChange(entity, (Long) id, propertyName, previousValue, currentValue, null, prev, curr);

				} else if (isEntity(types[i].getReturnedClass())) {
					final EntityId prevEntity = (EntityId) prev, currEntity = (EntityId) curr;
					final String previousValue = prevEntity == null ? null : prevEntity.getId().toString();
					final String currentValue = currEntity == null ? null : currEntity.getId().toString();

					if (!StringUtils.equals(previousValue, currentValue)) {
						// Notice cast to Long here!
						recordChange(entity, (Long) id, propertyName, previousValue, currentValue, types[i].getReturnedClass(), prev, curr);
					}
				} else {
					LOG.trace("Entity.{} {} is not a primitive. Ignoring value={}", propertyName, prev == null ? null : prev.getClass(), prev);
					// TODO Capture in audit log
				}
			}
		}
		return false;
	}

	/**
	 * Format value.
	 *
	 * @param someValue the some value
	 * @param type the type
	 * @param entityClass the entity class
	 * @param propertyName the property name
	 * @return the string
	 */
	private String formatValue(final Object someValue, final Type type, final Class entityClass, final String propertyName) {
		if (someValue == null) {
			return null;
		}

		// Check if field should be masked
		final Set securedFields = securedClassFields.get(entityClass);
		if (securedFields != null && securedFields.contains(propertyName)) {
			return AuditLog.FIELD_VALUE_NOT_AUDITED;
		}

		final Class returnedClass = type.getReturnedClass();

		if (Instant.class.equals(returnedClass)) {
			return DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.systemDefault()).format((Instant)someValue);
		} else if (LocalDate.class.equals(returnedClass)) {
			return DateTimeFormatter.ISO_DATE.format((LocalDate)someValue);
		} else if (Date.class.equals(returnedClass) || Calendar.class.equals(returnedClass)) {
			TemporalType temporalType = TemporalType.TIMESTAMP;
			try {
				final Field field = entityClass.getDeclaredField(propertyName);
				if ((field != null) && field.isAnnotationPresent(Temporal.class)) {
					final Temporal ta = field.getAnnotation(Temporal.class);
					temporalType = ta.value();
				}
			} catch (NoSuchFieldException | SecurityException e) {
				LOG.trace("Could not access field {}#{}", entityClass, propertyName);
			}

			switch (temporalType) {
				case TIMESTAMP:
					return dateTimeFormatter.format(someValue);
				case DATE:
					return dateFormatter.format(someValue);
				case TIME:
					return timeFormatter.format(someValue);
			}
		}

		return someValue.toString();
	}

	/**
	 * Checks if is entity.
	 *
	 * @param clazz the clazz
	 * @return true, if is entity
	 */
	private boolean isEntity(final Class clazz) {
		if (EntityId.class.isAssignableFrom(clazz)) {
			return true;
		}
		LOG.trace("{} is not an EntityId", clazz.getName());
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see org.hibernate.EmptyInterceptor#onDelete(java.lang.Object,
	 * java.io.Serializable, java.lang.Object[], java.lang.String[],
	 * org.hibernate.type.Type[])
	 */
	@Override
	public void onDelete(final Object entity, final Serializable id, final Object[] states, final String[] propertyNames, final Type[] types) {
		final Class entityClass = entity.getClass();
		if (LOG.isTraceEnabled()) {
			LOG.trace("Inspecting Entity.class={} id={}", entityClass, id);
		}

		if (!isAudited(entityClass)) {
			LOG.trace("{} is not audited", entityClass);
			return;
		}

		final Set entityIgnoredFields = ignoredClassFields.get(entityClass);

		for (int i = 0; i < states.length; i++) {
			final String propertyName = propertyNames[i];
			final Object state = states[i];

			if (ignoredProperties.contains(propertyName) || (entityIgnoredFields != null && entityIgnoredFields.contains(propertyName))) {
				continue;
			}

			if (state != null) {
				LOG.trace("Deleted prop={} state={} type={}", propertyName, state, types[i].getReturnedClass());

				if (isPrimitiveType(types[i].getReturnedClass())) {
					// Notice cast to Long here!
					recordDelete(entity, (Long) id, propertyName, state.toString(), null, null);
				} else if (isEntity(types[i].getReturnedClass())) {
					final EntityId prevEntity = (EntityId) state;
					final String previousValue = prevEntity == null ? null : prevEntity.getId().toString();
					// Notice cast to Long here!
					recordDelete(entity, (Long) id, propertyName, previousValue, types[i].getReturnedClass(), state);
				} else {
					LOG.trace("Entity.{} {} is not a primitive. Ignoring value={}", propertyName, state == null ? null : state.getClass(), state);
					// TODO Capture in audit log
					// PersistentBag
				}
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.hibernate.EmptyInterceptor#onCollectionRecreate(java.lang.Object,
	 * java.io.Serializable)
	 */
	@Override
	public void onCollectionRecreate(final Object collection, final Serializable key) throws CallbackException {
		LOG.trace("Collection recreated: key={} coll={}", key, collection);
	}

	/*
	 * (non-Javadoc)
	 * @see org.hibernate.EmptyInterceptor#onCollectionRemove(java.lang.Object,
	 * java.io.Serializable)
	 */
	@Override
	public void onCollectionRemove(final Object collection, final Serializable key) throws CallbackException {
		final PersistentCollection pc = (PersistentCollection) collection;
		if (!isAudited(pc.getOwner().getClass())) {
			LOG.trace("Class {} is not audited", pc.getOwner().getClass());
			return;
		}

		final Class ownerClass = pc.getOwner().getClass();
		final String propertyName = pc.getRole().substring(pc.getRole().lastIndexOf('.') + 1);
		final Class propertyType = findPropertyType(ownerClass, propertyName);
		LOG.trace("Property class: {}.{}={}", ownerClass.getName(), propertyName, propertyType);

		Collection deleted = new HashSet<>();

		if (pc.getValue() == null) {
			LOG.trace("onCollectionRemove is empty, no change", key);
			return;
		}

		if (pc.getValue() instanceof Collection) {
			deleted.addAll((Collection) pc.getValue());
		}

		if (deleted.size() == 0) {
			LOG.trace("onCollectionRemove is empty, no change", key);
			return;
		}

		Serializable snap = pc.getStoredSnapshot();
		LOG.trace("Collection remove: key={} coll={} snap={}", key, collection, snap);

		// If remaining is EntityId, convert to ID only
		Class referencedEntity = null;
		if (EntityId.class.isAssignableFrom(propertyType)) {
			LOG.trace("{} is EntityId, converting values.", propertyType.getName());
			referencedEntity = propertyType;
			deleted = convertEntityId(deleted);
		}

		LOG.trace("prev={} curr=null", deleted);
		recordDelete(pc.getOwner(), (Long) key, propertyName, deleted.toString(), referencedEntity, deleted);
	}

	/*
	 * (non-Javadoc)
	 * @see org.hibernate.EmptyInterceptor#onCollectionUpdate(java.lang.Object,
	 * java.io.Serializable)
	 */
	@Override
	public void onCollectionUpdate(final Object collection, final Serializable key) throws CallbackException {
		final PersistentCollection pc = (PersistentCollection) collection;
		if (!isAudited(pc.getOwner().getClass())) {
			LOG.trace("Class {} is not audited", pc.getOwner().getClass());
			return;
		}
		// if (pc.empty()) {
		// LOG.trace("onCollectionUpdate is empty, no change", key);
		// return;
		// }
		LOG.trace("Collection update: key={} coll={}", key, collection);

		final Class ownerClass = pc.getOwner().getClass();
		LOG.trace("ownerClass={} role={}", ownerClass.getName(), pc.getRole());
		final String propertyName = pc.getRole().substring(pc.getRole().lastIndexOf('.') + 1);
		final Class propertyType = findPropertyType(ownerClass, propertyName);
		LOG.trace("Property class: {}.{}={}", ownerClass.getName(), propertyName, propertyType);

		Collection remaining = new HashSet<>();

		if (pc.getValue() instanceof Collection) {
			remaining.addAll((Collection) pc.getValue());
		} else {
			LOG.trace("Can't handle pc={} val={}", pc.getValue().getClass(), pc);
		}

		Collection previous = null;
		final Serializable snap = pc.getStoredSnapshot();
		if (snap instanceof Map) {
			// LOG.trace("Snap keys: {}", ((Map) snap).keySet());
			// LOG.trace("Snap vals: {}", ((Map) snap).values());
			final Map snapMap = (Map) snap;
			previous = snapMap.keySet();
		} else if (snap instanceof Collection) {
			final Collection snapList = (Collection) snap;
			previous = snapList;
		} else {
			LOG.trace("Can't handle snap={} val={}", snap.getClass(), snap);
		}

		// If remaining is EntityId, convert to ID only
		Class referencedEntity = null;
		if (EntityId.class.isAssignableFrom(propertyType)) {
			LOG.trace("{} is EntityId, converting values.", propertyType.getName());
			referencedEntity = propertyType;
			previous = convertEntityId(previous);
			remaining = convertEntityId(remaining);
		}

		LOG.trace("prev={} curr={}", previous, remaining);
		recordChange(pc.getOwner(), (Long) key, propertyName, collectionToStringSorted(previous), collectionToStringSorted(remaining), referencedEntity, previous, remaining);
	}


	/**
	 * Produce a string representation of the collection. Set elements are sorted,
	 * other collection types return `#toString`.
	 *
	 * @param collection the collection
	 * @return the string
	 */
	private String collectionToStringSorted(Collection collection) {
		if (collection == null) {
			return null;
		}
		if (collection instanceof Set) {
			LOG.trace("Converting to sorted list {} -> {}", collection, collection.stream().sorted().map(Object::toString).collect(Collectors.joining(", ", "[", "]")));
			return collection.stream().sorted().map(Object::toString).collect(Collectors.joining(", ", "[", "]"));
		} else {
			LOG.trace("Not sorting {}: {}", collection.getClass(), collection);
		}
		return collection.toString();
	}

	/**
	 * Find property type.
	 *
	 * @param class1 the class 1
	 * @param propertyName the property name
	 * @return the class
	 */
	private Class findPropertyType(final Class class1, final String propertyName) {
		LOG.trace("Finding property type for {}.{}", class1.getName(), propertyName);

		// Field
		final Field field = ReflectionUtils.findField(class1, propertyName);
		if (field != null) {
			LOG.trace("Found field: {}\n\ttype={}\n\tgeneric={}\n\tgenericTN={}", field, field.getType(), field.getGenericType(), field.getGenericType().getTypeName());
			final ResolvableType t = ResolvableType.forField(field, class1);
			if (t.hasGenerics()) {
				LOG.trace("\tResoved={} returning={}", t.toString(), t.resolveGeneric(0));
				return t.resolveGeneric(0);
			} else {
				LOG.trace("Returning class itself={}", t.getRawClass());
				return t.getRawClass();
			}
		}

		// Getter
		try {
			final Method method = class1.getMethod("get" + StringUtils.capitalize(propertyName));
			if (method != null) {
				LOG.trace("Didn't find field, found the method: {}", method.getReturnType().getTypeParameters()[0]);
			}
		} catch (SecurityException | NoSuchMethodException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Convert entity id.
	 *
	 * @param previous the previous
	 * @return the collection
	 */
	private Collection convertEntityId(final Collection previous) {
		final List converted = new ArrayList<>();
		for (final Object p : previous) {
			if (p instanceof EntityId) {
				converted.add(((EntityId) p).getId());
			} else {
				converted.add(p);
			}
		}
		converted.sort((a, b) -> {
			if (a instanceof Long && b instanceof Long) {
				return Long.compare((Long) a, (Long) b);
			} else if (a == null || b == null){
				return 0;
			} else {
				return Integer.compare(a.hashCode(), b.hashCode());
			}
		});
		return converted;
	}

	/**
	 * Checks if class is a "primitive".
	 *
	 * @param class1 the property class
	 * @return true, if is primitive
	 */
	boolean isPrimitiveType(final Class class1) {
		if (class1.isPrimitive()) {
			return true;
		} else if (class1.isEnum()) {
			return true;
		} else if (class1.isArray()) {
			return false;
		} else if (Number.class.isAssignableFrom(class1)) {
			return true;
		} else if (String.class.equals(class1)) {
			return true;
		} else if (Character.class.equals(class1)) {
			return true;
		} else if (Boolean.class.equals(class1)) {
			return true;
		} else if (Instant.class.equals(class1)) {
			return true;
		} else if (LocalDate.class.equals(class1)) {
			return true;
		} else if (UUID.class.equals(class1)) {
			return true;
		}
		if (LOG.isDebugEnabled()) {
			LOG.trace("Class {} is not a primitive.", class1);
		}
		return false;
	}

	/**
	 * Record change.
	 *
	 * @param entity the entity
	 * @param id the id
	 * @param propertyName the property name
	 * @param previousState the previous state
	 * @param currentState the current state
	 * @param referencedEntity the referenced entity
	 * @param curr previousEntit
	 * @param prev currentEntity
	 */
	private void recordChange(final Object entity, final Long id, final String propertyName, final String previousState, final String currentState,
			final Class referencedEntity, Object prev, Object curr) {

		if (StringUtils.equals(previousState, currentState) && !StringUtils.equals(currentState, AuditLog.FIELD_VALUE_NOT_AUDITED)) {
			LOG.trace("No state change {}.{} {}=={}", entity.getClass(), id, previousState, currentState);
			return;
		}

		TransactionAuditLog change = auditTrailService.auditLogEntry(AuditAction.UPDATE, entity, id, propertyName, previousState, currentState, referencedEntity, prev, curr);
		if (auditLogStack.get().peek().remove(change)) {
			LOG.trace("Replacing existing changelog {}", change);
		} else {
			LOG.trace("Adding new changelog {}", change);
		}
		auditLogStack.get().peek().add(change);
	}

	/**
	 * Record delete.
	 *
	 * @param entity the entity
	 * @param id the id
	 * @param propertyName the property name
	 * @param state the state
	 * @param referencedEntity the referenced entity
	 */
	private void recordDelete(final Object entity, final Long id, final String propertyName, final String state, final Class referencedEntity, final Object prev) {
		String stateToLog = state;

		// Check fields masked with @HideAuditValue
		if (stateToLog != null) {
			final Set securedFields = securedClassFields.get(entity.getClass());
			stateToLog = securedFields != null && securedFields.contains(propertyName) ? AuditLog.FIELD_VALUE_NOT_AUDITED : state;
		}

		TransactionAuditLog delete = auditTrailService.auditLogEntry(AuditAction.DELETE, entity, id, propertyName, stateToLog, null, referencedEntity, prev, null);
		if (auditLogStack.get().peek().remove(delete)) {
			LOG.trace("Replacing exising changelog {}", delete);
		} else {
			LOG.trace("Adding new changelog {}", delete);
		}
		auditLogStack.get().peek().add(delete);
	}

	/**
	 * Determines if the entity class should be audited.
	 *
	 * @param entityClass entity class
	 * @return true when changes should be audited
	 */
	boolean isAudited(final Class entityClass) {
		// Cache lookup
		if (includedClasses.contains(entityClass)) {
			return true;
		}
		// Cache lookup
		if (ignoredClasses.contains(entityClass)) {
			return false;
		}

		final NotAudited notAuditedAnnotation = entityClass.getAnnotation(NotAudited.class);
		if (notAuditedAnnotation != null) {
			LOG.trace("{} is excluded from auditing", entityClass);
			// Register ignored class
			ignoredClasses.add(entityClass);
			return false;
		}

		final Audited auditedAnnotation = entityClass.getAnnotation(Audited.class);
		if (auditedAnnotation != null) {
			LOG.trace("{} is annotated for auditing", entityClass);
			// Register included class
			includedClasses.add(entityClass);

			ReflectionUtils.doWithFields(entityClass, field -> {
				if (field.getAnnotation(NotAudited.class) != null) {
					Set ignoredEntityFields = ignoredClassFields.getOrDefault(entityClass, new HashSet<>());
					LOG.trace("{} property of {} class is excluded from auditing", field.getName(), entityClass);
					ignoredEntityFields.add(field.getName());
					ignoredClassFields.put(entityClass, ignoredEntityFields);
				}
				if (field.getAnnotation(HideAuditValue.class) != null) {
					Set securedFields = securedClassFields.getOrDefault(entityClass, new HashSet<>());
					LOG.trace("Previous and a new value of {} property of {} class is excluded from persisting", field.getName(), entityClass);
					securedFields.add(field.getName());
					securedClassFields.put(entityClass, securedFields);
				}
			});
			return true;
		}

		for (final Class auditedClass : auditedClasses) {
			if (auditedClass.isAssignableFrom(entityClass)) {
				LOG.trace("{} is audited because it is an instance of {}", entityClass, auditedClass);
				// Register included class
				includedClasses.add(entityClass);
				return true;
			}
		}

		LOG.trace("{} is not audited", entityClass);
		// Register ignored entity class
		ignoredClasses.add(entityClass);
		return false;
	}

	// @Override
	// public boolean onSave(Object entity, Serializable id, Object[] state,
	// String[] propertyNames, Type[] types) {
	// LOG.trace("onSave entity={} id={} props={}", entity, id,
	// Arrays.toString(propertyNames));
	// return super.onSave(entity, id, state, propertyNames, types);
	// }

	@Override
	public void afterTransactionBegin(final Transaction tx) {
		// Push new auditLogs to the stack
		auditLogStack.get().push(new HashSet<>());

		LOG.trace("Starting transaction level={}", auditLogStack.get().size());

		// tx.registerSynchronization(new Synchronization() {
		//
		// @Override
		// public void beforeCompletion() {
		// LOG.trace("callback beforeCompletion");
		// }
		//
		// @Override
		// public void afterCompletion(int status) {
		// LOG.trace("callback afterCompletion status={}", status);
		// }
		// });
		super.afterTransactionBegin(tx);
	}

	// Called before a transaction is committed (but not before rollback).
	@Override
	public void beforeTransactionCompletion(final Transaction tx) {

		final long level = auditLogStack.get().size();
		Set currentAuditLogs = auditLogStack.get().pop(); // pop

		LOG.trace("beforeTransactionCompletion transaction level={} auditlogs={}", level, currentAuditLogs.size());

		// LOG.trace("Before transaction completion status={} tx={}",
		// tx.getLocalStatus(), tx);

		if (currentAuditLogs.size() > 0) {
			LOG.trace("We have {} auditlogs", currentAuditLogs.size());
			currentAuditLogs.stream().forEach(auditLog -> {
				LOG.debug("Audit log to save: {}", auditLog);
			});
			if (tx.getStatus().equals(TransactionStatus.ROLLED_BACK)) {
				LOG.warn("Transaction was rolled back. Audit logs likely won't be persisted");
			}
			this.auditTrailService.addAuditLogs(currentAuditLogs);
			currentAuditLogs.clear(); // not required anymore
		}

		super.beforeTransactionCompletion(tx);
	}

	// @SuppressWarnings({ "rawtypes", "unchecked" })
	// @Override
	// public void postFlush(final Iterator entities) {
	// // LOG.trace("postFlush {}", entities);
	// entities.forEachRemaining(entity -> {
	// LOG.trace("postFlush {}", entity);
	// });
	//
	// super.postFlush(entities);
	// }

	// Called after a transaction is committed or rolled back.
	@Override
	public void afterTransactionCompletion(Transaction tx) {

		final long level = auditLogStack.get().size();
		LOG.trace("afterTransactionCompletion transaction level={}", level);
		if (tx.getStatus().equals(TransactionStatus.COMMITTED)) {
			LOG.trace("Transaction was committed, level={}", level);
		} else if (tx.getStatus().equals(TransactionStatus.ROLLED_BACK)) {
			LOG.trace("Transaction was rolled back, level={}", level);
		}

		super.afterTransactionCompletion(tx);
	}

	@Override
	public Boolean isTransient(final Object entity) {
		if (entity instanceof BasicModel) {
			return ((BasicModel) entity).isNew();
		}
		// TODO Use Spring field access methods?
		try {
			return tryMethod(entity, "getVersion");
		} catch (final NoSuchMethodException e) {
			try {
				return tryMethod(entity, "getId");
			} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e1) {
				throw new RuntimeException(e.getMessage() + " on " + entity.getClass() + " e=" + entity, e);
			}
		} catch (final Throwable e) {
			throw new RuntimeException(e.getMessage() + " on " + entity.getClass() + " e=" + entity, e);
		}
	}

	/**
	 * Try method.
	 *
	 * @param entity the entity
	 * @param methodName the method name
	 * @return the boolean
	 * @throws NoSuchMethodException the no such method exception
	 * @throws IllegalAccessException the illegal access exception
	 * @throws InvocationTargetException the invocation target exception
	 */
	public Boolean tryMethod(final Object entity, final String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		final Method getter = entity.getClass().getMethod(methodName);
		if (getter != null) {
			final Object result = getter.invoke(entity);
			if (result == null) {
				LOG.trace("{} is transient, has {} == null", entity, methodName);
				return true;
			} else if (result instanceof Number) {
				final Number r = (Number) result;
				if (r.longValue() < 0) {
					LOG.trace("{} is transient, has {} = {} < 0", entity, methodName, result);
					return true;
				} else {
					// LOG.trace(entity + " is not transient, has " + methodName + " = " + result +
					// " >= 0");
					return false;
				}
			} else {
				// LOG.trace(entity + " is not transient, has " + methodName + " = " + result);
				return false;
			}
		} else {
			LOG.trace("No version method.");
			throw new RuntimeException("No version method");
		}
	}
}