Search in sources :

Example 6 with CsvField

use of io.atlasmap.csv.v2.CsvField 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 7 with CsvField

use of io.atlasmap.csv.v2.CsvField 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 8 with CsvField

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

Example 9 with CsvField

use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.

the class CsvFieldReaderTest method testWithSimpleDocumentWithoutHeader.

@Test
public void testWithSimpleDocumentWithoutHeader() throws Exception {
    CsvConfig csvConfig = new CsvConfig();
    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("1");
    csvField.setColumn(1);
    csvField.setPath("/<>/1");
    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("Johnson", 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 10 with CsvField

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

Aggregations

CsvField (io.atlasmap.csv.v2.CsvField)21 FieldGroup (io.atlasmap.v2.FieldGroup)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)8 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)6 Audits (io.atlasmap.v2.Audits)6 AtlasException (io.atlasmap.api.AtlasException)4 CsvComplexType (io.atlasmap.csv.v2.CsvComplexType)4 Field (io.atlasmap.v2.Field)4 CsvInspectionResponse (io.atlasmap.csv.v2.CsvInspectionResponse)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Response (javax.ws.rs.core.Response)3 CSVFormat (org.apache.commons.csv.CSVFormat)3 InputStreamReader (java.io.InputStreamReader)2 CSVParser (org.apache.commons.csv.CSVParser)2 CSVRecord (org.apache.commons.csv.CSVRecord)2 AtlasPath (io.atlasmap.core.AtlasPath)1 CsvFields (io.atlasmap.csv.v2.CsvFields)1