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;
}
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);
}
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);
}
}
Aggregations