Search in sources :

Example 6 with AttributeDefinition

use of io.jmix.dynattr.AttributeDefinition in project jmix by jmix-framework.

the class AttributeRecalculationManager method recalculateByAttribute.

/**
 * Performs recalculation for all dependent dynamic attributes. Recalculation is performed hierarchically.
 * <p>
 * Recalculation level limited by
 * {@code cuba.dynamicAttributes.maxRecalculationLevel} application property. If this property is not defined
 * then the default value is used (default value is 10).
 *
 * @param entity    entity with loaded dynamic attributes.
 * @param attribute an attribute from which the recalculation begins. Value for this attribute won't be changed,
 *                  it is assumed that this attribute was updated before
 */
public void recalculateByAttribute(Object entity, AttributeDefinition attribute) {
    Set<AttributeDefinition> dependentAttributes = attributeDependencies.getDependentAttributes(attribute);
    if (!dependentAttributes.isEmpty()) {
        Set<AttributeDefinition> needToRecalculate = new HashSet<>(dependentAttributes);
        int recalculationLevel = 1;
        while (!needToRecalculate.isEmpty()) {
            if (recalculationLevel > dynAttrUiProperties.getMaxRecalculationLevel()) {
                throw new IllegalStateException(String.format("Recalculation level has reached the maximum allowable value: %d. " + "Check Dynamic Attributes configuration.", dynAttrUiProperties.getMaxRecalculationLevel()));
            }
            Set<AttributeDefinition> nextLevelAttributes = new HashSet<>();
            for (AttributeDefinition dependentAttribute : needToRecalculate) {
                String script = dependentAttribute.getConfiguration().getRecalculationScript();
                if (Strings.isNullOrEmpty(script)) {
                    continue;
                }
                String propertyName = DynAttrUtils.getPropertyFromAttributeCode(dependentAttribute.getCode());
                Object oldValue = EntityValues.getValue(entity, propertyName);
                Object newValue = evaluateNewValue(entity, script);
                if (!Objects.equals(oldValue, newValue)) {
                    EntityValues.setValue(entity, propertyName, newValue);
                    nextLevelAttributes.addAll(attributeDependencies.getDependentAttributes(attribute));
                }
            }
            needToRecalculate = nextLevelAttributes;
            recalculationLevel++;
        }
    }
}
Also used : AttributeDefinition(io.jmix.dynattr.AttributeDefinition)

Example 7 with AttributeDefinition

use of io.jmix.dynattr.AttributeDefinition in project jmix by jmix-framework.

the class DynamicAttributesCondition method getLocCaption.

@Override
public String getLocCaption() {
    if (isBlank(caption) && !isBlank(propertyPath)) {
        MessageTools messageTools = AppBeans.get(MessageTools.class);
        String propertyCaption = messageTools.getPropertyCaption(metaClass, propertyPath);
        if (!isBlank(propertyCaption)) {
            return propertyCaption + "." + locCaption;
        }
    } else if (isNotBlank(caption)) {
        MessageTools messageTools = AppBeans.get(MessageTools.class);
        return messageTools.loadString(messagesPack, caption);
    }
    DynAttrMetadata dynamicModelMetadata = AppBeans.get(DynAttrMetadata.class);
    AttributeDefinition attribute = dynamicModelMetadata.getAttributes(metaClass).stream().filter(attr -> Objects.equals(EntityValues.getId(attr.getSource()), getCategoryAttributeId())).findFirst().orElse(null);
    if (attribute != null) {
        MsgBundleTools msgBundleTools = AppBeans.get(MsgBundleTools.class);
        return msgBundleTools.getLocalizedValue(attribute.getNameMsgBundle(), attribute.getName());
    }
    return super.getLocCaption();
}
Also used : MessageTools(io.jmix.core.MessageTools) DynAttrMetadata(io.jmix.dynattr.DynAttrMetadata) MsgBundleTools(io.jmix.dynattr.MsgBundleTools) AttributeDefinition(io.jmix.dynattr.AttributeDefinition)

Example 8 with AttributeDefinition

use of io.jmix.dynattr.AttributeDefinition in project jmix by jmix-framework.

the class RuntimePropertiesFrame method createFieldsForAttributes.

protected List<FieldGroup.FieldConfig> createFieldsForAttributes(FieldGroup newRuntimeFieldGroup) {
    @SuppressWarnings("unchecked") Collection<AttributeDefinition> attributes = rds.getAttributesByCategory();
    List<FieldGroup.FieldConfig> fields = new ArrayList<>(attributes.size());
    for (AttributeDefinition attribute : attributes) {
        String propertyName = DynAttrUtils.getPropertyFromAttributeCode(attribute.getCode());
        FieldGroup.FieldConfig field = newRuntimeFieldGroup.createField(propertyName);
        field.setProperty(propertyName);
        field.setCaption(msgBundleTools.getLocalizedValue(attribute.getNameMsgBundle(), attribute.getName()));
        field.setDescription(msgBundleTools.getLocalizedValue(attribute.getDescriptionsMsgBundle(), attribute.getDescription()));
        if (StringUtils.isNotBlank(attribute.getConfiguration().getFormWidth())) {
            field.setWidth(new SizeWithUnit(attribute.getConfiguration().getColumnWidth(), SizeUnit.PIXELS).stringValue());
        } else {
            field.setWidth(fieldWidth);
        }
        fields.add(field);
    }
    return fields;
}
Also used : SizeWithUnit(io.jmix.ui.component.SizeWithUnit) AttributeDefinition(io.jmix.dynattr.AttributeDefinition)

Example 9 with AttributeDefinition

use of io.jmix.dynattr.AttributeDefinition in project jmix by jmix-framework.

the class DynamicAttributesConditionFrame method fillAttributeSelect.

protected void fillAttributeSelect(CategoryDefinition category) {
    String currentId = condition.getCategoryAttributeId();
    AttributeDefinition selectedAttribute = null;
    Map<String, AttributeDefinition> options = new TreeMap<>();
    for (AttributeDefinition attribute : category.getAttributeDefinitions()) {
        options.put(msgBundleTools.getLocalizedValue(attribute.getNameMsgBundle(), attribute.getName()), attribute);
        if (Objects.equals(attribute.getId(), currentId)) {
            selectedAttribute = attribute;
        }
    }
    attributeLookup.setOptionsMap(options);
    attributeLookup.setValue(selectedAttribute);
}
Also used : AttributeDefinition(io.jmix.dynattr.AttributeDefinition)

Example 10 with AttributeDefinition

use of io.jmix.dynattr.AttributeDefinition in project jmix by jmix-framework.

the class DynamicAttributesConditionFrame method commit.

@Override
public boolean commit() {
    if (!super.commit())
        return false;
    String error = checkCondition();
    if (error != null) {
        showNotification(messages.getMessage(error), NotificationType.TRAY);
        return false;
    }
    AttributeDefinition attribute = attributeLookup.getValue();
    String cavAlias = "cav" + RandomStringUtils.randomNumeric(5);
    String paramName;
    String operation = operationLookup.getValue().forJpql();
    Op op = operationLookup.getValue();
    Class javaClass = attribute.getMetaProperty().getJavaType();
    String propertyPath = Strings.isNullOrEmpty(condition.getPropertyPath()) ? "" : "." + condition.getPropertyPath();
    ConditionParamBuilder paramBuilder = AppBeans.get(ConditionParamBuilder.class);
    paramName = paramBuilder.createParamName(condition);
    String cavEntityId = referenceToEntitySupport.getReferenceIdPropertyName(condition.getEntityMetaClass());
    String where;
    if (op == Op.NOT_EMPTY) {
        where = "(exists (select " + cavAlias + " from dynat_CategoryAttributeValue " + cavAlias + " where " + cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.getValue().getId() + "'))";
    } else {
        String valueFieldName = "stringValue";
        if (Entity.class.isAssignableFrom(javaClass))
            valueFieldName = "entityValue." + referenceToEntitySupport.getReferenceIdPropertyName(metadata.getClassNN(javaClass));
        else if (String.class.isAssignableFrom(javaClass))
            valueFieldName = "stringValue";
        else if (Integer.class.isAssignableFrom(javaClass))
            valueFieldName = "intValue";
        else if (Double.class.isAssignableFrom(javaClass))
            valueFieldName = "doubleValue";
        else if (Boolean.class.isAssignableFrom(javaClass))
            valueFieldName = "booleanValue";
        else if (Date.class.isAssignableFrom(javaClass))
            valueFieldName = "dateValue";
        if (attribute.isCollection()) {
            condition.setJoin(", dynat_CategoryAttributeValue " + cavAlias + " ");
            String paramStr = " ? ";
            where = cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + "." + valueFieldName + " " + operation + (op.isUnary() ? " " : paramStr) + "and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.getValue().getId() + "'";
            where = where.replace("?", ":" + paramName);
        } else {
            where = "(exists (select " + cavAlias + " from dynat_CategoryAttributeValue " + cavAlias + " where " + cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + "." + valueFieldName + " = :" + paramName + " and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.getValue().getId() + "'))";
        }
    }
    condition.setWhere(where);
    condition.setUnary(op.isUnary());
    condition.setEntityParamView(null);
    condition.setEntityParamWhere(null);
    condition.setInExpr(Op.IN.equals(op) || Op.NOT_IN.equals(op));
    condition.setOperator(operationLookup.getValue());
    Class paramJavaClass = op.isUnary() ? Boolean.class : javaClass;
    condition.setJavaClass(javaClass);
    Param param = Param.Builder.getInstance().setName(paramName).setJavaClass(paramJavaClass).setMetaClass(condition.getEntityMetaClass()).setProperty(attribute.getMetaProperty()).setInExpr(condition.getInExpr()).setRequired(condition.getRequired()).setCategoryAttrId(attribute.getId()).build();
    Object defaultValue = condition.getParam().getDefaultValue();
    param.setDefaultValue(defaultValue);
    condition.setParam(param);
    if (categoryLookup.getValue() != null) {
        condition.setCategoryId(categoryLookup.getValue().getId());
    }
    condition.setCategoryAttributeId(attribute.getId());
    condition.setIsCollection(attribute.isCollection());
    condition.setLocCaption(msgBundleTools.getLocalizedValue(attribute.getNameMsgBundle(), attribute.getName()));
    condition.setCaption(caption.getValue());
    return true;
}
Also used : Op(com.haulmont.cuba.core.global.filter.Op) ConditionParamBuilder(com.haulmont.cuba.gui.components.filter.ConditionParamBuilder) Param(com.haulmont.cuba.gui.components.filter.Param) AttributeDefinition(io.jmix.dynattr.AttributeDefinition) MetaClass(io.jmix.core.metamodel.model.MetaClass)

Aggregations

AttributeDefinition (io.jmix.dynattr.AttributeDefinition)13 MetaClass (io.jmix.core.metamodel.model.MetaClass)3 DynAttrMetadata (io.jmix.dynattr.DynAttrMetadata)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Strings (com.google.common.base.Strings)2 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)2 MsgBundleTools (io.jmix.dynattr.MsgBundleTools)2 StudioComponent (io.jmix.ui.meta.StudioComponent)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Op (com.haulmont.cuba.core.global.filter.Op)1 FieldGroup (com.haulmont.cuba.gui.components.FieldGroup)1 ConditionParamBuilder (com.haulmont.cuba.gui.components.filter.ConditionParamBuilder)1 Param (com.haulmont.cuba.gui.components.filter.Param)1 MessageTools (io.jmix.core.MessageTools)1 Messages (io.jmix.core.Messages)1 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)1 Range (io.jmix.core.metamodel.model.Range)1 CurrentAuthentication (io.jmix.core.security.CurrentAuthentication)1 AttributeType (io.jmix.dynattr.AttributeType)1