Search in sources :

Example 6 with MetadataResolvingException

use of org.mule.runtime.api.metadata.MetadataResolvingException in project mule by mulesoft.

the class TestMetadataResolverUtils method getMetadata.

public static MetadataType getMetadata(String key) throws MetadataResolvingException {
    final ObjectTypeBuilder objectBuilder = BaseTypeBuilder.create(JAVA).objectType();
    switch(key) {
        case MetadataConnection.CAR:
            objectBuilder.addField().key(TIRES).value().numberType();
            objectBuilder.addField().key(BRAND).value().stringType();
            break;
        case MetadataConnection.HOUSE:
            objectBuilder.addField().key(DIRECTION).value().stringType();
            objectBuilder.addField().key(SIZE).value().numberType();
            break;
        case MetadataConnection.PERSON:
            objectBuilder.addField().key(NAME).value().stringType();
            objectBuilder.addField().key(AGE).value().numberType();
            break;
        case MetadataConnection.NULL:
            return BaseTypeBuilder.create(JAVA).nullType().build();
        case MetadataConnection.VOID:
            return BaseTypeBuilder.create(JAVA).voidType().build();
        default:
            throw new MetadataResolvingException("Unknown key " + key, INVALID_METADATA_KEY);
    }
    return objectBuilder.build();
}
Also used : MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder)

Example 7 with MetadataResolvingException

use of org.mule.runtime.api.metadata.MetadataResolvingException 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 8 with MetadataResolvingException

use of org.mule.runtime.api.metadata.MetadataResolvingException in project mule by mulesoft.

the class MetadataKeyIdObjectResolver method instantiateFromFieldValue.

private Object instantiateFromFieldValue(Class<?> metadataKeyType, Map<Field, String> fieldValueMap) throws MetadataResolvingException {
    try {
        DefaultObjectBuilder objectBuilder = new DefaultObjectBuilder<>(metadataKeyType);
        fieldValueMap.forEach((f, v) -> objectBuilder.addPropertyResolver(f.getName(), new StaticValueResolver<>(v)));
        return objectBuilder.build(null);
    } catch (Exception e) {
        throw buildException(format("MetadataKey object of type '%s' from the component '%s' could not be instantiated", metadataKeyType.getSimpleName(), component.getName()), e);
    }
}
Also used : StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException)

Example 9 with MetadataResolvingException

use of org.mule.runtime.api.metadata.MetadataResolvingException in project mule by mulesoft.

the class MetadataKeyIdObjectResolver method resolve.

/**
 * Returns the populated key in the Type that the component parameter requires by looking for default values, if no
 * {@link MetadataKeyId} is present an empty value is returned since is a key less component.
 * <p>
 * If a key should be built and there is at least one default value missing an {@link IllegalArgumentException} is thrown.
 *
 * @return a new instance of the {@link MetadataKeyId} parameter {@code type}.
 * @throws MetadataResolvingException if the Parameter type is not instantiable.
 * @throws IllegalArgumentException if cannot found the required default values for an specified key.
 */
public Object resolve() throws MetadataResolvingException {
    if (isKeyLess()) {
        return NullMetadataKey.ID;
    }
    if (!keyParts.stream().allMatch(p -> p.getDefaultValue() != null)) {
        throw new IllegalArgumentException("Could not build metadata key from an object that does" + " not have a default value for all it's components.");
    }
    String id = keyParts.get(0).getDefaultValue().toString();
    final MetadataKeyIdModelProperty keyIdModelProperty = findMetadataKeyIdModelProperty(component);
    MetadataType type = keyIdModelProperty.getType();
    KeyMetadataTypeVisitor visitor = new KeyMetadataTypeVisitor(id, getType(type)) {

        @Override
        protected Map<Field, String> getFieldValuesMap() {
            return keyParts.stream().filter(p -> p.getModelProperty(DeclaringMemberModelProperty.class).isPresent()).collect(toMap(p -> p.getModelProperty(DeclaringMemberModelProperty.class).get().getDeclaringField(), p -> p.getDefaultValue().toString()));
        }
    };
    type.accept(visitor);
    return visitor.getResultId();
}
Also used : StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) IntrospectionUtils.getFieldValue(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getFieldValue) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) MetadataKeyId(org.mule.runtime.extension.api.annotation.metadata.MetadataKeyId) HashMap(java.util.HashMap) DsqlParser.isDsqlQuery(org.mule.runtime.extension.api.dsql.DsqlParser.isDsqlQuery) MetadataKeyBuilder(org.mule.runtime.api.metadata.MetadataKeyBuilder) QueryParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.QueryParameterModelProperty) MetadataKey(org.mule.runtime.api.metadata.MetadataKey) DsqlParser(org.mule.runtime.extension.api.dsql.DsqlParser) DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) Collectors.toMap(java.util.stream.Collectors.toMap) MetadataKeyPartModelProperty(org.mule.runtime.extension.api.property.MetadataKeyPartModelProperty) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) Map(java.util.Map) MetadataKeyBuilder.newKey(org.mule.runtime.api.metadata.MetadataKeyBuilder.newKey) INVALID_METADATA_KEY(org.mule.runtime.api.metadata.resolving.FailureCode.INVALID_METADATA_KEY) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) NullMetadataKey(org.mule.runtime.extension.api.metadata.NullMetadataKey) FailureCode(org.mule.runtime.api.metadata.resolving.FailureCode) ObjectType(org.mule.metadata.api.model.ObjectType) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) Field(java.lang.reflect.Field) String.format(java.lang.String.format) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) String.valueOf(java.lang.String.valueOf) StringType(org.mule.metadata.api.model.StringType) BooleanType(org.mule.metadata.api.model.BooleanType) Reference(org.mule.runtime.api.util.Reference) Function.identity(java.util.function.Function.identity) MetadataType(org.mule.metadata.api.model.MetadataType) Optional(java.util.Optional) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) Field(java.lang.reflect.Field) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) MetadataType(org.mule.metadata.api.model.MetadataType)

Example 10 with MetadataResolvingException

use of org.mule.runtime.api.metadata.MetadataResolvingException in project mule by mulesoft.

the class TestResolverWithCache method getInputMetadata.

@Override
public MetadataType getInputMetadata(MetadataContext context, String key) throws MetadataResolvingException, ConnectionException {
    MetadataCache cache = context.getCache();
    Optional<? extends Serializable> element = cache.get(BRAND);
    if (!element.isPresent()) {
        throw new MetadataResolvingException(MISSING_ELEMENT_ERROR_MESSAGE, FailureCode.RESOURCE_UNAVAILABLE);
    }
    return buildMetadataType((String) element.get());
}
Also used : MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) MetadataCache(org.mule.runtime.api.metadata.MetadataCache)

Aggregations

MetadataResolvingException (org.mule.runtime.api.metadata.MetadataResolvingException)10 MetadataType (org.mule.metadata.api.model.MetadataType)3 List (java.util.List)2 ObjectTypeBuilder (org.mule.metadata.api.builder.ObjectTypeBuilder)2 ObjectType (org.mule.metadata.api.model.ObjectType)2 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)2 ConnectionException (org.mule.runtime.api.connection.ConnectionException)2 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)2 MetadataContext (org.mule.runtime.api.metadata.MetadataContext)2 MetadataKeyBuilder (org.mule.runtime.api.metadata.MetadataKeyBuilder)2 DsqlQuery (org.mule.runtime.extension.api.dsql.DsqlQuery)2 DefaultMetadataContext (org.mule.runtime.module.extension.internal.metadata.DefaultMetadataContext)2 DefaultObjectBuilder (org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder)2 StaticValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver)2 String.format (java.lang.String.format)1 String.valueOf (java.lang.String.valueOf)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1