use of io.atlasmap.v2.Field in project atlasmap by atlasmap.
the class JavaToXMLJSONMarshallingTest method testCombineMappingDemarshaller.
@Test
public void testCombineMappingDemarshaller() throws Exception {
// this test is for AT-466: issue saving mappings in combine mode (parser
// complaining about strategy property)
// the json has been changed from what the UI was sending, now the "actions"
// property on the output field is "null" rather than "[]"
String filename = "src/test/resources/javaToXml/javaToXmlMapping-combine.json";
AtlasMapping uMapping = mapper.readValue(new File(filename), AtlasMapping.class);
assertNotNull(uMapping);
}
use of io.atlasmap.v2.Field in project atlasmap by atlasmap.
the class DefaultAtlasValidationService method validateLookupFieldMapping.
// mapping field validations
private void validateLookupFieldMapping(List<Mapping> fieldMappings, LookupTables lookupTables, List<Validation> validations, Set<String> usedIds) {
Set<String> lookupFieldMappingTableNameRefs = fieldMappings.stream().map(Mapping::getLookupTableName).collect(Collectors.toSet());
Set<String> tableNames = lookupTables.getLookupTable().stream().map(LookupTable::getName).collect(Collectors.toSet());
if (!lookupFieldMappingTableNameRefs.isEmpty() && !tableNames.isEmpty()) {
Set<String> disjoint = Stream.concat(lookupFieldMappingTableNameRefs.stream(), tableNames.stream()).collect(Collectors.toMap(Function.identity(), t -> true, (a, b) -> null)).keySet();
if (!disjoint.isEmpty()) {
boolean isInFieldList = !lookupFieldMappingTableNameRefs.stream().filter(disjoint::contains).collect(Collectors.toList()).isEmpty();
boolean isInTableNameList = !tableNames.stream().filter(disjoint::contains).collect(Collectors.toList()).isEmpty();
// which list has the disjoin.... if its the lookup fields then ERROR
if (isInFieldList) {
Validation validation = new Validation();
validation.setScope(ValidationScope.LOOKUP_TABLE);
validation.setMessage("One ore more LookupFieldMapping references a non existent LookupTable name in the mapping: " + disjoint.toString());
validation.setStatus(ValidationStatus.ERROR);
validations.add(validation);
}
// uses it, else WARN
if (isInTableNameList) {
Validation validation = new Validation();
validation.setScope(ValidationScope.LOOKUP_TABLE);
validation.setMessage("A LookupTable is defined but not used by any LookupField: " + disjoint.toString());
validation.setStatus(ValidationStatus.WARN);
validations.add(validation);
}
}
}
for (Mapping fieldMapping : fieldMappings) {
String mappingId = fieldMapping.getId();
validateMappingId(mappingId, usedIds, validations);
if (fieldMapping.getInputField() != null) {
Validators.MAP_INPUT_FIELD_NOT_EMPTY.get().validate(fieldMapping.getInputField(), validations, mappingId);
}
Validators.MAP_OUTPUT_NOT_NULL.get().validate(fieldMapping.getOutputField(), validations, mappingId, ValidationStatus.WARN);
if (fieldMapping.getOutputField() != null) {
Validators.MAP_OUTPUT_FIELD_NOT_EMPTY.get().validate(fieldMapping.getOutputField(), validations, mappingId, ValidationStatus.WARN);
}
}
}
use of io.atlasmap.v2.Field 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);
}
}
use of io.atlasmap.v2.Field 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;
}
use of io.atlasmap.v2.Field 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;
}
}
Aggregations