Search in sources :

Example 1 with AtlasConversionException

use of io.atlasmap.api.AtlasConversionException in project atlasmap by atlasmap.

the class DefaultAtlasConversionService method convertType.

@Override
public Object convertType(Object sourceValue, String sourceFormat, Class<?> targetType, String targetFormat) throws AtlasConversionException {
    if (sourceValue == null || targetType == null) {
        throw new AtlasConversionException("AutoConversion requires sourceValue and targetType to be specified");
    }
    if (targetType.isInstance(sourceValue)) {
        return sourceValue;
    }
    ConverterKey converterKey = new ConverterKey(sourceValue.getClass().getCanonicalName(), targetType.getCanonicalName());
    // use custom converter first
    ConverterMethodHolder methodHolder = customConverterMethods.get(converterKey);
    if (methodHolder == null) {
        // try the inbuilt defaults
        methodHolder = converterMethods.get(converterKey);
    }
    if (methodHolder != null) {
        try {
            Object target = methodHolder.staticMethod ? null : methodHolder.converter;
            if (methodHolder.containsFormat) {
                return methodHolder.method.invoke(target, sourceValue, sourceFormat, targetFormat);
            }
            return methodHolder.method.invoke(target, sourceValue);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new AtlasConversionException("Invoking type convertor failed", e);
        }
    }
    throw new AtlasConversionException("Type Conversion is not supported for sT=" + sourceValue.getClass().getCanonicalName() + " tT=" + targetType.getCanonicalName());
}
Also used : AtlasConversionException(io.atlasmap.api.AtlasConversionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with AtlasConversionException

use of io.atlasmap.api.AtlasConversionException 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 3 with AtlasConversionException

use of io.atlasmap.api.AtlasConversionException 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)

Example 4 with AtlasConversionException

use of io.atlasmap.api.AtlasConversionException in project atlasmap by atlasmap.

the class StringConverter method toInteger.

@AtlasConversionInfo(sourceType = FieldType.STRING, targetType = FieldType.INTEGER, concerns = { AtlasConversionConcern.FORMAT, AtlasConversionConcern.RANGE })
public Integer toInteger(String value) throws AtlasConversionException {
    if (value == null) {
        return null;
    }
    BigDecimal bd = null;
    Integer i = null;
    try {
        i = Integer.parseInt(value);
    } catch (NumberFormatException nfe) {
        try {
            bd = new BigDecimal(value);
            i = bd.intValue();
        } catch (NumberFormatException nfe2) {
            throw new AtlasConversionException(nfe);
        }
    }
    if (bd != null && bd.compareTo(BigDecimal.valueOf(i)) != 0) {
        throw new AtlasConversionException(String.format("String %s is greater than Integer.MAX_VALUE  or less than Integer.MIN_VALUE", value));
    }
    return i;
}
Also used : BigInteger(java.math.BigInteger) AtlasConversionException(io.atlasmap.api.AtlasConversionException) BigDecimal(java.math.BigDecimal) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo)

Example 5 with AtlasConversionException

use of io.atlasmap.api.AtlasConversionException in project atlasmap by atlasmap.

the class XmlFieldReader method read.

public void read(AtlasInternalSession session) throws AtlasException {
    if (document == null) {
        throw new AtlasException(new IllegalArgumentException("'document' cannot be null"));
    }
    Field field = session.head().getSourceField();
    if (field == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
    }
    seedDocumentNamespaces(document);
    XmlField xmlField = XmlField.class.cast(field);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Reading source value for field: " + xmlField.getPath());
    }
    if (document == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'document' cannot be null"));
    }
    if (xmlField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'xmlField' cannot be null"));
    }
    Element parentNode = document.getDocumentElement();
    for (SegmentContext sc : new XmlPath(xmlField.getPath()).getSegmentContexts(false)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Now processing segment: " + sc.getSegment());
            LOG.debug("Parent element is currently: " + XmlIOHelper.writeDocumentToString(true, parentNode));
        }
        if (sc.getPrev() == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Skipping root segment: " + sc);
            }
            // "/XOA/contact<>/firstName", skip.
            continue;
        }
        if (!XmlPath.isAttributeSegment(sc.getSegment())) {
            String childrenElementName = XmlPath.cleanPathSegment(sc.getSegment());
            String namespaceAlias = XmlPath.getNamespace(sc.getSegment());
            if (namespaceAlias != null && !"".equals(namespaceAlias)) {
                childrenElementName = namespaceAlias + ":" + childrenElementName;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Looking for children elements with name: " + childrenElementName);
            }
            List<Element> children = XmlIOHelper.getChildrenWithName(childrenElementName, parentNode);
            if (children == null || children.isEmpty()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Skipping source value set, couldn't find children with name '" + childrenElementName + "', for segment: " + sc);
                }
                return;
            }
            parentNode = children.get(0);
            if (XmlPath.isCollectionSegment(sc.getSegment())) {
                int index = XmlPath.indexOfSegment(sc.getSegment());
                if (index >= children.size()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Skipping source value set, children list can't fit index " + index + ", children list size: " + children.size());
                    }
                    return;
                }
                parentNode = children.get(index);
            }
        }
        if (sc.getNext() == null) {
            // last segment.
            String value = parentNode.getTextContent();
            if (XmlPath.isAttributeSegment(sc.getSegment())) {
                String attributeName = XmlPath.getAttribute(sc.getSegment());
                value = parentNode.getAttribute(attributeName);
            }
            if (value == null) {
                return;
            }
            if (xmlField.getFieldType() == null) {
                xmlField.setValue(value);
                xmlField.setFieldType(FieldType.STRING);
            } else {
                Object convertedValue;
                try {
                    convertedValue = conversionService.convertType(value, xmlField.getFormat(), xmlField.getFieldType(), null);
                    xmlField.setValue(convertedValue);
                } catch (AtlasConversionException e) {
                    AtlasUtil.addAudit(session, xmlField.getDocId(), String.format("Failed to convert field value '%s' into type '%s'", value, xmlField.getFieldType()), xmlField.getPath(), AuditStatus.ERROR, value);
                }
            }
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasConversionException(io.atlasmap.api.AtlasConversionException) XmlField(io.atlasmap.xml.v2.XmlField) Element(org.w3c.dom.Element) AtlasException(io.atlasmap.api.AtlasException)

Aggregations

AtlasConversionException (io.atlasmap.api.AtlasConversionException)10 Field (io.atlasmap.v2.Field)5 AtlasException (io.atlasmap.api.AtlasException)3 JsonField (io.atlasmap.json.v2.JsonField)2 AtlasConversionInfo (io.atlasmap.spi.AtlasConversionInfo)2 StringDelimiter (io.atlasmap.spi.StringDelimiter)2 ConstantField (io.atlasmap.v2.ConstantField)2 LookupTable (io.atlasmap.v2.LookupTable)2 PropertyField (io.atlasmap.v2.PropertyField)2 SimpleField (io.atlasmap.v2.SimpleField)2 XmlField (io.atlasmap.xml.v2.XmlField)2 BigDecimal (java.math.BigDecimal)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 AtlasFieldActionService (io.atlasmap.api.AtlasFieldActionService)1 AtlasPath (io.atlasmap.core.AtlasPath)1 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)1 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)1 JavaField (io.atlasmap.java.v2.JavaField)1 JsonFieldWriter (io.atlasmap.json.core.JsonFieldWriter)1 FieldType (io.atlasmap.v2.FieldType)1