Search in sources :

Example 61 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class ResponseBuilder method buildResponse.

/**
 * Convert loaded entity to data fetcher return format (Map<String, Object>)
 *
 * @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<String, Object>
 */
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)

Aggregations

FetchPlan (io.jmix.core.FetchPlan)61 Test (org.junit.jupiter.api.Test)48 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)45 View (com.haulmont.cuba.core.global.View)35 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)10 Pet (com.haulmont.cuba.core.model.Pet)5 SoftDeleteOneToOneA (com.haulmont.cuba.core.model.SoftDeleteOneToOneA)5 Group (com.haulmont.cuba.core.model.common.Group)5 User (com.haulmont.cuba.core.model.common.User)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 DataManager (com.haulmont.cuba.core.global.DataManager)3 LoadContext (com.haulmont.cuba.core.global.LoadContext)3 FetchPlanProperty (io.jmix.core.FetchPlanProperty)3 Metadata (io.jmix.core.Metadata)3 MetadataTools (io.jmix.core.MetadataTools)3 MetaClass (io.jmix.core.metamodel.model.MetaClass)3 SoftDeleteOneToOneB (com.haulmont.cuba.core.model.SoftDeleteOneToOneB)2 Permission (com.haulmont.cuba.core.model.common.Permission)2 QueryImpl (com.haulmont.cuba.core.sys.QueryImpl)2 Entity (io.jmix.core.Entity)2