Search in sources :

Example 36 with AtlasPath

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

the class ClassHelperTest method testParentObjectForPathList.

@Test
public void testParentObjectForPathList() throws Exception {
    SourceOrderList sourceOrderList = new SourceOrderList();
    List<BaseOrder> sourceOrders = new ArrayList<>();
    sourceOrderList.setOrders(sourceOrders);
    SourceAddress sourceAddress = new SourceAddress();
    SourceOrder sourceOrder = new SourceOrder();
    sourceOrder.setAddress(sourceAddress);
    sourceOrderList.getOrders().add(sourceOrder);
    Object parentObject = ClassHelper.parentObjectForPath(sourceOrderList, new AtlasPath("orders<>"), true);
    assertNotNull(parentObject);
    assertTrue(parentObject instanceof List<?>);
    assertEquals(sourceOrders, parentObject);
}
Also used : SourceAddress(io.atlasmap.java.test.SourceAddress) BaseOrder(io.atlasmap.java.test.BaseOrder) ArrayList(java.util.ArrayList) SourceOrder(io.atlasmap.java.test.SourceOrder) AtlasPath(io.atlasmap.core.AtlasPath) SourceOrderList(io.atlasmap.java.test.SourceOrderList) Test(org.junit.Test)

Example 37 with AtlasPath

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

the class JavaFieldWriter method prepareParentObject.

/**
 * Prepares the parent object.
 * @param session session
 * @return prepared
 * @throws AtlasException unexpected error
 */
public Object prepareParentObject(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    if (targetField == null) {
        throw new AtlasException("Target field cannot be null");
    }
    try {
        AtlasPath path = new AtlasPath(targetField.getPath());
        if (path.isRoot()) {
            return null;
        }
        if (rootObject == null) {
            throw new IllegalArgumentException("A root object must be set before process");
        }
        SegmentContext rootSegment = path.getRootSegment();
        Object parentObject = this.rootObject;
        if (rootSegment.getCollectionType() != CollectionType.NONE) {
            if (collectionItemClass == null) {
                throw new AtlasException(String.format("Collection item class must be specified to handle topmost collection, path=", path.toString()));
            }
            if (rootSegment.getCollectionIndex() == null) {
                // Cannot proceed from collection segment without index
                return null;
            }
            parentObject = writerUtil.getCollectionItem(rootObject, rootSegment);
            if (parentObject == null) {
                this.rootObject = writerUtil.adjustCollectionSize(this.rootObject, rootSegment);
                parentObject = writerUtil.createComplexCollectionItem(this.rootObject, collectionItemClass, rootSegment);
            }
        }
        for (SegmentContext segmentContext : path.getSegments(false)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing segment: " + segmentContext);
                LOG.debug("Parent object is currently: " + writeDocumentToString(false, parentObject));
            }
            if (segmentContext == path.getLastSegment()) {
                return parentObject;
            }
            Object childObject = writerUtil.getChildObject(parentObject, segmentContext);
            if (childObject == null) {
                childObject = writerUtil.createComplexChildObject(parentObject, segmentContext);
            }
            if (segmentContext.getCollectionType() != CollectionType.NONE) {
                if (segmentContext.getCollectionIndex() == null) {
                    // Cannot proceed from collection segment without index
                    return null;
                }
                Object item = writerUtil.getCollectionItem(childObject, segmentContext);
                if (item == null) {
                    Object adjusted = writerUtil.adjustCollectionSize(childObject, segmentContext);
                    if (adjusted != childObject) {
                        writerUtil.setChildObject(parentObject, adjusted, segmentContext);
                    }
                    item = writerUtil.createComplexCollectionItem(parentObject, adjusted, segmentContext);
                }
                childObject = item;
            }
            parentObject = childObject;
        }
        return null;
    } catch (Throwable t) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error occured while preparing parent object for: " + targetField.getPath(), t);
        }
        if (t instanceof AtlasException) {
            throw (AtlasException) t;
        }
        throw new AtlasException(t);
    }
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasPath(io.atlasmap.core.AtlasPath) AtlasException(io.atlasmap.api.AtlasException)

Example 38 with AtlasPath

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

the class JavaFieldWriter method doCommitWriting.

private void doCommitWriting(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    AtlasPath path = new AtlasPath(targetField.getPath());
    Object parentObject = this.pathParentQueue.get(targetField.getPath());
    this.pathParentQueue.remove(targetField.getPath());
    converter.convertTargetValue(session, parentObject, targetField);
    SegmentContext lastSegment = path.getLastSegment();
    if (path.isRoot()) {
        if (lastSegment.getCollectionType() == CollectionType.NONE) {
            this.rootObject = targetField.getValue();
        } else {
            Object adjusted = writerUtil.adjustCollectionSize(this.rootObject, lastSegment);
            if (adjusted != this.rootObject) {
                this.rootObject = adjusted;
            }
            writerUtil.setCollectionItem(adjusted, targetField.getValue(), lastSegment);
        }
        return;
    }
    if (parentObject == null) {
        return;
    }
    String targetClassName = null;
    if (targetField instanceof JavaField) {
        targetClassName = ((JavaField) targetField).getClassName();
    } else if (targetField instanceof JavaEnumField) {
        targetClassName = ((JavaEnumField) targetField).getClassName();
    }
    if (lastSegment.getCollectionType() == CollectionType.NONE) {
        // Don't handle null for JavaEnumField complex type using complex child object
        if (targetField.getFieldType() == FieldType.COMPLEX && !(targetField instanceof JavaEnumField) && targetField.getValue() == null) {
            if (targetClassName != null && !targetClassName.isEmpty()) {
                writerUtil.createComplexChildObject(parentObject, lastSegment, writerUtil.loadClass(targetClassName));
            } else {
                writerUtil.createComplexChildObject(parentObject, lastSegment);
            }
        } else {
            writerUtil.setChildObject(parentObject, targetField.getValue(), lastSegment);
        }
    } else {
        Object collection = writerUtil.getChildObject(parentObject, lastSegment);
        if (collection == null) {
            collection = writerUtil.createComplexChildObject(parentObject, lastSegment);
        }
        if (lastSegment.getCollectionIndex() == null) {
            // Collection field without index - just create collection object and keep it empty
            return;
        }
        Object adjusted = writerUtil.adjustCollectionSize(collection, lastSegment);
        if (adjusted != collection) {
            writerUtil.setChildObject(parentObject, adjusted, lastSegment);
        }
        if (targetField.getFieldType() == FieldType.COMPLEX && targetField.getValue() == null) {
            if (targetClassName != null && !targetClassName.isEmpty()) {
                writerUtil.createComplexChildObject(parentObject, lastSegment, writerUtil.loadClass(targetClassName));
            } else {
                writerUtil.createComplexChildObject(parentObject, lastSegment);
            }
        } else {
            writerUtil.setCollectionItem(adjusted, targetField.getValue(), lastSegment);
        }
    }
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) AtlasPath(io.atlasmap.core.AtlasPath)

Example 39 with AtlasPath

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

the class TargetValueConverter method populateTargetField.

/**
 * Populates target field.
 * @param session session
 * @param lookupTable lookup table
 * @param sourceField source field
 * @param parentObject parent
 * @param targetField target field
 * @throws AtlasException unexpected error
 */
public void populateTargetField(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
    if (sourceField == null) {
        AtlasUtil.addAudit(session, (String) null, "Source field cannot be null", AuditStatus.ERROR, null);
        return;
    }
    if (targetField == null) {
        AtlasUtil.addAudit(session, (String) null, "Target field cannot be null", AuditStatus.ERROR, null);
        return;
    }
    Object sourceValue = sourceField.getValue();
    if (LOG.isDebugEnabled()) {
        LOG.debug("processTargetMapping srcPath={} srcVal={} srcType={}  tgtPath={} tgtdocId={}", sourceField.getPath(), sourceField.getValue(), sourceField.getFieldType(), targetField.getPath(), targetField.getDocId());
    }
    String targetClassName = (targetField instanceof JavaField) ? ((JavaField) targetField).getClassName() : null;
    targetClassName = (targetField instanceof JavaEnumField) ? ((JavaEnumField) targetField).getClassName() : targetClassName;
    if (targetClassName == null && parentObject != null) {
        SegmentContext segment = new AtlasPath(targetField.getPath()).getLastSegment();
        Class<?> clazz = segment.getCollectionType() == CollectionType.NONE ? writerUtil.resolveChildClass(parentObject, segment) : writerUtil.resolveCollectionItemClass(parentObject, segment);
        if (targetField.getFieldType() == null) {
            targetField.setFieldType(conversionService.fieldTypeFromClass(clazz));
        }
        if (targetField.getFieldType() == FieldType.COMPLEX || targetField.getFieldType() == FieldType.ENUM) {
            targetClassName = clazz != null && !Modifier.isAbstract(clazz.getModifiers()) ? clazz.getName() : null;
            if (targetField instanceof JavaField) {
                ((JavaField) targetField).setClassName(targetClassName);
            } else {
                ((JavaEnumField) targetField).setClassName(targetClassName);
            }
        }
    }
    if (sourceField instanceof JavaEnumField && targetField instanceof JavaEnumField) {
        populateEnumValue(session, lookupTable, (JavaEnumField) sourceField, (JavaEnumField) targetField);
        return;
    }
    if (targetField instanceof JavaField) {
        JavaField javaTargetField = (JavaField) targetField;
        if (sourceValue == null) {
            if (targetField.getFieldType() != FieldType.COMPLEX) {
                AtlasUtil.addAudit(session, targetField, String.format("Null sourceValue for targetDocId=%s, targetPath=%s", targetField.getDocId(), targetField.getPath()), AuditStatus.WARN, sourceValue != null ? sourceValue.toString() : null);
                targetField.setValue(null);
                return;
            }
            if (javaTargetField.getClassName() != null) {
                Object created = writerUtil.instantiateObject(writerUtil.loadClass(javaTargetField.getClassName()));
                javaTargetField.setValue(created);
                return;
            }
        }
    }
    Object targetValue = doConvertTargetValue(session, sourceValue, targetClassName, targetField);
    targetField.setValue(targetValue);
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) JavaField(io.atlasmap.java.v2.JavaField) AtlasPath(io.atlasmap.core.AtlasPath)

Example 40 with AtlasPath

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

the class BaseJavaFieldWriterTest method setupPath.

public void setupPath(String fieldPath) {
    AtlasPath path = new AtlasPath(fieldPath);
    this.segmentContexts = path.getSegments(true);
    this.lastSegmentContext = segmentContexts.get(segmentContexts.size() - 1);
    this.field = createField(fieldPath, DEFAULT_VALUE);
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath)

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