Search in sources :

Example 1 with AttributeChanges

use of io.jmix.core.event.AttributeChanges in project jmix by jmix-framework.

the class EntityTrackingListener method processEntityChangedEvent.

protected void processEntityChangedEvent(EntityChangedEvent<?> event) {
    Id<?> entityId = event.getEntityId();
    Class<?> entityClass = entityId.getEntityClass();
    MetaClass metaClass = metadata.getClass(entityClass);
    EntityChangedEvent.Type eventType = event.getType();
    String entityName = metaClass.getName();
    AttributeChanges changes = event.getChanges();
    if (indexConfigurationManager.isDirectlyIndexed(entityName)) {
        log.debug("{} is directly indexed", entityId);
        switch(eventType) {
            case CREATED:
                indexingQueueManager.enqueueIndexByEntityId(entityId);
                break;
            case UPDATED:
                if (isUpdateRequired(entityClass, changes)) {
                    indexingQueueManager.enqueueIndexByEntityId(entityId);
                }
                break;
            case DELETED:
                indexingQueueManager.enqueueDeleteByEntityId(entityId);
                break;
        }
    }
    if (EntityChangedEvent.Type.UPDATED.equals(eventType)) {
        Set<Id<?>> dependentEntityIds = getEntityIdsDependentOnUpdatedEntity(entityId, metaClass, changes);
        if (!dependentEntityIds.isEmpty()) {
            indexingQueueManager.enqueueIndexCollectionByEntityIds(dependentEntityIds);
        }
    } else if (EntityChangedEvent.Type.DELETED.equals(eventType)) {
        Set<Id<?>> dependentEntityIds = removalDependencies.getIfPresent(entityId);
        if (CollectionUtils.isNotEmpty(dependentEntityIds)) {
            indexingQueueManager.enqueueIndexCollectionByEntityIds(dependentEntityIds);
            removalDependencies.invalidate(entityId);
        }
    }
}
Also used : AttributeChanges(io.jmix.core.event.AttributeChanges) MetaClass(io.jmix.core.metamodel.model.MetaClass) EntityChangedEvent(io.jmix.core.event.EntityChangedEvent)

Example 2 with AttributeChanges

use of io.jmix.core.event.AttributeChanges in project jmix by jmix-framework.

the class BaseAttributeChangesProvider method getAttributeChanges.

@Override
public AttributeChanges getAttributeChanges(Object entity) {
    checkEntityState(entity);
    AttributeChanges.Builder builder = AttributeChanges.Builder.create();
    if (!entityStates.isManaged(entity)) {
        return builder.build();
    }
    if (entityStates.isNew(entity)) {
        for (MetaProperty property : metadata.getClass(entity.getClass()).getProperties()) {
            if (metadataTools.isJpa(property)) {
                builder.withChange(property.getName(), EntityValues.getValue(entity, property.getName()));
            }
        }
        return builder.build();
    }
    buildChangesByImplementation(builder, entity, this::convertValueIfNeeded);
    buildExtraChanges(builder, entity);
    return builder.build();
}
Also used : AttributeChanges(io.jmix.core.event.AttributeChanges) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 3 with AttributeChanges

use of io.jmix.core.event.AttributeChanges in project jmix by jmix-framework.

the class EntityLogImpl method registerModify.

@Override
public void registerModify(Object entity, boolean auto, @Nullable AttributeChanges changes) {
    try {
        if (doNotRegister(entity))
            return;
        String entityName = getEntityName(entity);
        Set<String> attributes = getLoggedAttributes(entityName, auto);
        if (attributes != null && attributes.contains("*")) {
            attributes = getAllAttributes(entity);
        }
        if (attributes == null) {
            return;
        }
        MetaClass metaClass = metadata.getClass(entityName);
        attributes = filterRemovedAttributes(entity, attributes);
        String storeName = metaClass.getStore().getName();
        EntityLogItem item;
        // Create a new transaction in main DB if we are saving an entity from additional data store
        if (!Stores.isMain(storeName)) {
            Set<String> finalAttributes = attributes;
            item = transaction.execute(status -> internalRegisterModify(entity, changes, metaClass, storeName, finalAttributes));
        } else {
            item = internalRegisterModify(entity, changes, metaClass, storeName, attributes);
        }
        enqueueItem(item, storeName);
    } catch (Exception e) {
        logError(entity, e);
    }
}
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) MetaClass(io.jmix.core.metamodel.model.MetaClass) EntityLogItem(io.jmix.audit.entity.EntityLogItem) IOException(java.io.IOException)

Aggregations

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