Search in sources :

Example 1 with DynamicAttributesEntity

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;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) com.haulmont.cuba.gui.components.validation(com.haulmont.cuba.gui.components.validation) LookupAction(com.haulmont.cuba.gui.actions.picker.LookupAction) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) DynamicAttributesEntity(com.haulmont.cuba.gui.data.DynamicAttributesEntity) DecimalFormatSymbols(java.text.DecimalFormatSymbols) ZonedDateTime(java.time.ZonedDateTime) DatatypeRegistry(com.haulmont.chile.core.datatypes.DatatypeRegistry) AdaptiveNumberDatatype(com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype) StringUtils(org.apache.commons.lang3.StringUtils) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) BigDecimal(java.math.BigDecimal) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) PickerField(com.haulmont.cuba.gui.components.PickerField) OpenMode(com.haulmont.cuba.gui.screen.OpenMode) DynamicAttributes(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) GuiActionSupport(com.haulmont.cuba.gui.components.actions.GuiActionSupport) WindowParams(com.haulmont.cuba.gui.WindowParams) EntityOp(com.haulmont.cuba.security.entity.EntityOp) java.util(java.util) DatasourceValueSource(com.haulmont.cuba.gui.components.data.value.DatasourceValueSource) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) ParamsMap(com.haulmont.bali.util.ParamsMap) DynamicAttributesUtils(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils) BooleanUtils(org.apache.commons.lang3.BooleanUtils) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) Datatype(com.haulmont.chile.core.datatypes.Datatype) HasValue(com.haulmont.cuba.gui.components.HasValue) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) MapScreenOptions(com.haulmont.cuba.gui.screen.MapScreenOptions) Nullable(javax.annotation.Nullable) DynamicAttributesRecalculationTools(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesRecalculationTools) CommonLookupController(com.haulmont.cuba.gui.commonlookup.CommonLookupController) DecimalFormat(java.text.DecimalFormat) ScreensHelper(com.haulmont.cuba.gui.sys.ScreensHelper) Preconditions(com.haulmont.bali.util.Preconditions) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) WindowConfig(com.haulmont.cuba.gui.config.WindowConfig) PropertyType(com.haulmont.cuba.core.app.dynamicattributes.PropertyType) Action(com.haulmont.cuba.gui.components.Action) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) DatasourceValueSource(com.haulmont.cuba.gui.components.data.value.DatasourceValueSource) DynamicAttributesEntity(com.haulmont.cuba.gui.data.DynamicAttributesEntity) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) Component(org.springframework.stereotype.Component) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource)

Aggregations

Strings (com.google.common.base.Strings)1 ParamsMap (com.haulmont.bali.util.ParamsMap)1 Preconditions (com.haulmont.bali.util.Preconditions)1 Datatype (com.haulmont.chile.core.datatypes.Datatype)1 DatatypeRegistry (com.haulmont.chile.core.datatypes.DatatypeRegistry)1 AdaptiveNumberDatatype (com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype)1 MetaClass (com.haulmont.chile.core.model.MetaClass)1 DynamicAttributes (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes)1 DynamicAttributesRecalculationTools (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesRecalculationTools)1 DynamicAttributesUtils (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils)1 PropertyType (com.haulmont.cuba.core.app.dynamicattributes.PropertyType)1 com.haulmont.cuba.core.entity (com.haulmont.cuba.core.entity)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 OpenType (com.haulmont.cuba.gui.WindowManager.OpenType)1 WindowParams (com.haulmont.cuba.gui.WindowParams)1 LookupAction (com.haulmont.cuba.gui.actions.picker.LookupAction)1 CommonLookupController (com.haulmont.cuba.gui.commonlookup.CommonLookupController)1 Action (com.haulmont.cuba.gui.components.Action)1 HasValue (com.haulmont.cuba.gui.components.HasValue)1 PickerField (com.haulmont.cuba.gui.components.PickerField)1