Search in sources :

Example 16 with LookupTable

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

the class LookupTableNameValidator method validate.

@Override
public void validate(Object target, List<Validation> validations, String id, ValidationStatus status) {
    LookupTables lookupTables = (LookupTables) target;
    List<LookupTable> tables = lookupTables.getLookupTable();
    List<LookupTable> deduped = Collections.unmodifiableList(tables).stream().filter(distinctByKey(LookupTable::getName)).collect(Collectors.toList());
    if (deduped.size() != tables.size()) {
        String dupedName = findDuplicatedName(tables);
        Validation validation = new Validation();
        validation.setScope(ValidationScope.LOOKUP_TABLE);
        validation.setId(id);
        validation.setMessage(String.format(violationMessage, dupedName));
        validation.setStatus(status);
        validations.add(validation);
    }
}
Also used : Validation(io.atlasmap.v2.Validation) LookupTables(io.atlasmap.v2.LookupTables) LookupTable(io.atlasmap.v2.LookupTable)

Example 17 with LookupTable

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

the class DocumentJavaFieldWriter method write.

public void write(AtlasInternalSession session) throws AtlasException {
    LookupTable lookupTable = session.head().getLookupTable();
    Field sourceField = session.head().getSourceField();
    Field targetField = session.head().getTargetField();
    try {
        if (targetField == null) {
            throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
        }
        String targetFieldClassName = (targetField instanceof JavaField) ? ((JavaField) targetField).getClassName() : ((JavaEnumField) targetField).getClassName();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Now processing field: " + targetField);
            LOG.debug("Field type: " + targetField.getFieldType());
            LOG.debug("Field path: " + targetField.getPath());
            LOG.debug("Field value: " + targetField.getValue());
            LOG.debug("Field className: " + targetFieldClassName);
        }
        processedPaths.add(targetField.getPath());
        AtlasPath path = new AtlasPath(targetField.getPath());
        Object parentObject = rootObject;
        boolean segmentIsComplexSegment = true;
        for (SegmentContext segmentContext : path.getSegmentContexts(true)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing segment: " + segmentContext);
                LOG.debug("Parent object is currently: " + writeDocumentToString(false, parentObject));
            }
            if ("/".equals(segmentContext.getSegmentPath())) {
                if (rootObject == null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Creating root node: " + segmentContext);
                    }
                    rootObject = createParentObject(targetField, parentObject, segmentContext);
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Root node already exists, skipping segment: " + segmentContext);
                    }
                }
                parentObject = rootObject;
                continue;
            }
            // if we're on the last segment, the
            boolean segmentIsLastSegment = (segmentContext.getNext() == null);
            if (segmentIsLastSegment) {
                // detect field type from class name if exists
                if (targetField.getFieldType() == null && targetFieldClassName != null && (targetField instanceof JavaField)) {
                    FieldType fieldTypeFromClass = conversionService.fieldTypeFromClass(targetFieldClassName);
                    targetField.setFieldType(fieldTypeFromClass);
                }
                if (FieldType.COMPLEX.equals(targetField.getFieldType())) {
                    segmentIsComplexSegment = true;
                } else {
                    segmentIsComplexSegment = false;
                }
                if (targetField instanceof JavaEnumField) {
                    segmentIsComplexSegment = false;
                }
            }
            if (LOG.isDebugEnabled()) {
                if (segmentIsComplexSegment) {
                    LOG.debug("Now processing complex segment: " + segmentContext);
                } else if (targetField instanceof JavaEnumField) {
                    LOG.debug("Now processing field enum value segment: " + segmentContext);
                } else {
                    LOG.debug("Now processing field value segment: " + segmentContext);
                }
            }
            if (segmentIsComplexSegment) {
                // processing parent object
                Object childObject = findChildObject(targetField, segmentContext, parentObject);
                if (childObject == null) {
                    childObject = createParentObject(targetField, parentObject, segmentContext);
                }
                parentObject = childObject;
            } else {
                // processing field value
                if (AtlasPath.isCollectionSegment(segmentContext.getSegment())) {
                    parentObject = findOrCreateOrExpandParentCollectionObject(targetField, parentObject, segmentContext);
                }
                Object value = converter.convert(session, lookupTable, sourceField, parentObject, targetField);
                addChildObject(targetField, segmentContext, parentObject, value);
            }
        }
    } catch (Throwable t) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error occured while writing field: " + targetField.getPath(), t);
        }
        if (t instanceof AtlasException) {
            throw (AtlasException) t;
        }
        throw new AtlasException(t);
    }
}
Also used : AtlasException(io.atlasmap.api.AtlasException) FieldType(io.atlasmap.v2.FieldType) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) LookupTable(io.atlasmap.v2.LookupTable) AtlasPath(io.atlasmap.core.AtlasPath)

Example 18 with LookupTable

use of io.atlasmap.v2.LookupTable 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)

Example 19 with LookupTable

use of io.atlasmap.v2.LookupTable 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)

Example 20 with LookupTable

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

the class BaseDocumentWriterTest method reset.

@Before
public void reset() {
    classLoader = Thread.currentThread().getContextClassLoader();
    writer = new DocumentJavaFieldWriter(conversionService);
    writer.setTargetValueConverter(new TargetValueConverter(classLoader, conversionService) {

        public Object convert(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
            return targetField.getValue();
        }
    });
    field = null;
    segmentContexts = new LinkedList<>();
    targetTestClassInstance = new TargetTestClass();
    targetTestClassInstance.setContact(new TargetContact());
    targetTestClassInstance.setAddress(new TargetAddress());
    targetOrderListInstance = new TestListOrders();
    targetOrderListInstance.setOrders(new LinkedList<>());
    targetOrderListInstance.getOrders().add(new TargetOrder());
    targetOrderListInstance.getOrders().add(new TargetOrder());
    targetTestClassInstance.setListOrders(targetOrderListInstance);
    targetOrderArrayInstance = new TargetOrderArray();
    targetOrderArrayInstance.setOrders(new BaseOrder[2]);
    targetOrderArrayInstance.getOrders()[0] = new TargetOrder();
    targetOrderArrayInstance.getOrders()[1] = new TargetOrder();
    targetTestClassInstance.setOrderArray(targetOrderArrayInstance);
}
Also used : AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) TargetContact(io.atlasmap.java.test.TargetContact) TargetAddress(io.atlasmap.java.test.TargetAddress) AtlasException(io.atlasmap.api.AtlasException) TargetOrderArray(io.atlasmap.java.test.TargetOrderArray) Field(io.atlasmap.v2.Field) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) TestListOrders(io.atlasmap.java.test.TestListOrders) LookupTable(io.atlasmap.v2.LookupTable) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetTestClass(io.atlasmap.java.test.TargetTestClass) Before(org.junit.Before)

Aggregations

LookupTable (io.atlasmap.v2.LookupTable)16 Field (io.atlasmap.v2.Field)7 AtlasMapping (io.atlasmap.v2.AtlasMapping)6 LookupEntry (io.atlasmap.v2.LookupEntry)6 LookupTables (io.atlasmap.v2.LookupTables)6 AtlasException (io.atlasmap.api.AtlasException)5 Mapping (io.atlasmap.v2.Mapping)5 AtlasConversionException (io.atlasmap.api.AtlasConversionException)4 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)3 JavaField (io.atlasmap.java.v2.JavaField)3 FieldType (io.atlasmap.v2.FieldType)3 Test (org.junit.Test)3 BaseMapping (io.atlasmap.v2.BaseMapping)2 MockField (io.atlasmap.v2.MockField)2 Validation (io.atlasmap.v2.Validation)2 BaseValidatorTest (io.atlasmap.validators.BaseValidatorTest)2 AtlasFieldActionService (io.atlasmap.api.AtlasFieldActionService)1 AtlasPath (io.atlasmap.core.AtlasPath)1 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)1 TargetAddress (io.atlasmap.java.test.TargetAddress)1