Search in sources :

Example 46 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class DefaultAtlasContext method processTargetFieldMappings.

private void processTargetFieldMappings(DefaultAtlasSession session, Mapping mapping) throws AtlasException {
    MappingType mappingType = mapping.getMappingType();
    List<Field> sourceFields = mapping.getInputField();
    List<Field> targetFields = mapping.getOutputField();
    AtlasModule module = null;
    Field targetField = null;
    switch(mappingType) {
        case LOOKUP:
        case MAP:
            targetField = targetFields.get(0);
            module = resolveModule(FieldDirection.TARGET, targetField);
            if (!auditTargetFieldType(session, module, targetField)) {
                return;
            }
            session.head().setTargetField(targetField);
            module.processTargetFieldMapping(session);
            return;
        case COMBINE:
            targetField = targetFields.get(0);
            module = resolveModule(FieldDirection.TARGET, targetField);
            if (!auditTargetFieldType(session, module, targetField)) {
                return;
            }
            Field sourceField = processCombineField(session, mapping, sourceFields, targetField);
            session.head().setSourceField(sourceField).setTargetField(targetField);
            module.processTargetFieldMapping(session);
            return;
        case SEPARATE:
            Field sourceFieldsep = sourceFields.get(0);
            if ((sourceFieldsep.getFieldType() != null && !FieldType.STRING.equals(sourceFieldsep.getFieldType()) || (sourceFieldsep.getValue() == null || !sourceFieldsep.getValue().getClass().isAssignableFrom(String.class)))) {
                AtlasUtil.addAudit(session, sourceFieldsep.getDocId(), String.format("Separate requires String field type for sourceField.path=%s", sourceFieldsep.getPath()), sourceFieldsep.getPath(), AuditStatus.WARN, null);
                return;
            }
            List<Field> separatedFields = processSeparateField(session, mapping, sourceFields.get(0));
            for (Field f : targetFields) {
                targetField = f;
                module = resolveModule(FieldDirection.TARGET, targetField);
                if (!auditTargetFieldType(session, module, targetField)) {
                    continue;
                }
                if (targetField.getIndex() == null || targetField.getIndex() < 0) {
                    AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Separate requires zero or positive Index value to be set on targetField targetField.path=%s", targetField.getPath()), targetField.getPath(), AuditStatus.WARN, null);
                    continue;
                }
                if (separatedFields.size() <= targetField.getIndex()) {
                    String errorMessage = String.format("Separate returned fewer segments count=%s when targetField.path=%s requested index=%s", separatedFields.size(), targetField.getPath(), targetField.getIndex());
                    AtlasUtil.addAudit(session, targetField.getDocId(), errorMessage, targetField.getPath(), AuditStatus.WARN, null);
                    break;
                }
                session.head().setSourceField(separatedFields.get(targetField.getIndex())).setTargetField(targetField);
                module.processTargetFieldMapping(session);
            }
            return;
        default:
            AtlasUtil.addAudit(session, null, String.format("Unsupported mappingType=%s detected", mapping.getMappingType()), null, AuditStatus.ERROR, null);
    }
}
Also used : MappingType(io.atlasmap.v2.MappingType) PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) AtlasModule(io.atlasmap.spi.AtlasModule)

Example 47 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class DefaultAtlasContext method extractCollectionMappings.

private List<Mapping> extractCollectionMappings(DefaultAtlasSession session, BaseMapping baseMapping) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Generating Source Mappings from mapping: {}", baseMapping);
    }
    if (!baseMapping.getMappingType().equals(MappingType.COLLECTION)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Mapping is not a collection mapping, not cloning: {}", baseMapping);
        }
        return Arrays.asList((Mapping) baseMapping);
    }
    List<Mapping> mappings = new LinkedList<>();
    for (BaseMapping m : ((Collection) baseMapping).getMappings().getMapping()) {
        Mapping mapping = (Mapping) m;
        Field sourceField = mapping.getInputField().get(0);
        boolean sourceIsCollection = AtlasPath.isCollection(sourceField.getPath());
        if (!sourceIsCollection) {
            // just copy it over
            if (LOG.isDebugEnabled()) {
                LOG.debug("Internal mapping's source field is not a collection, not cloning: {}", mapping);
            }
            // output object to be created for our copied firstName value
            for (Field f : mapping.getOutputField()) {
                f.setPath(AtlasPath.overwriteCollectionIndex(f.getPath(), 0));
            }
            mappings.add(mapping);
            continue;
        }
        AtlasModule module = resolveModule(FieldDirection.SOURCE, sourceField);
        int sourceCollectionSize = module.getCollectionSize(session, sourceField);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Internal mapping's source field is a collection. Cloning it for each item ({} clones): {}", sourceCollectionSize, mapping);
        }
        for (int i = 0; i < sourceCollectionSize; i++) {
            Mapping cloneMapping = (Mapping) AtlasModelFactory.cloneMapping(mapping, false);
            for (Field f : mapping.getInputField()) {
                Field clonedField = module.cloneField(f);
                clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
                cloneMapping.getInputField().add(clonedField);
            }
            for (Field f : mapping.getOutputField()) {
                Field clonedField = module.cloneField(f);
                if (AtlasPath.isCollection(clonedField.getPath())) {
                    clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
                }
                cloneMapping.getOutputField().add(clonedField);
            }
            mappings.add(cloneMapping);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Generated {} mappings from mapping: {}", mappings.size(), baseMapping);
    }
    ((Collection) baseMapping).getMappings().getMapping().clear();
    ((Collection) baseMapping).getMappings().getMapping().addAll(mappings);
    return mappings;
}
Also used : PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) AtlasModule(io.atlasmap.spi.AtlasModule) Collection(io.atlasmap.v2.Collection) BaseMapping(io.atlasmap.v2.BaseMapping) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) LinkedList(java.util.LinkedList) BaseMapping(io.atlasmap.v2.BaseMapping)

Example 48 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class BaseModuleValidationService method validateFieldTypeConversion.

protected void validateFieldTypeConversion(String mappingId, Field inputField, Field outField, List<Validation> validations) {
    FieldType inFieldType = inputField.getFieldType();
    FieldType outFieldType = outField.getFieldType();
    Optional<AtlasConverter<?>> atlasConverter = conversionService.findMatchingConverter(inFieldType, outFieldType);
    if (!atlasConverter.isPresent()) {
        Validation validation = new Validation();
        validation.setScope(ValidationScope.MAPPING);
        validation.setId(mappingId);
        validation.setMessage(String.format("Conversion from '%s' to '%s' is required but no converter is available", inputField.getFieldType(), outField.getFieldType()));
        validation.setStatus(ValidationStatus.ERROR);
        validations.add(validation);
    } else {
        AtlasConversionInfo conversionInfo;
        // find the method that does the conversion
        Method[] methods = atlasConverter.get().getClass().getMethods();
        conversionInfo = Arrays.stream(methods).map(method -> method.getAnnotation(AtlasConversionInfo.class)).filter(atlasConversionInfo -> atlasConversionInfo != null).filter(atlasConversionInfo -> (atlasConversionInfo.sourceType().compareTo(inFieldType) == 0 && atlasConversionInfo.targetType().compareTo(outFieldType) == 0)).findFirst().orElse(null);
        if (conversionInfo != null) {
            populateConversionConcerns(mappingId, conversionInfo, getFieldName(inputField), getFieldName(outField), validations);
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Arrays(java.util.Arrays) FieldDirection(io.atlasmap.spi.FieldDirection) ValidationScope(io.atlasmap.v2.ValidationScope) DataSource(io.atlasmap.v2.DataSource) MappingType(io.atlasmap.v2.MappingType) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) ValidationStatus(io.atlasmap.v2.ValidationStatus) AtlasConversionConcern(io.atlasmap.spi.AtlasConversionConcern) FieldType(io.atlasmap.v2.FieldType) Validation(io.atlasmap.v2.Validation) ArrayList(java.util.ArrayList) Mapping(io.atlasmap.v2.Mapping) List(java.util.List) Field(io.atlasmap.v2.Field) AtlasModuleDetail(io.atlasmap.spi.AtlasModuleDetail) AtlasMapping(io.atlasmap.v2.AtlasMapping) AtlasModuleMode(io.atlasmap.spi.AtlasModuleMode) Optional(java.util.Optional) AtlasConversionService(io.atlasmap.api.AtlasConversionService) Method(java.lang.reflect.Method) BaseMapping(io.atlasmap.v2.BaseMapping) AtlasConverter(io.atlasmap.api.AtlasConverter) AtlasValidationService(io.atlasmap.api.AtlasValidationService) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.api.AtlasConverter) Method(java.lang.reflect.Method) FieldType(io.atlasmap.v2.FieldType)

Example 49 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class BaseModuleValidationService method validateCombineMapping.

protected void validateCombineMapping(Mapping mapping, List<Validation> validations) {
    if (mapping == null) {
        return;
    }
    List<Field> sourceFields = mapping.getInputField();
    Field targetField = mapping.getOutputField() != null ? mapping.getOutputField().get(0) : null;
    String mappingId = mapping.getId();
    if (getMode() == AtlasModuleMode.TARGET && matchDocIdOrNull(targetField.getDocId())) {
        if (sourceFields != null) {
            // we should convert per module validations to plugin style
            for (Field sourceField : sourceFields) {
                validateSourceAndTargetTypes(mappingId, sourceField, targetField, validations);
            }
        }
        // check that the output field is of type String else error
        if (targetField.getFieldType() != FieldType.STRING) {
            Validation validation = new Validation();
            validation.setScope(ValidationScope.MAPPING);
            validation.setId(mappingId);
            validation.setMessage(String.format("Output field '%s' must be of type '%s' for a Combine Mapping", getFieldName(targetField), FieldType.STRING));
            validation.setStatus(ValidationStatus.ERROR);
            validations.add(validation);
        }
        validateField(mappingId, targetField, FieldDirection.TARGET, validations);
    } else if (sourceFields != null) {
        // SOURCE
        for (Field sourceField : sourceFields) {
            if (matchDocIdOrNull(sourceField.getDocId())) {
                validateField(mappingId, sourceField, FieldDirection.SOURCE, validations);
            }
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Field(io.atlasmap.v2.Field)

Example 50 with Field

use of org.talend.hl7.Field in project atlasmap by atlasmap.

the class BaseModuleValidationService method validateSeparateMapping.

protected void validateSeparateMapping(Mapping mapping, List<Validation> validations) {
    if (mapping == null) {
        return;
    }
    final Field sourceField = mapping.getInputField() != null ? mapping.getInputField().get(0) : null;
    List<Field> targetFields = mapping.getOutputField();
    String mappingId = mapping.getId();
    if (getMode() == AtlasModuleMode.SOURCE && matchDocIdOrNull(sourceField.getDocId())) {
        // check that the input field is of type String else error
        if (sourceField.getFieldType() != FieldType.STRING) {
            Validation validation = new Validation();
            validation.setScope(ValidationScope.MAPPING);
            validation.setId(mapping.getId());
            validation.setMessage(String.format("Input field '%s' must be of type '%s' for a Separate Mapping", getFieldName(sourceField), FieldType.STRING));
            validation.setStatus(ValidationStatus.ERROR);
            validations.add(validation);
        }
        validateField(mappingId, sourceField, FieldDirection.SOURCE, validations);
        if (targetFields != null) {
            // we should convert per module validations to plugin style
            for (Field targetField : targetFields) {
                validateSourceAndTargetTypes(mappingId, sourceField, targetField, validations);
            }
        }
    } else if (targetFields != null) {
        // TARGET
        for (Field targetField : targetFields) {
            if (matchDocIdOrNull(targetField.getDocId())) {
                validateField(mappingId, targetField, FieldDirection.TARGET, validations);
            }
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Field(io.atlasmap.v2.Field)

Aggregations

Field (io.atlasmap.v2.Field)60 JavaField (io.atlasmap.java.v2.JavaField)15 Mapping (io.atlasmap.v2.Mapping)15 AtlasMapping (io.atlasmap.v2.AtlasMapping)14 SimpleField (io.atlasmap.v2.SimpleField)14 BaseMapping (io.atlasmap.v2.BaseMapping)12 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)11 Test (org.junit.Test)11 JsonField (io.atlasmap.json.v2.JsonField)8 LookupTable (io.atlasmap.v2.LookupTable)8 XmlField (io.atlasmap.xml.v2.XmlField)7 AtlasException (io.atlasmap.api.AtlasException)6 ConstantField (io.atlasmap.v2.ConstantField)6 AtlasConversionException (io.atlasmap.api.AtlasConversionException)5 JsonDocument (io.atlasmap.json.v2.JsonDocument)5 PropertyField (io.atlasmap.v2.PropertyField)5 ArrayList (java.util.ArrayList)5 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)4 AtlasModule (io.atlasmap.spi.AtlasModule)3 FieldType (io.atlasmap.v2.FieldType)3