Search in sources :

Example 11 with Range

use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.

the class GsonSerializationSupport method writeFields.

@SuppressWarnings("unchecked")
protected void writeFields(JsonWriter out, Entity entity) throws IOException {
    MetaClass metaClass = metadata.getClass(entity);
    for (MetaProperty property : metaClass.getProperties()) {
        if (!"id".equalsIgnoreCase(property.getName()) && !property.isReadOnly() && !exclude(entity.getClass(), property.getName()) && entityStates.isLoaded(entity, property.getName())) {
            Range propertyRange = property.getRange();
            if (propertyRange.isDatatype()) {
                writeSimpleProperty(out, entity, property);
            } else if (propertyRange.isClass()) {
                Object value = EntityValues.getValue(entity, property.getName());
                if (value instanceof Entity) {
                    out.name(property.getName());
                    writeEntity(out, (Entity) value);
                } else if (value instanceof Collection) {
                    out.name(property.getName());
                    writeCollection(out, (Collection) value);
                }
            } else if (propertyRange.isEnum()) {
                Object value = EntityValues.getValue(entity, property.getName());
                out.name(property.getName());
                out.value(propertyRange.asEnumeration().format(value));
            }
        }
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range)

Example 12 with Range

use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.

the class CollectionDsHelper method createProperties.

public static List<MetaPropertyPath> createProperties(FetchPlan fetchPlan, MetaClass metaClass) {
    List<MetaPropertyPath> properties = new ArrayList<>();
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    if (fetchPlan != null && metadataTools.isJpaEntity(metaClass)) {
        for (FetchPlanProperty property : fetchPlan.getProperties()) {
            final String name = property.getName();
            final MetaProperty metaProperty = metaClass.findProperty(name);
            if (metaProperty == null) {
                String message = String.format("Unable to find property %s for entity %s", name, metaClass.getName());
                throw new DevelopmentException(message);
            }
            if (!metadataTools.isJpa(metaProperty)) {
                String message = String.format("Specified transient property %s in view for datasource with persistent entity %s", name, metaClass.getName());
                LoggerFactory.getLogger(CollectionDsHelper.class).warn(message);
                continue;
            }
            final Range range = metaProperty.getRange();
            if (range == null) {
                continue;
            }
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
        // add all non-persistent properties
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            if (!metadataTools.isJpa(metaProperty)) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    } else {
        if (fetchPlan != null) {
            LoggerFactory.getLogger(CollectionDsHelper.class).warn("Specified view {} for datasource with not persistent entity {}", fetchPlan.getName(), metaClass.getName());
        }
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            final Range range = metaProperty.getRange();
            if (range == null)
                continue;
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    }
    return properties;
}
Also used : MetadataTools(io.jmix.core.MetadataTools) FetchPlanProperty(io.jmix.core.FetchPlanProperty) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) ArrayList(java.util.ArrayList) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range) DevelopmentException(io.jmix.core.DevelopmentException)

Example 13 with Range

use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.

the class MaskedFieldImpl method convertToPresentation.

@Override
protected String convertToPresentation(@Nullable 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();
        Range range = entityValueSource.getMetaPropertyPath().getRange();
        if (range.isDatatype()) {
            Datatype<V> propertyDataType = range.asDatatype();
            return nullToEmpty(propertyDataType.format(modelValue, locale));
        }
    }
    return nullToEmpty(super.convertToPresentation(modelValue));
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) Range(io.jmix.core.metamodel.model.Range)

Example 14 with Range

use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.

the class TextFieldImpl method convertToPresentation.

@SuppressWarnings("unchecked")
@Override
protected String convertToPresentation(@Nullable V modelValue) throws ConversionException {
    if (formatter != null) {
        return nullToEmpty(formatter.apply(modelValue));
    }
    if (datatype != null) {
        return nullToEmpty(datatype.format(modelValue, locale));
    }
    if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
        Range range = entityValueSource.getMetaPropertyPath().getRange();
        if (range.isDatatype()) {
            Datatype<V> propertyDataType = range.asDatatype();
            return nullToEmpty(propertyDataType.format(modelValue, locale));
        } else {
            setEditable(false);
            if (modelValue == null)
                return "";
            if (range.isClass()) {
                MetadataTools metadataTools = applicationContext.getBean(MetadataTools.class);
                if (range.getCardinality().isMany()) {
                    return ((Collection<Object>) modelValue).stream().map(metadataTools::getInstanceName).collect(Collectors.joining(", "));
                } else {
                    return metadataTools.getInstanceName(modelValue);
                }
            } else if (range.isEnum()) {
                Messages messages = applicationContext.getBean(Messages.class);
                return messages.getMessage((Enum) modelValue);
            }
        }
    }
    return nullToEmpty(super.convertToPresentation(modelValue));
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) MetadataTools(io.jmix.core.MetadataTools) Messages(io.jmix.core.Messages) Collection(java.util.Collection) Range(io.jmix.core.metamodel.model.Range)

Example 15 with Range

use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.

the class DomainModelBuilder method addProperty.

private void addProperty(EntityBuilder builder, MetaClass metaClass, MetaProperty prop) {
    String name = prop.getName();
    String userFriendlyName = null;
    if (loadCaptions) {
        userFriendlyName = messageTools.getPropertyCaption(metaClass, prop.getName());
    }
    boolean isEmbedded = metadataTools.isEmbedded(prop);
    MetaProperty.Type type = prop.getType();
    Class<?> javaType = prop.getJavaType();
    Range range = prop.getRange();
    switch(type) {
        case EMBEDDED:
        case COMPOSITION:
        case ASSOCIATION:
            if (range.isClass()) {
                MetaClass rangeClass = range.asClass();
                if (range.getCardinality().isMany()) {
                    builder.addCollectionReferenceAttribute(name, rangeClass.getName(), userFriendlyName);
                } else {
                    builder.addReferenceAttribute(name, rangeClass.getName(), userFriendlyName, isEmbedded);
                }
            } else {
                builder.addSingleValueAttribute(javaType, name, userFriendlyName);
            }
            break;
        case ENUM:
            builder.addSingleValueAttribute(javaType, name, userFriendlyName);
            break;
        case DATATYPE:
            builder.addSingleValueAttribute(javaType, name, userFriendlyName);
            break;
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range)

Aggregations

Range (io.jmix.core.metamodel.model.Range)27 MetaClass (io.jmix.core.metamodel.model.MetaClass)14 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)14 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)5 Nullable (javax.annotation.Nullable)5 MetadataTools (io.jmix.core.MetadataTools)3 Datatype (io.jmix.core.metamodel.datatype.Datatype)3 ParseException (java.text.ParseException)3 Entity (io.jmix.core.Entity)2 UiEntityAttributeContext (io.jmix.ui.accesscontext.UiEntityAttributeContext)2 UiEntityContext (io.jmix.ui.accesscontext.UiEntityContext)2 ValueSource (io.jmix.ui.component.data.ValueSource)2 Aggregation (io.jmix.ui.component.data.aggregation.Aggregation)2 EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)2 ContainerValueSource (io.jmix.ui.component.data.value.ContainerValueSource)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Element (org.dom4j.Element)2