use of com.haulmont.cuba.gui.data.DynamicAttributesEntity in project cuba by cuba-platform.
the class DynamicAttributesGuiTools method getValueChangeEventListener.
/**
* Returns {@code ValueChangeEventListener} for dynamic attribute that has one or more dependent attributes.
* This listener recalculates values for all dependent dynamic attributes hierarchically. The listener uses
* {@code recalculationInProgress} ThreadLocal variable to avoid unnecessary calculation.
*/
@SuppressWarnings("unchecked")
public Consumer<HasValue.ValueChangeEvent> getValueChangeEventListener(final CategoryAttribute attribute) {
if (attribute.getConfiguration().getDependentAttributes() != null && !attribute.getConfiguration().getDependentAttributes().isEmpty()) {
return valueChangeEvent -> {
if (Boolean.TRUE.equals(recalculationInProgress.get())) {
return;
}
try {
recalculationInProgress.set(true);
com.haulmont.cuba.gui.components.Component component = valueChangeEvent.getComponent();
if (component instanceof HasValueSource) {
{
BaseGenericIdEntity entity = null;
String attributeCode = DynamicAttributesUtils.encodeAttributeCode(attribute.getCode());
if (((HasValueSource) component).getValueSource() instanceof ContainerValueSource) {
ContainerValueSource valueSource = (ContainerValueSource) ((HasValueSource) component).getValueSource();
InstanceContainer container = valueSource.getContainer();
if (container.getItem() instanceof BaseGenericIdEntity) {
entity = (BaseGenericIdEntity) container.getItem();
}
} else if (((HasValueSource) component).getValueSource() instanceof DatasourceValueSource) {
DatasourceValueSource valueSource = (DatasourceValueSource) ((HasValueSource) component).getValueSource();
if (valueSource.getItem() instanceof BaseGenericIdEntity) {
entity = (BaseGenericIdEntity) valueSource.getItem();
} else if (valueSource.getItem() instanceof DynamicAttributesEntity) {
entity = ((DynamicAttributesEntity) valueSource.getItem()).getMainItem();
}
}
entity.setValue(attributeCode, valueChangeEvent.getValue());
recalculationTools.recalculateDynamicAttributes(entity, attribute);
}
}
} finally {
recalculationInProgress.remove();
}
};
}
return null;
}
Aggregations