AuditTrailServiceImpl.java

/*
 * 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.service.impl;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.math.NumberUtils;
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.model.filters.AuditLogFilter;
import org.genesys.blocks.auditlog.persistence.AuditLogRepository;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.model.ClassPK;
import org.genesys.blocks.model.EntityId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import lombok.extern.slf4j.Slf4j;

/**
 * Implementation of the {@link AuditTrailService} with JPA.
 *
 * @author Matija Obreza
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class AuditTrailServiceImpl implements AuditTrailService {

	/** The class pk service. */
	@Autowired
	private ClassPKService classPkService;

	/** The audit log repository. */
	@Autowired
	private AuditLogRepository auditLogRepository;

	/*
	 * (non-Javadoc)
	 * @see
	 * org.genesys.blocks.auditlog.service.AuditTrailService#addAuditLogs(java.util.
	 * Set)
	 */
	@Override
	@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
	public List<AuditLog> addAuditLogs(final Set<TransactionAuditLog> auditLogs) {
		return auditLogRepository.saveAll(auditLogs.stream().map(tlog -> toAuditLog(tlog)).collect(Collectors.toList()));
	}

	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.auditlog.service.AuditTrailService#auditLogEntry(org.
	 * genesys.blocks.auditlog.model.AuditAction, java.lang.Object, long,
	 * java.lang.String, java.lang.String, java.lang.String, java.lang.Class)
	 */
	@Override
	public TransactionAuditLog auditLogEntry(final AuditAction action, final Object entity, final long id, final String propertyName, final String previousState,
			final String currentState, final Class<?> referencedEntity, final Object previousObject, final Object currentObject) {

		final TransactionAuditLog auditLog = new TransactionAuditLog();
		auditLog.setAction(action);

		auditLog.setClassPk(classPkService.getClassPk(entity.getClass()));
		auditLog.setEntityId(id);
		auditLog.setPropertyName(propertyName);
		auditLog.setPreviousState(previousState);
		auditLog.setNewState(currentState);
		auditLog.setPreviousObject(previousObject); // Transient
		auditLog.setNewObject(currentObject); // Transient
		if (referencedEntity != null) {
			auditLog.setReferencedEntity(classPkService.getClassPk(referencedEntity));
		}

		log.trace("Creating {} audit log entity={} id={} prop={} old={} new={} ref={}", action, entity.getClass().getName(), id, propertyName, previousState, currentState,
			referencedEntity == null ? null : referencedEntity.getName());

		return auditLog;
	}

	/**
	 * To audit log.
	 *
	 * @param tlog the tlog
	 * @return the audit log
	 */
	private AuditLog toAuditLog(final TransactionAuditLog tlog) {
		final AuditLog auditLog = new AuditLog();
		auditLog.setLogDate(Instant.now());
		auditLog.setAction(tlog.getAction());
		auditLog.setClassPk(tlog.getClassPk());
		auditLog.setEntityId(tlog.getEntityId());
		auditLog.setNewState(tlog.getNewState());
		auditLog.setPreviousState(tlog.getPreviousState());
		auditLog.setPropertyName(tlog.getPropertyName());
		auditLog.setReferencedEntity(tlog.getReferencedEntity());
		auditLog.setPreviousEntity(tlog.getPreviousObject());
		auditLog.setNewEntity(tlog.getNewObject());
		return auditLog;
	}

	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.auditlog.service.AuditTrailService#listAuditLogs(org.
	 * genesys.blocks.auditlog.model.filters.AuditLogFilter,
	 * org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<AuditLog> listAuditLogs(final AuditLogFilter filters, final Pageable page) {
		return auditLogRepository.listAuditLogs(filters, page);
	}

	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.auditlog.service.AuditTrailService#listAuditLogs(org.
	 * genesys.blocks.model.EntityId)
	 */
	@Override
	public List<AuditLog> listAuditLogs(final EntityId entity) {
		List<AuditLog> auditLogs = auditLogRepository.listAuditLogs(entity);

		auditLogs.stream()
			// for logs with a referenced entity
			.filter(auditLog -> auditLog.getReferencedEntity() != null)
			// set previous and new objects
			.forEach(auditLog -> {
				ClassPK referencedClassPk = auditLog.getReferencedEntity();
				log.trace("Loading referenced entity {}", referencedClassPk);
				if (auditLog.getPreviousState() != null) {
					auditLog.setPreviousEntity(auditLogRepository.get(referencedClassPk, NumberUtils.toLong(auditLog.getPreviousState())));
				}
				if (auditLog.getNewState() != null) {
					auditLog.setNewEntity(auditLogRepository.get(referencedClassPk, NumberUtils.toLong(auditLog.getNewState())));
				}
			});

		return auditLogs;
	}

	/* (non-Javadoc)
	 * @see org.genesys.blocks.auditlog.service.AuditTrailService#auditLogs(org.genesys.blocks.model.EntityId)
	 */
	@Override
	public Map<String, List<AuditLog>> auditLogs(EntityId entity) {
		Map<String, List<AuditLog>> logMap = new HashMap<>();
		listAuditLogs(entity).stream().forEach(auditLog -> {
			List<AuditLog> m = logMap.get(auditLog.getPropertyName());
			if (m == null) {
				logMap.put(auditLog.getPropertyName(), m = new ArrayList<>());
			}
			m.add(auditLog);
		});
		return logMap;
	}
}