Search in sources :

Example 1 with CategoryAttributeValue

use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.

the class DynAttrManagerImpl method fetchEntityValues.

/**
 * Method loads entity values for CategoryAttributeValues of entity type and sets entity values to the corresponding
 * property of the {@code CategoryAttributeValue} entity.
 */
@SuppressWarnings("unchecked")
protected void fetchEntityValues(Collection<AccessConstraint<?>> accessConstraints, List<CategoryAttributeValue> values) {
    Multimap<MetaClass, Object> entityIds = HashMultimap.create();
    Multimap<MetaClass, CategoryAttributeValue> valuesByType = HashMultimap.create();
    for (CategoryAttributeValue value : values) {
        String className = value.getCategoryAttribute().getEntityClass();
        try {
            Class<?> aClass = ReflectionHelper.loadClass(className);
            MetaClass metaClass = metadata.getClass(aClass);
            CrudEntityContext crudEntityContext = new CrudEntityContext(metaClass);
            accessManager.applyConstraints(crudEntityContext, accessConstraints);
            if (crudEntityContext.isReadPermitted()) {
                entityIds.put(metaClass, value.getObjectEntityValueId());
                valuesByType.put(metaClass, value);
            }
        } catch (ClassNotFoundException e) {
            log.error("Class {} not found", className);
        }
    }
    EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
    for (Map.Entry<MetaClass, Collection<Object>> entry : entityIds.asMap().entrySet()) {
        MetaClass metaClass = entry.getKey();
        Collection<Object> ids = entry.getValue();
        if (!ids.isEmpty()) {
            String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(metaClass);
            List<?> resultList = entityManager.createQuery(String.format("select e from %s e where e.%s in :ids", metaClass.getName(), pkName)).setParameter("ids", ids).setHint(PersistenceHints.FETCH_PLAN, fetchPlanRepository.getFetchPlan(metaClass, FetchPlan.INSTANCE_NAME)).getResultList();
            Map<Object, Object> entityById = new LinkedHashMap<>();
            for (Object entity : resultList) {
                entityById.put(EntityValues.getId(entity), entity);
            }
            for (CategoryAttributeValue value : valuesByType.get(metaClass)) {
                value.setTransientEntityValue(entityById.get(value.getObjectEntityValueId()));
            }
        }
    }
}
Also used : CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue) EntityManager(javax.persistence.EntityManager) MetaClass(io.jmix.core.metamodel.model.MetaClass) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext)

Example 2 with CategoryAttributeValue

use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.

the class DynAttrManagerImpl method fetchCollectionValues.

protected List<CategoryAttributeValue> fetchCollectionValues(List<CategoryAttributeValue> values) {
    EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
    List<UUID> ids = values.stream().map(CategoryAttributeValue::getId).collect(Collectors.toList());
    FetchPlan fetchPlan = fetchPlans.builder(CategoryAttributeValue.class).addFetchPlan(FetchPlan.LOCAL).add("childValues", FetchPlan.LOCAL).add("categoryAttribute", builder -> builder.addFetchPlan(FetchPlan.LOCAL).add("category")).build();
    return entityManager.createQuery("select v from dynat_CategoryAttributeValue v where v.id in :ids", CategoryAttributeValue.class).setParameter("ids", ids).setHint(PersistenceHints.FETCH_PLAN, fetchPlan).getResultList();
}
Also used : EntitySystemAccess(io.jmix.core.entity.EntitySystemAccess) MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) io.jmix.dynattr(io.jmix.dynattr) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) io.jmix.core(io.jmix.core) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Multimap(com.google.common.collect.Multimap) EntityValues(io.jmix.core.entity.EntityValues) BigDecimal(java.math.BigDecimal) HashMultimap(com.google.common.collect.HashMultimap) CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) PersistenceHints(io.jmix.data.PersistenceHints) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) LocalDate(java.time.LocalDate) ReflectionHelper(io.jmix.core.common.util.ReflectionHelper) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) AccessConstraint(io.jmix.core.constraint.AccessConstraint) StoreAwareLocator(io.jmix.data.StoreAwareLocator) EntityManager(javax.persistence.EntityManager) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue)

Example 3 with CategoryAttributeValue

use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.

the class DynAttrManagerImpl method doStoreValues.

@SuppressWarnings("unchecked")
protected void doStoreValues(Object entity, Collection<AccessConstraint<?>> accessConstraints) {
    DynamicAttributesState state = getExtraState(entity, DynamicAttributesState.class);
    if (state != null && state.getDynamicAttributes() != null) {
        EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
        DynamicAttributes dynamicModel = state.getDynamicAttributes();
        DynamicAttributes.Changes changes = dynamicModel.getChanges();
        if (changes.hasChanges()) {
            MetaClass metaClass = metadata.getClass(entity.getClass());
            List<CategoryAttributeValue> attributeValues = loadValues(metaClass, accessConstraints, Collections.singletonList(referenceToEntitySupport.getReferenceId(entity)));
            for (CategoryAttributeValue attributeValue : attributeValues) {
                String attributeName = attributeValue.getCode();
                if (changes.isDeleted(attributeName)) {
                    setValueToCategoryAttributeValue(attributeValue, null);
                    entityManager.remove(attributeValue);
                } else if (changes.isUpdated(attributeName)) {
                    setValueToCategoryAttributeValue(attributeValue, dynamicModel.getValue(attributeName));
                    if (BooleanUtils.isTrue(attributeValue.getCategoryAttribute().getIsCollection())) {
                        doStoreCollectionValue(attributeValue);
                    }
                }
            }
            List<String> existing = attributeValues.stream().map(CategoryAttributeValue::getCode).collect(Collectors.toList());
            List<String> toPersist = Stream.concat(changes.getCreated().keySet().stream(), // Haulmont/jmix-data#43
            changes.getUpdated().keySet().stream().filter(a -> !existing.contains(a))).collect(Collectors.toList());
            for (String attributeName : toPersist) {
                dynAttrMetadata.getAttributeByCode(metaClass, attributeName).ifPresent(attribute -> {
                    CategoryAttributeValue attributeValue = metadata.create(CategoryAttributeValue.class);
                    setValueToCategoryAttributeValue(attributeValue, dynamicModel.getValue(attributeName));
                    attributeValue.setObjectEntityId(referenceToEntitySupport.getReferenceId(entity));
                    attributeValue.setCode(attributeName);
                    attributeValue.setCategoryAttribute((CategoryAttribute) attribute.getSource());
                    entityManager.persist(attributeValue);
                    if (attribute.isCollection()) {
                        doStoreCollectionValue(attributeValue);
                    }
                });
            }
        }
    // todo: refresh state
    // state.setValues(mergedValues);
    }
}
Also used : EntityManager(javax.persistence.EntityManager) MetaClass(io.jmix.core.metamodel.model.MetaClass) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue)

Example 4 with CategoryAttributeValue

use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.

the class DynAttrManagerImpl method findValuesByEntityIds.

protected List<CategoryAttributeValue> findValuesByEntityIds(MetaClass metaClass, List<Object> entityIds) {
    EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
    FetchPlan fetchPlan = fetchPlans.builder(CategoryAttributeValue.class).add("categoryAttribute", builder -> {
        builder.addFetchPlan(FetchPlan.LOCAL);
        builder.add("defaultEntity", FetchPlan.LOCAL);
        builder.add("category");
    }).build();
    List<CategoryAttributeValue> result;
    if (metadataTools.hasUuid(metaClass)) {
        result = entityManager.createQuery(String.format("select v from dynat_CategoryAttributeValue v where v.entity.%s in :ids and v.parent is null", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), CategoryAttributeValue.class).setParameter("ids", entityIds).setHint(PersistenceHints.FETCH_PLAN, fetchPlan).getResultList();
    } else {
        result = entityManager.createQuery(String.format("select v from dynat_CategoryAttributeValue v where v.entity.%s in :ids " + "and v.categoryAttribute.categoryEntityType = :entityType and v.parent is null", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), CategoryAttributeValue.class).setParameter("ids", entityIds).setParameter("entityType", metaClass.getName()).setHint(PersistenceHints.FETCH_PLAN, fetchPlan).getResultList();
    }
    return result.stream().filter(v -> v.getDeleteTs() == null).collect(Collectors.toList());
}
Also used : EntitySystemAccess(io.jmix.core.entity.EntitySystemAccess) MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) io.jmix.dynattr(io.jmix.dynattr) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) io.jmix.core(io.jmix.core) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Multimap(com.google.common.collect.Multimap) EntityValues(io.jmix.core.entity.EntityValues) BigDecimal(java.math.BigDecimal) HashMultimap(com.google.common.collect.HashMultimap) CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) PersistenceHints(io.jmix.data.PersistenceHints) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) LocalDate(java.time.LocalDate) ReflectionHelper(io.jmix.core.common.util.ReflectionHelper) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) AccessConstraint(io.jmix.core.constraint.AccessConstraint) StoreAwareLocator(io.jmix.data.StoreAwareLocator) EntityManager(javax.persistence.EntityManager) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue)

Example 5 with CategoryAttributeValue

use of io.jmix.dynattr.model.CategoryAttributeValue in project jmix by jmix-framework.

the class DynAttrManagerImpl method doStoreCollectionValue.

/**
 * Removes nested {@code CategoryAttributeValue} entities for items that were removed from the collection value
 * and creates new child {@code CategoryAttributeValue} instances for just added collection value items.
 *
 * @param collectionAttributeValue
 */
protected void doStoreCollectionValue(CategoryAttributeValue collectionAttributeValue) {
    EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
    List<Object> collection = collectionAttributeValue.getTransientCollectionValue();
    List<Object> newCollection = new ArrayList<>(collection);
    if (collectionAttributeValue.getChildValues() != null) {
        for (CategoryAttributeValue existingChild : collectionAttributeValue.getChildValues()) {
            if (existingChild.getDeleteTs() == null) {
                if (!collection.contains(existingChild.getValue())) {
                    entityManager.remove(existingChild);
                }
                newCollection.remove(existingChild.getValue());
            }
        }
    }
    for (Object value : newCollection) {
        CategoryAttributeValue childValue = metadata.create(CategoryAttributeValue.class);
        childValue.setParent(collectionAttributeValue);
        setValueToCategoryAttributeValue(childValue, value);
        if (collectionAttributeValue.getObjectEntityId() != null) {
            childValue.setObjectEntityId(collectionAttributeValue.getObjectEntityId());
        }
        childValue.setCode(collectionAttributeValue.getCode());
        childValue.setCategoryAttribute(collectionAttributeValue.getCategoryAttribute());
        entityManager.persist(childValue);
    }
}
Also used : EntityManager(javax.persistence.EntityManager) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue)

Aggregations

CategoryAttributeValue (io.jmix.dynattr.model.CategoryAttributeValue)7 EntityManager (javax.persistence.EntityManager)7 MetaClass (io.jmix.core.metamodel.model.MetaClass)6 CrudEntityContext (io.jmix.core.accesscontext.CrudEntityContext)5 HashMultimap (com.google.common.collect.HashMultimap)4 Multimap (com.google.common.collect.Multimap)4 io.jmix.core (io.jmix.core)4 ReflectionHelper (io.jmix.core.common.util.ReflectionHelper)4 AccessConstraint (io.jmix.core.constraint.AccessConstraint)4 EntitySystemAccess (io.jmix.core.entity.EntitySystemAccess)4 EntityValues (io.jmix.core.entity.EntityValues)4 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)4 PersistenceHints (io.jmix.data.PersistenceHints)4 StoreAwareLocator (io.jmix.data.StoreAwareLocator)4 io.jmix.dynattr (io.jmix.dynattr)4 CategoryAttribute (io.jmix.dynattr.model.CategoryAttribute)4 BigDecimal (java.math.BigDecimal)4 LocalDate (java.time.LocalDate)4 java.util (java.util)4 Collectors (java.util.stream.Collectors)4