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++;
}
}
}
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();
}
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;
}
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);
}
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;
}
Aggregations