Search in sources :

Example 1 with MetadataKeyId

use of org.mule.runtime.extension.api.annotation.metadata.MetadataKeyId 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)

Aggregations

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 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Function.identity (java.util.function.Function.identity)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 BooleanType (org.mule.metadata.api.model.BooleanType)1 MetadataType (org.mule.metadata.api.model.MetadataType)1 ObjectType (org.mule.metadata.api.model.ObjectType)1 StringType (org.mule.metadata.api.model.StringType)1 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)1 JavaTypeUtils.getType (org.mule.metadata.java.api.utils.JavaTypeUtils.getType)1 ComponentModel (org.mule.runtime.api.meta.model.ComponentModel)1 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)1 MetadataKey (org.mule.runtime.api.metadata.MetadataKey)1 MetadataKeyBuilder (org.mule.runtime.api.metadata.MetadataKeyBuilder)1