Search in sources :

Example 91 with Field

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);
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) File(java.io.File) Test(org.junit.Test)

Example 92 with Field

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);
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) BaseMapping(io.atlasmap.v2.BaseMapping) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping)

Example 93 with Field

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);
    }
}
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 94 with Field

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;
}
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 95 with Field

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;
    }
}
Also used : LookupEntry(io.atlasmap.v2.LookupEntry) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException)

Aggregations

Field (io.atlasmap.v2.Field)66 Test (org.junit.Test)27 JavaField (io.atlasmap.java.v2.JavaField)26 AtlasMapping (io.atlasmap.v2.AtlasMapping)26 Mapping (io.atlasmap.v2.Mapping)25 BaseMapping (io.atlasmap.v2.BaseMapping)17 SimpleField (io.atlasmap.v2.SimpleField)17 Validation (io.atlasmap.v2.Validation)14 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)13 AtlasException (io.atlasmap.api.AtlasException)12 FieldType (io.atlasmap.v2.FieldType)12 JsonField (io.atlasmap.json.v2.JsonField)10 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)10 XmlField (io.atlasmap.xml.v2.XmlField)9 LookupTable (io.atlasmap.v2.LookupTable)8 ArrayList (java.util.ArrayList)8 AtlasConversionException (io.atlasmap.api.AtlasConversionException)7 ConstantField (io.atlasmap.v2.ConstantField)7 JavaClass (io.atlasmap.java.v2.JavaClass)6 Head (io.atlasmap.spi.AtlasInternalSession.Head)6