Search in sources :

Example 1 with StringDelimiter

use of io.atlasmap.spi.StringDelimiter in project atlasmap by atlasmap.

the class DefaultAtlasContext method processCombineField.

private Field processCombineField(DefaultAtlasSession session, Mapping mapping, List<Field> sourceFields, Field targetField) throws AtlasException {
    Map<Integer, String> combineValues = null;
    for (Field sourceField : sourceFields) {
        if (sourceField.getIndex() == null || sourceField.getIndex() < 0) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Combine requires zero or positive Index value to be set on all sourceFields sourceField.path=%s", sourceField.getPath()), targetField.getPath(), AuditStatus.WARN, null);
            continue;
        }
        if (combineValues == null) {
            // We need to support a sorted map w/ null values
            combineValues = new HashMap<>();
        }
        if ((sourceField.getFieldType() != null) || (sourceField.getValue() != null)) {
            String sourceValue;
            try {
                sourceValue = (String) factory.getConversionService().convertType(sourceField.getValue(), sourceField.getFormat(), FieldType.STRING, null);
            } catch (AtlasConversionException e) {
                AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Suitable converter for sourceField.path=%s hasn't been found", sourceField.getPath()), targetField.getPath(), AuditStatus.WARN, null);
                sourceValue = sourceField.getValue() != null ? sourceField.getValue().toString() : null;
            }
            combineValues.put(sourceField.getIndex(), sourceValue);
            continue;
        }
    }
    String combinedValue = null;
    StringDelimiter delimiter = StringDelimiter.fromName(mapping.getDelimiter());
    if (delimiter != null) {
        combinedValue = session.getAtlasContext().getContextFactory().getCombineStrategy().combineValues(combineValues, delimiter);
    } else {
        combinedValue = session.getAtlasContext().getContextFactory().getCombineStrategy().combineValues(combineValues);
    }
    Field answer = AtlasModelFactory.cloneFieldToSimpleField(sourceFields.get(0));
    if (combinedValue == null || combinedValue.trim().isEmpty()) {
        LOG.debug(String.format("Empty combined string for Combine mapping targetField.path=%s", targetField.getPath()));
    } else {
        answer.setValue(combinedValue);
    }
    return answer;
}
Also used : PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) AtlasConversionException(io.atlasmap.api.AtlasConversionException) StringDelimiter(io.atlasmap.spi.StringDelimiter)

Example 2 with StringDelimiter

use of io.atlasmap.spi.StringDelimiter in project atlasmap by atlasmap.

the class DefaultAtlasContext method processSeparateField.

private List<Field> processSeparateField(DefaultAtlasSession session, Mapping mapping, Field sourceField) throws AtlasException {
    List<Field> answer = new ArrayList<>();
    String sourceValue;
    try {
        sourceValue = (String) factory.getConversionService().convertType(sourceField.getValue(), sourceField.getFormat(), FieldType.STRING, null);
    } catch (AtlasConversionException e) {
        AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Suitable converter for sourceField.path=%s hasn't been found", sourceField.getPath()), sourceField.getPath(), AuditStatus.WARN, null);
        sourceValue = sourceField.getValue().toString();
    }
    List<String> separatedValues = null;
    StringDelimiter delimiter = StringDelimiter.fromName(mapping.getDelimiter());
    if (mapping.getDelimiter() != null) {
        separatedValues = session.getAtlasContext().getContextFactory().getSeparateStrategy().separateValue(sourceValue, delimiter);
    } else {
        separatedValues = session.getAtlasContext().getContextFactory().getSeparateStrategy().separateValue(sourceValue);
    }
    if (separatedValues == null || separatedValues.isEmpty()) {
        LOG.debug(String.format("Empty string for Separate mapping sourceField.path=%s", sourceField.getPath()));
    } else {
        for (String separatedValue : separatedValues) {
            SimpleField simpleField = AtlasModelFactory.cloneFieldToSimpleField(sourceField);
            simpleField.setValue(separatedValue);
            simpleField.setFieldType(FieldType.STRING);
            answer.add(simpleField);
        }
    }
    return answer;
}
Also used : PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) AtlasConversionException(io.atlasmap.api.AtlasConversionException) ArrayList(java.util.ArrayList) SimpleField(io.atlasmap.v2.SimpleField) StringDelimiter(io.atlasmap.spi.StringDelimiter)

Aggregations

AtlasConversionException (io.atlasmap.api.AtlasConversionException)2 StringDelimiter (io.atlasmap.spi.StringDelimiter)2 ConstantField (io.atlasmap.v2.ConstantField)2 Field (io.atlasmap.v2.Field)2 PropertyField (io.atlasmap.v2.PropertyField)2 SimpleField (io.atlasmap.v2.SimpleField)2 ArrayList (java.util.ArrayList)1