コード例 #1
0
 private void calculateVersion(EntityAuditEvent auditedEntity) {
   log.trace("Version calculation. for update/remove");
   Integer lastCommitVersion =
       auditingEntityRepository.findMaxCommitVersion(
           auditedEntity.getEntityType(), auditedEntity.getEntityId());
   log.trace("Last commit version of entity => {}", lastCommitVersion);
   if (lastCommitVersion != null && lastCommitVersion != 0) {
     log.trace("Present. Adding version..");
     auditedEntity.setCommitVersion(lastCommitVersion + 1);
   } else {
     log.trace("No entities.. Adding new version 1");
     auditedEntity.setCommitVersion(1);
   }
 }
コード例 #2
0
 /**
  * Method to prepare auditing entity
  *
  * @param entity
  * @param action
  * @return
  */
 private EntityAuditEvent prepareAuditEntity(final Object entity, EntityAuditAction action) {
   EntityAuditEvent auditedEntity = new EntityAuditEvent();
   Class<?> entityClass = entity.getClass(); // Retrieve entity class with reflection
   auditedEntity.setAction(action.value());
   auditedEntity.setEntityType(entityClass.getName());
   Long entityId;
   String entityData;
   log.trace("Getting Entity Id and Content");
   try {
     Field privateLongField = entityClass.getDeclaredField("id");
     privateLongField.setAccessible(true);
     entityId = (Long) privateLongField.get(entity);
     privateLongField.setAccessible(false);
     entityData = objectMapper.writeValueAsString(entity);
   } catch (IllegalArgumentException
       | IllegalAccessException
       | NoSuchFieldException
       | SecurityException
       | IOException e) {
     log.error("Exception while getting entity ID and content {}", e);
     // returning null as we dont want to raise an application exception here
     return null;
   }
   auditedEntity.setEntityId(entityId);
   auditedEntity.setEntityValue(entityData);
   final AbstractAuditingEntity abstractAuditEntity = (AbstractAuditingEntity) entity;
   if (EntityAuditAction.CREATE.equals(action)) {
     auditedEntity.setModifiedBy(abstractAuditEntity.getCreatedBy());
     auditedEntity.setModifiedDate(abstractAuditEntity.getCreatedDate());
     auditedEntity.setCommitVersion(1);
   } else {
     auditedEntity.setModifiedBy(abstractAuditEntity.getLastModifiedBy());
     auditedEntity.setModifiedDate(abstractAuditEntity.getLastModifiedDate());
     calculateVersion(auditedEntity);
   }
   log.trace("Audit Entity --> {} ", auditedEntity.toString());
   return auditedEntity;
 }