Search in sources :

Example 1 with ActionParameter

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());
            }
        }
    }
}
Also used : ActionDetail(io.atlasmap.v2.ActionDetail) ActionParameter(io.atlasmap.v2.ActionParameter) Test(org.junit.jupiter.api.Test)

Example 2 with ActionParameter

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;
}
Also used : ActionParameters(io.atlasmap.v2.ActionParameters) Parameter(java.lang.reflect.Parameter) ActionParameter(io.atlasmap.v2.ActionParameter) Method(java.lang.reflect.Method) ActionParameter(io.atlasmap.v2.ActionParameter) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasException(io.atlasmap.api.AtlasException)

Example 3 with ActionParameter

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));
            }
        };
    }
}
Also used : ActionParameters(io.atlasmap.v2.ActionParameters) FieldGroup(io.atlasmap.v2.FieldGroup) Expression(io.atlasmap.expression.Expression) FunctionResolver(io.atlasmap.expression.FunctionResolver) HashMap(java.util.HashMap) ServiceLoader(java.util.ServiceLoader) ParseException(io.atlasmap.expression.parser.ParseException) ArrayList(java.util.ArrayList) List(java.util.List) FunctionFactory(io.atlasmap.spi.FunctionFactory) Field(io.atlasmap.v2.Field) Map(java.util.Map) ActionProcessor(io.atlasmap.spi.ActionProcessor) ActionParameter(io.atlasmap.v2.ActionParameter) FieldGroup(io.atlasmap.v2.FieldGroup) ActionParameter(io.atlasmap.v2.ActionParameter) Expression(io.atlasmap.expression.Expression) ActionParameters(io.atlasmap.v2.ActionParameters) ArrayList(java.util.ArrayList) List(java.util.List) ActionProcessor(io.atlasmap.spi.ActionProcessor) HashMap(java.util.HashMap) Map(java.util.Map) FunctionFactory(io.atlasmap.spi.FunctionFactory)

Aggregations

ActionParameter (io.atlasmap.v2.ActionParameter)3 ActionParameters (io.atlasmap.v2.ActionParameters)2 AtlasConversionException (io.atlasmap.api.AtlasConversionException)1 AtlasException (io.atlasmap.api.AtlasException)1 Expression (io.atlasmap.expression.Expression)1 FunctionResolver (io.atlasmap.expression.FunctionResolver)1 ParseException (io.atlasmap.expression.parser.ParseException)1 ActionProcessor (io.atlasmap.spi.ActionProcessor)1 FunctionFactory (io.atlasmap.spi.FunctionFactory)1 ActionDetail (io.atlasmap.v2.ActionDetail)1 Field (io.atlasmap.v2.Field)1 FieldGroup (io.atlasmap.v2.FieldGroup)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ServiceLoader (java.util.ServiceLoader)1 Test (org.junit.jupiter.api.Test)1