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

org.genesys.blocks.auditlog.persistence.AuditLogRepositoryCustomImpl Maven / Gradle / Ivy

/*
 * Copyright 2017 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.persistence;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.model.filters.AuditLogFilter;
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.PageRequest;
import org.springframework.data.domain.Pageable;

/**
 * The Interface AuditLogRepository.
 *
 * @author Matija Obreza
 */
public class AuditLogRepositoryCustomImpl implements AuditLogCustomRepository {

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

	/** The repository. */
	@Autowired
	private AuditLogRepository repository;

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

	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.auditlog.persistence.AuditLogCustomRepository#
	 * listAuditLogs(org.genesys.blocks.model.EntityId)
	 */
	@Override
	public List listAuditLogs(final EntityId entity) {
		if (entity == null) {
			return new ArrayList<>();
		}
		return repository.listAuditLogs(entity.getClass().getName(), entity.getId());
	}

	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.auditlog.persistence.AuditLogCustomRepository#
	 * getLastAuditLog(org.genesys.blocks.model.EntityId)
	 */
	@Override
	public AuditLog getLastAuditLog(final EntityId entity) {
		final List l = repository.listAuditLogs(entity.getClass().getName(), entity.getId(), new PageRequest(0, 1));
		return (l == null) || l.isEmpty() ? null : l.get(0);
	}

	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.auditlog.persistence.AuditLogCustomRepository#
	 * getLastAuditLog(org.genesys.blocks.model.EntityId, java.lang.String)
	 */
	@Override
	public AuditLog getLastAuditLog(final EntityId entity, final String propertyName) {
		final List l = repository.listAuditLogs(entity.getClass().getName(), entity.getId(), propertyName, new PageRequest(0, 1));
		return (l == null) || l.isEmpty() ? null : l.get(0);
	}

	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.auditlog.persistence.AuditLogCustomRepository#
	 * listAuditLogs(org.genesys.blocks.auditlog.model.filters.AuditLogFilter,
	 * org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page listAuditLogs(final AuditLogFilter filters, final Pageable page) {
		return repository.findAll(filters.buildQuery(), page);
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.genesys.blocks.auditlog.persistence.AuditLogCustomRepository#get(org.
	 * genesys.blocks.model.ClassPK, java.lang.Long)
	 */
	@Override
	public Object get(ClassPK classPk, Long id) {
		LOG.trace("Looking up {} id={}", classPk.getClassname(), id);
		if (id == null || classPk == null || classPk.getClassname() == null) {
			return null;
		}
		try {
			return entityManager.find(Class.forName(classPk.getClassname()), id);
		} catch (ClassNotFoundException e) {
			LOG.error(e.getMessage(), e);
			return null;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy