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