org.genesys.blocks.auditlog.persistence.AuditLogRepositoryCustomImpl 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.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(), PageRequest.of(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, PageRequest.of(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.buildPredicate(), 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) {
if (id == null || classPk == null || classPk.getClassname() == null) {
return null;
}
LOG.trace("Looking up {} id={}", classPk.getClassname(), id);
try {
return entityManager.find(Class.forName(classPk.getClassname()), id);
} catch (ClassNotFoundException e) {
LOG.warn("Audit log references a missing class: {}", e.getMessage());
return null;
}
}
}