use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class FILTER method create.
@Override
public Expression create(List<Expression> args) throws ParseException {
if (args.size() != 2) {
throw new ParseException("FILTER expects 2 arguments.");
}
Expression parentExpression = args.get(0);
BooleanExpression filterExpression = BooleanExpression.asBooleanExpression(args.get(1));
return (ctx) -> {
Field parent = (Field) parentExpression.evaluate(ctx);
List<Field> collection = parent instanceof FieldGroup ? ((FieldGroup) parent).getField() : Arrays.asList(parent);
FieldGroup filtered = AtlasModelFactory.createFieldGroupFrom(parent, true);
int index = 0;
for (Field f : collection) {
if (filterExpression.matches((subCtx) -> {
return AtlasPath.extractChildren(f, subCtx);
})) {
adjustRootCollectionIndex(f, index);
index++;
filtered.getField().add(f);
}
}
return filtered;
};
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class SELECT method create.
@Override
public Expression create(List<Expression> args) throws ParseException {
if (args.size() != 2) {
throw new ParseException("SELECT expects 2 arguments.");
}
Expression parentExpression = args.get(0);
Expression selectExpression = args.get(1);
return (ctx) -> {
Field parent = parentExpression.evaluate(ctx);
List<Field> collection = parent instanceof FieldGroup ? ((FieldGroup) parent).getField() : Arrays.asList(parent);
List<Field> selected = new ArrayList<>();
final FieldGroup answer = AtlasModelFactory.createFieldGroupFrom(parent, true);
answer.setPath(AtlasModelFactory.GENERATED_PATH);
for (Field f : collection) {
Field fs = (Field) selectExpression.evaluate((subCtx) -> {
if (subCtx != null && AtlasModelFactory.GENERATED_PATH.equals(answer.getPath())) {
answer.setPath(parent.getPath() + (subCtx.startsWith(AtlasPath.PATH_SEPARATOR) ? subCtx : (AtlasPath.PATH_SEPARATOR + subCtx)));
}
return AtlasPath.extractChildren(f, subCtx);
});
selected.add(fs);
}
if (selected.size() == 1) {
return selected.get(0);
}
answer.getField().addAll(selected);
return answer;
};
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class NotEmptyValidator method validate.
@Override
public void validate(Object target, List<Validation> validations, String id, ValidationStatus status) {
if (!supports(target)) {
return;
}
if (((Collection<?>) target).isEmpty()) {
Validation validation = new Validation();
validation.setScope(scope);
validation.setId(id);
validation.setMessage(this.violationMessage);
validation.setStatus(status);
validations.add(validation);
}
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class BaseModuleValidationService method validateField.
/**
* Validates the field.
* @param mappingId mapping ID
* @param sourceField source field
* @param targetField target field
* @param direction direction
* @param validations a container to put the result validations
*/
@SuppressWarnings("unchecked")
protected void validateField(String mappingId, Field sourceField, Field targetField, FieldDirection direction, List<Validation> validations) {
if (targetField == null) {
return;
}
if (direction == FieldDirection.TARGET) {
Integer sourceCollectionCount = null;
if (sourceField != null) {
sourceCollectionCount = collectionHelper.determineSourceCollectionCount(null, sourceField);
}
Integer targetCollectionCount = collectionHelper.determineTargetCollectionCount(targetField);
if (sourceCollectionCount != null) {
if (sourceCollectionCount > targetCollectionCount) {
Validation validation = new Validation();
validation.setScope(ValidationScope.MAPPING);
validation.setId(mappingId);
String message = String.format("Target [%s] has %s collection(s) on the path, whereas source has %s. Values from the %s rightmost " + "source collections on the path will be added in depth-first order to the rightmost target " + "collection(s) unless transformed explicitly.", targetField.getPath(), targetCollectionCount, sourceCollectionCount, sourceCollectionCount - targetCollectionCount + 1);
validation.setMessage(message);
validation.setStatus(ValidationStatus.WARN);
validations.add(validation);
} else if (sourceCollectionCount < targetCollectionCount) {
Validation validation = new Validation();
validation.setScope(ValidationScope.MAPPING);
validation.setId(mappingId);
validation.setMessage(String.format("The 0 index will be used for any extra parent collections in " + "target [%s], since target has %s collections on the path, whereas source has %s.", targetField.getPath(), targetCollectionCount, sourceCollectionCount));
validation.setStatus(ValidationStatus.WARN);
validations.add(validation);
}
}
}
if (getFieldType().isAssignableFrom(targetField.getClass()) && matchDocIdOrNull(targetField.getDocId())) {
validateModuleField(mappingId, (T) targetField, direction, validations);
}
}
use of io.atlasmap.v2.Collection 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