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);
}
}
}
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);
}
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);
}
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);
}
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);
}
};
}
Aggregations