Search in sources :

Example 1 with QueryEntityResolver

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);
    }
}
Also used : ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) ObjectType(org.mule.metadata.api.model.ObjectType) OutputTypeResolver(org.mule.runtime.api.metadata.resolving.OutputTypeResolver) Field(org.mule.runtime.extension.api.dsql.Field) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) List(java.util.List) ConnectionException(org.mule.runtime.api.connection.ConnectionException) MetadataContext(org.mule.runtime.api.metadata.MetadataContext) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder) QueryEntityResolver(org.mule.runtime.api.metadata.resolving.QueryEntityResolver) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) Field(org.mule.runtime.extension.api.dsql.Field) ObjectType(org.mule.metadata.api.model.ObjectType) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder)

Example 2 with QueryEntityResolver

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());
    }
}
Also used : QueryEntityResolver(org.mule.runtime.api.metadata.resolving.QueryEntityResolver) MetadataKeysContainerBuilder(org.mule.runtime.api.metadata.MetadataKeysContainerBuilder) MetadataKey(org.mule.runtime.api.metadata.MetadataKey)

Aggregations

QueryEntityResolver (org.mule.runtime.api.metadata.resolving.QueryEntityResolver)2 List (java.util.List)1 BaseTypeBuilder (org.mule.metadata.api.builder.BaseTypeBuilder)1 ObjectFieldTypeBuilder (org.mule.metadata.api.builder.ObjectFieldTypeBuilder)1 ObjectTypeBuilder (org.mule.metadata.api.builder.ObjectTypeBuilder)1 MetadataType (org.mule.metadata.api.model.MetadataType)1 ObjectType (org.mule.metadata.api.model.ObjectType)1 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)1 ConnectionException (org.mule.runtime.api.connection.ConnectionException)1 MetadataContext (org.mule.runtime.api.metadata.MetadataContext)1 MetadataKey (org.mule.runtime.api.metadata.MetadataKey)1 MetadataKeysContainerBuilder (org.mule.runtime.api.metadata.MetadataKeysContainerBuilder)1 MetadataResolvingException (org.mule.runtime.api.metadata.MetadataResolvingException)1 OutputTypeResolver (org.mule.runtime.api.metadata.resolving.OutputTypeResolver)1 DsqlQuery (org.mule.runtime.extension.api.dsql.DsqlQuery)1 Field (org.mule.runtime.extension.api.dsql.Field)1