use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class DefaultAtlasValidationService method validateMapMapping.
private void validateMapMapping(List<Mapping> fieldMappings, List<Validation> validations, Set<String> usedIds) {
for (Mapping fieldMapping : fieldMappings) {
String mappingId = fieldMapping.getId();
FieldGroup sourceFieldGroup = fieldMapping.getInputFieldGroup();
List<Field> sourceFields = sourceFieldGroup != null ? sourceFieldGroup.getField() : fieldMapping.getInputField();
validateMappingId(mappingId, usedIds, validations);
Validators.MAP_INPUT_NOT_NULL.get().validate(sourceFields, validations, mappingId);
if (fieldMapping.getInputField() != null) {
Validators.MAP_INPUT_FIELD_NOT_EMPTY.get().validate(sourceFields, validations, mappingId);
}
Validators.MAP_OUTPUT_NOT_NULL.get().validate(fieldMapping.getOutputField(), validations, mappingId, ValidationStatus.WARN);
if (fieldMapping.getOutputField() != null) {
Validators.MAP_OUTPUT_FIELD_NOT_EMPTY.get().validate(fieldMapping.getOutputField(), validations, mappingId, ValidationStatus.WARN);
}
}
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class JsonModule method writeTargetValue.
@Override
public void writeTargetValue(AtlasInternalSession session) throws AtlasException {
JsonFieldWriter writer = session.getFieldWriter(getDocId(), JsonFieldWriter.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 JsonModule 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);
JsonField targetSubField = new JsonField();
AtlasJsonModelFactory.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 {
JsonField targetSubField = new JsonField();
AtlasJsonModelFactory.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 AtlasJsonModelFactory method cloneFieldGroup.
/**
* Clones the FieldGroup.
* @param group FieldGroup
* @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(cloneField((JsonField) child, true));
}
}
clone.getField().addAll(newChildren);
return clone;
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class JsonFieldReaderTest method testJsonFieldTopmostArrayObject.
@Test
public void testJsonFieldTopmostArrayObject() throws Exception {
final String document = "[\n" + "\t{\n" + "\t\t\"color\": \"red\",\n" + "\t\t\"value\": \"#f00\"\n" + "\t},\n" + "\t{\n" + "\t\t\"color\": \"green\",\n" + "\t\t\"value\": \"#0f0\"\n" + "\t},\n" + "\t{\n" + "\t\t\"color\": \"blue\",\n" + "\t\t\"value\": \"#00f\"\n" + "\t}]";
reader.setDocument(document);
JsonField field = AtlasJsonModelFactory.createJsonField();
field.setPath("/<0>/color");
AtlasInternalSession session = mock(AtlasInternalSession.class);
when(session.head()).thenReturn(mock(Head.class));
when(session.head().getSourceField()).thenReturn(field);
reader.read(session);
assertNotNull(field.getValue());
assertEquals("red", field.getValue());
field.setPath("/<1>/value");
reader.read(session);
assertNotNull(field.getValue());
assertEquals("#0f0", field.getValue());
field.setFieldType(null);
field.setPath("/<>/color");
Field readField = reader.read(session);
assertEquals(FieldGroup.class, readField.getClass());
FieldGroup readFieldGroup = (FieldGroup) readField;
assertEquals(3, readFieldGroup.getField().size());
assertEquals("red", readFieldGroup.getField().get(0).getValue());
assertEquals("green", readFieldGroup.getField().get(1).getValue());
assertEquals("blue", readFieldGroup.getField().get(2).getValue());
}
Aggregations