Search in sources :

Example 1 with SubString

use of io.atlasmap.v2.SubString in project atlasmap by atlasmap.

the class DefaultAtlasFieldActionService method detectFieldActionParameters.

protected Properties detectFieldActionParameters(String actionClassName) throws ClassNotFoundException {
    Class<?> actionClazz = Class.forName(actionClassName);
    Properties props = null;
    for (Method method : actionClazz.getMethods()) {
        // Find setters to avoid the get / is confusion
        if (method.getParameterCount() == 1 && method.getName().startsWith("set")) {
            // We have a parameter
            if (props == null) {
                props = new Properties();
            }
            Property prop = null;
            for (Parameter param : method.getParameters()) {
                prop = new Property();
                prop.setName(camelize(method.getName().substring("set".length())));
                prop.setFieldType(getConversionService().fieldTypeFromClass(param.getType()));
                props.getProperty().add(prop);
            }
        }
    }
    return props;
}
Also used : Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) Properties(io.atlasmap.v2.Properties) Property(io.atlasmap.v2.Property)

Example 2 with SubString

use of io.atlasmap.v2.SubString in project atlasmap by atlasmap.

the class DefaultAtlasFieldActionService method buildAndProcessAction.

/**
 * Instantiate the field action impl class from {@link ActionProcessor} and processes it.
 * @param actionProcessor action processor
 * @param actionParameters action parameters
 * @param field field
 * @return processed field
 */
public Field buildAndProcessAction(ActionProcessor actionProcessor, Map<String, Object> actionParameters, Field field) {
    FieldType valueType = determineFieldType(field);
    try {
        Action action = actionProcessor.getActionClass().getDeclaredConstructor().newInstance();
        for (Map.Entry<String, Object> property : actionParameters.entrySet()) {
            String setter = "set" + property.getKey().substring(0, 1).toUpperCase() + property.getKey().substring(1);
            action.getClass().getMethod(setter, property.getValue().getClass()).invoke(action, property.getValue());
        }
        return processAction(action, actionProcessor, valueType, field);
    } catch (Exception e) {
        throw new IllegalArgumentException(String.format("The action '%s' cannot be processed", actionProcessor.getActionDetail().getName()), e);
    }
}
Also used : CustomAction(io.atlasmap.v2.CustomAction) AtlasFieldAction(io.atlasmap.spi.AtlasFieldAction) Action(io.atlasmap.v2.Action) Map(java.util.Map) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasException(io.atlasmap.api.AtlasException) FieldType(io.atlasmap.v2.FieldType)

Example 3 with SubString

use of io.atlasmap.v2.SubString 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 4 with SubString

use of io.atlasmap.v2.SubString in project atlasmap by atlasmap.

the class StringComplexFieldActionsTest method testSubString.

@Test
public void testSubString() {
    SubString action = new SubString();
    action.setStartIndex(2);
    action.setEndIndex(4);
    assertNull(StringComplexFieldActions.subString(action, null));
    assertEquals("", StringComplexFieldActions.subString(action, ""));
    try {
        StringComplexFieldActions.subString(null, "aa");
        fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException e) {
        assertTrue(true);
    }
}
Also used : SubString(io.atlasmap.v2.SubString) Test(org.junit.jupiter.api.Test)

Example 5 with SubString

use of io.atlasmap.v2.SubString in project atlasmap by atlasmap.

the class AtlasBaseActionsTest method testActions.

@Test
public void testActions() throws Exception {
    List<ActionDetail> actions = DefaultAtlasContextFactory.getInstance().getFieldActionService().listActionDetails();
    for (ActionDetail d : actions) {
        System.out.println(d.getName());
    }
    this.runActionTest(new Uppercase(), "fname", "FNAME", String.class);
    this.runActionTest(new Lowercase(), "fnAme", "fname", String.class);
    this.runActionTest(new Trim(), " fname ", "fname", String.class);
    this.runActionTest(new TrimLeft(), " fname ", "fname ", String.class);
    this.runActionTest(new TrimRight(), " fname ", " fname", String.class);
    this.runActionTest(new Capitalize(), "fname", "Fname", String.class);
    this.runActionTest(new SeparateByDash(), "f:name", "f-name", String.class);
    this.runActionTest(new SeparateByUnderscore(), "f-na_me", "f_na_me", String.class);
    SubString s = new SubString();
    s.setStartIndex(0);
    s.setEndIndex(3);
    this.runActionTest(s, "12345", "123", String.class);
    SubStringAfter s1 = new SubStringAfter();
    s1.setStartIndex(3);
    s1.setEndIndex(null);
    s1.setMatch("foo");
    this.runActionTest(s1, "foobarblah", "blah", String.class);
    SubStringBefore s2 = new SubStringBefore();
    s2.setStartIndex(3);
    s2.setEndIndex(null);
    s2.setMatch("blah");
    this.runActionTest(s2, "foobarblah", "bar", String.class);
    PadStringRight ps = new PadStringRight();
    ps.setPadCharacter("X");
    ps.setPadCount(5);
    this.runActionTest(ps, "fname", "fnameXXXXX", String.class);
    PadStringLeft pl = new PadStringLeft();
    pl.setPadCharacter("X");
    pl.setPadCount(5);
    this.runActionTest(pl, "fname", "XXXXXfname", String.class);
/* ref https://github.com/atlasmap/atlasmap/issues/674
        String result = (String) runActionTest(new GenerateUUID(), "fname", null, String.class);
        assertTrue(Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}").matcher(result)
                .matches());
        */
}
Also used : ActionDetail(io.atlasmap.v2.ActionDetail) SeparateByUnderscore(io.atlasmap.v2.SeparateByUnderscore) Trim(io.atlasmap.v2.Trim) TrimRight(io.atlasmap.v2.TrimRight) SubString(io.atlasmap.v2.SubString) SubStringAfter(io.atlasmap.v2.SubStringAfter) PadStringLeft(io.atlasmap.v2.PadStringLeft) SeparateByDash(io.atlasmap.v2.SeparateByDash) TrimLeft(io.atlasmap.v2.TrimLeft) Uppercase(io.atlasmap.v2.Uppercase) Lowercase(io.atlasmap.v2.Lowercase) Capitalize(io.atlasmap.v2.Capitalize) PadStringRight(io.atlasmap.v2.PadStringRight) SubStringBefore(io.atlasmap.v2.SubStringBefore) Test(org.junit.jupiter.api.Test)

Aggregations

SubString (io.atlasmap.v2.SubString)3 AtlasConversionException (io.atlasmap.api.AtlasConversionException)2 AtlasException (io.atlasmap.api.AtlasException)2 ActionDetail (io.atlasmap.v2.ActionDetail)2 Capitalize (io.atlasmap.v2.Capitalize)2 Lowercase (io.atlasmap.v2.Lowercase)2 PadStringLeft (io.atlasmap.v2.PadStringLeft)2 PadStringRight (io.atlasmap.v2.PadStringRight)2 SeparateByDash (io.atlasmap.v2.SeparateByDash)2 SeparateByUnderscore (io.atlasmap.v2.SeparateByUnderscore)2 SubStringAfter (io.atlasmap.v2.SubStringAfter)2 SubStringBefore (io.atlasmap.v2.SubStringBefore)2 Trim (io.atlasmap.v2.Trim)2 TrimLeft (io.atlasmap.v2.TrimLeft)2 TrimRight (io.atlasmap.v2.TrimRight)2 Uppercase (io.atlasmap.v2.Uppercase)2 Method (java.lang.reflect.Method)2 Parameter (java.lang.reflect.Parameter)2 Test (org.junit.jupiter.api.Test)2 AtlasFieldAction (io.atlasmap.spi.AtlasFieldAction)1