use of io.atlasmap.api.AtlasFieldActionService 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;
}
Aggregations