Search in sources :

Example 36 with Collection

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;
    };
}
Also used : AtlasModelFactory(io.atlasmap.v2.AtlasModelFactory) Arrays(java.util.Arrays) List(java.util.List) FieldGroup(io.atlasmap.v2.FieldGroup) Expression(io.atlasmap.expression.Expression) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) Field(io.atlasmap.v2.Field) AtlasPath(io.atlasmap.core.AtlasPath) BaseFunctionFactory(io.atlasmap.core.BaseFunctionFactory) ParseException(io.atlasmap.expression.parser.ParseException) Field(io.atlasmap.v2.Field) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) Expression(io.atlasmap.expression.Expression) BooleanExpression(io.atlasmap.expression.internal.BooleanExpression) FieldGroup(io.atlasmap.v2.FieldGroup) List(java.util.List) ParseException(io.atlasmap.expression.parser.ParseException)

Example 37 with Collection

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;
    };
}
Also used : AtlasModelFactory(io.atlasmap.v2.AtlasModelFactory) Arrays(java.util.Arrays) List(java.util.List) FieldGroup(io.atlasmap.v2.FieldGroup) Expression(io.atlasmap.expression.Expression) Field(io.atlasmap.v2.Field) AtlasPath(io.atlasmap.core.AtlasPath) BaseFunctionFactory(io.atlasmap.core.BaseFunctionFactory) ParseException(io.atlasmap.expression.parser.ParseException) ArrayList(java.util.ArrayList) Field(io.atlasmap.v2.Field) Expression(io.atlasmap.expression.Expression) FieldGroup(io.atlasmap.v2.FieldGroup) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(io.atlasmap.expression.parser.ParseException)

Example 38 with Collection

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);
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Collection(java.util.Collection)

Example 39 with Collection

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);
    }
}
Also used : Validation(io.atlasmap.v2.Validation)

Example 40 with Collection

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

Field (io.atlasmap.v2.Field)25 FieldGroup (io.atlasmap.v2.FieldGroup)15 AtlasPath (io.atlasmap.core.AtlasPath)14 AtlasMapping (io.atlasmap.v2.AtlasMapping)14 AtlasException (io.atlasmap.api.AtlasException)11 ArrayList (java.util.ArrayList)10 Mapping (io.atlasmap.v2.Mapping)9 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)8 Collection (io.atlasmap.v2.Collection)7 SimpleField (io.atlasmap.v2.SimpleField)7 List (java.util.List)7 Test (org.junit.jupiter.api.Test)7 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)6 JavaField (io.atlasmap.java.v2.JavaField)6 LinkedList (java.util.LinkedList)6 AtlasContext (io.atlasmap.api.AtlasContext)5 AtlasSession (io.atlasmap.api.AtlasSession)5 ADMArchiveHandler (io.atlasmap.core.ADMArchiveHandler)5 ConstantField (io.atlasmap.v2.ConstantField)5 InputStream (java.io.InputStream)5