Search in sources :

Example 6 with FieldGroup

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

the class CsvFieldWriter method write.

/**
 * Write is not performed until after the whole target document is ready and toCsv is called.
 *
 * @param session {@link AtlasInternalSession}
 * @throws AtlasException unexpected error
 */
@Override
public void write(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    Field sourceField = session.head().getSourceField();
    if (sourceField instanceof FieldGroup) {
        FieldGroup targetFieldGroup = AtlasModelFactory.createFieldGroupFrom(targetField, true);
        for (Field sourceSubField : ((FieldGroup) sourceField).getField()) {
            CsvField targetCsvField = (CsvField) targetField;
            CsvField targetCsvSubField = new CsvField();
            AtlasModelFactory.copyField(targetField, targetCsvSubField, false);
            targetCsvSubField.setColumn(targetCsvField.getColumn());
            targetCsvSubField.setValue(sourceSubField.getValue());
            targetFieldGroup.getField().add(targetCsvSubField);
        }
        targetField = targetFieldGroup;
        session.head().setTargetField(targetFieldGroup);
    } else {
        targetField.setValue(sourceField.getValue());
    }
    document.getFields().getField().add(targetField);
}
Also used : CsvField(io.atlasmap.csv.v2.CsvField) Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField)

Example 7 with FieldGroup

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

the class CsvFieldWriterTest method testWithSimpleDocumentWithoutHeader.

@Test
public void testWithSimpleDocumentWithoutHeader() throws Exception {
    CsvConfig csvConfig = new CsvConfig();
    csvConfig.setSkipHeaderRecord(true);
    CsvFieldWriter writer = new CsvFieldWriter(csvConfig);
    FieldGroup sourceField = new FieldGroup();
    sourceField.setName("name");
    sourceField.setPath("/<>/name");
    CsvField sourceSubField0 = new CsvField();
    sourceSubField0.setName("name");
    sourceSubField0.setPath("/<0>/name");
    sourceSubField0.setValue("Bob");
    sourceField.getField().add(sourceSubField0);
    CsvField sourceSubField1 = new CsvField();
    sourceSubField1.setName("name");
    sourceSubField1.setPath("/<1>/name");
    sourceSubField1.setValue("Andrew");
    sourceField.getField().add(sourceSubField1);
    CsvField targetField = new CsvField();
    targetField.setName("givenName");
    targetField.setPath("/<>/givenName");
    write(writer, sourceField, targetField);
    String csv = writer.toCsv();
    assertEquals("Bob\r\nAndrew\r\n", csv);
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 8 with FieldGroup

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

the class CsvFieldWriterTest method testWithHeaderAndIgnoreHeaderCase.

@Test
public void testWithHeaderAndIgnoreHeaderCase() throws Exception {
    CsvConfig csvConfig = new CsvConfig();
    csvConfig.setHeaders("FAMILYNAME,GIVENNAME");
    csvConfig.setIgnoreHeaderCase(true);
    CsvFieldWriter writer = new CsvFieldWriter(csvConfig);
    FieldGroup sourceField = new FieldGroup();
    sourceField.setName("name");
    sourceField.setPath("/<>/name");
    CsvField sourceSubField0 = new CsvField();
    sourceSubField0.setName("name");
    sourceSubField0.setPath("/<0>/name");
    sourceSubField0.setValue("Bob");
    sourceField.getField().add(sourceSubField0);
    CsvField sourceSubField1 = new CsvField();
    sourceSubField1.setName("name");
    sourceSubField1.setPath("/<1>/name");
    sourceSubField1.setValue("Andrew");
    sourceField.getField().add(sourceSubField1);
    CsvField targetField = new CsvField();
    targetField.setName("givenName");
    targetField.setPath("/<>/givenName");
    write(writer, sourceField, targetField);
    String csv = writer.toCsv();
    assertEquals("FAMILYNAME,GIVENNAME\r\n,Bob\r\n,Andrew\r\n", csv);
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 9 with FieldGroup

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

the class CsvFieldWriterTest method testWithNoMatchingHeader.

@Test
public void testWithNoMatchingHeader() throws Exception {
    CsvConfig csvConfig = new CsvConfig();
    csvConfig.setHeaders("FAMILYNAME,GIVENNAME");
    CsvFieldWriter writer = new CsvFieldWriter(csvConfig);
    FieldGroup sourceField = new FieldGroup();
    sourceField.setName("name");
    sourceField.setPath("/<>/name");
    CsvField sourceSubField0 = new CsvField();
    sourceSubField0.setName("name");
    sourceSubField0.setPath("/<0>/name");
    sourceSubField0.setValue("Bob");
    sourceField.getField().add(sourceSubField0);
    CsvField sourceSubField1 = new CsvField();
    sourceSubField1.setName("name");
    sourceSubField1.setPath("/<1>/name");
    sourceSubField1.setValue("Andrew");
    sourceField.getField().add(sourceSubField1);
    CsvField targetField = new CsvField();
    targetField.setName("givenName");
    targetField.setPath("/<>/givenName");
    write(writer, sourceField, targetField);
    String csv = writer.toCsv();
    assertEquals("FAMILYNAME,GIVENNAME\r\n,\r\n,\r\n", csv);
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 10 with FieldGroup

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

the class CsvFieldWriterTest method testWithSimpleDocumentWithHeaderSpecified.

@Test
public void testWithSimpleDocumentWithHeaderSpecified() throws Exception {
    CsvConfig csvConfig = new CsvConfig();
    csvConfig.setHeaders("givenName");
    CsvFieldWriter writer = new CsvFieldWriter(csvConfig);
    FieldGroup sourceField = new FieldGroup();
    sourceField.setName("name");
    sourceField.setPath("/<>/name");
    CsvField sourceSubField0 = new CsvField();
    sourceSubField0.setName("name");
    sourceSubField0.setPath("/<0>/name");
    sourceSubField0.setValue("Bob");
    sourceField.getField().add(sourceSubField0);
    CsvField sourceSubField1 = new CsvField();
    sourceSubField1.setName("name");
    sourceSubField1.setPath("/<1>/name");
    sourceSubField1.setValue("Andrew");
    sourceField.getField().add(sourceSubField1);
    CsvField targetField = new CsvField();
    targetField.setName("givenName");
    targetField.setPath("/<>/givenName");
    write(writer, sourceField, targetField);
    String csv = writer.toCsv();
    assertEquals("givenName\r\nBob\r\nAndrew\r\n", csv);
}
Also used : 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