Search in sources :

Example 6 with AtlasConversionException

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

the class JsonModule method processTargetFieldMapping.

@Override
public void processTargetFieldMapping(AtlasInternalSession session) throws AtlasException {
    Field sourceField = session.head().getSourceField();
    Field targetField = session.head().getTargetField();
    // Attempt to Auto-detect field type based on input value
    if (targetField.getFieldType() == null && sourceField.getValue() != null) {
        targetField.setFieldType(getConversionService().fieldTypeFromClass(sourceField.getValue().getClass()));
    }
    Object targetValue = null;
    // Do auto-conversion
    if (sourceField.getFieldType() != null && sourceField.getFieldType().equals(targetField.getFieldType())) {
        targetValue = sourceField.getValue();
    } else if (sourceField.getValue() != null) {
        try {
            targetValue = getConversionService().convertType(sourceField.getValue(), sourceField.getFormat(), targetField.getFieldType(), targetField.getFormat());
        } catch (AtlasConversionException e) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Unable to auto-convert for sT=%s tT=%s tF=%s msg=%s", sourceField.getFieldType(), targetField.getFieldType(), targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, null);
            return;
        }
    }
    targetField.setValue(targetValue);
    LookupTable lookupTable = session.head().getLookupTable();
    if (lookupTable != null) {
        processLookupField(session, lookupTable, targetField.getValue(), targetField);
    }
    if (isAutomaticallyProcessOutputFieldActions() && targetField.getActions() != null && targetField.getActions().getActions() != null) {
        getFieldActionService().processActions(targetField.getActions(), targetField);
    }
    JsonFieldWriter writer = session.getFieldWriter(getDocId(), JsonFieldWriter.class);
    writer.write(session);
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonFieldWriter(io.atlasmap.json.core.JsonFieldWriter) AtlasConversionException(io.atlasmap.api.AtlasConversionException) LookupTable(io.atlasmap.v2.LookupTable)

Example 7 with AtlasConversionException

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

the class JsonFieldReader method read.

@Override
public void read(AtlasInternalSession session) throws AtlasException {
    JsonField jsonField = JsonField.class.cast(session.head().getSourceField());
    if (rootNode == null) {
        throw new AtlasException("document is not set");
    }
    if (jsonField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'jsonField' cannot be null"));
    }
    JsonNode valueNode = null;
    AtlasPath path = new AtlasPath(jsonField.getPath());
    if (path.getSegments().size() >= 1) {
        if (rootNode.size() == 1 && !path.getSegments().get(0).startsWith(rootNode.fieldNames().next())) {
            // peel off a rooted object
            valueNode = rootNode.elements().next();
        } else {
            valueNode = rootNode;
        }
        // need to walk the path....
        for (String nodeName : path.getSegments()) {
            if (valueNode == null) {
                break;
            }
            valueNode = getValueNode(valueNode, nodeName);
        }
    }
    if (valueNode == null) {
        return;
    }
    if (valueNode.isNull()) {
        jsonField.setValue(null);
    // we can't detect field type if it's null node
    } else {
        if (jsonField.getFieldType() != null) {
            // mapping is overriding the fieldType
            try {
                Object convertedValue = conversionService.convertType(valueNode.asText(), jsonField.getFormat(), jsonField.getFieldType(), null);
                jsonField.setValue(convertedValue);
            } catch (AtlasConversionException e) {
                AtlasUtil.addAudit(session, jsonField.getDocId(), String.format("Failed to convert field value '%s' into type '%s'", valueNode.asText(), jsonField.getFieldType()), jsonField.getPath(), AuditStatus.ERROR, valueNode.asText());
            }
        } else {
            if (valueNode.isTextual()) {
                handleTextualNode(valueNode, jsonField);
            } else if (valueNode.isNumber()) {
                handleNumberNode(valueNode, jsonField);
            } else if (valueNode.isBoolean()) {
                handleBooleanNode(valueNode, jsonField);
            } else if (valueNode.isContainerNode()) {
                handleContainerNode(valueNode, jsonField);
            } else if (valueNode.isNull()) {
                jsonField.setValue(null);
            } else {
                LOG.warn(String.format("Detected unsupported json type for field p=%s docId=%s", jsonField.getPath(), jsonField.getDocId()));
                jsonField.setValue(valueNode.toString());
                jsonField.setFieldType(FieldType.UNSUPPORTED);
            }
        }
    }
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) AtlasException(io.atlasmap.api.AtlasException)

Example 8 with AtlasConversionException

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

the class XmlModule method processTargetFieldMapping.

@Override
public void processTargetFieldMapping(AtlasInternalSession session) throws AtlasException {
    Field sourceField = session.head().getSourceField();
    Field targetField = session.head().getTargetField();
    // Attempt to Auto-detect field type based on input value
    if (targetField.getFieldType() == null && sourceField.getValue() != null) {
        targetField.setFieldType(getConversionService().fieldTypeFromClass(sourceField.getValue().getClass()));
    }
    Object outputValue = null;
    // Do auto-conversion
    if (sourceField.getFieldType() != null && sourceField.getFieldType().equals(targetField.getFieldType())) {
        outputValue = sourceField.getValue();
    } else if (sourceField.getValue() != null) {
        try {
            outputValue = getConversionService().convertType(sourceField.getValue(), sourceField.getFormat(), targetField.getFieldType(), targetField.getFormat());
        } catch (AtlasConversionException e) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Unable to auto-convert for sT=%s tT=%s tF=%s msg=%s", sourceField.getFieldType(), targetField.getFieldType(), targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, null);
            return;
        }
    }
    targetField.setValue(outputValue);
    LookupTable lookupTable = session.head().getLookupTable();
    if (lookupTable != null) {
        processLookupField(session, lookupTable, targetField.getValue(), targetField);
    }
    if (isAutomaticallyProcessOutputFieldActions() && targetField.getActions() != null && targetField.getActions().getActions() != null) {
        getFieldActionService().processActions(targetField.getActions(), targetField);
    }
    XmlFieldWriter writer = session.getFieldWriter(getDocId(), XmlFieldWriter.class);
    writer.write(session);
    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());
    }
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) AtlasConversionException(io.atlasmap.api.AtlasConversionException) LookupTable(io.atlasmap.v2.LookupTable) XmlFieldWriter(io.atlasmap.xml.core.XmlFieldWriter)

Example 9 with AtlasConversionException

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

the class StringConverter method toLong.

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

Example 10 with AtlasConversionException

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

the class TargetValueConverter method convert.

public Object convert(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
    FieldType sourceType = sourceField.getFieldType();
    Object sourceValue = sourceField.getValue();
    Object targetValue = null;
    FieldType targetType = targetField.getFieldType();
    if (LOG.isDebugEnabled()) {
        LOG.debug("processTargetMapping iPath=" + sourceField.getPath() + " iV=" + sourceValue + " iT=" + sourceType + " oPath=" + targetField.getPath() + " docId: " + targetField.getDocId());
    }
    if (sourceValue == null) {
        // TODO: Finish targetValue = null processing
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Null sourceValue for targetDocId=%s, targetPath=%s", targetField.getDocId(), targetField.getPath()), targetField.getPath(), AuditStatus.WARN, sourceValue != null ? sourceValue.toString() : null);
        return null;
    }
    String targetClassName = (targetField instanceof JavaField) ? ((JavaField) targetField).getClassName() : null;
    targetClassName = (targetField instanceof JavaEnumField) ? ((JavaEnumField) targetField).getClassName() : targetClassName;
    if (targetType == null || targetClassName == null) {
        try {
            Method setter = resolveTargetSetMethod(parentObject, targetField, null);
            if (setter != null && setter.getParameterCount() == 1) {
                if (targetField instanceof JavaField) {
                    ((JavaField) targetField).setClassName(setter.getParameterTypes()[0].getName());
                } else if (targetField instanceof JavaEnumField) {
                    ((JavaEnumField) targetField).setClassName(setter.getParameterTypes()[0].getName());
                }
                targetType = conversionService.fieldTypeFromClass(setter.getParameterTypes()[0]);
                targetField.setFieldType(targetType);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Auto-detected targetType as {} for class={} path={}", targetType, parentObject.toString(), targetField.getPath());
                }
            }
        } catch (Exception e) {
            LOG.debug("Unable to auto-detect targetType for class={} path={}", parentObject.toString(), targetField.getPath());
        }
    }
    if (sourceField instanceof JavaEnumField || targetField instanceof JavaEnumField) {
        if (!(sourceField instanceof JavaEnumField) || !(targetField instanceof JavaEnumField)) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Value conversion between enum fields and non-enum fields is not yet supported: sourceType=%s targetType=%s targetPath=%s msg=%s", sourceType, targetType, targetField.getPath()), targetField.getPath(), AuditStatus.ERROR, sourceValue != null ? sourceValue.toString() : null);
        }
        return populateEnumValue(session, lookupTable, (JavaEnumField) sourceField, (JavaEnumField) targetField);
    }
    Class<?> targetClazz = null;
    if (targetClassName == null) {
        if (targetType != null) {
            targetClazz = conversionService.classFromFieldType(targetType);
        } else {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Target field doesn't have fieldType nor className: automatic conversion won't work: targetPath=%s", targetField.getPath()), targetField.getPath(), AuditStatus.WARN, sourceValue != null ? sourceValue.toString() : null);
        }
    } else if (conversionService.isPrimitive(targetClassName)) {
        targetClazz = conversionService.boxOrUnboxPrimitive(targetClassName);
    } else {
        try {
            targetClazz = classLoader.loadClass(targetClassName);
        } catch (ClassNotFoundException e) {
            AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Target field class '%s' was not found: sourceType=%s targetType=%s targetPath=%s msg=%s", ((JavaField) targetField).getClassName(), sourceType, targetType, targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue != null ? targetValue.toString() : null);
            return null;
        }
    }
    if (targetClazz != null) {
        targetValue = conversionService.convertType(sourceValue, null, targetClazz, null);
    } else {
        targetValue = sourceValue;
    }
    AtlasFieldActionService fieldActionService = session.getAtlasContext().getContextFactory().getFieldActionService();
    try {
        targetValue = fieldActionService.processActions(targetField.getActions(), targetValue, targetType);
        if (targetValue != null) {
            if (targetClazz != null) {
                targetValue = conversionService.convertType(targetValue, null, targetClazz, null);
            } else {
                FieldType conversionInputType = conversionService.fieldTypeFromClass(targetValue.getClass());
                targetValue = conversionService.convertType(targetValue, conversionInputType, targetType);
            }
        }
    } catch (AtlasConversionException e) {
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Unable to auto-convert for sourceType=%s targetType=%s targetPath=%s msg=%s", sourceType, targetType, targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue != null ? targetValue.toString() : null);
        return null;
    }
    return targetValue;
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) AtlasFieldActionService(io.atlasmap.api.AtlasFieldActionService) AtlasConversionException(io.atlasmap.api.AtlasConversionException) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) FieldType(io.atlasmap.v2.FieldType)

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