Search in sources :

Example 1 with IdmAudit

use of eu.bcvsolutions.idm.core.audit.entity.IdmAudit in project CzechIdMng by bcvsolutions.

the class DefaultAuditService method getNameChangedColumns.

@Override
public <T> List<String> getNameChangedColumns(Class<T> entityClass, UUID entityId, Long currentRevId, T currentEntity) {
    List<String> changedColumns = new ArrayList<>();
    T previousEntity = null;
    if (currentRevId == null) {
        IdmAudit currentRevision = this.getAuditReader().getCurrentRevision(IdmAudit.class, true);
        // current revision doesn't exist return empty list
        if (currentRevision == null) {
            return Collections.emptyList();
        }
        currentRevId = Long.valueOf(currentRevision.getId().toString());
    }
    previousEntity = this.findPreviousVersion(entityClass, entityId, currentRevId);
    // previous revision doesn't exist return empty list
    if (previousEntity == null) {
        return Collections.emptyList();
    }
    Class<?> clazz = entityClass;
    while (!(clazz.equals(AbstractEntity.class))) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getAnnotation(Audited.class) != null) {
                Object previousValue;
                Object currentValue;
                try {
                    PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(currentEntity, field.getName());
                    Assert.notNull(propertyDescriptor, MessageFormat.format("DefaultAuditService: read method for audited field {0}, can't be null.", field.getName()));
                    // 
                    Method readMethod = propertyDescriptor.getReadMethod();
                    // check if exists readMethod
                    Assert.notNull(readMethod, MessageFormat.format("DefaultAuditService: read method for audited field {0}, can't be null.", field.getName()));
                    previousValue = readMethod.invoke(previousEntity);
                    currentValue = readMethod.invoke(currentEntity);
                    if (previousValue == null && currentValue == null) {
                        continue;
                    }
                    if (previousValue == null || !previousValue.equals(currentValue)) {
                        changedColumns.add(field.getName());
                    }
                } catch (IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
                    throw new IllegalArgumentException(MessageFormat.format("For entity class [{0}] with id [{1}] and revision id [{2}], name of changed columns cannot be found.", clazz, entityId, currentRevId), ex);
                } catch (EntityNotFoundException e) {
                    // TODO: Try to found better solution for get entity that was not found
                    LOG.info("Audit service entity not found. Method [getNameChangedColumns]", e);
                    break;
                }
            }
        }
        clazz = clazz.getSuperclass();
    }
    return changedColumns;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IdmAudit(eu.bcvsolutions.idm.core.audit.entity.IdmAudit) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) EntityNotFoundException(javax.persistence.EntityNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) Audited(org.hibernate.envers.Audited)

Example 2 with IdmAudit

use of eu.bcvsolutions.idm.core.audit.entity.IdmAudit in project CzechIdMng by bcvsolutions.

the class DefaultAuditService method get.

/**
 * Is necessary to override method get, because old get transform id to UUID and audits have Long ID.
 *
 * @param id
 * @return
 */
@Override
public IdmAuditDto get(Serializable id, BasePermission... permission) {
    // TODO: add permission, now can't be use find, because Authentication object is null when call from IdmAuditLisener
    Assert.notNull(id, "Id is required");
    IdmAudit audit = this.auditRepository.findOneById(Long.valueOf(id.toString()));
    if (audit == null) {
        throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("audit", id));
    }
    // return only one element
    return this.toDto(audit);
}
Also used : IdmAudit(eu.bcvsolutions.idm.core.audit.entity.IdmAudit) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException)

Example 3 with IdmAudit

use of eu.bcvsolutions.idm.core.audit.entity.IdmAudit in project CzechIdMng by bcvsolutions.

the class IdmAuditListener method entityChanged.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void entityChanged(Class entityClass, String entityName, Serializable entityId, RevisionType revisionType, Object revisionEntity) {
    // autowire services
    autowireServices();
    IdmAudit auditEntity = (IdmAudit) revisionEntity;
    if (auditEntity.getEntityId() != null) {
        // child revision
        IdmAuditDto childRevision = new IdmAuditDto();
        childRevision.setChangedAttributes(auditEntity.getTemporaryChangedColumns());
        childRevision.setTimestamp(((IdmAudit) revisionEntity).getTimestamp());
        this.changeRevisionDto((Class<AbstractEntity>) entityClass, entityName, (UUID) entityId, childRevision, revisionType);
        this.auditService.save(childRevision);
    } else {
        // parent revision
        this.changeRevisionEntity((Class<AbstractEntity>) entityClass, entityName, (UUID) entityId, auditEntity, revisionType);
    }
}
Also used : IdmAudit(eu.bcvsolutions.idm.core.audit.entity.IdmAudit) AbstractEntity(eu.bcvsolutions.idm.core.api.entity.AbstractEntity) IdmAuditDto(eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto)

Aggregations

IdmAudit (eu.bcvsolutions.idm.core.audit.entity.IdmAudit)3 IdmAuditDto (eu.bcvsolutions.idm.core.api.audit.dto.IdmAuditDto)1 AbstractEntity (eu.bcvsolutions.idm.core.api.entity.AbstractEntity)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 Audited (org.hibernate.envers.Audited)1