Search in sources :

Example 16 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class JsonModule method processSourceFieldMapping.

@Override
public void processSourceFieldMapping(AtlasInternalSession session) throws AtlasException {
    Field sourceField = session.head().getSourceField();
    JsonFieldReader reader = session.getFieldReader(getDocId(), JsonFieldReader.class);
    if (reader == null) {
        AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Source document '%s' doesn't exist", getDocId()), sourceField.getPath(), AuditStatus.ERROR, null);
        return;
    }
    reader.read(session);
    if (sourceField.getActions() != null && sourceField.getActions().getActions() != null) {
        getFieldActionService().processActions(sourceField.getActions(), sourceField);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("{}: processSourceFieldMapping completed: SourceField:[docId={}, path={}, type={}, value={}]", getDocId(), sourceField.getDocId(), sourceField.getPath(), sourceField.getFieldType(), sourceField.getValue());
    }
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonFieldReader(io.atlasmap.json.core.JsonFieldReader)

Example 17 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class JsonModule method processTargetFieldMapping.

@Override
public void processTargetFieldMapping(AtlasInternalSession session) throws AtlasException {
    Field sourceField = session.head().getSourceField();
    Field targetField = session.head().getTargetField();
    // Attempt to Auto-detect field type based on input value
    if (targetField.getFieldType() == null && sourceField.getValue() != null) {
        targetField.setFieldType(getConversionService().fieldTypeFromClass(sourceField.getValue().getClass()));
    }
    Object targetValue = null;
    // Do auto-conversion
    if (sourceField.getFieldType() != null && sourceField.getFieldType().equals(targetField.getFieldType())) {
        targetValue = sourceField.getValue();
    } else if (sourceField.getValue() != null) {
        try {
            targetValue = getConversionService().convertType(sourceField.getValue(), sourceField.getFormat(), targetField.getFieldType(), targetField.getFormat());
        } catch (AtlasConversionException e) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Unable to auto-convert for sT=%s tT=%s tF=%s msg=%s", sourceField.getFieldType(), targetField.getFieldType(), targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, null);
            return;
        }
    }
    targetField.setValue(targetValue);
    LookupTable lookupTable = session.head().getLookupTable();
    if (lookupTable != null) {
        processLookupField(session, lookupTable, targetField.getValue(), targetField);
    }
    if (isAutomaticallyProcessOutputFieldActions() && targetField.getActions() != null && targetField.getActions().getActions() != null) {
        getFieldActionService().processActions(targetField.getActions(), targetField);
    }
    JsonFieldWriter writer = session.getFieldWriter(getDocId(), JsonFieldWriter.class);
    writer.write(session);
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonFieldWriter(io.atlasmap.json.core.JsonFieldWriter) AtlasConversionException(io.atlasmap.api.AtlasConversionException) LookupTable(io.atlasmap.v2.LookupTable)

Example 18 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class XmlFieldWriter method write.

@Override
public void write(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    if (targetField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Now processing field p={} t={} v={}", targetField.getPath(), targetField.getFieldType(), targetField.getValue());
    }
    XmlPath path = new XmlPath(targetField.getPath());
    String lastSegment = path.getLastSegment();
    Element parentNode = null;
    String parentSegment = null;
    for (String segment : path.getSegments()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Now processing segment: {}", segment);
            LOG.debug("Parent element is currently: {}", XmlIOHelper.writeDocumentToString(true, parentNode));
        }
        if (parentNode == null) {
            // processing root node
            parentNode = document.getDocumentElement();
            String cleanedSegment = XmlPath.cleanPathSegment(segment);
            if (parentNode == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Creating root element with name: {}", cleanedSegment);
                }
                // no root node exists yet, create root node with this segment name;
                Element rootNode = createElement(segment);
                addNamespacesToElement(rootNode, namespaces);
                document.appendChild(rootNode);
                parentNode = rootNode;
            } else if (!(parentNode.getNodeName().equals(segment))) {
                // make sure root element's name matches.
                throw new AtlasException(String.format("Root element name '%s' does not match expected name '%s' from path: %s", parentNode.getNodeName(), segment, targetField.getPath()));
            }
            parentSegment = segment;
        } else {
            if (LOG.isDebugEnabled()) {
                if (segment.equals(lastSegment)) {
                    LOG.debug("Now processing field value segment: {}", segment);
                } else {
                    LOG.debug("Now processing parent segment: {}", segment);
                }
            }
            if (segment.equals(lastSegment) && targetField.getValue() == null) {
                break;
            }
            if (!XmlPath.isAttributeSegment(segment)) {
                // if current segment of path isn't attribute, it refers to a child element,
                // find it or create it..
                Element childNode = getChildNode(parentNode, parentSegment, segment);
                if (childNode == null) {
                    childNode = createParentNode(parentNode, parentSegment, segment);
                }
                parentNode = childNode;
                parentSegment = segment;
            }
            if (segment.equals(lastSegment)) {
                writeValue(parentNode, segment, targetField);
            }
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) Element(org.w3c.dom.Element) AtlasException(io.atlasmap.api.AtlasException)

Example 19 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class SchemaInspectorTest method inspectFlatPrimitiveNoRoot.

@Test
public void inspectFlatPrimitiveNoRoot() throws Exception {
    final String instance = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/flatprimitive-base-unrooted.json")));
    JsonDocument document = inspectionService.inspectJsonSchema(instance);
    assertNotNull(document);
    assertEquals(5, document.getFields().getField().size());
    List<Field> fields = document.getFields().getField();
    JsonField field = (JsonField) fields.get(0);
    assertEquals("booleanField", field.getName());
    assertEquals("/booleanField", field.getPath());
    assertEquals(FieldType.BOOLEAN, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
    field = (JsonField) fields.get(1);
    assertEquals("stringField", field.getName());
    assertEquals("/stringField", field.getPath());
    assertEquals(FieldType.STRING, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
    field = (JsonField) fields.get(2);
    assertEquals("numberField", field.getName());
    assertEquals("/numberField", field.getPath());
    assertEquals(FieldType.NUMBER, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
    field = (JsonField) fields.get(3);
    assertEquals("intField", field.getName());
    assertEquals("/intField", field.getPath());
    assertEquals(FieldType.INTEGER, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
    field = (JsonField) fields.get(4);
    assertEquals("nullField", field.getName());
    assertEquals("/nullField", field.getPath());
    assertEquals(FieldType.NONE, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonField(io.atlasmap.json.v2.JsonField) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.Test)

Example 20 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class JsonFieldWriterTest method write.

private void write(Field field) throws Exception {
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(mock(Field.class));
    when(session.head().getTargetField()).thenReturn(field);
    writer.write(session);
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession)

Aggregations

Field (io.atlasmap.v2.Field)60 JavaField (io.atlasmap.java.v2.JavaField)15 Mapping (io.atlasmap.v2.Mapping)15 AtlasMapping (io.atlasmap.v2.AtlasMapping)14 SimpleField (io.atlasmap.v2.SimpleField)14 BaseMapping (io.atlasmap.v2.BaseMapping)12 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)11 Test (org.junit.Test)11 JsonField (io.atlasmap.json.v2.JsonField)8 LookupTable (io.atlasmap.v2.LookupTable)8 XmlField (io.atlasmap.xml.v2.XmlField)7 AtlasException (io.atlasmap.api.AtlasException)6 ConstantField (io.atlasmap.v2.ConstantField)6 AtlasConversionException (io.atlasmap.api.AtlasConversionException)5 JsonDocument (io.atlasmap.json.v2.JsonDocument)5 PropertyField (io.atlasmap.v2.PropertyField)5 ArrayList (java.util.ArrayList)5 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)4 AtlasModule (io.atlasmap.spi.AtlasModule)3 FieldType (io.atlasmap.v2.FieldType)3