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