Search in sources :

Example 16 with FieldType

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

the class SchemaInspector method printSimpleType.

private void printSimpleType(XSSimpleType simpleType, XmlField xmlField) {
    if (xmlField.getFieldType() == null) {
        FieldType attrType = getFieldType(simpleType.getName());
        xmlField.setFieldType(attrType);
    }
}
Also used : FieldType(io.atlasmap.v2.FieldType)

Example 17 with FieldType

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

the class JsonFieldWriterTest method read.

private AtlasInternalSession read(Path path, FieldType outputFieldType, String fieldPath) throws IOException, AtlasException {
    String document = new String(Files.readAllBytes(path));
    reader.setDocument(document);
    JsonField jsonField = AtlasJsonModelFactory.createJsonField();
    jsonField.setPath(fieldPath);
    jsonField.setFieldType(outputFieldType);
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(jsonField);
    Audits audits = new Audits();
    when(session.getAudits()).thenReturn(audits);
    reader.read(session);
    return session;
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Head(io.atlasmap.spi.AtlasInternalSession.Head) Audits(io.atlasmap.v2.Audits) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession)

Example 18 with FieldType

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

the class XmlFieldWriter method convertValue.

private String convertValue(Field field) {
    FieldType type = field.getFieldType();
    Object originalValue = field.getValue();
    String value = originalValue != null ? String.valueOf(originalValue) : null;
    if (LOG.isDebugEnabled()) {
        String valueClass = originalValue == null ? "null" : originalValue.getClass().getName();
        LOG.debug("Converted field value. Type: {}, originalValue: {}({}), to: '{}", type, originalValue, valueClass, value);
    }
    return value;
}
Also used : FieldType(io.atlasmap.v2.FieldType)

Example 19 with FieldType

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

the class JsonFieldWriterTest method testJsonFieldBooleanString.

@Test
public void testJsonFieldBooleanString() throws Exception {
    Path path = Paths.get("target" + File.separator + "test-write-field-byte-string.json");
    String fieldPath = "/primitiveValue";
    Object testObject = "abcd";
    FieldType inputFieldType = FieldType.STRING;
    FieldType outputFieldType = FieldType.BOOLEAN;
    write(path, fieldPath, testObject, inputFieldType);
    AtlasInternalSession session = read(path, outputFieldType, fieldPath);
    assertEquals(false, session.head().getSourceField().getValue());
}
Also used : Path(java.nio.file.Path) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) FieldType(io.atlasmap.v2.FieldType) Test(org.junit.Test)

Example 20 with FieldType

use of io.atlasmap.v2.FieldType 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)

Aggregations

FieldType (io.atlasmap.v2.FieldType)13 Field (io.atlasmap.v2.Field)8 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)6 SimpleField (io.atlasmap.v2.SimpleField)6 Test (org.junit.Test)4 Head (io.atlasmap.spi.AtlasInternalSession.Head)3 Audits (io.atlasmap.v2.Audits)3 XmlField (io.atlasmap.xml.v2.XmlField)3 Path (java.nio.file.Path)3 AtlasConverter (io.atlasmap.api.AtlasConverter)2 AtlasException (io.atlasmap.api.AtlasException)2 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)2 JavaField (io.atlasmap.java.v2.JavaField)2 Method (java.lang.reflect.Method)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 XSAttributeDecl (com.sun.xml.xsom.XSAttributeDecl)1 XSAttributeUse (com.sun.xml.xsom.XSAttributeUse)1 XSSimpleType (com.sun.xml.xsom.XSSimpleType)1 AtlasConversionException (io.atlasmap.api.AtlasConversionException)1 AtlasConversionService (io.atlasmap.api.AtlasConversionService)1