Search in sources :

Example 46 with FieldGroup

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

the class KafkaConnectModule method populateTargetField.

@Override
public void populateTargetField(AtlasInternalSession session) throws AtlasException {
    Field sourceField = session.head().getSourceField();
    Field targetField = session.head().getTargetField();
    AtlasPath path = new AtlasPath(targetField.getPath());
    FieldGroup targetFieldGroup = null;
    if (path.hasCollection() && !path.isIndexedCollection()) {
        targetFieldGroup = AtlasModelFactory.createFieldGroupFrom(targetField, true);
        session.head().setTargetField(targetFieldGroup);
    }
    // Attempt to Auto-detect field type based on input value
    if (targetField.getFieldType() == null && sourceField.getValue() != null) {
        targetField.setFieldType(getConversionService().fieldTypeFromClass(sourceField.getValue().getClass()));
    }
    if (targetFieldGroup == null) {
        if (sourceField instanceof FieldGroup) {
            List<Field> subFields = ((FieldGroup) sourceField).getField();
            if (subFields != null && subFields.size() > 0) {
                Integer index = targetField.getIndex();
                if (index != null) {
                    if (subFields.size() > index) {
                        sourceField = subFields.get(index);
                    } else {
                        AtlasUtil.addAudit(session, getDocId(), String.format("The number of source fields (%s) is smaller than target index (%s) - ignoring", subFields.size(), index), AuditStatus.WARN, null);
                        return;
                    }
                } else {
                    // The last one wins for compatibility
                    sourceField = subFields.get(subFields.size() - 1);
                }
                session.head().setSourceField(sourceField);
            }
        }
        super.populateTargetField(session);
    } else if (sourceField instanceof FieldGroup) {
        Field previousTargetSubField = null;
        for (int i = 0; i < ((FieldGroup) sourceField).getField().size(); i++) {
            Field sourceSubField = ((FieldGroup) sourceField).getField().get(i);
            KafkaConnectField targetSubField = AtlasKafkaConnectModelFactory.createKafkaConnectField();
            AtlasKafkaConnectModelFactory.copyField(targetField, targetSubField, false);
            getCollectionHelper().copyCollectionIndexes(sourceField, sourceSubField, targetSubField, previousTargetSubField);
            previousTargetSubField = targetSubField;
            targetFieldGroup.getField().add(targetSubField);
            session.head().setSourceField(sourceSubField);
            session.head().setTargetField(targetSubField);
            super.populateTargetField(session);
        }
        session.head().setSourceField(sourceField);
        session.head().setTargetField(targetFieldGroup);
    } else {
        KafkaConnectField targetSubField = new KafkaConnectField();
        AtlasKafkaConnectModelFactory.copyField(targetField, targetSubField, false);
        path.setVacantCollectionIndex(0);
        targetSubField.setPath(path.toString());
        targetFieldGroup.getField().add(targetSubField);
        session.head().setTargetField(targetSubField);
        super.populateTargetField(session);
        session.head().setTargetField(targetFieldGroup);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("{}: processTargetFieldMapping completed: SourceField:[docId={}, path={}, type={}, value={}], TargetField:[docId={}, path={}, type={}, value={}]", getDocId(), sourceField.getDocId(), sourceField.getPath(), sourceField.getFieldType(), sourceField.getValue(), targetField.getDocId(), targetField.getPath(), targetField.getFieldType(), targetField.getValue());
    }
}
Also used : Field(io.atlasmap.v2.Field) KafkaConnectEnumField(io.atlasmap.kafkaconnect.v2.KafkaConnectEnumField) KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath) KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField)

Example 47 with FieldGroup

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

the class KafkaConnectModule method writeTargetValue.

@Override
public void writeTargetValue(AtlasInternalSession session) throws AtlasException {
    KafkaConnectFieldWriter writer = session.getFieldWriter(getDocId(), KafkaConnectFieldWriter.class);
    if (session.head().getTargetField() instanceof FieldGroup) {
        FieldGroup targetFieldGroup = (FieldGroup) session.head().getTargetField();
        if (targetFieldGroup.getField().size() > 0) {
            for (Field f : targetFieldGroup.getField()) {
                session.head().setTargetField(f);
                writer.write(session);
            }
            return;
        }
    }
    writer.write(session);
}
Also used : Field(io.atlasmap.v2.Field) KafkaConnectEnumField(io.atlasmap.kafkaconnect.v2.KafkaConnectEnumField) KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField) FieldGroup(io.atlasmap.v2.FieldGroup) KafkaConnectFieldWriter(io.atlasmap.kafkaconnect.core.KafkaConnectFieldWriter)

Example 48 with FieldGroup

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

the class XmlFieldReader method populateCollectionItems.

private FieldGroup populateCollectionItems(AtlasInternalSession session, Optional<XmlNamespaces> xmlNamespaces, List<Element> elements, Field field) throws AtlasException {
    FieldGroup group = field instanceof FieldGroup ? (FieldGroup) field : AtlasModelFactory.createFieldGroupFrom(field, true);
    for (int i = 0; i < elements.size(); i++) {
        XmlPath itemPath = new XmlPath(group.getPath());
        List<SegmentContext> segments = itemPath.getSegments(true);
        itemPath.setCollectionIndex(segments.size() - 1, i);
        if (field instanceof FieldGroup) {
            FieldGroup itemGroup = AtlasXmlModelFactory.cloneFieldGroup((FieldGroup) field);
            AtlasPath.setCollectionIndexRecursively(itemGroup, segments.size(), i);
            populateChildFields(session, xmlNamespaces, elements.get(i), itemGroup, itemPath);
            group.getField().add(itemGroup);
        } else {
            XmlField itemField = (XmlField) AtlasXmlModelFactory.cloneField((XmlField) field, false);
            itemField.setPath(itemPath.toString());
            copyValue(session, xmlNamespaces, itemPath.getLastSegment(), elements.get(i), itemField);
            group.getField().add(itemField);
        }
    }
    return group;
}
Also used : XmlSegmentContext(io.atlasmap.xml.core.XmlPath.XmlSegmentContext) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) FieldGroup(io.atlasmap.v2.FieldGroup) XmlField(io.atlasmap.xml.v2.XmlField)

Example 49 with FieldGroup

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

the class KafkaConnectFieldReaderTest method assertfcl0.

private void assertfcl0(List<Field> fields, String rootPrefix, int rootIndex) {
    assertEquals(3, fields.size());
    for (int i = 0; i < 3; i++) {
        FieldGroup fcl0Item = (FieldGroup) fields.get(i);
        assertEquals(rootPrefix + "/fcl0<" + i + ">", fcl0Item.getPath());
        assertEquals(FieldType.COMPLEX, fcl0Item.getFieldType());
        assertEquals(1, fcl0Item.getField().size());
        Field fcl0Itemf0 = fcl0Item.getField().get(0);
        assertEquals(rootPrefix + "/fcl0<" + i + ">/f0", fcl0Itemf0.getPath());
        assertEquals(FieldType.STRING, fcl0Itemf0.getFieldType());
        assertEquals("fcl0f0val" + rootIndex + "-" + i, fcl0Itemf0.getValue());
    }
}
Also used : KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField) Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup)

Example 50 with FieldGroup

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

the class KafkaConnectFieldReaderTest method createFcl0Field.

private FieldGroup createFcl0Field(AtlasPath parentPath) {
    FieldGroup fcl0Field = new FieldGroup();
    AtlasPath path = parentPath.clone().appendField("fcl0<>");
    fcl0Field.setPath(path.toString());
    fcl0Field.setFieldType(FieldType.COMPLEX);
    fcl0Field.setCollectionType(CollectionType.LIST);
    fcl0Field.getField().add(createF0Field(path));
    return fcl0Field;
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath)

Aggregations

FieldGroup (io.atlasmap.v2.FieldGroup)110 Field (io.atlasmap.v2.Field)89 Test (org.junit.jupiter.api.Test)48 SimpleField (io.atlasmap.v2.SimpleField)32 AtlasPath (io.atlasmap.core.AtlasPath)28 ArrayList (java.util.ArrayList)24 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)17 CsvField (io.atlasmap.csv.v2.CsvField)16 AtlasException (io.atlasmap.api.AtlasException)15 Audits (io.atlasmap.v2.Audits)14 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)13 ConstantField (io.atlasmap.v2.ConstantField)13 Head (io.atlasmap.spi.AtlasInternalSession.Head)12 JsonField (io.atlasmap.json.v2.JsonField)11 Mapping (io.atlasmap.v2.Mapping)11 PropertyField (io.atlasmap.v2.PropertyField)11 JavaField (io.atlasmap.java.v2.JavaField)10 XmlField (io.atlasmap.xml.v2.XmlField)9 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)8 LinkedList (java.util.LinkedList)8