use of org.mule.runtime.api.metadata.resolving.QueryEntityResolver in project mule by mulesoft.
the class DsqlQueryMetadataResolver method getOutputType.
/**
* Automatically resolves the output metadata for the {@link DsqlQuery}.
* <p>
* The base entity is resolved using the component {@link QueryEntityResolver} and assuming the key of the entity is the DSQL
* {@link DsqlQuery#getType() type}.
*
* @param context {@link MetadataContext} of the MetaData resolution
* @param query the {@link DsqlQuery} to resolve the output metadata from.
*/
@Override
public MetadataType getOutputType(MetadataContext context, Object query) throws MetadataResolvingException, ConnectionException {
if (query instanceof DsqlQuery) {
DsqlQuery dsqlQuery = (DsqlQuery) query;
MetadataType entityMetadata = entityResolver.getEntityMetadata(context, dsqlQuery.getType().getName());
BaseTypeBuilder builder = context.getTypeBuilder();
final List<Field> fields = dsqlQuery.getFields();
if (fields.size() == 1 && fields.get(0).getName().equals("*")) {
return entityMetadata;
}
entityMetadata.accept(new MetadataTypeVisitor() {
@Override
public void visitObject(ObjectType objectType) {
ObjectTypeBuilder objectTypeBuilder = builder.objectType();
objectType.getFields().stream().filter(p -> fields.stream().anyMatch(f -> f.getName().equalsIgnoreCase(p.getKey().getName().getLocalPart()))).forEach(p -> {
ObjectFieldTypeBuilder field = objectTypeBuilder.addField();
field.key(p.getKey().getName());
field.value(p.getValue());
});
}
});
return builder.build();
} else {
return nativeOutputResolver.getOutputType(context, query);
}
}
use of org.mule.runtime.api.metadata.resolving.QueryEntityResolver in project mule by mulesoft.
the class EntityMetadataMediator method getEntityKeys.
public MetadataResult<MetadataKeysContainer> getEntityKeys(MetadataContext context) {
try {
QueryEntityResolver queryEntityResolver = resolverFactory.getQueryEntityResolver();
Set<MetadataKey> entityKeys = queryEntityResolver.getEntityKeys(context);
final MetadataKeysContainerBuilder keyBuilder = MetadataKeysContainerBuilder.getInstance();
if (entityKeys.stream().anyMatch(key -> key.getChilds().size() > 0)) {
return failure(newFailure().withMessage("Error retrieving entity keys, Only single level keys are supported for entity metadata").withReason("There are at least one key that contains childs").withFailureCode(INVALID_METADATA_KEY).onKeys());
}
return success(keyBuilder.add(queryEntityResolver.getResolverName(), entityKeys).build());
} catch (Exception e) {
return failure(newFailure(e).onKeys());
}
}
Aggregations