Search in sources :

Example 1 with HasValueSource

use of com.haulmont.cuba.gui.components.data.HasValueSource in project cuba by cuba-platform.

the class EditorBuilderProcessor method buildEditor.

@SuppressWarnings("unchecked")
public <E extends Entity, S extends Screen> S buildEditor(EditorBuilder<E> builder) {
    FrameOwner origin = builder.getOrigin();
    Screens screens = getScreenContext(origin).getScreens();
    ListComponent<E> listComponent = builder.getListComponent();
    CollectionContainer<E> container = builder.getContainer();
    if (container == null && listComponent != null) {
        DataUnit items = listComponent.getItems();
        container = items instanceof ContainerDataUnit ? ((ContainerDataUnit) items).getContainer() : null;
    }
    E entity = initEntity(builder, container);
    if (builder.getMode() == EditMode.EDIT && entity == null) {
        throw new IllegalStateException(String.format("Editor of %s cannot be open with mode EDIT, entity is not set", builder.getEntityClass()));
    }
    Screen screen = createScreen(builder, screens, entity);
    EditorScreen<E> editorScreen = (EditorScreen<E>) screen;
    editorScreen.setEntityToEdit(entity);
    DataContext parentDataContext = setupParentDataContext(origin, screen, container, builder.getParentDataContext());
    if (container != null) {
        CollectionContainer<E> ct = container;
        screen.addAfterCloseListener(event -> {
            CloseAction closeAction = event.getCloseAction();
            if (isCommitCloseAction(closeAction)) {
                E entityFromEditor = getCommittedEntity(editorScreen, parentDataContext);
                E reloadedEntity = reloadIfNeeded(entityFromEditor, ct, builder);
                E committedEntity = transform(reloadedEntity, builder);
                E mergedEntity = merge(committedEntity, origin, parentDataContext);
                if (builder.getMode() == EditMode.CREATE) {
                    boolean addsFirst;
                    if (!(ct instanceof Nested)) {
                        addsFirst = clientConfig.getCreateActionAddsFirst();
                        if (builder.getAddFirst() != null) {
                            addsFirst = builder.getAddFirst();
                        }
                    } else {
                        addsFirst = false;
                    }
                    if (ct instanceof Nested || !addsFirst) {
                        ct.getMutableItems().add(mergedEntity);
                    } else {
                        ct.getMutableItems().add(0, mergedEntity);
                    }
                } else {
                    ct.replaceItem(mergedEntity);
                }
            }
            if (listComponent instanceof com.haulmont.cuba.gui.components.Component.Focusable) {
                ((com.haulmont.cuba.gui.components.Component.Focusable) listComponent).focus();
            }
        });
    }
    HasValue<E> field = builder.getField();
    if (field != null) {
        if (parentDataContext == null && field instanceof HasValueSource) {
            ValueSource fieldValueSource = ((HasValueSource) field).getValueSource();
            if (fieldValueSource instanceof EntityValueSource) {
                if (isCompositionProperty((EntityValueSource) fieldValueSource)) {
                    DataContext thisDataContext = UiControllerUtils.getScreenData(origin).getDataContext();
                    DataContext dataContext = UiControllerUtils.getScreenData(screen).getDataContext();
                    checkDataContext(screen, dataContext);
                    dataContext.setParent(thisDataContext);
                }
            }
        }
        screen.addAfterCloseListener(event -> {
            CloseAction closeAction = event.getCloseAction();
            if (isCommitCloseAction(closeAction)) {
                E entityFromEditor = editorScreen.getEditedEntity();
                E editedEntity = transform(entityFromEditor, builder);
                if (field instanceof LookupPickerField) {
                    LookupPickerField lookupPickerField = ((LookupPickerField) field);
                    Options options = lookupPickerField.getOptions();
                    if (options instanceof EntityOptions) {
                        EntityOptions entityOptions = (EntityOptions) options;
                        if (entityOptions.containsItem(editedEntity)) {
                            entityOptions.updateItem(editedEntity);
                        }
                    }
                }
                if (field instanceof SupportsUserAction) {
                    ((SupportsUserAction) field).setValueFromUser(editedEntity);
                } else {
                    field.setValue(editedEntity);
                }
            }
            if (field instanceof com.haulmont.cuba.gui.components.Component.Focusable) {
                ((com.haulmont.cuba.gui.components.Component.Focusable) field).focus();
            }
        });
    }
    if (builder instanceof EditorClassBuilder) {
        @SuppressWarnings("unchecked") Consumer<AfterScreenCloseEvent> closeListener = ((EditorClassBuilder) builder).getCloseListener();
        if (closeListener != null) {
            screen.addAfterCloseListener(new AfterCloseListenerAdapter(closeListener));
        }
    }
    return (S) screen;
}
Also used : EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions) Options(com.haulmont.cuba.gui.components.data.Options) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions) DataUnit(com.haulmont.cuba.gui.components.data.DataUnit) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) Screens(com.haulmont.cuba.gui.Screens) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit)

Example 2 with HasValueSource

use of com.haulmont.cuba.gui.components.data.HasValueSource 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)

Example 3 with HasValueSource

use of com.haulmont.cuba.gui.components.data.HasValueSource in project cuba by cuba-platform.

the class EditorBuilderProcessor method initEntity.

protected <E extends Entity> E initEntity(EditorBuilder<E> builder, CollectionContainer<E> container) {
    E entity;
    boolean oneToOneComposition = false;
    EntityValueSource entityValueSource = null;
    HasValue<E> field = builder.getField();
    if (field instanceof HasValueSource) {
        ValueSource valueSource = ((HasValueSource) field).getValueSource();
        if (valueSource instanceof EntityValueSource) {
            entityValueSource = (EntityValueSource) valueSource;
            oneToOneComposition = isCompositionProperty(entityValueSource);
        }
    }
    if (builder.getMode() == EditMode.CREATE || (oneToOneComposition && field.getValue() == null)) {
        if (builder.getNewEntity() == null) {
            entity = metadata.create(builder.getEntityClass());
        } else {
            entity = builder.getNewEntity();
        }
        if (container instanceof Nested) {
            initializeNestedEntity(entity, (Nested) container);
        }
        if (oneToOneComposition) {
            Entity ownerEntity = entityValueSource.getItem();
            MetaProperty inverseProp = entityValueSource.getMetaPropertyPath().getMetaProperty().getInverse();
            if (inverseProp != null) {
                entity.setValue(inverseProp.getName(), ownerEntity);
            }
        }
        if (builder.getInitializer() != null) {
            builder.getInitializer().accept(entity);
        }
    } else {
        entity = builder.getEditedEntity();
    }
    return entity;
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 4 with HasValueSource

use of com.haulmont.cuba.gui.components.data.HasValueSource in project cuba by cuba-platform.

the class LookupBuilderProcessor method getViewForField.

/**
 * If the value for a component (e.g. {@link com.haulmont.cuba.gui.components.PickerField}) is selected from lookup screen then there may be cases
 * when in entities in lookup screen some attributes required in the editor are not loaded.
 * <p>
 * The method evaluates the view that is used for the entity in the given {@code field}
 *
 * @return a view or null if the view cannot be evaluated
 */
@Nullable
protected <E extends Entity> View getViewForField(HasValue<E> field) {
    if (field instanceof HasValueSource) {
        ValueSource valueSource = ((HasValueSource) field).getValueSource();
        if (valueSource instanceof ContainerValueSource) {
            ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
            InstanceContainer<E> container = containerValueSource.getContainer();
            View view = container.getView();
            if (view != null) {
                MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
                View curView = view;
                for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
                    ViewProperty viewProperty = curView.getProperty(metaProperty.getName());
                    if (viewProperty != null) {
                        curView = viewProperty.getView();
                    }
                    if (curView == null)
                        break;
                }
                if (curView != view) {
                    return curView;
                }
            }
        }
    }
    return null;
}
Also used : ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 5 with HasValueSource

use of com.haulmont.cuba.gui.components.data.HasValueSource in project cuba by cuba-platform.

the class AttributeAccessSupport method visitComponent.

protected void visitComponent(com.haulmont.cuba.gui.components.Component component, boolean reset) {
    if (!(component instanceof HasValueSource)) {
        return;
    }
    ValueSource valueSource = ((HasValueSource) component).getValueSource();
    if (!(valueSource instanceof EntityValueSource)) {
        return;
    }
    EntityValueSource entityValueSource = (EntityValueSource) valueSource;
    MetaPropertyPath propertyPath = entityValueSource.getMetaPropertyPath();
    if (valueSource.getState() != BindingState.ACTIVE || propertyPath == null) {
        return;
    }
    if (reset) {
        component.setVisible(security.isEntityAttrReadPermitted(entityValueSource.getEntityMetaClass(), propertyPath.toString()));
        if (component instanceof Editable) {
            ((Editable) component).setEditable(security.isEntityAttrUpdatePermitted(entityValueSource.getEntityMetaClass(), propertyPath.toString()));
        }
        if (component instanceof Field) {
            ((Field) component).setRequired(propertyPath.getMetaProperty().isMandatory());
        }
    }
    Entity item = entityValueSource.getItem();
    ComponentState componentState = calculateComponentState(item, propertyPath);
    if (metadataTools.isEmbeddable(item.getMetaClass()) && entityValueSource instanceof DatasourceValueSource) {
        Datasource ds = ((DatasourceValueSource) entityValueSource).getDatasource();
        if (ds instanceof EmbeddedDatasource) {
            Datasource masterDs = ((EmbeddedDatasource) ds).getMaster();
            item = masterDs.getItem();
            componentState = calculateComponentState(item, metadataTools.resolveMetaPropertyPath(masterDs.getMetaClass(), ((EmbeddedDatasource) ds).getProperty().getName() + "." + propertyPath));
        }
    }
    if (componentState.hidden) {
        component.setVisible(false);
    }
    if (componentState.readOnly) {
        if (component instanceof Editable) {
            ((Editable) component).setEditable(false);
        }
    }
    if (component instanceof Field) {
        if (componentState.required && ((Field) component).isEditable() && component.isVisibleRecursive()) {
            ((Field) component).setRequired(true);
        }
    }
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) EmbeddedDatasource(com.haulmont.cuba.gui.data.EmbeddedDatasource) EmbeddedDatasource(com.haulmont.cuba.gui.data.EmbeddedDatasource) Field(com.haulmont.cuba.gui.components.Field) Entity(com.haulmont.cuba.core.entity.Entity) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) DatasourceValueSource(com.haulmont.cuba.gui.components.data.value.DatasourceValueSource) DatasourceValueSource(com.haulmont.cuba.gui.components.data.value.DatasourceValueSource) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) Editable(com.haulmont.cuba.gui.components.Component.Editable) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource)

Aggregations

HasValueSource (com.haulmont.cuba.gui.components.data.HasValueSource)9 EntityValueSource (com.haulmont.cuba.gui.components.data.meta.EntityValueSource)6 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)5 ValueSource (com.haulmont.cuba.gui.components.data.ValueSource)5 MetaClass (com.haulmont.chile.core.model.MetaClass)3 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 DynamicAttributesUtils.getCategoryAttribute (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils.getCategoryAttribute)2 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)2 Entity (com.haulmont.cuba.core.entity.Entity)2 ContainerValueSource (com.haulmont.cuba.gui.components.data.value.ContainerValueSource)2 DatasourceValueSource (com.haulmont.cuba.gui.components.data.value.DatasourceValueSource)2 ComponentPosition (com.haulmont.cuba.gui.components.form.ComponentPosition)2 Datasource (com.haulmont.cuba.gui.data.Datasource)2 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 DynamicAttributes (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes)1