Search in sources :

Example 6 with LookupEntry

use of io.atlasmap.v2.LookupEntry in project atlasmap by atlasmap.

the class TargetValueConverter method populateEnumValue.

@SuppressWarnings("unchecked")
private Object populateEnumValue(AtlasInternalSession session, LookupTable lookupTable, JavaEnumField sourceField, JavaEnumField targetField) {
    if (sourceField == null || sourceField.getValue() == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Input enum field or value is null, field: " + sourceField);
        }
        return null;
    }
    String sourceValue = ((Enum<?>) sourceField.getValue()).name();
    String targetValue = sourceValue;
    if (lookupTable != null) {
        for (LookupEntry e : lookupTable.getLookupEntry()) {
            if (e.getSourceValue().equals(sourceValue)) {
                targetValue = e.getTargetValue();
                break;
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Mapped input enum value '" + sourceValue + "' to output enum value '" + targetValue + "'.");
    }
    if (targetValue == null) {
        return null;
    }
    @SuppressWarnings("rawtypes") Class enumClass = null;
    try {
        enumClass = Class.forName(targetField.getClassName());
    } catch (Exception e) {
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Could not find class for output field class '%s': %s", targetField.getClassName(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue);
        return null;
    }
    try {
        return Enum.valueOf(enumClass, targetValue);
    } catch (IllegalArgumentException e) {
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("No enum entry found for value '%s': %s", targetValue, e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue);
        return null;
    }
}
Also used : LookupEntry(io.atlasmap.v2.LookupEntry) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException)

Aggregations

LookupEntry (io.atlasmap.v2.LookupEntry)6 LookupTable (io.atlasmap.v2.LookupTable)4 AtlasConversionException (io.atlasmap.api.AtlasConversionException)1 AtlasException (io.atlasmap.api.AtlasException)1 AtlasMapping (io.atlasmap.v2.AtlasMapping)1 BaseMapping (io.atlasmap.v2.BaseMapping)1 FieldType (io.atlasmap.v2.FieldType)1 Mapping (io.atlasmap.v2.Mapping)1 Test (org.junit.Test)1