Search in sources :

Example 1 with FieldGroup

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

the class DefaultAtlasFieldActionService method packExpressionActionOutcomeIntoField.

private Field packExpressionActionOutcomeIntoField(Object values, Field field) {
    if (values instanceof List) {
        // n -> n and 1 -> n - create new FieldGroup
        FieldGroup fieldGroup = new FieldGroup();
        // Make sure fieldGroup is of a collection type
        AtlasPath groupPath = new AtlasPath(AtlasModelFactory.GENERATED_PATH);
        fieldGroup.setCollectionType(CollectionType.LIST);
        groupPath = new AtlasPath(groupPath.toString() + AtlasPath.PATH_LIST_SUFFIX);
        fieldGroup.setPath(groupPath.toString());
        List<?> tmpSourceList = (List<?>) values;
        while (tmpSourceList.size() == 1 && (tmpSourceList.get(0) instanceof List)) {
            tmpSourceList = (List<Object>) tmpSourceList.get(0);
        }
        FieldType type = null;
        for (int i = 0; i < tmpSourceList.size(); i++) {
            Object subValue = tmpSourceList.get(i);
            if (type == null && subValue != null) {
                type = getConversionService().fieldTypeFromClass(subValue.getClass());
            }
            Field subField = new SimpleField();
            AtlasPath subPath = groupPath.clone();
            subPath.setVacantCollectionIndex(i);
            AtlasModelFactory.copyField(fieldGroup, subField, false);
            subField.setPath(subPath.toString());
            subField.setIndex(null);
            subField.setValue(subValue);
            subField.setFieldType(type);
            subField.setCollectionType(CollectionType.NONE);
            fieldGroup.getField().add(subField);
        }
        return fieldGroup;
    }
    if (values != null) {
        field = new SimpleField();
        field.setPath(AtlasModelFactory.GENERATED_PATH);
        field.setValue(values);
        field.setFieldType(getConversionService().fieldTypeFromClass(values.getClass()));
    }
    return field;
}
Also used : Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) SimpleField(io.atlasmap.v2.SimpleField) FieldType(io.atlasmap.v2.FieldType)

Example 2 with FieldGroup

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

the class DefaultAtlasFieldActionService method processAction.

private Field processAction(Action action, ActionProcessor processor, FieldType sourceType, Field field) throws AtlasException {
    ActionDetail detail = processor.getActionDetail();
    Multiplicity multiplicity = detail.getMultiplicity() != null ? detail.getMultiplicity() : Multiplicity.ONE_TO_ONE;
    if (multiplicity == Multiplicity.MANY_TO_ONE) {
        return processManyToOne(action, processor, sourceType, field);
    }
    if (multiplicity == Multiplicity.ONE_TO_MANY) {
        if (field instanceof FieldGroup) {
            while (field instanceof FieldGroup) {
                field = ((FieldGroup) field).getField().get(0);
            }
        }
        return processOneToMany(action, processor, sourceType, field);
    }
    if (field instanceof FieldGroup) {
        return processActionForEachCollectionItem(action, processor, sourceType, (FieldGroup) field);
    }
    Object value = field.getValue();
    if (value != null && !isAssignableFieldType(detail.getSourceType(), sourceType)) {
        value = getConversionService().convertType(value, sourceType, detail.getSourceType());
    }
    value = processor.process(action, value);
    field.setFieldType(processor.getActionDetail().getTargetType());
    field.setValue(value);
    return field;
}
Also used : ActionDetail(io.atlasmap.v2.ActionDetail) FieldGroup(io.atlasmap.v2.FieldGroup) Multiplicity(io.atlasmap.v2.Multiplicity)

Example 3 with FieldGroup

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

the class DefaultAtlasFieldActionService method extractNestedListValuesForExpressionAction.

private void extractNestedListValuesForExpressionAction(Field field, List<Object> fieldValues) {
    if (!(field instanceof FieldGroup)) {
        fieldValues.add(field.getValue());
        return;
    }
    FieldGroup fieldGroup = (FieldGroup) field;
    if (fieldGroup == null || fieldGroup.getField() == null || fieldGroup.getField().isEmpty()) {
        return;
    }
    List<Field> fields = null;
    // passed in to Expression.
    while (fieldGroup.getPath() == null || fieldGroup.getPath().isEmpty()) {
        if (fieldGroup.getField().size() == 1 && (fieldGroup.getField().get(0) instanceof FieldGroup)) {
            fieldGroup = (FieldGroup) fieldGroup.getField().get(0);
        } else {
            fields = fieldGroup.getField();
            break;
        }
    }
    if (fields == null) {
        fields = new LinkedList<>();
        fields.add(fieldGroup);
    }
    doExtractValuesForExpressionAction(fields, fieldValues);
}
Also used : Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) FieldGroup(io.atlasmap.v2.FieldGroup)

Example 4 with FieldGroup

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

the class DefaultAtlasFieldActionService method processActionForEachCollectionItem.

private FieldGroup processActionForEachCollectionItem(Action action, ActionProcessor processor, FieldType sourceType, FieldGroup fieldGroup) throws AtlasException {
    for (Field subField : fieldGroup.getField()) {
        if (subField instanceof FieldGroup) {
            processActionForEachCollectionItem(action, processor, sourceType, (FieldGroup) subField);
            continue;
        }
        Object value = subField.getValue();
        if (value != null && isAssignableFieldType(processor.getActionDetail().getSourceType(), sourceType)) {
            value = getConversionService().convertType(value, sourceType, processor.getActionDetail().getSourceType());
        }
        value = processor.process(action, value);
        subField.setValue(value);
    }
    return fieldGroup;
}
Also used : Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) FieldGroup(io.atlasmap.v2.FieldGroup)

Example 5 with FieldGroup

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

the class AtlasPath method extractChildren.

/**
 * Extract child fields by feeding relative path.
 *
 * @param f Parent field to extract from
 * @param path Relative path string
 * @return extracted field(s)
 */
public static Field extractChildren(Field f, String path) {
    if (f == null || path == null || path.isEmpty()) {
        return null;
    }
    if (path.equals(PATH_SEPARATOR)) {
        return f;
    }
    if (!(f instanceof FieldGroup)) {
        return null;
    }
    List<Field> extracted = new ArrayList<>();
    FieldGroup entryField = (FieldGroup) f;
    extracted.add(entryField);
    List<SegmentContext> entrySegments = new AtlasPath(entryField.getPath()).getSegments(true);
    SegmentContext entrySegment = entrySegments.get(entrySegments.size() - 1);
    List<SegmentContext> extractedSegments = new ArrayList<>(entrySegments);
    List<SegmentContext> relativeSegments = new AtlasPath(path).getSegments(true);
    SegmentContext relativeRootSegment = relativeSegments.get(0);
    List<Field> selected = new ArrayList<>();
    if (relativeRootSegment.getCollectionType() == null || relativeRootSegment.getCollectionType() == CollectionType.NONE) {
        if (entrySegment.getCollectionType() != null && entrySegment.getCollectionType() != CollectionType.NONE && entrySegment.getCollectionIndex() == null) {
            selected.addAll(entryField.getField());
        } else {
            selected.add(entryField);
        }
    } else if (relativeRootSegment.getCollectionIndex() != null) {
        if (entrySegment.getCollectionIndex() != null) {
            if (entrySegment.getCollectionIndex() == relativeRootSegment.getCollectionIndex()) {
                selected.add(entryField);
            }
        } else {
            selected.add(entryField.getField().get(relativeRootSegment.getCollectionIndex()));
            entrySegment.collectionIndex = relativeRootSegment.getCollectionIndex();
            extractedSegments.set(entrySegments.size() - 1, entrySegment.rebuild());
        }
    } else {
        selected.addAll(entryField.getField());
    }
    extracted = selected;
    for (int i = 1; i < relativeSegments.size(); i++) {
        SegmentContext segment = relativeSegments.get(i);
        extractedSegments.add(segment);
        selected = new ArrayList<>();
        for (Field f1 : extracted) {
            FieldGroup f1Group = (FieldGroup) f1;
            for (Field f2 : f1Group.getField()) {
                AtlasPath f2Path = new AtlasPath(f2.getPath());
                if (!segment.getName().equals(f2Path.getLastSegment().getName())) {
                    continue;
                }
                if (segment.getCollectionType() == CollectionType.NONE) {
                    selected.add(f2);
                } else {
                    FieldGroup f2Group = (FieldGroup) f2;
                    if (segment.getCollectionIndex() != null) {
                        selected.add((f2Group.getField().get(segment.getCollectionIndex())));
                    } else {
                        selected.addAll(f2Group.getField());
                    }
                }
                break;
            }
        }
        extracted = selected;
    }
    if (extracted.size() == 1) {
        return extracted.get(0);
    }
    FieldGroup answer = AtlasModelFactory.createFieldGroupFrom(f, true);
    answer.setPath(new AtlasPath(extractedSegments).toString());
    answer.getField().addAll(extracted);
    return answer;
}
Also used : Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList)

Aggregations

FieldGroup (io.atlasmap.v2.FieldGroup)110 Field (io.atlasmap.v2.Field)89 Test (org.junit.jupiter.api.Test)48 SimpleField (io.atlasmap.v2.SimpleField)32 AtlasPath (io.atlasmap.core.AtlasPath)28 ArrayList (java.util.ArrayList)24 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)17 CsvField (io.atlasmap.csv.v2.CsvField)16 AtlasException (io.atlasmap.api.AtlasException)15 Audits (io.atlasmap.v2.Audits)14 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)13 ConstantField (io.atlasmap.v2.ConstantField)13 Head (io.atlasmap.spi.AtlasInternalSession.Head)12 JsonField (io.atlasmap.json.v2.JsonField)11 Mapping (io.atlasmap.v2.Mapping)11 PropertyField (io.atlasmap.v2.PropertyField)11 JavaField (io.atlasmap.java.v2.JavaField)10 XmlField (io.atlasmap.xml.v2.XmlField)9 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)8 LinkedList (java.util.LinkedList)8