use of io.atlasmap.v2.ActionParameter in project atlasmap by atlasmap.
the class DefaultAtlasFieldActionsServiceTest method testListActionDetails.
@Test
public void testListActionDetails() {
assertNotNull(fieldActionsService);
List<ActionDetail> actionDetails = fieldActionsService.listActionDetails();
for (ActionDetail d : actionDetails) {
if (d.getParameters() != null) {
System.out.println("Action: " + d.getName());
for (ActionParameter param : d.getParameters().getParameter()) {
System.out.println("\t param: " + param.getName());
System.out.println("\t type: " + param.getFieldType().value());
}
}
}
}
use of io.atlasmap.v2.ActionParameter in project atlasmap by atlasmap.
the class DefaultAtlasFieldActionService method detectFieldActionParameters.
private ActionParameters detectFieldActionParameters(Class<?> actionClazz) throws ClassNotFoundException {
ActionParameters params = null;
// Java does not return methods in any consistent order, so sort
// methods by name to ensure parameter types and values get
// assigned to the correct method. This means that field actions with
// multiple parameters must define their setter methods in alphabetical
// order to be processed correctly. Not an ideal situation, but the only
// other option would be to force the specification of an order in the
// AtlasActionProperty annotation via a new parameter, which is also
// clunky.
Method[] methods = actionClazz.getMethods();
Arrays.sort(methods, new Comparator<Method>() {
@Override
public int compare(Method method1, Method method2) {
return method1.getName().compareToIgnoreCase(method2.getName());
}
});
for (Method method : methods) {
// Find setters to avoid the get / is confusion
if (method.getParameterCount() == 1 && method.getName().startsWith("set")) {
// We have a parameter
if (params == null) {
params = new ActionParameters();
}
ActionParameter actionParam = null;
for (Parameter methodParam : method.getParameters()) {
actionParam = new ActionParameter();
actionParam.setName(camelize(method.getName().substring("set".length())));
// TODO set displayName/description - https://github.com/atlasmap/atlasmap/issues/96
actionParam.setFieldType(getConversionService().fieldTypeFromClass(methodParam.getType()));
// TODO fix this dirty hack for https://github.com/atlasmap/atlasmap/issues/386
if (methodParam.getType().isEnum()) {
actionParam.setFieldType(FieldType.STRING);
try {
for (Object e : methodParam.getType().getEnumConstants()) {
Method m = e.getClass().getDeclaredMethod("value", new Class[0]);
actionParam.getValues().add(m.invoke(e, new Object[0]).toString());
}
} catch (Exception e) {
LOG.debug("Failed to populate possible enum parameter values, ignoring...", e);
}
}
params.getParameter().add(actionParam);
}
}
}
return params;
}
use of io.atlasmap.v2.ActionParameter in project atlasmap by atlasmap.
the class DefaultAtlasFunctionResolver method resolve.
@Override
public Expression resolve(final String name, List<Expression> args) throws ParseException {
String functionName = name.toUpperCase();
FunctionFactory f = functions.get(functionName);
if (f != null) {
return f.create(args);
} else {
// lookup action
return (ctx) -> {
List<Field> arguments = new ArrayList<>();
for (Expression arg : args) {
arguments.add(arg.evaluate(ctx));
}
Object valueForTypeEvaluation = null;
if (arguments.isEmpty()) {
return null;
} else {
valueForTypeEvaluation = arguments.get(arguments.size() - 1);
}
ActionProcessor actionProcessor = fieldActionService.findActionProcessor(name, valueForTypeEvaluation);
if (actionProcessor != null) {
Map<String, Object> actionParameters = new HashMap<>();
ActionParameters actionDetailParameters = actionProcessor.getActionDetail().getParameters();
if (actionDetailParameters != null && actionDetailParameters.getParameter() != null) {
for (ActionParameter parameter : actionDetailParameters.getParameter()) {
if (!arguments.isEmpty()) {
Object parameterValue = arguments.remove(0).getValue();
actionParameters.put(parameter.getName(), parameterValue);
} else {
throw new IllegalArgumentException(String.format("The transformation '%s' expects more parameters. The parameter '%s' is missing", name, parameter.getName()));
}
}
}
if (arguments.isEmpty()) {
throw new IllegalArgumentException(String.format("The transformation '%s' expects more arguments", name));
}
FieldGroup fields = new FieldGroup();
fields.getField().addAll(arguments);
return fieldActionService.buildAndProcessAction(actionProcessor, actionParameters, fields);
} else {
throw new IllegalArgumentException(String.format("The expression function or transformation '%s' was not found", name));
}
};
}
}
Aggregations