Search in sources :

Example 21 with EntityValueSource

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

the class WebAbstractTextArea method convertToModel.

@Override
protected V convertToModel(String componentRawValue) throws ConversionException {
    String value = emptyToNull(componentRawValue);
    if (isTrimming()) {
        value = StringUtils.trimToNull(value);
    }
    if (datatype != null) {
        try {
            return datatype.parse(value, locale);
        } catch (ValueConversionException e) {
            throw new ConversionException(e.getLocalizedMessage(), e);
        } catch (ParseException e) {
            throw new ConversionException(getConversionErrorMessageInternal(), e);
        }
    }
    if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
        Datatype<V> propertyDataType = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
        try {
            return propertyDataType.parse(value, locale);
        } catch (ValueConversionException e) {
            throw new ConversionException(e.getLocalizedMessage(), e);
        } catch (ParseException e) {
            throw new ConversionException(getConversionErrorMessageInternal(), e);
        }
    }
    return super.convertToModel(value);
}
Also used : ConversionException(com.haulmont.cuba.gui.components.data.ConversionException) ValueConversionException(com.haulmont.chile.core.datatypes.ValueConversionException) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) ParseException(java.text.ParseException) ValueConversionException(com.haulmont.chile.core.datatypes.ValueConversionException)

Example 22 with EntityValueSource

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

the class WebAbstractTextArea method convertToPresentation.

@Override
protected String convertToPresentation(V modelValue) throws ConversionException {
    if (datatype != null) {
        return nullToEmpty(datatype.format(modelValue, locale));
    }
    if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
        Datatype<V> propertyDataType = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
        return nullToEmpty(propertyDataType.format(modelValue));
    }
    return nullToEmpty(super.convertToPresentation(modelValue));
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource)

Example 23 with EntityValueSource

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

the class WebCheckBoxGroup method valueBindingConnected.

@Override
protected void valueBindingConnected(ValueSource<Collection<V>> valueSource) {
    super.valueBindingConnected(valueSource);
    if (valueSource instanceof EntityValueSource) {
        DataAwareComponentsTools dataAwareComponentsTools = beanLocator.get(DataAwareComponentsTools.class);
        dataAwareComponentsTools.setupOptions(this, (EntityValueSource) valueSource);
    }
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) DataAwareComponentsTools(com.haulmont.cuba.gui.components.data.DataAwareComponentsTools)

Example 24 with EntityValueSource

use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource 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 25 with EntityValueSource

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

the class FormLoader method loadComponent.

protected ComponentPosition loadComponent(Element element, @Nullable String columnWidth, @Nullable Float flex) {
    Component component;
    if ("field".equals(element.getName())) {
        component = loadField(element);
    } else {
        LayoutLoader loader = getLayoutLoader();
        ComponentLoader childComponentLoader = loader.createComponent(element);
        childComponentLoader.loadComponent();
        component = childComponentLoader.getResultComponent();
    }
    // Set default width
    String componentWidth = element.attributeValue("width");
    if (Strings.isNullOrEmpty(componentWidth)) {
        if (columnWidth != null) {
            component.setWidth(columnWidth);
        } else if (flex != null) {
            component.setWidthFull();
        }
    }
    // Set default caption
    if (component instanceof HasValueSource && ((HasValueSource) component).getValueSource() instanceof EntityValueSource && component instanceof Component.HasCaption && ((Component.HasCaption) component).getCaption() == null) {
        EntityValueSource valueSource = ((EntityValueSource) ((HasValueSource) component).getValueSource());
        MetaPropertyPath metaPropertyPath = valueSource.getMetaPropertyPath();
        String propertyName = metaPropertyPath != null ? metaPropertyPath.getMetaProperty().getName() : null;
        if (metaPropertyPath != null) {
            if (isDynamicAttribute(metaPropertyPath.getMetaProperty())) {
                CategoryAttribute categoryAttribute = getCategoryAttribute(metaPropertyPath.getMetaProperty());
                ((Component.HasCaption) component).setCaption(categoryAttribute != null ? categoryAttribute.getLocaleName() : propertyName);
                ((Component.HasCaption) component).setDescription(categoryAttribute != null ? categoryAttribute.getLocaleDescription() : null);
            } else {
                MetaClass propertyMetaClass = getMetadataTools().getPropertyEnclosingMetaClass(metaPropertyPath);
                String propertyCaption = getMessageTools().getPropertyCaption(propertyMetaClass, propertyName);
                ((Component.HasCaption) component).setCaption(propertyCaption);
            }
        }
    }
    int colspan = loadSpan(element, "colspan");
    int rowspan = loadSpan(element, "rowspan");
    return new ComponentPosition(component, colspan, rowspan);
}
Also used : LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) ComponentPosition(com.haulmont.cuba.gui.components.form.ComponentPosition) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) DynamicAttributesUtils.getCategoryAttribute(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils.getCategoryAttribute) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader)

Aggregations

EntityValueSource (com.haulmont.cuba.gui.components.data.meta.EntityValueSource)39 MetaProperty (com.haulmont.chile.core.model.MetaProperty)10 DataAwareComponentsTools (com.haulmont.cuba.gui.components.data.DataAwareComponentsTools)10 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)9 ValueSource (com.haulmont.cuba.gui.components.data.ValueSource)9 HasValueSource (com.haulmont.cuba.gui.components.data.HasValueSource)6 Messages (com.haulmont.cuba.core.global.Messages)5 ConversionException (com.haulmont.cuba.gui.components.data.ConversionException)5 Datatype (com.haulmont.chile.core.datatypes.Datatype)4 ValueConversionException (com.haulmont.chile.core.datatypes.ValueConversionException)4 Entity (com.haulmont.cuba.core.entity.Entity)4 ParseException (java.text.ParseException)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Range (com.haulmont.chile.core.model.Range)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2 Field (com.haulmont.cuba.gui.components.Field)2 DatasourceValueSource (com.haulmont.cuba.gui.components.data.value.DatasourceValueSource)2 Pair (com.haulmont.bali.datastruct.Pair)1 Subscription (com.haulmont.bali.events.Subscription)1 Preconditions (com.haulmont.bali.util.Preconditions)1