Search in sources :

Example 1 with EntityLogItem

use of io.jmix.audit.entity.EntityLogItem in project jmix by jmix-framework.

the class EntityLogImpl method internalRegisterModify.

protected EntityLogItem internalRegisterModify(Object entity, @Nullable AttributeChanges changes, MetaClass metaClass, String storeName, Set<String> attributes) {
    EntityLogItem item = null;
    Date ts = timeSource.currentTimestamp();
    Set<String> dirty = calculateDirtyFields(entity, changes);
    Set<EntityLogAttr> entityLogAttrs;
    EntityLogItem.Type type;
    if (isSoftDeleteEntityRestored(entity, dirty)) {
        type = EntityLogItem.Type.RESTORE;
        entityLogAttrs = createLogAttributes(entity, attributes, changes);
    } else {
        type = EntityLogItem.Type.MODIFY;
        Set<String> dirtyAttributes = new HashSet<>();
        for (String attributePath : attributes) {
            if (metadataTools.isAdditionalProperty(metaClass, attributePath)) {
                if (dirty.contains(attributePath)) {
                    dirtyAttributes.add(attributePath);
                }
                continue;
            }
            MetaPropertyPath propertyPath = metaClass.getPropertyPath(attributePath);
            Preconditions.checkNotNullArgument(propertyPath, "Property path %s isn't exists for type %s", attributePath, metaClass.getName());
            if (dirty.contains(attributePath)) {
                dirtyAttributes.add(attributePath);
            } else if (!stores.getAdditional().isEmpty()) {
                String idAttributePath = getIdAttributePath(propertyPath, storeName);
                if (idAttributePath != null && dirty.contains(idAttributePath)) {
                    dirtyAttributes.add(attributePath);
                }
            }
        }
        entityLogAttrs = createLogAttributes(entity, dirtyAttributes, changes);
    }
    if (!entityLogAttrs.isEmpty() || type == EntityLogItem.Type.RESTORE) {
        item = metadata.create(EntityLogItem.class);
        item.setEventTs(ts);
        item.setUsername(findUsername());
        item.setType(type);
        item.setEntity(extendedEntities.getOriginalOrThisMetaClass(metaClass).getName());
        item.setEntityInstanceName(metadataTools.getInstanceName(entity));
        item.getEntityRef().setObjectEntityId(referenceToEntitySupport.getReferenceId(entity));
        item.setAttributes(entityLogAttrs);
    }
    return item;
}
Also used : EntityLogAttr(io.jmix.audit.entity.EntityLogAttr) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) EntityLogItem(io.jmix.audit.entity.EntityLogItem)

Example 2 with EntityLogItem

use of io.jmix.audit.entity.EntityLogItem in project jmix by jmix-framework.

the class EntityLogImpl method computeChanges.

protected void computeChanges(EntityLogItem itemToSave, List<EntityLogItem> sameEntityList) {
    Set<String> attributes = sameEntityList.stream().flatMap(entityLogItem -> entityLogItem.getAttributes().stream().map(EntityLogAttr::getName)).collect(Collectors.toSet());
    processAttributes(itemToSave, sameEntityList, attributes);
    Properties properties = new Properties();
    for (EntityLogAttr attr : itemToSave.getAttributes()) {
        properties.setProperty(attr.getName(), Strings.nullToEmpty(attr.getValue()));
        if (attr.getValueId() != null) {
            properties.setProperty(attr.getName() + EntityLogAttr.VALUE_ID_SUFFIX, attr.getValueId());
        }
        if (attr.getOldValue() != null) {
            properties.setProperty(attr.getName() + EntityLogAttr.OLD_VALUE_SUFFIX, attr.getOldValue());
        }
        if (attr.getOldValueId() != null) {
            properties.setProperty(attr.getName() + EntityLogAttr.OLD_VALUE_ID_SUFFIX, attr.getOldValueId());
        }
        if (attr.getMessagesPack() != null) {
            properties.setProperty(attr.getName() + EntityLogAttr.MP_SUFFIX, attr.getMessagesPack());
        }
    }
    if (itemToSave.getType() == EntityLogItem.Type.MODIFY) {
        sameEntityList.stream().filter(entityLogItem -> entityLogItem.getType() == EntityLogItem.Type.CREATE).findFirst().ifPresent(entityLogItem -> itemToSave.setType(EntityLogItem.Type.CREATE));
    }
    itemToSave.setChanges(getChanges(properties));
}
Also used : EntitySystemAccess(io.jmix.core.entity.EntitySystemAccess) EntityLogItem(io.jmix.audit.entity.EntityLogItem) MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) Preconditions(io.jmix.core.common.util.Preconditions) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) TransactionDefinition(org.springframework.transaction.TransactionDefinition) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditProperties(io.jmix.audit.AuditProperties) io.jmix.core(io.jmix.core) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) EntityLog(io.jmix.audit.EntityLog) TypedQuery(javax.persistence.TypedQuery) EntityValues(io.jmix.core.entity.EntityValues) EntityOp(io.jmix.core.security.EntityOp) Strings(com.google.common.base.Strings) DatatypeRegistry(io.jmix.core.metamodel.datatype.DatatypeRegistry) AuditInfoProvider(io.jmix.data.AuditInfoProvider) UserDetails(org.springframework.security.core.userdetails.UserDetails) Range(io.jmix.core.metamodel.model.Range) Nullable(javax.annotation.Nullable) AttributeChangesProvider(io.jmix.data.AttributeChangesProvider) EntityLogAttr(io.jmix.audit.entity.EntityLogAttr) Logger(org.slf4j.Logger) LoggedEntity(io.jmix.audit.entity.LoggedEntity) AttributeChanges(io.jmix.core.event.AttributeChanges) StringWriter(java.io.StringWriter) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) Datatype(io.jmix.core.metamodel.datatype.Datatype) JpaLifecycleListener(io.jmix.data.impl.JpaLifecycleListener) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) LoggedAttribute(io.jmix.audit.entity.LoggedAttribute) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) org.springframework.transaction.support(org.springframework.transaction.support) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) EntityLogAttr(io.jmix.audit.entity.EntityLogAttr) AuditProperties(io.jmix.audit.AuditProperties)

Example 3 with EntityLogItem

use of io.jmix.audit.entity.EntityLogItem in project jmix by jmix-framework.

the class EntityLogImpl method internalRegisterCreate.

protected void internalRegisterCreate(Object entity, String entityName, String storeName, Set<String> attributes) {
    EntityLogItem item;
    // Create a new transaction in main DB if we are saving an entity from additional data store
    if (!Stores.isMain(storeName)) {
        item = transaction.execute(status -> generateEntityLogItem(entity, entityName, attributes, EntityLogItem.Type.CREATE));
    } else {
        item = generateEntityLogItem(entity, entityName, attributes, EntityLogItem.Type.CREATE);
    }
    assert item != null;
    enqueueItem(item, storeName);
}
Also used : EntitySystemAccess(io.jmix.core.entity.EntitySystemAccess) EntityLogItem(io.jmix.audit.entity.EntityLogItem) MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) Preconditions(io.jmix.core.common.util.Preconditions) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) TransactionDefinition(org.springframework.transaction.TransactionDefinition) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditProperties(io.jmix.audit.AuditProperties) io.jmix.core(io.jmix.core) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) EntityLog(io.jmix.audit.EntityLog) TypedQuery(javax.persistence.TypedQuery) EntityValues(io.jmix.core.entity.EntityValues) EntityOp(io.jmix.core.security.EntityOp) Strings(com.google.common.base.Strings) DatatypeRegistry(io.jmix.core.metamodel.datatype.DatatypeRegistry) AuditInfoProvider(io.jmix.data.AuditInfoProvider) UserDetails(org.springframework.security.core.userdetails.UserDetails) Range(io.jmix.core.metamodel.model.Range) Nullable(javax.annotation.Nullable) AttributeChangesProvider(io.jmix.data.AttributeChangesProvider) EntityLogAttr(io.jmix.audit.entity.EntityLogAttr) Logger(org.slf4j.Logger) LoggedEntity(io.jmix.audit.entity.LoggedEntity) AttributeChanges(io.jmix.core.event.AttributeChanges) StringWriter(java.io.StringWriter) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) Datatype(io.jmix.core.metamodel.datatype.Datatype) JpaLifecycleListener(io.jmix.data.impl.JpaLifecycleListener) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) LoggedAttribute(io.jmix.audit.entity.LoggedAttribute) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) org.springframework.transaction.support(org.springframework.transaction.support) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) EntityLogItem(io.jmix.audit.entity.EntityLogItem)

Example 4 with EntityLogItem

use of io.jmix.audit.entity.EntityLogItem in project jmix by jmix-framework.

the class EntityLogImpl method flush.

@Override
public void flush(String storeName) {
    EntityLogResourceHolder holder = getEntityLogResourceHolder();
    List<EntityLogItem> items = holder.getItems(storeName);
    if (items == null || items.isEmpty())
        return;
    Set<EntityLogItem> saved = new LinkedHashSet<>();
    for (EntityLogItem item : items) {
        List<EntityLogItem> sameEntityList = items.stream().filter(entityLogItem -> entityLogItem.getDbGeneratedIdEntity() != null ? entityLogItem.getDbGeneratedIdEntity().equals(item.getDbGeneratedIdEntity()) : entityLogItem.getEntityRef().getObjectEntityId().equals(item.getEntityRef().getObjectEntityId())).collect(Collectors.toList());
        EntityLogItem itemToSave = sameEntityList.get(0);
        if (!saved.contains(itemToSave)) {
            computeChanges(itemToSave, sameEntityList);
            saved.add(itemToSave);
            saveItem(itemToSave);
        }
    }
}
Also used : EntitySystemAccess(io.jmix.core.entity.EntitySystemAccess) EntityLogItem(io.jmix.audit.entity.EntityLogItem) MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) Preconditions(io.jmix.core.common.util.Preconditions) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) TransactionDefinition(org.springframework.transaction.TransactionDefinition) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditProperties(io.jmix.audit.AuditProperties) io.jmix.core(io.jmix.core) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) EntityLog(io.jmix.audit.EntityLog) TypedQuery(javax.persistence.TypedQuery) EntityValues(io.jmix.core.entity.EntityValues) EntityOp(io.jmix.core.security.EntityOp) Strings(com.google.common.base.Strings) DatatypeRegistry(io.jmix.core.metamodel.datatype.DatatypeRegistry) AuditInfoProvider(io.jmix.data.AuditInfoProvider) UserDetails(org.springframework.security.core.userdetails.UserDetails) Range(io.jmix.core.metamodel.model.Range) Nullable(javax.annotation.Nullable) AttributeChangesProvider(io.jmix.data.AttributeChangesProvider) EntityLogAttr(io.jmix.audit.entity.EntityLogAttr) Logger(org.slf4j.Logger) LoggedEntity(io.jmix.audit.entity.LoggedEntity) AttributeChanges(io.jmix.core.event.AttributeChanges) StringWriter(java.io.StringWriter) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) Datatype(io.jmix.core.metamodel.datatype.Datatype) JpaLifecycleListener(io.jmix.data.impl.JpaLifecycleListener) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) LoggedAttribute(io.jmix.audit.entity.LoggedAttribute) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) org.springframework.transaction.support(org.springframework.transaction.support) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) EntityLogItem(io.jmix.audit.entity.EntityLogItem)

Example 5 with EntityLogItem

use of io.jmix.audit.entity.EntityLogItem in project jmix by jmix-framework.

the class EntityLogImpl method generateEntityLogItem.

protected EntityLogItem generateEntityLogItem(Object entity, String entityName, Set<String> attributes, EntityLogItem.Type type) {
    EntityLogItem item;
    Date ts = timeSource.currentTimestamp();
    item = metadata.create(EntityLogItem.class);
    item.setEventTs(ts);
    item.setUsername(findUsername());
    item.setType(type);
    item.setEntity(entityName);
    item.setEntityInstanceName(metadataTools.getInstanceName(entity));
    if (metadataTools.hasDbGeneratedPrimaryKey(metadata.getClass(entity)) && EntityLogItem.Type.CREATE.equals(type)) {
        item.setDbGeneratedIdEntity(entity);
    } else {
        item.getEntityRef().setObjectEntityId(referenceToEntitySupport.getReferenceId(entity));
    }
    item.setAttributes(createLogAttributes(entity, attributes, null));
    return item;
}
Also used : EntityLogItem(io.jmix.audit.entity.EntityLogItem)

Aggregations

EntityLogItem (io.jmix.audit.entity.EntityLogItem)7 EntityLogAttr (io.jmix.audit.entity.EntityLogAttr)6 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)6 Strings (com.google.common.base.Strings)5 AuditProperties (io.jmix.audit.AuditProperties)5 EntityLog (io.jmix.audit.EntityLog)5 LoggedAttribute (io.jmix.audit.entity.LoggedAttribute)5 LoggedEntity (io.jmix.audit.entity.LoggedEntity)5 io.jmix.core (io.jmix.core)5 Preconditions (io.jmix.core.common.util.Preconditions)5 EntitySystemAccess (io.jmix.core.entity.EntitySystemAccess)5 EntityValues (io.jmix.core.entity.EntityValues)5 AttributeChanges (io.jmix.core.event.AttributeChanges)5 Datatype (io.jmix.core.metamodel.datatype.Datatype)5 DatatypeRegistry (io.jmix.core.metamodel.datatype.DatatypeRegistry)5 EnumClass (io.jmix.core.metamodel.datatype.impl.EnumClass)5 MetaClass (io.jmix.core.metamodel.model.MetaClass)5 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)5 Range (io.jmix.core.metamodel.model.Range)5 EntityOp (io.jmix.core.security.EntityOp)5