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;
}
}
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());
}
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());
}
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());
}
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);
}
Aggregations