Search in sources :

Example 61 with FieldGroup

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

the class AtlasJavaModelFactory method cloneFieldGroup.

/**
 * Clones {@link FieldGroup}.
 * @param group source
 * @return cloned
 */
public static FieldGroup cloneFieldGroup(FieldGroup group) {
    FieldGroup clone = AtlasModelFactory.copyFieldGroup(group);
    List<Field> newChildren = new ArrayList<>();
    for (Field child : group.getField()) {
        if (child instanceof FieldGroup) {
            newChildren.add(cloneFieldGroup((FieldGroup) child));
        } else {
            newChildren.add(cloneJavaField(child, true));
        }
    }
    clone.getField().addAll(newChildren);
    return clone;
}
Also used : Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList)

Example 62 with FieldGroup

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

the class CsvFieldReader method read.

@Override
public Field read(AtlasInternalSession session) throws AtlasException {
    Field field = session.head().getSourceField();
    if (document == null) {
        AtlasUtil.addAudit(session, field, String.format("Cannot read field '%s' of document '%s', document is null", field.getPath(), field.getDocId()), AuditStatus.ERROR, null);
        return field;
    }
    if (field == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
    }
    if (!(field instanceof CsvField) && !(field instanceof FieldGroup)) {
        throw new AtlasException(String.format("Unsupported field type '%s'", field.getClass()));
    }
    if (field instanceof FieldGroup) {
        // complex field
        FieldGroup fieldGroup = (FieldGroup) field;
        List<Field> fields = fieldGroup.getField();
        FieldGroup readFieldGroup = AtlasModelFactory.copyFieldGroup(fieldGroup);
        for (Field subField : fields) {
            if (subField instanceof FieldGroup) {
                // support only one level grouping
                subField = ((FieldGroup) subField).getField().get(0);
            }
            if (subField instanceof CsvField) {
                Field readSubField = readFields((CsvField) subField);
                readFieldGroup.getField().add(readSubField);
            }
        }
        session.head().setSourceField(readFieldGroup);
        return readFieldGroup;
    } else {
        Field readField = readFields((CsvField) field);
        session.head().setSourceField(readField);
        return readField;
    }
}
Also used : CsvField(io.atlasmap.csv.v2.CsvField) Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) AtlasException(io.atlasmap.api.AtlasException)

Example 63 with FieldGroup

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

the class CsvFieldReader method readFields.

private Field readFields(CsvField field) throws AtlasException {
    List<Field> fields = new ArrayList<>();
    CsvField csvField = field;
    CSVFormat csvFormat = csvConfig.newCsvFormat();
    try {
        document.mark(Integer.MAX_VALUE);
        CSVParser parser = csvFormat.parse(new InputStreamReader(document));
        AtlasPath atlasPath = new AtlasPath(csvField.getPath());
        int i = 0;
        Integer fieldIndex = atlasPath.getRootSegment().getCollectionIndex();
        if (fieldIndex != null) {
            for (CSVRecord record : parser) {
                if (i == fieldIndex) {
                    CsvField newField = CsvField.cloneOf(csvField);
                    // do not copy over index if set
                    newField.setIndex(null);
                    String value;
                    if (csvField.getColumn() != null) {
                        value = record.get(csvField.getColumn());
                    } else {
                        value = record.get(csvField.getName());
                    }
                    newField.setValue(value);
                    fields.add(newField);
                    break;
                }
                i++;
            }
        } else {
            for (CSVRecord record : parser) {
                CsvField collectionField = CsvField.cloneOf(csvField);
                // do not copy over index if set
                collectionField.setIndex(null);
                String value;
                if (csvField.getColumn() != null) {
                    value = record.get(csvField.getColumn());
                } else {
                    value = record.get(csvField.getName());
                }
                collectionField.setValue(value);
                AtlasPath collectionFieldPath = new AtlasPath(collectionField.getPath());
                collectionFieldPath.setCollectionIndex(0, i);
                collectionField.setPath(collectionFieldPath.toString());
                fields.add(collectionField);
                i++;
            }
        }
        document.reset();
    } catch (IOException e) {
        throw new AtlasException(e);
    }
    if (fields.size() == 1) {
        return fields.get(0);
    } else {
        FieldGroup fieldGroup = AtlasModelFactory.createFieldGroupFrom(field, true);
        fieldGroup.getField().addAll(fields);
        return fieldGroup;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AtlasException(io.atlasmap.api.AtlasException) CsvField(io.atlasmap.csv.v2.CsvField) Field(io.atlasmap.v2.Field) CsvField(io.atlasmap.csv.v2.CsvField) CSVParser(org.apache.commons.csv.CSVParser) AtlasPath(io.atlasmap.core.AtlasPath) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord)

Example 64 with FieldGroup

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

the class CsvFieldReaderTest method testWithSimpleDocumentWithHeader.

@Test
public void testWithSimpleDocumentWithHeader() throws Exception {
    CsvConfig csvConfig = new CsvConfig();
    csvConfig.setFirstRecordAsHeader(true);
    CsvFieldReader csvFieldReader = new CsvFieldReader(csvConfig);
    csvFieldReader.setDocument(new ByteArrayInputStream("givenName,familyName\nBob,Smith\nAndrew,Johnson".getBytes()));
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(AtlasInternalSession.Head.class));
    CsvField csvField = new CsvField();
    csvField.setName("givenName");
    csvField.setPath("/<>/givenName");
    when(session.head().getSourceField()).thenReturn(csvField);
    Audits audits = new Audits();
    when(session.getAudits()).thenReturn(audits);
    FieldGroup field = (FieldGroup) csvFieldReader.read(session);
    assertEquals(0, audits.getAudit().size());
    assertEquals("Bob", field.getField().get(0).getValue());
    assertEquals("Andrew", field.getField().get(1).getValue());
}
Also used : Audits(io.atlasmap.v2.Audits) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) ByteArrayInputStream(java.io.ByteArrayInputStream) FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 65 with FieldGroup

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

the class CsvFieldReaderTest method testIgnoreHeaderCase.

@Test
public void testIgnoreHeaderCase() throws Exception {
    CsvConfig csvConfig = new CsvConfig();
    csvConfig.setIgnoreHeaderCase(true);
    csvConfig.setHeaders("givenName,familyName");
    CsvFieldReader csvFieldReader = new CsvFieldReader(csvConfig);
    csvFieldReader.setDocument(new ByteArrayInputStream("Bob,Smith\nAndrew,Johnson".getBytes()));
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(AtlasInternalSession.Head.class));
    CsvField csvField = new CsvField();
    csvField.setName("FAMILYNAME");
    csvField.setPath("/<>/FAMILYNAME");
    when(session.head().getSourceField()).thenReturn(csvField);
    Audits audits = new Audits();
    when(session.getAudits()).thenReturn(audits);
    FieldGroup field = (FieldGroup) csvFieldReader.read(session);
    assertEquals(0, audits.getAudit().size());
    assertEquals("Smith", field.getField().get(0).getValue());
    assertEquals("/<0>/FAMILYNAME", field.getField().get(0).getPath());
    assertEquals("Johnson", field.getField().get(1).getValue());
    assertEquals("/<1>/FAMILYNAME", field.getField().get(1).getPath());
}
Also used : Audits(io.atlasmap.v2.Audits) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) ByteArrayInputStream(java.io.ByteArrayInputStream) FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

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