示例#1
0
  /**
   * Persists a Audit
   *
   * @param object
   * @param persisted
   * @param auditingType
   */
  public void audit(Object object, Object persisted, AuditingType auditingType) {

    try {

      if (isEntity(object)) {

        Field[] fields = object.getClass().getDeclaredFields();
        Method[] methods = object.getClass().getDeclaredMethods();
        Method.setAccessible(methods, true);
        Field.setAccessible(fields, true);

        AbstractAuditing auditing = Configuration.getAbstractAuditing();
        auditing.setIdentifier(Long.valueOf(getId(object).toString()));
        auditing.setEntity(getEntityName(object.getClass()));
        auditing.setAuditingType(auditingType);
        auditing.setEventDate(new Date());
        if (FacesContext.getCurrentInstance() != null) {
          auditing.setIp(FacesUtils.getIP());
        }
        auditing.setAuditClass(object.getClass());
        AbstractAuditingListener listener = Configuration.getAuditingListener();
        if (listener != null) {
          listener.onSave(auditing);
        }

        List<AbstractMetadata> metadatas = null;
        boolean auditPersited = false;
        if (auditingType.equals(AuditingType.INSERT) || auditingType.equals(AuditingType.DELETE)) {
          entityManager.persist(auditing);
          metadatas = getMetadata(object, null, auditing);
          auditPersited = true;
        } else if (auditingType.equals(AuditingType.UPDATE)) {
          metadatas = getMetadata(object, persisted, auditing);
          if (metadatas != null && !metadatas.isEmpty()) {
            entityManager.persist(auditing);
            auditPersited = true;
          }
        }

        auditing.setMetadatas(metadatas);
        // add to context
        if (auditPersited == true) {
          AuditContext context = AuditContext.getCurrentInstance();
          if (context != null) {
            context.setAuditing(object, auditing);
          }
        }

        if (metadatas != null && !metadatas.isEmpty()) {
          for (AbstractMetadata metadata : metadatas) {
            entityManager.persist(metadata);
          }
        }
      }
    } catch (Throwable t) {
      logger.log(Level.SEVERE, t.getMessage(), t);
    }
  }
示例#2
0
  public List<AbstractMetadata> getMetadata(
      Object object, Object persisted, AbstractAuditing auditing) throws Exception {
    List<Method> methodsGet = getMethods(object);
    List<AbstractMetadata> metadatas = new ArrayList<AbstractMetadata>();
    boolean isDelete =
        auditing.getAuditingType() != null
            && auditing.getAuditingType().equals(AuditingType.DELETE);
    for (Method method : methodsGet) {
      try {
        Object fieldValue = method.invoke(object);
        Object fieldOld = null;
        if (persisted != null) {
          fieldOld = method.invoke(persisted);
        }
        AbstractMetadata metadata = Configuration.getAbstractMetadata();
        if (fieldValue != null && fieldValue.getClass().isAnnotationPresent(Embeddable.class)) {
          List<AbstractMetadata> embedableMetadata = getMetadata(fieldValue, persisted, auditing);
          if (embedableMetadata != null && !embedableMetadata.isEmpty()) {
            metadatas.addAll(embedableMetadata);
          }
        } else {
          boolean addMetadata = persisted == null;
          if (fieldValue instanceof Collection) { // para as coleções
            Collection collectionNew = (Collection<Object>) fieldValue;
            Collection collectionOld = (Collection<Object>) fieldOld;
            StringBuilder newValue = new StringBuilder();
            if (fieldOld == null) {
              for (Object item : collectionNew) {
                newValue.append(item.toString()).append("; ");
              }
              addMetadata = true;
            } else {
              StringBuilder oldValue = new StringBuilder();
              if ((!(collectionNew instanceof PersistentBag)
                      && !(collectionNew instanceof PersistentCollection))
                  || isDelete == true) {
                if ((collectionNew == null && collectionOld != null)
                    || (collectionNew != null && collectionOld == null)
                    || (collectionNew.size() != collectionOld.size())) {
                  addMetadata = true;
                } else {
                  for (Object current : collectionNew) {
                    if (collectionOld != null && !collectionOld.contains(current)) {
                      for (Object currentOld : collectionOld) {
                        if (!currentOld.equals(current)) {
                          addMetadata = true;
                          break;
                        }
                      }
                    }
                  }
                }
                for (Object old : collectionOld) {
                  oldValue.append(old).append("; ");
                }
                for (Object item : collectionNew) {
                  newValue.append(item.toString()).append("; ");
                }
                metadata.setOldValue(oldValue.toString());
              }
            }
            metadata.setNewValue(newValue.toString());
          } else if (isEntity(method.getReturnType())) {
            Object newId = getId(fieldValue);
            // a proxy doesnt has value changed
            if (!(fieldValue instanceof HibernateProxy) || isDelete == true) {
              /** One to One cascade ALL */
              if (isOneToOneCascadeAll(method)) {
                List<AbstractMetadata> embedableMetadata = null;
                // add metadata for oneToOne cascade all based on new object
                if (persisted == null) {
                  embedableMetadata = getMetadata(fieldValue, null, auditing);
                } else {
                  embedableMetadata = getMetadata(fieldValue, getPersisted(fieldValue), auditing);
                }

                if (embedableMetadata != null && !embedableMetadata.isEmpty()) {
                  metadatas.addAll(embedableMetadata);
                }
              }

              Object oldId = null;
              if (fieldOld instanceof HibernateProxy) {
                oldId = ((HibernateProxy) fieldOld).getHibernateLazyInitializer().getIdentifier();
              } else {
                oldId = getId(fieldOld);
              }
              metadata.setOldIdentifier(oldId == null ? null : Long.valueOf(oldId.toString()));
              metadata.setOldValue(fieldOld == null ? "" : fieldOld.toString());
              if ((oldId == null && newId != null)
                  || (oldId != null && newId == null)
                  || (oldId != null && !oldId.equals(newId))) {
                addMetadata = true;
              }
              metadata.setEntity(method.getDeclaringClass().getName());
              metadata.setNewIdentifier(newId == null ? null : Long.valueOf(newId.toString()));
              metadata.setNewValue(fieldValue == null ? "" : fieldValue.toString());
            }
          } else {
            if (fieldOld != null) {
              metadata.setOldValue(getToString(fieldOld));
            }
            if (fieldValue != null) {
              metadata.setNewValue(getToString(fieldValue));
            }
            // verify empty String
            if (fieldValue instanceof String) {
              if ((fieldOld == null && fieldValue != null && !fieldValue.toString().isEmpty())
                  || (fieldOld != null && !fieldOld.toString().isEmpty() && fieldValue == null)
                  || (fieldOld != null && !fieldOld.equals(fieldValue))) {
                addMetadata = true;
              }
            } else {
              if ((fieldOld == null && fieldValue != null)
                  || (fieldOld != null && fieldValue == null)
                  || (fieldOld != null && !fieldOld.equals(fieldValue))) {
                addMetadata = true;
              }
            }
          }
          metadata.setField(getMethodName(method));
          metadata.setAuditing(auditing);
          if (addMetadata) {
            metadatas.add(metadata);
          }
        }
      } catch (Throwable t) {
        logger.log(Level.SEVERE, t.getMessage(), t);
      }
    }
    return metadatas;
  }