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