Search in sources :

Example 1 with AtlasActionProcessor

use of io.atlasmap.spi.AtlasActionProcessor in project atlasmap by atlasmap.

the class DefaultAtlasFieldActionService method createActionProcessor.

private void createActionProcessor(AtlasFieldAction atlasFieldAction, List<ActionProcessor> answer) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loading FieldAction class: " + atlasFieldAction.getClass().getCanonicalName());
    }
    Class<?> clazz = atlasFieldAction.getClass();
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        // Continue supporting creating details from @AtlasFieldActionInfo
        ActionProcessor det = createDetailFromFieldActionInfo(clazz, method);
        if (det != null) {
            answer.add(det);
        }
        // Also support using new simpler @AtlasActionProcessor
        det = createDetailFromProcessor(clazz, method);
        if (det != null) {
            answer.add(det);
        }
    }
}
Also used : Method(java.lang.reflect.Method) ActionProcessor(io.atlasmap.spi.ActionProcessor) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor)

Example 2 with AtlasActionProcessor

use of io.atlasmap.spi.AtlasActionProcessor in project atlasmap by atlasmap.

the class NumberFieldActions method convertMassUnit.

/**
 * Converts mass unit.
 * @param convertMassUnit action model
 * @param input source
 * @return converted
 */
@AtlasActionProcessor
public static Number convertMassUnit(ConvertMassUnit convertMassUnit, Number input) {
    if (input == null) {
        return 0;
    }
    if (convertMassUnit == null || convertMassUnit.getFromUnit() == null || convertMassUnit.getToUnit() == null) {
        throw new IllegalArgumentException("ConvertMassUnit must be specified  with fromUnit and toUnit");
    }
    MassUnitType fromUnit = convertMassUnit.getFromUnit();
    MassUnitType toUnit = convertMassUnit.getToUnit();
    double rate = massConvertionTable.get(fromUnit).get(toUnit);
    return doMultiply(input, rate);
}
Also used : MassUnitType(io.atlasmap.v2.MassUnitType) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor)

Example 3 with AtlasActionProcessor

use of io.atlasmap.spi.AtlasActionProcessor in project atlasmap by atlasmap.

the class StringComplexFieldActions method replaceFirst.

/**
 * Replaces first hit with the regular expression specified as a parameter.
 * @param replaceFirst action model
 * @param input source
 * @return processed
 */
@AtlasActionProcessor
public static String replaceFirst(ReplaceFirst replaceFirst, String input) {
    if (replaceFirst == null || replaceFirst.getMatch() == null || replaceFirst.getMatch().isEmpty()) {
        throw new IllegalArgumentException("ReplaceFirst action must be specified with a non-empty old string");
    }
    String match = replaceFirst.getMatch();
    String newString = replaceFirst.getNewString();
    return input == null ? null : input.replaceFirst(match, newString == null ? "" : newString);
}
Also used : SubString(io.atlasmap.v2.SubString) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor)

Example 4 with AtlasActionProcessor

use of io.atlasmap.spi.AtlasActionProcessor in project atlasmap by atlasmap.

the class ExpressionFieldAction method process.

/**
 * Processes expression field action.
 * @param action action model
 * @param args expression arguments
 * @return processed
 * @throws ExpressionException expression processing error
 */
@Deprecated
@AtlasActionProcessor
public static Object process(io.atlasmap.v2.Expression action, List<Object> args) throws ExpressionException {
    if (action.getExpression() == null || action.getExpression().trim().isEmpty()) {
        return null;
    }
    Expression parsedExpression = Expression.parse(action.getExpression(), DefaultAtlasFunctionResolver.getInstance());
    Field answer = parsedExpression.evaluate((index) -> {
        try {
            return wrapWithField(args.get(Integer.parseInt(index)));
        } catch (Throwable e) {
            throw new ExpressionException("Invalid variable: " + index);
        }
    });
    return unwrapField(answer);
}
Also used : Field(io.atlasmap.v2.Field) AtlasModelFactory.unwrapField(io.atlasmap.v2.AtlasModelFactory.unwrapField) AtlasModelFactory.wrapWithField(io.atlasmap.v2.AtlasModelFactory.wrapWithField) Expression(io.atlasmap.expression.Expression) ExpressionException(io.atlasmap.expression.ExpressionException) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor)

Example 5 with AtlasActionProcessor

use of io.atlasmap.spi.AtlasActionProcessor in project atlasmap by atlasmap.

the class DefaultAtlasFieldActionService method createDetailFromProcessor.

private ActionProcessor createDetailFromProcessor(Class<?> clazz, Method method) {
    AtlasActionProcessor annotation = method.getAnnotation(AtlasActionProcessor.class);
    if (annotation == null) {
        return null;
    }
    if (method.getParameterCount() < 1) {
        LOG.debug("Invalid @AtlasActionProcessor method.  Expected at least 1 parameter: " + method);
    }
    Class<? extends Action> actionClazz = null;
    if (Action.class.isAssignableFrom(method.getParameterTypes()[0])) {
        actionClazz = (Class<? extends Action>) method.getParameterTypes()[0];
    } else {
        LOG.debug("Invalid @AtlasActionProcessor method.  1st parameter does not subclass " + Action.class.getName() + ": " + method);
    }
    final Class<?> targetClass = method.getReturnType();
    String name = actionResolver.toId(actionClazz);
    ActionDetail det = new ActionDetail();
    det.setClassName(clazz.getName());
    det.setMethod(method.getName());
    det.setName(name);
    det.setTargetType(toFieldType(targetClass, method.getGenericReturnType()));
    if (!clazz.getPackage().getName().equals("io.atlasmap.actions")) {
        det.setCustom(true);
    }
    Type[] genericParameterTypes = method.getGenericParameterTypes();
    if (genericParameterTypes.length >= 2) {
        Class<?> sourceClass = method.getParameterTypes()[1];
        if (annotation.sourceType() != FieldType.NONE) {
            det.setSourceType(annotation.sourceType());
        } else {
            det.setSourceType(toFieldType(sourceClass, method.getGenericParameterTypes()[1]));
        }
        CollectionType sourceCollection = toFieldCollectionType(sourceClass);
        CollectionType targetCollection = toFieldCollectionType(targetClass);
        if (sourceCollection != CollectionType.NONE) {
            if (targetCollection != CollectionType.NONE) {
                det.setMultiplicity(Multiplicity.MANY_TO_MANY);
            } else {
                det.setMultiplicity(Multiplicity.MANY_TO_ONE);
            }
        } else if (targetCollection != CollectionType.NONE) {
            det.setMultiplicity(Multiplicity.ONE_TO_MANY);
        } else {
            det.setMultiplicity(Multiplicity.ONE_TO_ONE);
        }
    } else if (genericParameterTypes.length == 1) {
        det.setMultiplicity(Multiplicity.ZERO_TO_ONE);
    }
    try {
        det.setActionSchema(actionClazz);
    } catch (Exception e) {
        LOG.error(String.format("Could not get json schema for action=%s msg=%s", clazz.getName(), e.getMessage()), e);
    }
    try {
        det.setParameters(detectFieldActionParameters(actionClazz));
    } catch (ClassNotFoundException e) {
        LOG.error(String.format("Error detecting parameters for field action=%s msg=%s", det.getName(), e.getMessage()), e);
    }
    Object o = null;
    try {
        o = Modifier.isStatic(method.getModifiers()) ? clazz.getDeclaredConstructor().newInstance() : null;
    } catch (Throwable e) {
        LOG.error(String.format("Error creating object instance for action=%s msg=%s", det.getName(), e.getMessage()), e);
    }
    final Object object = o;
    Class<? extends Action> finalActionClazz = actionClazz;
    return new ActionProcessor() {

        @Override
        public ActionDetail getActionDetail() {
            return det;
        }

        @Override
        public Class<? extends Action> getActionClass() {
            return finalActionClazz;
        }

        @Override
        public Object process(Action action, Object sourceObject) throws AtlasException {
            try {
                if (det.getMultiplicity() == Multiplicity.ZERO_TO_ONE) {
                    return method.invoke(object, action);
                } else {
                    sourceObject = convertSourceObject(sourceObject);
                    return method.invoke(object, action, sourceObject);
                }
            } catch (Throwable e) {
                throw new AtlasException(String.format("Error processing action %s", det.getName()), e);
            }
        }

        private Object convertSourceObject(Object sourceObject) throws AtlasConversionException {
            if (sourceObject == null) {
                return null;
            }
            Class<?> paramType;
            paramType = method.getParameterTypes()[1];
            CollectionType paramCollectionType = toFieldCollectionType(paramType);
            CollectionType sourceCollectionType = toFieldCollectionType(sourceObject.getClass());
            if (paramCollectionType != CollectionType.NONE) {
                List<Object> sourceList;
                Type itemType = method.getGenericParameterTypes()[1];
                Class<?> itemClass = paramType.isArray() ? paramType.getComponentType() : (Class<?>) ((ParameterizedType) itemType).getActualTypeArguments()[0];
                if (sourceCollectionType != CollectionType.NONE) {
                    if (sourceCollectionType == CollectionType.ARRAY) {
                        sourceList = Arrays.asList(sourceObject);
                    } else if (sourceCollectionType == CollectionType.LIST) {
                        sourceList = (List<Object>) sourceObject;
                    } else if (sourceCollectionType == CollectionType.MAP) {
                        sourceList = new ArrayList(((Map) sourceObject).values());
                    } else {
                        sourceList = new ArrayList((Collection) sourceObject);
                    }
                } else {
                    sourceList = Arrays.asList(sourceObject);
                }
                convertItems(sourceList, itemClass);
                if (paramType.isArray()) {
                    return sourceList.toArray();
                } else {
                    return sourceList;
                }
            } else if (paramType.isInstance(sourceObject)) {
                return sourceObject;
            }
            return conversionService.convertType(sourceObject, null, paramType, null);
        }
    };
}
Also used : CustomAction(io.atlasmap.v2.CustomAction) AtlasFieldAction(io.atlasmap.spi.AtlasFieldAction) Action(io.atlasmap.v2.Action) ActionDetail(io.atlasmap.v2.ActionDetail) ArrayList(java.util.ArrayList) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasException(io.atlasmap.api.AtlasException) FieldType(io.atlasmap.v2.FieldType) CollectionType(io.atlasmap.v2.CollectionType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CollectionType(io.atlasmap.v2.CollectionType) Collection(java.util.Collection) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ActionProcessor(io.atlasmap.spi.ActionProcessor) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor) Map(java.util.Map)

Aggregations

AtlasActionProcessor (io.atlasmap.spi.AtlasActionProcessor)11 SubString (io.atlasmap.v2.SubString)4 ActionProcessor (io.atlasmap.spi.ActionProcessor)2 AtlasConversionException (io.atlasmap.api.AtlasConversionException)1 AtlasException (io.atlasmap.api.AtlasException)1 Expression (io.atlasmap.expression.Expression)1 ExpressionException (io.atlasmap.expression.ExpressionException)1 AtlasFieldAction (io.atlasmap.spi.AtlasFieldAction)1 Action (io.atlasmap.v2.Action)1 ActionDetail (io.atlasmap.v2.ActionDetail)1 AreaUnitType (io.atlasmap.v2.AreaUnitType)1 AtlasModelFactory.unwrapField (io.atlasmap.v2.AtlasModelFactory.unwrapField)1 AtlasModelFactory.wrapWithField (io.atlasmap.v2.AtlasModelFactory.wrapWithField)1 CollectionType (io.atlasmap.v2.CollectionType)1 CustomAction (io.atlasmap.v2.CustomAction)1 DistanceUnitType (io.atlasmap.v2.DistanceUnitType)1 Field (io.atlasmap.v2.Field)1 FieldType (io.atlasmap.v2.FieldType)1 MassUnitType (io.atlasmap.v2.MassUnitType)1 VolumeUnitType (io.atlasmap.v2.VolumeUnitType)1