Search in sources :

Example 1 with Category

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

the class DynAttrMetadataImpl method loadCategoryDefinitions.

protected List<CategoryDefinition> loadCategoryDefinitions(String entityName) {
    TransactionTemplate template = storeAwareLocator.getTransactionTemplate(dynamicAttributesStore);
    template.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    List<CategoryDefinition> result = template.execute(transactionStatus -> {
        EntityManager entityManager = storeAwareLocator.getEntityManager(dynamicAttributesStore);
        FetchPlan fetchPlan = fetchPlans.builder(Category.class).addFetchPlan(FetchPlan.LOCAL).add("categoryAttrs", builder -> {
            builder.addFetchPlan(FetchPlan.LOCAL);
            builder.add("category", FetchPlan.LOCAL);
            builder.add("defaultEntity", FetchPlan.LOCAL);
        }).build();
        return entityManager.createQuery("select c from dynat_Category c where c.entityType = :entityType", Category.class).setParameter("entityType", entityName).setHint(PersistenceHints.FETCH_PLAN, fetchPlan).getResultList().stream().map(this::buildCategoryDefinition).collect(Collectors.toList());
    });
    return result == null ? Collections.emptyList() : result;
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) Cache(org.springframework.cache.Cache) TransactionDefinition(org.springframework.transaction.TransactionDefinition) io.jmix.dynattr(io.jmix.dynattr) PersistenceHints(io.jmix.data.PersistenceHints) Autowired(org.springframework.beans.factory.annotation.Autowired) io.jmix.core(io.jmix.core) EntityManager(javax.persistence.EntityManager) Datatype(io.jmix.core.metamodel.datatype.Datatype) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Component(org.springframework.stereotype.Component) DatatypeRegistry(io.jmix.core.metamodel.datatype.DatatypeRegistry) CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) CacheManager(org.springframework.cache.CacheManager) Category(io.jmix.dynattr.model.Category) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PostConstruct(javax.annotation.PostConstruct) ReflectionHelper(io.jmix.core.common.util.ReflectionHelper) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) StoreAwareLocator(io.jmix.data.StoreAwareLocator) EntityManager(javax.persistence.EntityManager) Category(io.jmix.dynattr.model.Category) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate)

Example 2 with Category

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

the class DynAttrMetadataImpl method buildCategoryDefinition.

protected CategoryDefinition buildCategoryDefinition(Category category) {
    MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(metadata.getClass(category.getEntityType()));
    List<AttributeDefinition> attributes;
    if (category.getCategoryAttrs() != null) {
        attributes = Collections.unmodifiableList(category.getCategoryAttrs().stream().map(attr -> new CommonAttributeDefinition(attr, buildMetaProperty(attr, metaClass))).collect(Collectors.toList()));
    } else {
        attributes = Collections.emptyList();
    }
    return new CommonCategoryDefinition(category, attributes);
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) Cache(org.springframework.cache.Cache) TransactionDefinition(org.springframework.transaction.TransactionDefinition) io.jmix.dynattr(io.jmix.dynattr) PersistenceHints(io.jmix.data.PersistenceHints) Autowired(org.springframework.beans.factory.annotation.Autowired) io.jmix.core(io.jmix.core) EntityManager(javax.persistence.EntityManager) Datatype(io.jmix.core.metamodel.datatype.Datatype) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Component(org.springframework.stereotype.Component) DatatypeRegistry(io.jmix.core.metamodel.datatype.DatatypeRegistry) CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) CacheManager(org.springframework.cache.CacheManager) Category(io.jmix.dynattr.model.Category) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PostConstruct(javax.annotation.PostConstruct) ReflectionHelper(io.jmix.core.common.util.ReflectionHelper) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) StoreAwareLocator(io.jmix.data.StoreAwareLocator) MetaClass(io.jmix.core.metamodel.model.MetaClass)

Example 3 with Category

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

the class CategoryEdit method onIsDefaultFieldValueChange.

@Subscribe("isDefaultField")
protected void onIsDefaultFieldValueChange(HasValue.ValueChangeEvent<Boolean> event) {
    if (Boolean.TRUE.equals(event.getValue())) {
        FetchPlan fetchPlan = fetchPlans.builder(Category.class).add("isDefault").build();
        LoadContext<Category> lc = new LoadContext(metadata.getClass(Category.class)).setFetchPlan(fetchPlan);
        Category category = getEditedEntity();
        lc.setQueryString("select c from dynat_Category c where c.entityType = :entityType and not c.id = :id").setParameter("entityType", category.getEntityType()).setParameter("id", category.getId());
        List<Category> result = dataManager.loadList(lc);
        for (Category cat : result) {
            cat.setIsDefault(false);
        }
        SaveContext saveContext = new SaveContext().saving(result);
        dataManager.save(saveContext);
    }
}
Also used : Category(io.jmix.dynattr.model.Category)

Example 4 with Category

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

the class DynamicAttributesGuiTools method setDefaultAttributeValue.

protected void setDefaultAttributeValue(Entity item, AttributeDefinition attribute, boolean entityIsCategorized, ZonedDateTime currentTimestamp) {
    String propertyName = DynAttrUtils.getPropertyFromAttributeCode(attribute.getCode());
    if (entityIsCategorized) {
        Category entityCategory = ((Categorized) item).getCategory();
        Category attributeCategory = ((CategoryAttribute) attribute.getSource()).getCategory();
        if (!Objects.equals(entityCategory, attributeCategory)) {
            // cleanup attributes from not dedicated category
            EntityValues.setValue(item, propertyName, null);
            return;
        }
    }
    if (EntityValues.getValue(item, propertyName) != null) {
        // skip not null attributes
        return;
    }
    if (attribute.getDefaultValue() != null) {
        if (attribute.getDataType() == AttributeType.ENTITY) {
            MetaClass entityMetaClass = metadata.getClassNN(attribute.getJavaType());
            LoadContext<Object> lc = new LoadContext<>(entityMetaClass).setFetchPlan(fetchPlanRepository.getFetchPlan(entityMetaClass, FetchPlan.INSTANCE_NAME));
            String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(entityMetaClass);
            lc.setQueryString(format("select e from %s e where e.%s = :entityId", entityMetaClass.getName(), pkName)).setParameter("entityId", attribute.getDefaultValue());
            Entity defaultEntity = (Entity) dataManager.load(lc);
            EntityValues.setValue(item, propertyName, defaultEntity);
        } else if (attribute.isCollection()) {
            List<Object> list = new ArrayList<>();
            list.add(attribute.getDefaultValue());
            EntityValues.setValue(item, propertyName, list);
        } else {
            EntityValues.setValue(item, propertyName, attribute.getDefaultValue());
        }
    } else if (Boolean.TRUE.equals(attribute.isDefaultDateCurrent())) {
        if (attribute.getDataType() == AttributeType.DATE_WITHOUT_TIME) {
            EntityValues.setValue(item, propertyName, currentTimestamp.toLocalDate());
        } else {
            EntityValues.setValue(item, propertyName, Date.from(currentTimestamp.toInstant()));
        }
    }
}
Also used : Categorized(io.jmix.dynattr.model.Categorized) Category(io.jmix.dynattr.model.Category) CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) MetaClass(io.jmix.core.metamodel.model.MetaClass)

Example 5 with Category

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

the class CategoryAttrsEdit method onValidation.

@Subscribe
protected void onValidation(ValidationEvent event) {
    ValidationErrors validationErrors = new ValidationErrors();
    CategoryAttribute attribute = getEditedEntity();
    AttributeType dataType = attribute.getDataType();
    CategoryAttributeConfiguration configuration = attribute.getConfiguration();
    if (INTEGER.equals(dataType)) {
        ValidationErrors errors = validateNumbers(INTEGER, configuration.getMinInt(), configuration.getMaxInt(), attribute.getDefaultInt());
        validationErrors.addAll(errors);
    } else if (DOUBLE.equals(dataType)) {
        ValidationErrors errors = validateNumbers(DOUBLE, configuration.getMinDouble(), configuration.getMaxDouble(), attribute.getDefaultDouble());
        validationErrors.addAll(errors);
    } else if (DECIMAL.equals(dataType)) {
        ValidationErrors errors = validateNumbers(DECIMAL, configuration.getMinDecimal(), configuration.getMaxDecimal(), attribute.getDefaultDecimal());
        validationErrors.addAll(errors);
    } else if (ENUMERATION.equals(dataType)) {
        ValidationErrors errors = validateEnumeration(attribute.getEnumeration(), attribute.getDefaultString());
        validationErrors.addAll(errors);
    }
    Category category = getEditedEntity().getCategory();
    if (category != null && category.getCategoryAttrs() != null) {
        for (CategoryAttribute categoryAttribute : category.getCategoryAttrs()) {
            if (!categoryAttribute.equals(attribute)) {
                if (categoryAttribute.getName().equals(attribute.getName())) {
                    validationErrors.add(messages.getMessage(CategoryAttrsEdit.class, "uniqueName"));
                    return;
                } else if (categoryAttribute.getCode().equals(attribute.getCode())) {
                    validationErrors.add(messages.getMessage(CategoryAttrsEdit.class, "uniqueCode"));
                    return;
                }
            }
        }
    }
    event.addErrors(validationErrors);
}
Also used : CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) Category(io.jmix.dynattr.model.Category) CategoryAttributeConfiguration(io.jmix.dynattr.model.CategoryAttributeConfiguration) AttributeType(io.jmix.dynattr.AttributeType)

Aggregations

Category (io.jmix.dynattr.model.Category)7 MetaClass (io.jmix.core.metamodel.model.MetaClass)4 CategoryAttribute (io.jmix.dynattr.model.CategoryAttribute)4 io.jmix.core (io.jmix.core)2 ReflectionHelper (io.jmix.core.common.util.ReflectionHelper)2 Datatype (io.jmix.core.metamodel.datatype.Datatype)2 DatatypeRegistry (io.jmix.core.metamodel.datatype.DatatypeRegistry)2 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)2 PersistenceHints (io.jmix.data.PersistenceHints)2 StoreAwareLocator (io.jmix.data.StoreAwareLocator)2 io.jmix.dynattr (io.jmix.dynattr)2 Serializable (java.io.Serializable)2 java.util (java.util)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 PostConstruct (javax.annotation.PostConstruct)2 EntityManager (javax.persistence.EntityManager)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Cache (org.springframework.cache.Cache)2 CacheManager (org.springframework.cache.CacheManager)2