Search in sources :

Example 36 with Action

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

the class StringComplexFieldActions method replaceFirst.

@AtlasFieldActionInfo(name = "ReplaceFirst", sourceType = FieldType.STRING, targetType = FieldType.STRING, sourceCollectionType = CollectionType.NONE, targetCollectionType = CollectionType.NONE)
public static String replaceFirst(Action action, String input) {
    if (action == null || !(action instanceof ReplaceFirst)) {
        throw new IllegalArgumentException("Action must be a ReplaceFirst action");
    }
    ReplaceFirst replaceFirst = (ReplaceFirst) action;
    String match = replaceFirst.getMatch();
    if (match == null || match.length() == 0) {
        throw new IllegalArgumentException("ReplaceFirst action must be specified with a non-empty old string");
    }
    String newString = replaceFirst.getNewString();
    return input == null ? null : input.replaceFirst(match, newString == null ? "" : newString);
}
Also used : SubString(io.atlasmap.v2.SubString) ReplaceFirst(io.atlasmap.v2.ReplaceFirst) AtlasFieldActionInfo(io.atlasmap.spi.AtlasFieldActionInfo)

Example 37 with Action

use of io.atlasmap.v2.Action 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 38 with Action

use of io.atlasmap.v2.Action 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 39 with Action

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

the class AtlasBaseActionsTest method runActionTestList.

public Object runActionTestList(List<Action> actions, String sourceFirstName, Object targetExpected, Class<?> targetClassExpected) throws Exception {
    System.out.println("Now running test for actions: " + actions);
    System.out.println("Input: " + sourceFirstName);
    System.out.println("Expected output: " + targetExpected);
    Mapping m = new Mapping();
    m.setMappingType(MappingType.MAP);
    m.getInputField().add(this.sourceField);
    m.getOutputField().add(this.targetField);
    if (actions != null) {
        m.getOutputField().get(0).setActions(new Actions());
        m.getOutputField().get(0).getActions().getActions().addAll(actions);
    }
    DataSource src = new DataSource();
    src.setDataSourceType(DataSourceType.SOURCE);
    src.setUri(this.docURI);
    DataSource tgt = new DataSource();
    tgt.setDataSourceType(DataSourceType.TARGET);
    tgt.setUri(this.docURI);
    AtlasMapping atlasMapping = new AtlasMapping();
    atlasMapping.setName("fieldactiontest");
    atlasMapping.setMappings(new Mappings());
    atlasMapping.getMappings().getMapping().add(m);
    atlasMapping.getDataSource().add(src);
    atlasMapping.getDataSource().add(tgt);
    String tmpFile = "target/fieldactions-" + this.getClass().getSimpleName() + "-tmp.txt";
    DefaultAtlasContextFactory.getInstance().getMappingService().saveMappingAsFile(atlasMapping, new File(tmpFile), AtlasMappingFormat.XML);
    AtlasContext context = atlasContextFactory.createContext(new File(tmpFile).toURI());
    AtlasSession session = context.createSession();
    session.setDefaultSourceDocument(createSource(sourceFirstName));
    context.process(session);
    Object targetActual = session.getDefaultTargetDocument();
    assertNotNull(targetActual);
    targetActual = getTargetValue(targetActual, targetClassExpected);
    if (targetExpected != null) {
        assertEquals(targetExpected, targetActual);
    }
    return targetActual;
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) Mappings(io.atlasmap.v2.Mappings) Actions(io.atlasmap.v2.Actions) AtlasContext(io.atlasmap.api.AtlasContext) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) SubString(io.atlasmap.v2.SubString) File(java.io.File) AtlasSession(io.atlasmap.api.AtlasSession) DataSource(io.atlasmap.v2.DataSource)

Example 40 with Action

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

the class StringComplexFieldActionsTest method testPrepend.

@Test
public void testPrepend() {
    Prepend action = new Prepend();
    assertEquals(null, StringComplexFieldActions.prepend(action, null));
    assertEquals("foo", StringComplexFieldActions.prepend(action, "foo"));
    assertEquals("1", StringComplexFieldActions.prepend(action, 1));
    action.setString("");
    assertEquals("", StringComplexFieldActions.prepend(action, null));
    assertEquals("foo", StringComplexFieldActions.prepend(action, "foo"));
    action.setString("bar");
    assertEquals("bar", StringComplexFieldActions.prepend(action, null));
    assertEquals("barfoo", StringComplexFieldActions.prepend(action, "foo"));
    assertEquals("bar1", StringComplexFieldActions.prepend(action, 1));
}
Also used : Prepend(io.atlasmap.v2.Prepend) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)28 AtlasFieldActionInfo (io.atlasmap.spi.AtlasFieldActionInfo)13 Action (io.atlasmap.v2.Action)6 ActionDetail (io.atlasmap.v2.ActionDetail)6 SubString (io.atlasmap.v2.SubString)6 ConvertMassUnit (io.atlasmap.v2.ConvertMassUnit)5 Action (com.opensymphony.xwork2.Action)3 AtlasFieldAction (io.atlasmap.api.AtlasFieldAction)3 AtlasMapping (io.atlasmap.v2.AtlasMapping)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 AbsoluteValue (io.atlasmap.v2.AbsoluteValue)2 Actions (io.atlasmap.v2.Actions)2 Append (io.atlasmap.v2.Append)2 Concatenate (io.atlasmap.v2.Concatenate)2 ConvertAreaUnit (io.atlasmap.v2.ConvertAreaUnit)2 ConvertDistanceUnit (io.atlasmap.v2.ConvertDistanceUnit)2 ConvertVolumeUnit (io.atlasmap.v2.ConvertVolumeUnit)2 DataSource (io.atlasmap.v2.DataSource)2 LastIndexOf (io.atlasmap.v2.LastIndexOf)2