use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class KafkaConnectModule method populateTargetField.
@Override
public void populateTargetField(AtlasInternalSession session) throws AtlasException {
Field sourceField = session.head().getSourceField();
Field targetField = session.head().getTargetField();
AtlasPath path = new AtlasPath(targetField.getPath());
FieldGroup targetFieldGroup = null;
if (path.hasCollection() && !path.isIndexedCollection()) {
targetFieldGroup = AtlasModelFactory.createFieldGroupFrom(targetField, true);
session.head().setTargetField(targetFieldGroup);
}
// Attempt to Auto-detect field type based on input value
if (targetField.getFieldType() == null && sourceField.getValue() != null) {
targetField.setFieldType(getConversionService().fieldTypeFromClass(sourceField.getValue().getClass()));
}
if (targetFieldGroup == null) {
if (sourceField instanceof FieldGroup) {
List<Field> subFields = ((FieldGroup) sourceField).getField();
if (subFields != null && subFields.size() > 0) {
Integer index = targetField.getIndex();
if (index != null) {
if (subFields.size() > index) {
sourceField = subFields.get(index);
} else {
AtlasUtil.addAudit(session, getDocId(), String.format("The number of source fields (%s) is smaller than target index (%s) - ignoring", subFields.size(), index), AuditStatus.WARN, null);
return;
}
} else {
// The last one wins for compatibility
sourceField = subFields.get(subFields.size() - 1);
}
session.head().setSourceField(sourceField);
}
}
super.populateTargetField(session);
} else if (sourceField instanceof FieldGroup) {
Field previousTargetSubField = null;
for (int i = 0; i < ((FieldGroup) sourceField).getField().size(); i++) {
Field sourceSubField = ((FieldGroup) sourceField).getField().get(i);
KafkaConnectField targetSubField = AtlasKafkaConnectModelFactory.createKafkaConnectField();
AtlasKafkaConnectModelFactory.copyField(targetField, targetSubField, false);
getCollectionHelper().copyCollectionIndexes(sourceField, sourceSubField, targetSubField, previousTargetSubField);
previousTargetSubField = targetSubField;
targetFieldGroup.getField().add(targetSubField);
session.head().setSourceField(sourceSubField);
session.head().setTargetField(targetSubField);
super.populateTargetField(session);
}
session.head().setSourceField(sourceField);
session.head().setTargetField(targetFieldGroup);
} else {
KafkaConnectField targetSubField = new KafkaConnectField();
AtlasKafkaConnectModelFactory.copyField(targetField, targetSubField, false);
path.setVacantCollectionIndex(0);
targetSubField.setPath(path.toString());
targetFieldGroup.getField().add(targetSubField);
session.head().setTargetField(targetSubField);
super.populateTargetField(session);
session.head().setTargetField(targetFieldGroup);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{}: processTargetFieldMapping completed: SourceField:[docId={}, path={}, type={}, value={}], TargetField:[docId={}, path={}, type={}, value={}]", getDocId(), sourceField.getDocId(), sourceField.getPath(), sourceField.getFieldType(), sourceField.getValue(), targetField.getDocId(), targetField.getPath(), targetField.getFieldType(), targetField.getValue());
}
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class KafkaConnectModule method writeTargetValue.
@Override
public void writeTargetValue(AtlasInternalSession session) throws AtlasException {
KafkaConnectFieldWriter writer = session.getFieldWriter(getDocId(), KafkaConnectFieldWriter.class);
if (session.head().getTargetField() instanceof FieldGroup) {
FieldGroup targetFieldGroup = (FieldGroup) session.head().getTargetField();
if (targetFieldGroup.getField().size() > 0) {
for (Field f : targetFieldGroup.getField()) {
session.head().setTargetField(f);
writer.write(session);
}
return;
}
}
writer.write(session);
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class XmlFieldReader method populateCollectionItems.
private FieldGroup populateCollectionItems(AtlasInternalSession session, Optional<XmlNamespaces> xmlNamespaces, List<Element> elements, Field field) throws AtlasException {
FieldGroup group = field instanceof FieldGroup ? (FieldGroup) field : AtlasModelFactory.createFieldGroupFrom(field, true);
for (int i = 0; i < elements.size(); i++) {
XmlPath itemPath = new XmlPath(group.getPath());
List<SegmentContext> segments = itemPath.getSegments(true);
itemPath.setCollectionIndex(segments.size() - 1, i);
if (field instanceof FieldGroup) {
FieldGroup itemGroup = AtlasXmlModelFactory.cloneFieldGroup((FieldGroup) field);
AtlasPath.setCollectionIndexRecursively(itemGroup, segments.size(), i);
populateChildFields(session, xmlNamespaces, elements.get(i), itemGroup, itemPath);
group.getField().add(itemGroup);
} else {
XmlField itemField = (XmlField) AtlasXmlModelFactory.cloneField((XmlField) field, false);
itemField.setPath(itemPath.toString());
copyValue(session, xmlNamespaces, itemPath.getLastSegment(), elements.get(i), itemField);
group.getField().add(itemField);
}
}
return group;
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class KafkaConnectFieldReaderTest method assertfcl0.
private void assertfcl0(List<Field> fields, String rootPrefix, int rootIndex) {
assertEquals(3, fields.size());
for (int i = 0; i < 3; i++) {
FieldGroup fcl0Item = (FieldGroup) fields.get(i);
assertEquals(rootPrefix + "/fcl0<" + i + ">", fcl0Item.getPath());
assertEquals(FieldType.COMPLEX, fcl0Item.getFieldType());
assertEquals(1, fcl0Item.getField().size());
Field fcl0Itemf0 = fcl0Item.getField().get(0);
assertEquals(rootPrefix + "/fcl0<" + i + ">/f0", fcl0Itemf0.getPath());
assertEquals(FieldType.STRING, fcl0Itemf0.getFieldType());
assertEquals("fcl0f0val" + rootIndex + "-" + i, fcl0Itemf0.getValue());
}
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class KafkaConnectFieldReaderTest method createFcl0Field.
private FieldGroup createFcl0Field(AtlasPath parentPath) {
FieldGroup fcl0Field = new FieldGroup();
AtlasPath path = parentPath.clone().appendField("fcl0<>");
fcl0Field.setPath(path.toString());
fcl0Field.setFieldType(FieldType.COMPLEX);
fcl0Field.setCollectionType(CollectionType.LIST);
fcl0Field.getField().add(createF0Field(path));
return fcl0Field;
}
Aggregations