use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class BaseModuleValidationService method validateMapMapping.
/**
* Validates MAP mapping.
* @param mapping mapping
* @param validations a container to put the result validations
*/
protected void validateMapMapping(Mapping mapping, List<Validation> validations) {
if (mapping == null || mapping.getInputField() == null || (mapping.getInputFieldGroup() == null && mapping.getInputField().size() <= 0) || mapping.getOutputField() == null || mapping.getOutputField().size() <= 0) {
return;
}
String mappingId = mapping.getId();
if (getMode() == AtlasModuleMode.SOURCE) {
FieldGroup sourceFieldGroup = mapping.getInputFieldGroup();
if (sourceFieldGroup != null) {
validateFieldGroup(mappingId, sourceFieldGroup, FieldDirection.SOURCE, validations);
} else {
List<Field> sourceFields = mapping.getInputField();
sourceFields.forEach(sourceField -> {
validateField(mappingId, null, sourceField, FieldDirection.SOURCE, validations);
});
}
} else if (getMode() == AtlasModuleMode.TARGET) {
List<Field> targetFields = mapping.getOutputField();
if (targetFields.size() == 1 && Integer.valueOf(0).equals(targetFields.get(0).getIndex())) {
// The index should not have been set as there's only one item
targetFields.get(0).setIndex(null);
}
int i = 0;
List<Field> sourceFields = mapping.getInputField();
for (Field targetField : targetFields) {
if (sourceFields.size() > i) {
validateField(mappingId, sourceFields.get(i), targetField, FieldDirection.TARGET, validations);
} else {
validateField(mappingId, null, targetField, FieldDirection.TARGET, validations);
}
i++;
}
}
if (getMode() == AtlasModuleMode.SOURCE) {
validateFieldCombinations(mapping, validations);
}
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class BaseModuleValidationService method validateFieldCombinations.
/**
* Validate the combination of source field(s) and target field(s).
* @param mapping mapping
* @param validations a container to put the result validations
*/
protected void validateFieldCombinations(Mapping mapping, List<Validation> validations) {
String mappingId = mapping.getId();
FieldGroup sourceFieldGroup = mapping.getInputFieldGroup();
List<Field> sourceFields = mapping.getInputField();
List<Field> targetFields = mapping.getOutputField();
if (sourceFieldGroup != null || (sourceFields != null && sourceFields.size() > 1)) {
if (targetFields.size() > 1) {
Validation validation = new Validation();
validation.setScope(ValidationScope.MAPPING);
validation.setId(mappingId);
validation.setMessage("Multiple fields can not be selected on both of Source and Target");
validation.setStatus(ValidationStatus.ERROR);
validations.add(validation);
}
if (sourceFieldGroup != null) {
mappingFieldPairValidator.validateFieldTypes(validations, mappingId, sourceFieldGroup, targetFields.get(0));
} else {
mappingFieldPairValidator.validateFieldTypes(validations, mappingId, sourceFields, targetFields.get(0));
}
} else if (targetFields != null && targetFields.size() > 1) {
mappingFieldPairValidator.validateFieldTypes(validations, mappingId, sourceFields.get(0), targetFields);
} else {
mappingFieldPairValidator.validateFieldTypes(validations, mappingId, sourceFields.get(0), targetFields.get(0));
}
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class DefaultAtlasFieldActionService method packExpressionActionOutcomeIntoField.
private Field packExpressionActionOutcomeIntoField(Object values, Field field) {
if (values instanceof List) {
// n -> n and 1 -> n - create new FieldGroup
FieldGroup fieldGroup = new FieldGroup();
// Make sure fieldGroup is of a collection type
AtlasPath groupPath = new AtlasPath(AtlasModelFactory.GENERATED_PATH);
fieldGroup.setCollectionType(CollectionType.LIST);
groupPath = new AtlasPath(groupPath.toString() + AtlasPath.PATH_LIST_SUFFIX);
fieldGroup.setPath(groupPath.toString());
List<?> tmpSourceList = (List<?>) values;
while (tmpSourceList.size() == 1 && (tmpSourceList.get(0) instanceof List)) {
tmpSourceList = (List<Object>) tmpSourceList.get(0);
}
FieldType type = null;
for (int i = 0; i < tmpSourceList.size(); i++) {
Object subValue = tmpSourceList.get(i);
if (type == null && subValue != null) {
type = getConversionService().fieldTypeFromClass(subValue.getClass());
}
Field subField = new SimpleField();
AtlasPath subPath = groupPath.clone();
subPath.setVacantCollectionIndex(i);
AtlasModelFactory.copyField(fieldGroup, subField, false);
subField.setPath(subPath.toString());
subField.setIndex(null);
subField.setValue(subValue);
subField.setFieldType(type);
subField.setCollectionType(CollectionType.NONE);
fieldGroup.getField().add(subField);
}
return fieldGroup;
}
if (values != null) {
field = new SimpleField();
field.setPath(AtlasModelFactory.GENERATED_PATH);
field.setValue(values);
field.setFieldType(getConversionService().fieldTypeFromClass(values.getClass()));
}
return field;
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class AtlasPath method extractChildren.
/**
* Extract child fields by feeding relative path.
*
* @param f Parent field to extract from
* @param path Relative path string
* @return extracted field(s)
*/
public static Field extractChildren(Field f, String path) {
if (f == null || path == null || path.isEmpty()) {
return null;
}
if (path.equals(PATH_SEPARATOR)) {
return f;
}
if (!(f instanceof FieldGroup)) {
return null;
}
List<Field> extracted = new ArrayList<>();
FieldGroup entryField = (FieldGroup) f;
extracted.add(entryField);
List<SegmentContext> entrySegments = new AtlasPath(entryField.getPath()).getSegments(true);
SegmentContext entrySegment = entrySegments.get(entrySegments.size() - 1);
List<SegmentContext> extractedSegments = new ArrayList<>(entrySegments);
List<SegmentContext> relativeSegments = new AtlasPath(path).getSegments(true);
SegmentContext relativeRootSegment = relativeSegments.get(0);
List<Field> selected = new ArrayList<>();
if (relativeRootSegment.getCollectionType() == null || relativeRootSegment.getCollectionType() == CollectionType.NONE) {
if (entrySegment.getCollectionType() != null && entrySegment.getCollectionType() != CollectionType.NONE && entrySegment.getCollectionIndex() == null) {
selected.addAll(entryField.getField());
} else {
selected.add(entryField);
}
} else if (relativeRootSegment.getCollectionIndex() != null) {
if (entrySegment.getCollectionIndex() != null) {
if (entrySegment.getCollectionIndex() == relativeRootSegment.getCollectionIndex()) {
selected.add(entryField);
}
} else {
selected.add(entryField.getField().get(relativeRootSegment.getCollectionIndex()));
entrySegment.collectionIndex = relativeRootSegment.getCollectionIndex();
extractedSegments.set(entrySegments.size() - 1, entrySegment.rebuild());
}
} else {
selected.addAll(entryField.getField());
}
extracted = selected;
for (int i = 1; i < relativeSegments.size(); i++) {
SegmentContext segment = relativeSegments.get(i);
extractedSegments.add(segment);
selected = new ArrayList<>();
for (Field f1 : extracted) {
FieldGroup f1Group = (FieldGroup) f1;
for (Field f2 : f1Group.getField()) {
AtlasPath f2Path = new AtlasPath(f2.getPath());
if (!segment.getName().equals(f2Path.getLastSegment().getName())) {
continue;
}
if (segment.getCollectionType() == CollectionType.NONE) {
selected.add(f2);
} else {
FieldGroup f2Group = (FieldGroup) f2;
if (segment.getCollectionIndex() != null) {
selected.add((f2Group.getField().get(segment.getCollectionIndex())));
} else {
selected.addAll(f2Group.getField());
}
}
break;
}
}
extracted = selected;
}
if (extracted.size() == 1) {
return extracted.get(0);
}
FieldGroup answer = AtlasModelFactory.createFieldGroupFrom(f, true);
answer.setPath(new AtlasPath(extractedSegments).toString());
answer.getField().addAll(extracted);
return answer;
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class AtlasPath method setCollectionIndexRecursively.
/**
* Sets the collection indexes recursively, which includes modifying path of the subsequent children.
* @param group parent field
* @param segmentIndex target segment index
* @param index index to set
*/
public static void setCollectionIndexRecursively(FieldGroup group, int segmentIndex, int index) {
AtlasPath path = new AtlasPath(group.getPath());
path.setCollectionIndex(segmentIndex, index);
group.setPath(path.toString());
for (Field f : group.getField()) {
if (f instanceof FieldGroup) {
setCollectionIndexRecursively((FieldGroup) f, segmentIndex, index);
} else {
AtlasPath fpath = new AtlasPath(f.getPath());
fpath.setCollectionIndex(segmentIndex, index);
f.setPath(fpath.toString());
}
}
}
Aggregations