Search in sources :

Example 1 with ValueSource

use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.

the class WebTokenList method getViewForField.

/**
 * If the value for a component is selected from the lookup screen there may be cases when lookup screen contains a list of entities loaded with a
 * view that doesn't contain all attributes, required by the token list.
 * <p>
 * The method evaluates the view that was is for loading entities in the token list
 *
 * @return a view or null if the view cannot be evaluated
 */
@Nullable
protected FetchPlan getViewForField() {
    ValueSource valueSource = getValueSource();
    if (valueSource instanceof ContainerValueSource) {
        ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
        InstanceContainer container = containerValueSource.getContainer();
        FetchPlan view = container.getFetchPlan();
        if (view != null) {
            MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
            FetchPlan curView = view;
            if (metaPropertyPath != null) {
                for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
                    FetchPlanProperty viewProperty = curView.getProperty(metaProperty.getName());
                    if (viewProperty != null) {
                        curView = viewProperty.getFetchPlan();
                        if (curView == null)
                            return null;
                    }
                }
                MetaProperty inverseMetaProperty = metaPropertyPath.getMetaProperty().getInverse();
                if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
                    curView = fetchPlans.builder(curView).add(inverseMetaProperty.getName()).build();
                }
            }
            if (curView != view) {
                return curView;
            }
        }
    }
    return null;
}
Also used : ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) LegacyCollectionDsValueSource(com.haulmont.cuba.gui.components.data.value.LegacyCollectionDsValueSource) ValueSource(io.jmix.ui.component.data.ValueSource) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) InstanceContainer(io.jmix.ui.model.InstanceContainer) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 2 with ValueSource

use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.

the class EditorBuilderProcessor method initEntity.

@Nullable
protected <E> E initEntity(EditorBuilder<E> builder, @Nullable 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) {
            Object ownerEntity = entityValueSource.getItem();
            MetaProperty inverseProp = entityValueSource.getMetaPropertyPath().getMetaProperty().getInverse();
            if (inverseProp != null) {
                EntityValues.setValue(entity, inverseProp.getName(), ownerEntity);
            }
        }
        if (builder.getInitializer() != null) {
            builder.getInitializer().accept(entity);
        }
    } else {
        entity = builder.getEditedEntity();
    }
    return entity;
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) ValueSource(io.jmix.ui.component.data.ValueSource) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) HasValueSource(io.jmix.ui.component.data.HasValueSource) Nested(io.jmix.ui.model.Nested) HasValueSource(io.jmix.ui.component.data.HasValueSource) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 3 with ValueSource

use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.

the class LookupBuilderProcessor method getFetchPlanForField.

/**
 * The method evaluates the fetch plan that is used for the entity in the given {@code field}
 * <p>
 * If the value for a component (e.g. {@link EntityPicker}) 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.
 *
 * @return a view or {@code null} if the fetch plan cannot be evaluated
 */
@Nullable
protected FetchPlan getFetchPlanForField(HasValue field) {
    if (field instanceof HasValueSource) {
        ValueSource valueSource = ((HasValueSource) field).getValueSource();
        if (valueSource instanceof ContainerValueSource) {
            ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
            InstanceContainer container = containerValueSource.getContainer();
            FetchPlan fetchPlan = container.getFetchPlan();
            if (fetchPlan != null) {
                MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
                FetchPlan curFetchPlan = fetchPlan;
                for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
                    FetchPlanProperty viewProperty = curFetchPlan.getProperty(metaProperty.getName());
                    if (viewProperty != null) {
                        curFetchPlan = viewProperty.getFetchPlan();
                    }
                    if (curFetchPlan == null)
                        break;
                }
                if (curFetchPlan != fetchPlan) {
                    return curFetchPlan;
                }
            }
        }
    }
    return null;
}
Also used : ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) ValueSource(io.jmix.ui.component.data.ValueSource) HasValueSource(io.jmix.ui.component.data.HasValueSource) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) InstanceContainer(io.jmix.ui.model.InstanceContainer) HasValueSource(io.jmix.ui.component.data.HasValueSource) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 4 with ValueSource

use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.

the class EntityFieldCreationSupport method addDefaultActions.

public boolean addDefaultActions(EntityPicker entityPicker) {
    ValueSource valueSource = entityPicker.getValueSource();
    if (!(valueSource instanceof EntityValueSource)) {
        return false;
    }
    EntityValueSource entityValueSource = (EntityValueSource) entityPicker.getValueSource();
    MetaPropertyPath mpp = entityValueSource.getMetaPropertyPath();
    MetaClass metaClass = mpp.getMetaProperty().getRange().asClass();
    List<String> actionIds = componentProperties.getEntityFieldActions().get(metaClass.getName());
    if (actionIds == null || actionIds.isEmpty()) {
        return false;
    }
    addActions(entityPicker, actionIds);
    return true;
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) MetaClass(io.jmix.core.metamodel.model.MetaClass) ValueSource(io.jmix.ui.component.data.ValueSource) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath)

Example 5 with ValueSource

use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.

the class DateFieldImpl method valueBindingConnected.

@Override
protected void valueBindingConnected(ValueSource<V> valueSource) {
    super.valueBindingConnected(valueSource);
    if (valueSource instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueSource;
        DataAwareComponentsTools dataAwareComponentsTools = applicationContext.getBean(DataAwareComponentsTools.class);
        dataAwareComponentsTools.setupDateFormat(this, entityValueSource);
        dataAwareComponentsTools.setupZoneId(this, entityValueSource);
        if (valueSourceStateChangeSubscription != null) {
            valueSourceStateChangeSubscription.remove();
        }
        // setup dateRange after valueSource is activated and value is set because
        // Vaadin dateField rejects value if it is not in range
        valueSourceStateChangeSubscription = valueSource.addStateChangeListener(event -> {
            if (event.getState() == BindingState.ACTIVE) {
                dataAwareComponentsTools.setupDateRange(this, entityValueSource);
            }
        });
    }
}
Also used : java.util(java.util) Preconditions(io.jmix.core.common.util.Preconditions) WrapperUtils.toVaadinTimeMode(io.jmix.ui.component.impl.WrapperUtils.toVaadinTimeMode) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) InitializingBean(org.springframework.beans.factory.InitializingBean) Messages(io.jmix.core.Messages) Notifications(io.jmix.ui.Notifications) DataAwareComponentsTools(io.jmix.ui.component.data.DataAwareComponentsTools) java.time(java.time) HasValue(com.vaadin.data.HasValue) TestIdManager(io.jmix.ui.sys.TestIdManager) ConversionException(io.jmix.ui.component.data.ConversionException) DateResolution(com.vaadin.shared.ui.datefield.DateResolution) JmixCssActionsLayout(io.jmix.ui.widget.JmixCssActionsLayout) Nullable(javax.annotation.Nullable) Preconditions.checkNotNullArgument(io.jmix.core.common.util.Preconditions.checkNotNullArgument) CurrentAuthentication(io.jmix.core.security.CurrentAuthentication) FormatStringsRegistry(io.jmix.core.metamodel.datatype.FormatStringsRegistry) Subscription(io.jmix.core.common.event.Subscription) ValueSource(io.jmix.ui.component.data.ValueSource) ThemeConstants(io.jmix.ui.theme.ThemeConstants) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) ErrorMessage(com.vaadin.server.ErrorMessage) Datatype(io.jmix.core.metamodel.datatype.Datatype) JmixTimeFieldWrapper(io.jmix.ui.widget.JmixTimeFieldWrapper) Validator(io.jmix.ui.component.validation.Validator) AbstractComponent(com.vaadin.ui.AbstractComponent) AppUI(io.jmix.ui.AppUI) App(io.jmix.ui.App) WrapperUtils.fromVaadinTimeMode(io.jmix.ui.component.impl.WrapperUtils.fromVaadinTimeMode) UserError(com.vaadin.server.UserError) JmixDateField(io.jmix.ui.widget.JmixDateField) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) BindingState(io.jmix.ui.component.data.BindingState) DateTimeTransformations(io.jmix.core.DateTimeTransformations) io.jmix.ui.component(io.jmix.ui.component) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) DataAwareComponentsTools(io.jmix.ui.component.data.DataAwareComponentsTools)

Aggregations

ValueSource (io.jmix.ui.component.data.ValueSource)14 EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)10 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)7 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)5 ContainerValueSource (io.jmix.ui.component.data.value.ContainerValueSource)5 HasValueSource (io.jmix.ui.component.data.HasValueSource)4 Nullable (javax.annotation.Nullable)4 Datatype (io.jmix.core.metamodel.datatype.Datatype)3 MetaClass (io.jmix.core.metamodel.model.MetaClass)3 UiEntityAttributeContext (io.jmix.ui.accesscontext.UiEntityAttributeContext)3 Range (io.jmix.core.metamodel.model.Range)2 UiEntityContext (io.jmix.ui.accesscontext.UiEntityContext)2 io.jmix.ui.component (io.jmix.ui.component)2 DataContext (io.jmix.ui.model.DataContext)2 InstanceContainer (io.jmix.ui.model.InstanceContainer)2 DatasourceValueSource (com.haulmont.cuba.gui.components.data.value.DatasourceValueSource)1 LegacyCollectionDsValueSource (com.haulmont.cuba.gui.components.data.value.LegacyCollectionDsValueSource)1 HasValue (com.vaadin.data.HasValue)1 ErrorMessage (com.vaadin.server.ErrorMessage)1 UserError (com.vaadin.server.UserError)1