Search in sources :

Example 16 with Range

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

the class DynAttrPropertyFilterComponentGenerationStrategy method createDatatypeField.

@Override
protected Component createDatatypeField(ComponentGenerationContext context, AttributeDefinition attribute) {
    Component field = super.createDatatypeField(context, attribute);
    Range range = attribute.getMetaProperty().getRange();
    if (field instanceof HasDatatype && range.isDatatype()) {
        ((HasDatatype<?>) field).setDatatype(range.asDatatype());
    }
    return field;
}
Also used : Range(io.jmix.core.metamodel.model.Range)

Example 17 with Range

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

the class ResponseBuilder method buildResponse.

/**
 * Convert loaded entity to data fetcher return format (Map&lt;String, Object&gt;)
 *
 * @param entity loaded entity
 * @param fetchPlan loaded entity properties
 * @param metaClass entity meta class
 * @param props we need pass full set of properties to have information about system props such '_instanceName'
 * @return entity converted to response as Map&lt;String, Object&gt;
 */
public Map<String, Object> buildResponse(Entity entity, FetchPlan fetchPlan, MetaClass metaClass, Set<String> props) {
    Map<String, Object> entityAsMap = new HashMap<>();
    // check and evaluate _instanceName, if required
    if (environmentUtils.hasInstanceNameProperty(props)) {
        entityAsMap.put(NamingUtils.SYS_ATTR_INSTANCE_NAME, metadataTools.getInstanceName(entity));
    }
    // must include id
    writeIdField(entity, entityAsMap);
    // compose result object by iterating over fetch plan props
    fetchPlan.getProperties().forEach(prop -> {
        String propName = prop.getName();
        MetaProperty metaProperty = metaClass.getProperty(propName);
        Object fieldValue = EntityValues.getValue(entity, propName);
        Range propertyRange = metaProperty.getRange();
        if (fieldValue == null) {
            entityAsMap.put(propName, null);
            return;
        }
        if (propertyRange.isDatatype() || propertyRange.isEnum()) {
            entityAsMap.put(propName, fieldValue);
            return;
        }
        if (propertyRange.isClass()) {
            Set<String> nestedProps = environmentUtils.getNestedProps(props, propName);
            if (fieldValue instanceof Entity) {
                entityAsMap.put(propName, buildResponse((Entity) fieldValue, prop.getFetchPlan(), propertyRange.asClass(), nestedProps));
                return;
            }
            if (fieldValue instanceof Collection) {
                Collection<Object> values = ((Collection<Entity>) fieldValue).stream().map(e -> buildResponse(e, prop.getFetchPlan(), propertyRange.asClass(), nestedProps)).collect(Collectors.toList());
                entityAsMap.put(propName, values);
                return;
            }
        }
        log.warn("buildResponse: failed for {}.{} unsupported range type ", metaClass.getName(), prop.getName());
        throw new IllegalStateException("Unsupported range type " + propertyRange);
    });
    return entityAsMap;
}
Also used : NamingUtils(io.jmix.graphql.NamingUtils) MetaClass(io.jmix.core.metamodel.model.MetaClass) Logger(org.slf4j.Logger) FetchPlan(io.jmix.core.FetchPlan) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) Metadata(io.jmix.core.Metadata) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) EntityValues(io.jmix.core.entity.EntityValues) Component(org.springframework.stereotype.Component) Map(java.util.Map) Entity(io.jmix.core.Entity) Range(io.jmix.core.metamodel.model.Range) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) MetadataTools(io.jmix.core.MetadataTools) ID_ATTR_NAME(io.jmix.graphql.NamingUtils.ID_ATTR_NAME) Entity(io.jmix.core.Entity) HashMap(java.util.HashMap) Collection(java.util.Collection) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range)

Example 18 with Range

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

the class EntityLogImpl method getAllAttributes.

protected Set<String> getAllAttributes(Object entity) {
    if (entity == null) {
        return null;
    }
    Set<String> attributes = new HashSet<>();
    MetaClass metaClass = metadata.getClass(entity.getClass());
    for (MetaProperty metaProperty : metaClass.getProperties()) {
        Range range = metaProperty.getRange();
        if (range.isClass() && range.getCardinality().isMany()) {
            continue;
        }
        attributes.add(metaProperty.getName());
    }
    for (MetaProperty object : metadataTools.getAdditionalProperties(metaClass)) {
        attributes.add(object.getName());
    }
    return attributes;
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range)

Example 19 with Range

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

the class AppSettingsGridLayoutBuilder method addRowToGrid.

protected void addRowToGrid(InstanceContainer container, GridLayout gridLayout, int currentRow, MetaProperty metaProperty) {
    MetaClass metaClass = container.getEntityMetaClass();
    Range range = metaProperty.getRange();
    UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaClass, metaProperty.getName());
    accessManager.applyRegisteredConstraints(attributeContext);
    if (!attributeContext.canView()) {
        return;
    }
    if (range.isClass()) {
        UiEntityContext entityContext = new UiEntityContext(range.asClass());
        accessManager.applyRegisteredConstraints(entityContext);
        if (!entityContext.isViewPermitted()) {
            return;
        }
    }
    // add label
    Label fieldLabel = uiComponents.create(Label.class);
    fieldLabel.setValue(getPropertyCaption(metaClass, metaProperty));
    fieldLabel.setAlignment(io.jmix.ui.component.Component.Alignment.MIDDLE_LEFT);
    gridLayout.add(fieldLabel, 0, currentRow + 1);
    // current field
    ValueSource valueSource = new ContainerValueSource<>(container, metaProperty.getName());
    ComponentGenerationContext componentContext = new ComponentGenerationContext(metaClass, metaProperty.getName());
    componentContext.setValueSource(valueSource);
    gridLayout.add(createField(metaProperty, range, componentContext), 1, currentRow + 1);
    // default value
    ComponentGenerationContext componentContextForDefaultField = new ComponentGenerationContext(metaClass, metaProperty.getName());
    ValueSource valueSourceForDefaultField = new ContainerValueSource<>(dataComponents.createInstanceContainer(metaClass.getJavaClass()), metaProperty.getName());
    componentContextForDefaultField.setValueSource(valueSourceForDefaultField);
    Field defaultValueField = createField(metaProperty, range, componentContextForDefaultField);
    defaultValueField.setValue(appSettingsTools.getDefaultPropertyValue(metaClass.getJavaClass(), metaProperty.getName()));
    defaultValueField.setEditable(false);
    gridLayout.add(defaultValueField, 2, currentRow + 1);
}
Also used : UiEntityContext(io.jmix.ui.accesscontext.UiEntityContext) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) MetaClass(io.jmix.core.metamodel.model.MetaClass) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) ValueSource(io.jmix.ui.component.data.ValueSource) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext) Range(io.jmix.core.metamodel.model.Range)

Example 20 with Range

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

the class FetchPlanLoader method loadFetchPlanProperties.

public void loadFetchPlanProperties(Element fetchPlanElem, FetchPlanBuilder fetchPlanBuilder, boolean systemProperties, BiFunction<MetaClass, String, FetchPlan> refFetchPlanResolver) {
    final MetaClass metaClass = metadata.getClass(fetchPlanBuilder.getEntityClass());
    final String fetchPlanName = fetchPlanBuilder.getName();
    Set<String> propertyNames = new HashSet<>();
    for (Element propElem : fetchPlanElem.elements("property")) {
        String propertyName = propElem.attributeValue("name");
        if (propertyNames.contains(propertyName)) {
            throw new DevelopmentException(String.format("Fetch plan %s/%s definition error: fetch plan declared property %s twice", metaClass.getName(), fetchPlanName, propertyName));
        }
        propertyNames.add(propertyName);
        MetaProperty metaProperty = metaClass.getProperty(propertyName);
        if (metaProperty == null) {
            throw new DevelopmentException(String.format("Fetch plan %s/%s definition error: property %s doesn't exist", metaClass.getName(), fetchPlanName, propertyName));
        }
        FetchPlanBuilder refFetchPlanBuilder = null;
        String refFetchPlanName = propElem.attributeValue("fetchPlan");
        if (refFetchPlanName == null) {
            refFetchPlanName = propElem.attributeValue("view");
        }
        MetaClass refMetaClass;
        Range range = metaProperty.getRange();
        if (range == null) {
            throw new RuntimeException("cannot find range for meta property: " + metaProperty);
        }
        // by default specify "_base" fetchPlan in case for entity property it is missing
        if (refFetchPlanName == null && range.isClass()) {
            refFetchPlanName = FetchPlan.BASE;
        }
        final List<Element> propertyElements = propElem.elements("property");
        boolean inlineFetchPlan = !propertyElements.isEmpty();
        if (!range.isClass() && (refFetchPlanName != null || inlineFetchPlan)) {
            throw new DevelopmentException(String.format("Fetch plan %s/%s definition error: property %s is not an entity", metaClass.getName(), fetchPlanName, propertyName));
        }
        if (refFetchPlanName != null) {
            refMetaClass = getMetaClass(propElem, range);
            refFetchPlanBuilder = fetchPlans.builder(refMetaClass.getJavaClass()).name(refFetchPlanName).addFetchPlan(refFetchPlanResolver.apply(refMetaClass, refFetchPlanName));
        }
        if (inlineFetchPlan) {
            // try to import anonymous fetch plan
            Class<?> rangeClass = range.asClass().getJavaClass();
            if (refFetchPlanBuilder == null) {
                refFetchPlanBuilder = fetchPlans.builder(rangeClass);
                if (systemProperties)
                    refFetchPlanBuilder.addSystem();
            }
            loadFetchPlanProperties(propElem, refFetchPlanBuilder, systemProperties, refFetchPlanResolver);
        }
        FetchMode fetchMode = FetchMode.AUTO;
        String fetch = propElem.attributeValue("fetch");
        if (fetch != null)
            fetchMode = FetchMode.valueOf(fetch);
        fetchPlanBuilder.mergeProperty(propertyName, refFetchPlanBuilder != null ? refFetchPlanBuilder.build() : null, fetchMode);
    }
}
Also used : Element(org.dom4j.Element) Range(io.jmix.core.metamodel.model.Range) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) HashSet(java.util.HashSet)

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