Search in sources :

Example 1 with Range

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

the class CustomerService method printOrderProperties.

public void printOrderProperties() {
    // <1>
    MetaClass metaClass = metadata.getClass(Order.class);
    for (MetaProperty metaProperty : metaClass.getProperties()) {
        // <2>
        // <3>
        String propertyName = metaProperty.getName();
        // <4>
        MetaProperty.Type propertyType = metaProperty.getType();
        // <5>
        Class<?> javaType = metaProperty.getJavaType();
        // <6>
        Range propertyRange = metaProperty.getRange();
        String info = "name: " + propertyName + "\n type: " + propertyType + "\n Java type: " + javaType + "\n range: " + propertyRange;
        if (propertyRange.isClass()) {
            // <7>
            // <8>
            MetaClass refMetaClass = propertyRange.asClass();
            // <9>
            Range.Cardinality cardinality = propertyRange.getCardinality();
            info += "\n reference to: " + refMetaClass;
            info += "\n cardinality: " + cardinality;
        } else if (propertyRange.isEnum()) {
            // <10>
            // <11>
            Enumeration<?> enumeration = propertyRange.asEnumeration();
            info += "\n enum: " + enumeration;
        } else if (propertyRange.isDatatype()) {
            // <12>
            // <13>
            Datatype<Object> propertyDatatype = propertyRange.asDatatype();
            info += "\n data type: " + propertyDatatype;
        }
        System.out.println(info);
    }
}
Also used : Enumeration(io.jmix.core.metamodel.datatype.Enumeration) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range)

Example 2 with Range

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

the class DynAttrMetaPropertyPathResolver method resolveMetaPropertyPath.

@Override
public MetaPropertyPath resolveMetaPropertyPath(MetaClass metaClass, String propertyPath) {
    String[] properties = propertyPath.split("\\.");
    MetaProperty[] metaProperties = new MetaProperty[properties.length];
    MetaProperty currentProperty;
    MetaClass currentClass = metaClass;
    for (int i = 0; i < properties.length; i++) {
        if (currentClass == null) {
            return null;
        }
        currentProperty = currentClass.findProperty(properties[i]);
        if (currentProperty == null) {
            if (!DynAttrUtils.isDynamicAttributeProperty(properties[i])) {
                return null;
            }
            AttributeDefinition attribute = dynAttrMetadata.getAttributeByCode(currentClass, DynAttrUtils.getAttributeCodeFromProperty(properties[i])).orElse(null);
            if (attribute == null) {
                return null;
            }
            currentProperty = attribute.getMetaProperty();
        }
        Range range = currentProperty.getRange();
        currentClass = range.isClass() ? range.asClass() : null;
        metaProperties[i] = currentProperty;
    }
    return new MetaPropertyPath(metaClass, metaProperties);
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) AttributeDefinition(io.jmix.dynattr.AttributeDefinition) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range)

Example 3 with Range

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

the class DateIntervalUtils method isIntervalTypeSupportsDatatype.

/**
 * @param mpp   meta property path
 * @param value date interval
 * @return {@code true} if date interval type supports provided property type
 */
public boolean isIntervalTypeSupportsDatatype(BaseDateInterval value, MetaPropertyPath mpp) {
    Preconditions.checkNotNullArgument(mpp);
    Preconditions.checkNotNullArgument(value);
    Range range = mpp.getRange();
    return range.isDatatype();
}
Also used : Range(io.jmix.core.metamodel.model.Range)

Example 4 with Range

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

the class EntityImportPlanJsonBuilderImpl method buildFromJsonObject.

protected EntityImportPlan buildFromJsonObject(JsonObject jsonObject, MetaClass metaClass) {
    EntityImportPlanBuilder importPlanBuilder = entityImportPlans.builder(metaClass.getJavaClass());
    ExportImportEntityContext importContext = new ExportImportEntityContext(metaClass);
    accessManager.applyRegisteredConstraints(importContext);
    for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
        String propertyName = entry.getKey();
        MetaProperty metaProperty = metaClass.findProperty(propertyName);
        if (metaProperty == null) {
            continue;
        }
        Range propertyRange = metaProperty.getRange();
        Class<?> propertyType = metaProperty.getJavaType();
        if (propertyRange.isDatatype() || propertyRange.isEnum()) {
            if (importContext.canImported(propertyName)) {
                importPlanBuilder.addLocalProperty(propertyName);
            }
        } else if (propertyRange.isClass()) {
            if (Entity.class.isAssignableFrom(propertyType)) {
                if (metaProperty.getType() == MetaProperty.Type.EMBEDDED) {
                    MetaClass propertyMetaClass = metadata.getClass(propertyType);
                    JsonElement propertyJsonObject = entry.getValue();
                    if (!propertyJsonObject.isJsonObject()) {
                        throw new RuntimeException("JsonObject was expected for property " + propertyName);
                    }
                    if (importContext.canImported(propertyName)) {
                        EntityImportPlan propertyImportPlan = buildFromJsonObject(propertyJsonObject.getAsJsonObject(), propertyMetaClass);
                        importPlanBuilder.addEmbeddedProperty(propertyName, propertyImportPlan);
                    }
                } else {
                    MetaClass propertyMetaClass = metadata.getClass(propertyType);
                    if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) {
                        JsonElement propertyJsonObject = entry.getValue();
                        if (importContext.canImported(propertyName)) {
                            if (propertyJsonObject.isJsonNull()) {
                                // in case of null we must add such import behavior to update the reference with null value later
                                if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) {
                                    importPlanBuilder.addManyToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING);
                                } else {
                                    importPlanBuilder.addOneToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING);
                                }
                            } else {
                                if (!propertyJsonObject.isJsonObject()) {
                                    throw new RuntimeException("JsonObject was expected for property " + propertyName);
                                }
                                EntityImportPlan propertyImportPlan = buildFromJsonObject(propertyJsonObject.getAsJsonObject(), propertyMetaClass);
                                if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) {
                                    importPlanBuilder.addManyToOneProperty(propertyName, propertyImportPlan);
                                } else {
                                    importPlanBuilder.addOneToOneProperty(propertyName, propertyImportPlan);
                                }
                            }
                        }
                    } else {
                        if (importContext.canImported(propertyName))
                            if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) {
                                importPlanBuilder.addManyToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING);
                            } else {
                                importPlanBuilder.addOneToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING);
                            }
                    }
                }
            } else if (Collection.class.isAssignableFrom(propertyType)) {
                MetaClass propertyMetaClass = metaProperty.getRange().asClass();
                switch(metaProperty.getRange().getCardinality()) {
                    case MANY_TO_MANY:
                        {
                            if (importContext.canImported(propertyName))
                                importPlanBuilder.addManyToManyProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING, CollectionImportPolicy.REMOVE_ABSENT_ITEMS);
                            break;
                        }
                    case ONE_TO_MANY:
                        {
                            if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) {
                                JsonElement compositionJsonArray = entry.getValue();
                                if (!compositionJsonArray.isJsonArray()) {
                                    throw new RuntimeException("JsonArray was expected for property " + propertyName);
                                }
                                EntityImportPlan propertyImportPlan = buildFromJsonArray(compositionJsonArray.getAsJsonArray(), propertyMetaClass);
                                if (importContext.canImported(propertyName))
                                    importPlanBuilder.addOneToManyProperty(propertyName, propertyImportPlan, CollectionImportPolicy.REMOVE_ABSENT_ITEMS);
                            }
                            break;
                        }
                    default:
                        // ignore other options
                        break;
                }
            }
        }
    }
    return importPlanBuilder.build();
}
Also used : ExportImportEntityContext(io.jmix.core.accesscontext.ExportImportEntityContext) Range(io.jmix.core.metamodel.model.Range) MetaClass(io.jmix.core.metamodel.model.MetaClass) JsonElement(com.google.gson.JsonElement) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Map(java.util.Map)

Example 5 with Range

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

the class KeyValueMetaPropertyBuilder method build.

public KeyValueMetaProperty build(MetaClass metaClass, String name, Class javaClass) {
    MetaProperty.Type type;
    Range range;
    Session metadataSession = metadata.getSession();
    if (Entity.class.isAssignableFrom(javaClass)) {
        range = new ClassRange(metadataSession.findClass(javaClass));
        type = MetaProperty.Type.ASSOCIATION;
    } else if (EnumClass.class.isAssignableFrom(javaClass)) {
        @SuppressWarnings("unchecked") EnumerationImpl enumeration = new EnumerationImpl(javaClass);
        range = new EnumerationRange(enumeration);
        type = MetaProperty.Type.ENUM;
    } else {
        @SuppressWarnings("unchecked") Datatype datatype = datatypeRegistry.get(javaClass);
        range = new DatatypeRange(datatype);
        type = MetaProperty.Type.DATATYPE;
    }
    return new KeyValueMetaProperty(metaClass, name, javaClass, range, type);
}
Also used : ClassRange(io.jmix.core.metamodel.model.impl.ClassRange) EnumerationImpl(io.jmix.core.metamodel.datatype.impl.EnumerationImpl) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) EnumerationRange(io.jmix.core.metamodel.model.impl.EnumerationRange) ClassRange(io.jmix.core.metamodel.model.impl.ClassRange) Range(io.jmix.core.metamodel.model.Range) DatatypeRange(io.jmix.core.metamodel.model.impl.DatatypeRange) EnumerationRange(io.jmix.core.metamodel.model.impl.EnumerationRange) DatatypeRange(io.jmix.core.metamodel.model.impl.DatatypeRange) Session(io.jmix.core.metamodel.model.Session) Datatype(io.jmix.core.metamodel.datatype.Datatype)

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