Пример #1
0
 public void onPostUpdate(PostUpdateEvent event) {
   GenericModel entity = (GenericModel) event.getEntity();
   if (entity.getClass().isAnnotationPresent(Auditable.class)) {
     String model = entity.getClass().getName().replaceFirst("models.", "");
     String modelId = entity.getEntityId().toString();
     String[] properties = event.getPersister().getPropertyNames();
     Object[] oldValues = event.getOldState();
     Object[] values = event.getState();
     for (int i = 0; i < properties.length; i++) {
       boolean updated = false;
       if (oldValues[i] == null) {
         if (values[i] != null) {
           updated = true;
         }
       } else if (!oldValues[i].equals(values[i])) {
         updated = true;
       }
       if (updated) {
         AuditLog.invoke(
             "onUpdate",
             model,
             modelId,
             properties[i],
             oldValues[i] == null ? "NULL" : oldValues[i].toString(),
             values[i] == null ? "NULL" : values[i].toString());
       }
     }
   }
 }
Пример #2
0
 public void onPostDelete(PostDeleteEvent event) {
   GenericModel entity = (GenericModel) event.getEntity();
   if (entity.getClass().isAnnotationPresent(Auditable.class)) {
     String model = entity.getClass().getName().replaceFirst("models.", "");
     String modelId = entity.getEntityId().toString();
     String[] properties = event.getPersister().getPropertyNames();
     Object[] values = event.getDeletedState();
     for (int i = 0; i < properties.length; i++) {
       AuditLog.invoke(
           "onDelete",
           model,
           modelId,
           properties[i],
           values[i] == null ? "NULL" : values[i].toString());
     }
   }
 }