Search in sources :

Example 6 with ActionDetail

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

the class DefaultAtlasFieldActionsServiceTest method testProcessActionWithActionActionDetailObjectAtlasException.

@Test(expected = AtlasException.class)
public void testProcessActionWithActionActionDetailObjectAtlasException() throws AtlasException {
    Action action = new AbsoluteValue();
    Object sourceObject = new Integer("1");
    ActionDetail actionDetail = new ActionDetail();
    actionDetail.setClassName("io.atlasmap.actions.NumberFieldActions");
    actionDetail.setSourceType(FieldType.INTEGER);
    actionDetail.setMethod("absoluteValue");
    fieldActionsService.processAction(action, actionDetail, sourceObject);
}
Also used : Action(io.atlasmap.v2.Action) AbsoluteValue(io.atlasmap.v2.AbsoluteValue) ActionDetail(io.atlasmap.v2.ActionDetail) Test(org.junit.Test)

Example 7 with ActionDetail

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

the class DefaultAtlasFieldActionsServiceTest method testFindActionDetail.

@Test
public void testFindActionDetail() {
    ActionDetail actionDetail = fieldActionsService.findActionDetail("IndexOf", FieldType.STRING);
    assertNotNull(actionDetail);
    actionDetail = fieldActionsService.findActionDetail("Index", FieldType.STRING);
    assertNull(actionDetail);
    ActionDetail ad = new ActionDetail();
    ad.setName("IndexOf");
    ad.setSourceType(FieldType.INTEGER);
    fieldActionsService.listActionDetails().add(ad);
    actionDetail = fieldActionsService.findActionDetail("IndexOf", null);
    assertNotNull(actionDetail);
    actionDetail = fieldActionsService.findActionDetail("IndexOf", FieldType.STRING);
    assertNotNull(actionDetail);
    actionDetail = fieldActionsService.findActionDetail("IndexOf", FieldType.ANY);
    assertNotNull(actionDetail);
    actionDetail = fieldActionsService.findActionDetail("IndexOf", FieldType.NONE);
    assertNotNull(actionDetail);
    actionDetail = fieldActionsService.findActionDetail("IndexOf", FieldType.BOOLEAN);
    assertNotNull(actionDetail);
}
Also used : ActionDetail(io.atlasmap.v2.ActionDetail) Test(org.junit.Test)

Example 8 with ActionDetail

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

the class DefaultAtlasFieldActionService method loadFieldActions.

protected void loadFieldActions() {
    ClassLoader classLoader = this.getClass().getClassLoader();
    final ServiceLoader<AtlasFieldAction> fieldActionServiceLoader = ServiceLoader.load(AtlasFieldAction.class, classLoader);
    for (final AtlasFieldAction atlasFieldAction : fieldActionServiceLoader) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Loading FieldAction class: " + atlasFieldAction.getClass().getCanonicalName());
        }
        Class<?> clazz = atlasFieldAction.getClass();
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            AtlasFieldActionInfo annotation = method.getAnnotation(AtlasFieldActionInfo.class);
            if (annotation != null) {
                ActionDetail det = new ActionDetail();
                det.setClassName(clazz.getName());
                det.setMethod(method.getName());
                det.setName(annotation.name());
                det.setSourceType(annotation.sourceType());
                det.setTargetType(annotation.targetType());
                det.setSourceCollectionType(annotation.sourceCollectionType());
                det.setTargetCollectionType(annotation.targetCollectionType());
                try {
                    det.setParameters(detectFieldActionParameters("io.atlasmap.v2." + annotation.name()));
                } catch (ClassNotFoundException e) {
                    LOG.error(String.format("Error detecting parameters for field action=%s msg=%s", annotation.name(), e.getMessage()), e);
                }
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Loaded FieldAction: " + det.getName());
                }
                listActionDetails().add(det);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Loaded %s Field Actions", listActionDetails().size()));
    }
}
Also used : ActionDetail(io.atlasmap.v2.ActionDetail) AtlasFieldAction(io.atlasmap.api.AtlasFieldAction) Method(java.lang.reflect.Method) AtlasFieldActionInfo(io.atlasmap.spi.AtlasFieldActionInfo)

Example 9 with ActionDetail

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

the class DefaultAtlasFieldActionService method processAction.

protected Object processAction(Action action, ActionDetail actionDetail, Object sourceObject) throws AtlasException {
    Object targetObject = null;
    if (actionDetail != null) {
        Object actionObject = null;
        try {
            Class<?> actionClazz = Class.forName(actionDetail.getClassName());
            actionObject = actionClazz.newInstance();
            Method method = null;
            if (actionDetail.getSourceType() != null) {
                switch(actionDetail.getSourceType()) {
                    case ANY:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Object.class);
                        break;
                    case BIG_INTEGER:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, BigInteger.class);
                        break;
                    case BOOLEAN:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Boolean.class);
                        break;
                    case BYTE:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Byte.class);
                        break;
                    case BYTE_ARRAY:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Byte[].class);
                        break;
                    case CHAR:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Character.class);
                        break;
                    case DATE:
                    case DATE_TIME:
                    case DATE_TZ:
                    case TIME_TZ:
                    case DATE_TIME_TZ:
                    case ANY_DATE:
                        if (sourceObject instanceof Calendar) {
                            sourceObject = DateTimeHelper.toZonedDateTime((Calendar) sourceObject);
                        } else if (sourceObject instanceof Date) {
                            sourceObject = DateTimeHelper.toZonedDateTime((Date) sourceObject, null);
                        } else if (sourceObject instanceof LocalDate) {
                            sourceObject = DateTimeHelper.toZonedDateTime((LocalDate) sourceObject, null);
                        } else if (sourceObject instanceof LocalTime) {
                            sourceObject = DateTimeHelper.toZonedDateTime((LocalTime) sourceObject, null);
                        } else if (sourceObject instanceof LocalDateTime) {
                            sourceObject = DateTimeHelper.toZonedDateTime((LocalDateTime) sourceObject, null);
                        } else if (!(sourceObject instanceof ZonedDateTime)) {
                            LOG.warn(String.format("Unsupported sourceObject type=%s in actionClass=%s", sourceObject.getClass(), actionDetail.getClassName()));
                            break;
                        }
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, ZonedDateTime.class);
                        break;
                    case DECIMAL:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, BigDecimal.class);
                        break;
                    case DOUBLE:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Double.class);
                        break;
                    case FLOAT:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Float.class);
                        break;
                    case INTEGER:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Integer.class);
                        break;
                    case LONG:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Long.class);
                        break;
                    case NUMBER:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Number.class);
                        break;
                    case SHORT:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Short.class);
                        break;
                    case STRING:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, String.class);
                        break;
                    default:
                        LOG.warn(String.format("Unsupported sourceType=%s in actionClass=%s", actionDetail.getSourceType().value(), actionDetail.getClassName()));
                        break;
                }
            }
            if (method == null) {
                throw new AtlasException(String.format("Unable to locate field action className=%s method=%s sourceType=%s", actionDetail.getClassName(), actionDetail.getMethod(), actionDetail.getSourceType()));
            }
            if (Modifier.isStatic(method.getModifiers())) {
                targetObject = method.invoke(null, action, sourceObject);
            } else {
                targetObject = method.invoke(actionObject, action, sourceObject);
            }
        } catch (Throwable e) {
            throw new AtlasException(String.format("Error processing action %s", actionDetail.getName()), e);
        }
        return targetObject;
    }
    return sourceObject;
}
Also used : LocalDateTime(java.time.LocalDateTime) AtlasFieldAction(io.atlasmap.api.AtlasFieldAction) Action(io.atlasmap.v2.Action) LocalTime(java.time.LocalTime) Calendar(java.util.Calendar) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) ZonedDateTime(java.time.ZonedDateTime) BigInteger(java.math.BigInteger)

Example 10 with ActionDetail

use of io.atlasmap.v2.ActionDetail 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);
    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) GenerateUUID(io.atlasmap.v2.GenerateUUID) SubString(io.atlasmap.v2.SubString) 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.Test)

Aggregations

ActionDetail (io.atlasmap.v2.ActionDetail)9 Test (org.junit.Test)7 Action (io.atlasmap.v2.Action)5 AtlasFieldAction (io.atlasmap.api.AtlasFieldAction)3 AbsoluteValue (io.atlasmap.v2.AbsoluteValue)2 GenerateUUID (io.atlasmap.v2.GenerateUUID)2 Trim (io.atlasmap.v2.Trim)2 Method (java.lang.reflect.Method)2 AtlasException (io.atlasmap.api.AtlasException)1 AtlasFieldActionInfo (io.atlasmap.spi.AtlasFieldActionInfo)1 Capitalize (io.atlasmap.v2.Capitalize)1 Field (io.atlasmap.v2.Field)1 FieldType (io.atlasmap.v2.FieldType)1 Lowercase (io.atlasmap.v2.Lowercase)1 PadStringLeft (io.atlasmap.v2.PadStringLeft)1 PadStringRight (io.atlasmap.v2.PadStringRight)1 Property (io.atlasmap.v2.Property)1 SeparateByDash (io.atlasmap.v2.SeparateByDash)1 SeparateByUnderscore (io.atlasmap.v2.SeparateByUnderscore)1 SimpleField (io.atlasmap.v2.SimpleField)1