Search in sources :

Example 6 with SegmentContext

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

the class DocumentJavaFieldWriter method write.

public void write(AtlasInternalSession session) throws AtlasException {
    LookupTable lookupTable = session.head().getLookupTable();
    Field sourceField = session.head().getSourceField();
    Field targetField = session.head().getTargetField();
    try {
        if (targetField == null) {
            throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
        }
        String targetFieldClassName = (targetField instanceof JavaField) ? ((JavaField) targetField).getClassName() : ((JavaEnumField) targetField).getClassName();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Now processing field: " + targetField);
            LOG.debug("Field type: " + targetField.getFieldType());
            LOG.debug("Field path: " + targetField.getPath());
            LOG.debug("Field value: " + targetField.getValue());
            LOG.debug("Field className: " + targetFieldClassName);
        }
        processedPaths.add(targetField.getPath());
        AtlasPath path = new AtlasPath(targetField.getPath());
        Object parentObject = rootObject;
        boolean segmentIsComplexSegment = true;
        for (SegmentContext segmentContext : path.getSegmentContexts(true)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing segment: " + segmentContext);
                LOG.debug("Parent object is currently: " + writeDocumentToString(false, parentObject));
            }
            if ("/".equals(segmentContext.getSegmentPath())) {
                if (rootObject == null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Creating root node: " + segmentContext);
                    }
                    rootObject = createParentObject(targetField, parentObject, segmentContext);
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Root node already exists, skipping segment: " + segmentContext);
                    }
                }
                parentObject = rootObject;
                continue;
            }
            // if we're on the last segment, the
            boolean segmentIsLastSegment = (segmentContext.getNext() == null);
            if (segmentIsLastSegment) {
                // detect field type from class name if exists
                if (targetField.getFieldType() == null && targetFieldClassName != null && (targetField instanceof JavaField)) {
                    FieldType fieldTypeFromClass = conversionService.fieldTypeFromClass(targetFieldClassName);
                    targetField.setFieldType(fieldTypeFromClass);
                }
                if (FieldType.COMPLEX.equals(targetField.getFieldType())) {
                    segmentIsComplexSegment = true;
                } else {
                    segmentIsComplexSegment = false;
                }
                if (targetField instanceof JavaEnumField) {
                    segmentIsComplexSegment = false;
                }
            }
            if (LOG.isDebugEnabled()) {
                if (segmentIsComplexSegment) {
                    LOG.debug("Now processing complex segment: " + segmentContext);
                } else if (targetField instanceof JavaEnumField) {
                    LOG.debug("Now processing field enum value segment: " + segmentContext);
                } else {
                    LOG.debug("Now processing field value segment: " + segmentContext);
                }
            }
            if (segmentIsComplexSegment) {
                // processing parent object
                Object childObject = findChildObject(targetField, segmentContext, parentObject);
                if (childObject == null) {
                    childObject = createParentObject(targetField, parentObject, segmentContext);
                }
                parentObject = childObject;
            } else {
                // processing field value
                if (AtlasPath.isCollectionSegment(segmentContext.getSegment())) {
                    parentObject = findOrCreateOrExpandParentCollectionObject(targetField, parentObject, segmentContext);
                }
                Object value = converter.convert(session, lookupTable, sourceField, parentObject, targetField);
                addChildObject(targetField, segmentContext, parentObject, value);
            }
        }
    } catch (Throwable t) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error occured while writing field: " + targetField.getPath(), t);
        }
        if (t instanceof AtlasException) {
            throw (AtlasException) t;
        }
        throw new AtlasException(t);
    }
}
Also used : AtlasException(io.atlasmap.api.AtlasException) FieldType(io.atlasmap.v2.FieldType) 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) LookupTable(io.atlasmap.v2.LookupTable) AtlasPath(io.atlasmap.core.AtlasPath)

Example 7 with SegmentContext

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

the class BaseDocumentWriterTest method setupPath.

public void setupPath(String fieldPath) {
    this.segmentContexts = new AtlasPath(fieldPath).getSegmentContexts(true);
    for (SegmentContext ctx : this.segmentContexts) {
        addClassForFieldPath(ctx.getSegmentPath(), String.class);
    }
    this.lastSegmentContext = segmentContexts.get(segmentContexts.size() - 1);
    this.field = createField(fieldPath, DEFAULT_VALUE);
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasPath(io.atlasmap.core.AtlasPath)

Aggregations

SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)7 AtlasException (io.atlasmap.api.AtlasException)4 AtlasPath (io.atlasmap.core.AtlasPath)3 AtlasConversionException (io.atlasmap.api.AtlasConversionException)2 Field (io.atlasmap.v2.Field)2 IOException (java.io.IOException)2 Element (org.w3c.dom.Element)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 AtlasValidationException (io.atlasmap.api.AtlasValidationException)1 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)1 JavaField (io.atlasmap.java.v2.JavaField)1 FieldType (io.atlasmap.v2.FieldType)1 LookupTable (io.atlasmap.v2.LookupTable)1 XmlPath (io.atlasmap.xml.core.XmlPath)1 XmlField (io.atlasmap.xml.v2.XmlField)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1