Search in sources :

Example 46 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class FILTER method adjustRootCollectionIndex.

private void adjustRootCollectionIndex(Field f, int index) {
    AtlasPath filteredPath = new AtlasPath(f.getPath());
    Integer collectionSegmentIndex = null;
    List<AtlasPath.SegmentContext> filteredSegments = filteredPath.getSegments(true);
    if (filteredSegments.get(0).getCollectionIndex() != null) {
        collectionSegmentIndex = 0;
    } else if (filteredSegments.get(1).getCollectionIndex() != null) {
        collectionSegmentIndex = 1;
    }
    if (collectionSegmentIndex != null) {
        if (f instanceof FieldGroup) {
            AtlasPath.setCollectionIndexRecursively((FieldGroup) f, collectionSegmentIndex, index);
        } else {
            filteredPath.setCollectionIndex(collectionSegmentIndex, index);
            f.setPath(filteredPath.getOriginalPath());
        }
    }
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath)

Example 47 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class JsonFieldReader method populateChildFields.

private void populateChildFields(AtlasInternalSession session, JsonNode node, FieldGroup fieldGroup, AtlasPath path) throws AtlasException {
    List<Field> newChildren = new ArrayList<>();
    for (Field child : fieldGroup.getField()) {
        AtlasPath childPath = new AtlasPath(child.getPath());
        JsonNode childNode = node.get(childPath.getLastSegment().getName());
        if (childPath.getLastSegment().getCollectionType() != CollectionType.NONE) {
            FieldGroup childGroup = populateCollectionItems(session, (ArrayNode) childNode, child);
            newChildren.add(childGroup);
        } else {
            if (child instanceof FieldGroup) {
                populateChildFields(session, childNode, (FieldGroup) child, childPath);
            } else {
                Object value = handleValueNode(session, childNode, (JsonField) child);
                child.setValue(value);
            }
            newChildren.add(child);
        }
    }
    fieldGroup.getField().clear();
    fieldGroup.getField().addAll(newChildren);
}
Also used : JsonEnumField(io.atlasmap.json.v2.JsonEnumField) Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 48 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class JsonFieldReader method populateCollectionItems.

private FieldGroup populateCollectionItems(AtlasInternalSession session, JsonNode node, Field field) throws AtlasException {
    if (!node.isArray()) {
        throw new AtlasException(String.format("Couldn't find JSON array for field %s:%s", field.getDocId(), field.getPath()));
    }
    FieldGroup group = field instanceof FieldGroup ? (FieldGroup) field : AtlasModelFactory.createFieldGroupFrom(field, true);
    ArrayNode arrayNode = (ArrayNode) node;
    for (int i = 0; i < arrayNode.size(); i++) {
        AtlasPath itemPath = new AtlasPath(group.getPath());
        List<SegmentContext> segments = itemPath.getSegments(true);
        itemPath.setCollectionIndex(segments.size() - 1, i);
        if (field instanceof FieldGroup) {
            FieldGroup itemGroup = AtlasJsonModelFactory.cloneFieldGroup((FieldGroup) field);
            AtlasPath.setCollectionIndexRecursively(itemGroup, segments.size(), i);
            populateChildFields(session, arrayNode.get(i), itemGroup, itemPath);
            group.getField().add(itemGroup);
        } else {
            JsonField itemField = AtlasJsonModelFactory.cloneField((JsonField) field, false);
            itemField.setPath(itemPath.toString());
            Object value = handleValueNode(session, arrayNode.get(i), itemField);
            itemField.setValue(value);
            group.getField().add(itemField);
        }
    }
    return group;
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) JsonField(io.atlasmap.json.v2.JsonField) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AtlasException(io.atlasmap.api.AtlasException)

Example 49 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class JsonFieldWriter method write.

@Override
public void write(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    if (targetField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'jsonField' cannot be null"));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Field: " + AtlasModelFactory.toString(targetField));
        LOG.debug("Field type=" + targetField.getFieldType() + " path=" + targetField.getPath() + " v=" + targetField.getValue());
    }
    AtlasPath path = new AtlasPath(targetField.getPath());
    SegmentContext lastSegment = path.getLastSegment();
    if (this.rootNode == null) {
        if (path.hasCollectionRoot()) {
            this.rootNode = objectMapper.createArrayNode();
        } else {
            this.rootNode = objectMapper.createObjectNode();
        }
    }
    ContainerNode<?> parentNode = this.rootNode;
    SegmentContext parentSegment = null;
    for (SegmentContext segment : path.getSegments(true)) {
        if (!segment.equals(lastSegment)) {
            // this is a parent node.
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing parent segment: " + segment);
            }
            JsonNode childNode;
            if (segment.isRoot()) {
                if (parentNode instanceof ArrayNode) {
                    // taking care of topmost collection
                    childNode = parentNode;
                } else {
                    parentSegment = segment;
                    continue;
                }
            } else {
                childNode = getChildNode(parentNode, parentSegment, segment);
            }
            if (childNode == null) {
                childNode = createParentNode(parentNode, parentSegment, segment);
            } else if (childNode instanceof ArrayNode) {
                Integer index = segment.getCollectionIndex();
                if (index == null) {
                    return;
                }
                ArrayNode arrayChild = (ArrayNode) childNode;
                if (arrayChild.size() < (index + 1)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Object Array is too small, resizing to accomodate index: " + index + ", current array: " + arrayChild);
                    }
                    // index available
                    while (arrayChild.size() < (index + 1)) {
                        arrayChild.addObject();
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Object Array after resizing: " + arrayChild);
                    }
                }
                childNode = arrayChild.get(index);
            }
            if (childNode == null) {
                return;
            }
            parentNode = (ObjectNode) childNode;
            parentSegment = segment;
        } else {
            // this is the last segment of the path, write the value
            if (targetField.getFieldType() == FieldType.COMPLEX) {
                createParentNode(parentNode, parentSegment, segment);
                return;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing field value segment: " + segment);
            }
            writeValue(parentNode, parentSegment, segment, targetField);
        }
    }
}
Also used : BigInteger(java.math.BigInteger) Field(io.atlasmap.v2.Field) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AtlasException(io.atlasmap.api.AtlasException)

Aggregations

AtlasPath (io.atlasmap.core.AtlasPath)49 Field (io.atlasmap.v2.Field)27 FieldGroup (io.atlasmap.v2.FieldGroup)26 AtlasException (io.atlasmap.api.AtlasException)17 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)14 JavaField (io.atlasmap.java.v2.JavaField)12 ArrayList (java.util.ArrayList)12 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)11 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)11 JsonField (io.atlasmap.json.v2.JsonField)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 JsonEnumField (io.atlasmap.json.v2.JsonEnumField)4 Method (java.lang.reflect.Method)4 List (java.util.List)4 Test (org.junit.Test)4 SourceAddress (io.atlasmap.java.test.SourceAddress)3 SourceOrder (io.atlasmap.java.test.SourceOrder)3 KafkaConnectEnumField (io.atlasmap.kafkaconnect.v2.KafkaConnectEnumField)3 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)3 Head (io.atlasmap.spi.AtlasInternalSession.Head)3