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;
}
Aggregations