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

org.genesys.blocks.auditlog.service.impl.AuditTrailServiceImpl 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.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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

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

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

	/** 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 addAuditLogs(final Set 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 log = new TransactionAuditLog();
		log.setAction(action);

		log.setClassPk(classPkService.getClassPk(entity.getClass()));
		log.setEntityId(id);
		log.setPropertyName(propertyName);
		log.setPreviousState(previousState);
		log.setNewState(currentState);
		log.setPreviousObject(previousObject); // Transient
		log.setNewObject(currentObject); // Transient
		if (referencedEntity != null) {
			log.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 log;
	}

	/**
	 * 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 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 listAuditLogs(final EntityId entity) {
		List 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> auditLogs(EntityId entity) {
		Map> logMap = new HashMap<>();
		listAuditLogs(entity).stream().forEach(auditLog -> {
			List m = logMap.get(auditLog.getPropertyName());
			if (m == null) {
				logMap.put(auditLog.getPropertyName(), m = new ArrayList<>());
			}
			m.add(auditLog);
		});
		return logMap;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy