Commit 36bb3422 authored by Matija Obreza's avatar Matija Obreza

Source code cleanup

parent 43d6e21f
......@@ -23,7 +23,8 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Add this annotation to persisted entities to explicitly include them in auditing.
* Add this annotation to persisted entities to explicitly include them in
* auditing.
*
* @author Matija Obreza
*/
......
......@@ -15,6 +15,13 @@
*/
package org.genesys.blocks.auditlog.model;
/**
* The Enum AuditAction.
*/
public enum AuditAction {
UPDATE, DELETE
/** The update. */
UPDATE,
/** The delete. */
DELETE
}
......@@ -45,6 +45,7 @@ import org.springframework.data.annotation.CreatedBy;
@NotAudited
public class AuditLog implements Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = -2254427722756061411L;
/** The id. */
......@@ -53,41 +54,44 @@ public class AuditLog implements Serializable {
@Column(nullable = false, length = 20)
protected Long id;
/** The created by. */
@CreatedBy
private Long createdBy;
/** The log date. */
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "logdate", nullable = false)
private Date logDate;
/** Class name of the referenced entity */
/** Class name of the referenced entity. */
@ManyToOne(optional = false)
@JoinColumn(name = "classPk")
private ClassPK classPk;
/** ID of the referenced entity */
/** ID of the referenced entity. */
@Column(name = "entityId")
private long entityId;
/** The name of the property modified */
/** The name of the property modified. */
@Column(nullable = false, name = "prop", length = 50)
private String propertyName;
/** The type of entity referenced in the changed property */
/** The type of entity referenced in the changed property. */
@ManyToOne(optional = true)
@JoinColumn(name = "entityClassPk")
private ClassPK referencedEntity;
/** String representation of the previous state */
/** String representation of the previous state. */
@Lob
@Column(nullable = true)
private String previousState;
/** String representation of the updated state */
/** String representation of the updated state. */
@Lob
@Column(nullable = true)
private String newState;
/** The action. */
@Enumerated(EnumType.STRING)
@Column(length = 10, nullable = false, updatable = false)
private AuditAction action;
......@@ -254,10 +258,20 @@ public class AuditLog implements Serializable {
return newState;
}
/**
* Sets the action.
*
* @param action the new action
*/
public void setAction(final AuditAction action) {
this.action = action;
}
/**
* Gets the action.
*
* @return the action
*/
public AuditAction getAction() {
return action;
}
......
/*
* 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.model.filters;
import java.util.Set;
......@@ -11,16 +26,33 @@ import org.genesys.blocks.auditlog.model.QAuditLog;
import org.genesys.blocks.model.filters.DateFilter;
/**
* Search filter
* Search filter.
*/
public class AuditLogFilter {
/** The classname. */
public String classname;
/** The entity id. */
public Long entityId;
/** The created by. */
public Set<Long> createdBy;
/** The action. */
public Set<AuditAction> action;
/** The property name. */
public Set<String> propertyName;
/** The log date. */
public DateFilter logDate;
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery() {
final BooleanBuilder and = new BooleanBuilder();
if (classname != null) {
......
......@@ -23,12 +23,42 @@ import org.genesys.blocks.model.EntityId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
/**
* The Interface AuditLogCustomRepository.
*/
public interface AuditLogCustomRepository {
/**
* List audit logs.
*
* @param entity the entity
* @return the list
*/
List<AuditLog> listAuditLogs(EntityId entity);
/**
* Gets the last audit log.
*
* @param entity the entity
* @return the last audit log
*/
AuditLog getLastAuditLog(EntityId entity);
/**
* Gets the last audit log.
*
* @param entity the entity
* @param propertyName the property name
* @return the last audit log
*/
AuditLog getLastAuditLog(EntityId entity, String propertyName);
/**
* List audit logs.
*
* @param filters the filters
* @param page the page
* @return the page
*/
Page<AuditLog> listAuditLogs(AuditLogFilter filters, Pageable page);
}
......@@ -33,13 +33,36 @@ import org.springframework.stereotype.Repository;
@Repository
public interface AuditLogRepository extends AuditLogCustomRepository, JpaRepository<AuditLog, Long>, QueryDslPredicateExecutor<AuditLog> {
/** List audit log entries in descending order (most recent first) */
/**
* List audit log entries in descending order (most recent first).
*
* @param classname the classname
* @param entityId the entity id
* @return the list
*/
@Query("select al from AuditLog al where al.entityId = ?2 and al.classPk.classname = ?1 order by al.logDate desc")
List<AuditLog> listAuditLogs(String classname, Serializable entityId);
/**
* List audit logs.
*
* @param name the name
* @param id the id
* @param page the page
* @return the list
*/
@Query("select al from AuditLog al where al.entityId = ?2 and al.classPk.classname = ?1 order by al.logDate desc")
List<AuditLog> listAuditLogs(String name, Serializable id, Pageable page);
/**
* List audit logs.
*
* @param name the name
* @param id the id
* @param propertyName the property name
* @param page the page
* @return the list
*/
@Query("select al from AuditLog al where al.entityId = ?2 and al.classPk.classname = ?1 and al.propertyName = ?3 order by al.logDate desc")
List<AuditLog> listAuditLogs(String name, Serializable id, String propertyName, Pageable page);
......
......@@ -32,26 +32,48 @@ import org.springframework.data.domain.Pageable;
*/
public class AuditLogRepositoryCustomImpl implements AuditLogCustomRepository {
/** The repository. */
@Autowired
private AuditLogRepository repository;
/*
* (non-Javadoc)
* @see org.genesys.blocks.auditlog.persistence.AuditLogCustomRepository#
* listAuditLogs(org.genesys.blocks.model.EntityId)
*/
@Override
public List<AuditLog> listAuditLogs(final EntityId entity) {
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<AuditLog> l = repository.listAuditLogs(entity.getClass().getName(), entity.getId(), new PageRequest(0, 1));
return l == null || l.isEmpty() ? null : l.get(0);
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<AuditLog> l = repository.listAuditLogs(entity.getClass().getName(), entity.getId(), propertyName, new PageRequest(0, 1));
return l == null || l.isEmpty() ? null : l.get(0);
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<AuditLog> listAuditLogs(final AuditLogFilter filters, final Pageable page) {
return repository.findAll(filters.buildQuery(), page);
......
......@@ -32,6 +32,7 @@ public interface AuditTrailService {
/**
* Record a new entry in the audit log.
*
* @param logDate the log date
* @param entity the entity
* @param id entity ID
* @param propertyName property name
......@@ -43,8 +44,9 @@ public interface AuditTrailService {
AuditLog recordChange(Date logDate, Object entity, long id, String propertyName, String previousState, String currentState, Class<?> referencedEntity);
/**
* Record deletion in the audit log
* Record deletion in the audit log.
*
* @param logDate the log date
* @param entity the entity
* @param id entity ID
* @param propertyName property name
......@@ -55,10 +57,10 @@ public interface AuditTrailService {
AuditLog recordDelete(Date logDate, Object entity, long id, String propertyName, String state, Class<?> referencedEntity);
/**
* Return logs matching <code>AuditSearch</code> filter
* Return logs matching <code>AuditSearch</code> filter.
*
* @param filters filter
* @param pageRequest pagination
* @param page the page
* @return list of matching logs
*/
Page<AuditLog> listAuditLogs(AuditLogFilter filters, Pageable page);
......
......@@ -33,20 +33,26 @@ public interface ClassPKService {
ClassPK getClassPk(Class<?> class1);
/**
* Get classPk classname
* Get classPk classname.
*
* @param id
* @param id the id
* @return classname of persisted class
*/
String getClassName(Long id);
/**
* Find by short name.
*
* @param classPKShortName the class PK short name
* @return the class PK
*/
ClassPK findByShortName(String classPKShortName);
/**
* Get ClassPK ID for full class name
*
* @param className
* @return
* Get ClassPK ID for full class name.
*
* @param className the class name
* @return the class pk id
*/
Long getClassPkId(String className);
......
......@@ -41,19 +41,24 @@ import org.springframework.transaction.annotation.Transactional;
@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#recordChange(java.lang.Object, long, java.lang.String,
* java.lang.String, java.lang.String, java.lang.Class)
* @see
* org.genesys.blocks.auditlog.service.AuditTrailService#recordChange(java.lang.
* Object, long, java.lang.String, java.lang.String, java.lang.String,
* java.lang.Class)
*/
@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
......@@ -74,11 +79,18 @@ public class AuditTrailServiceImpl implements AuditTrailService {
if (LOG.isDebugEnabled()) {
LOG.debug("Recording update audit log entity={} id={} prop={} old={} new={} ref={}", entity.getClass().getName(), id, propertyName, previousState, currentState,
referencedEntity == null ? null : referencedEntity.getName());
referencedEntity == null ? null : referencedEntity.getName());
}
return auditLogRepository.save(log);
}
/*
* (non-Javadoc)
* @see
* org.genesys.blocks.auditlog.service.AuditTrailService#recordDelete(java.util.
* Date, java.lang.Object, long, java.lang.String, java.lang.String,
* java.lang.Class)
*/
@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
public AuditLog recordDelete(final Date logDate, final Object entity, final long id, final String propertyName, final String state, final Class<?> referencedEntity) {
......@@ -96,12 +108,18 @@ public class AuditTrailServiceImpl implements AuditTrailService {
}
if (LOG.isDebugEnabled()) {
LOG.debug("Recording delete audit log entity={} id={} prop={} old={} ref={}", entity.getClass().getName(), id, propertyName, state,
referencedEntity == null ? null : referencedEntity.getName());
LOG.debug("Recording delete audit log entity={} id={} prop={} old={} ref={}", entity.getClass().getName(), id, propertyName, state, referencedEntity == null ? null
: referencedEntity.getName());
}
return auditLogRepository.save(log);
}
/*
* (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);
......
......@@ -36,27 +36,36 @@ import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class ClassPKServiceImpl implements ClassPKService {
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(ClassPKServiceImpl.class);
/** The class pk repository. */
@Autowired
private ClassPKRepository classPkRepository;
/*
* (non-Javadoc)
*
* @see org.genesys.blocks.auditlog.service.ClassPKService#getClassPk(java.lang.Class)
* @see org.genesys.blocks.auditlog.service.ClassPKService#getClassPk(java.lang.
* Class)
*/
@Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
public ClassPK getClassPk(final Class<?> class1) {
ClassPK classPk = classPkRepository.getByClassname(class1.getName());
final ClassPK classPk = classPkRepository.getByClassname(class1.getName());
if (classPk != null) {
return classPk;
}
return createNewClassPkForClass(class1);
}
private String generateShortPkName(String shortName) {
/**
* Generate short pk name.
*
* @param shortName the short name
* @return the string
*/
private String generateShortPkName(final String shortName) {
ClassPK classPk = null;
String candidate = shortName;
......@@ -76,28 +85,46 @@ public class ClassPKServiceImpl implements ClassPKService {
throw new PersistenceException("Can't generate a shortName for ClassPK for " + shortName);
}
/*
* (non-Javadoc)
* @see
* org.genesys.blocks.auditlog.service.ClassPKService#getClassName(java.lang.
* Long)
*/
@Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
public String getClassName(final Long id) {
ClassPK classPK = classPkRepository.findOne(id);
final ClassPK classPK = classPkRepository.findOne(id);
return classPK.getClassname();
}
/*
* (non-Javadoc)
* @see
* org.genesys.blocks.auditlog.service.ClassPKService#findByShortName(java.lang.
* String)
*/
@Override
public ClassPK findByShortName(String classPKShortName) {
public ClassPK findByShortName(final String classPKShortName) {
return classPkRepository.findByShortName(classPKShortName);
}
/*
* (non-Javadoc)
* @see
* org.genesys.blocks.auditlog.service.ClassPKService#getClassPkId(java.lang.
* String)
*/
@Override
@Transactional
public Long getClassPkId(String classname) {
Long id = classPkRepository.getClassPkId(classname);
public Long getClassPkId(final String classname) {
final Long id = classPkRepository.getClassPkId(classname);
if (id == null) {
try {
Class<?> aClass = Class.forName(classname);
final Class<?> aClass = Class.forName(classname);
return getClassPk(aClass).getId();
} catch (ClassNotFoundException e) {
} catch (final ClassNotFoundException e) {
return null;
}
} else {
......@@ -105,8 +132,14 @@ public class ClassPKServiceImpl implements ClassPKService {
}
}
private ClassPK createNewClassPkForClass(Class<?> aClass) {
ClassPK classPk = new ClassPK();
/**
* Creates the new class pk for class.
*
* @param aClass the a class
* @return the class PK
*/
private ClassPK createNewClassPkForClass(final Class<?> aClass) {
final ClassPK classPk = new ClassPK();
classPk.setClassname(aClass.getName());
classPk.setShortName(generateShortPkName(aClass.getSimpleName().toLowerCase()));
......
......@@ -15,8 +15,8 @@
*/
package org.genesys.blocks.auditlog.component;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.util.Date;
import java.util.UUID;
......@@ -24,7 +24,6 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.auditlog.component.AuditTrailInterceptor;
import org.junit.Test;
public class AuditTrailInterceptorTest {
......
......@@ -15,13 +15,14 @@
*/
package org.genesys.blocks.auditlog.model;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.util.Date;
import org.genesys.blocks.auditlog.model.AuditAction;
import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.persistence.AuditLogRepository;
import org.genesys.blocks.auditlog.test.EntityTest;
import org.genesys.blocks.model.ClassPK;
......
......@@ -15,8 +15,8 @@
*/
package org.genesys.blocks.auditlog.model;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import org.genesys.blocks.auditlog.test.EntityTest;
import org.genesys.blocks.model.ClassPK;
......
......@@ -15,14 +15,14 @@
*/
package org.genesys.blocks.auditlog.service;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.util.Date;
import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.test.ServiceTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
......
......@@ -15,8 +15,10 @@
*/
package org.genesys.blocks.auditlog.service;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import org.genesys.blocks.auditlog.test.ServiceTest;
import org.genesys.blocks.model.ClassPK;
......@@ -48,7 +50,7 @@ public class ClassPKServiceTest extends ServiceTest {
assertThat(cpk2.getClassname(), equalTo(Long.class.getName()));
assertThat(cpk.getId(), equalTo(cpk2.getId()));
}
@Test
public void testGetClassPkId() {
final ClassPK cpk = classPkService.getClassPk(Long.class);
......@@ -63,13 +65,13 @@ public class ClassPKServiceTest extends ServiceTest {
public void testCreateClassPkId() {
final Long id = classPkService.getClassPkId(Long.class.getName());
assertThat(id, not(nullValue()));