Search in sources :

Example 16 with Fields

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

the class SchemaInspectorTest method doInspectJsonSchemaCalendar.

private void doInspectJsonSchemaCalendar(String instance) throws Exception {
    JsonDocument document = inspectionService.inspectJsonSchema(instance);
    List<Field> fields = document.getFields().getField();
    JsonField f = (JsonField) fields.get(0);
    assertEquals("dtstart", f.getName());
    assertEquals("/dtstart", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(1);
    assertEquals("dtend", f.getName());
    assertEquals("/dtend", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(2);
    assertEquals("summary", f.getName());
    assertEquals("/summary", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(3);
    assertEquals("location", f.getName());
    assertEquals("/location", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(4);
    assertEquals("url", f.getName());
    assertEquals("/url", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(5);
    assertEquals("duration", f.getName());
    assertEquals("/duration", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(6);
    assertEquals("rdate", f.getName());
    assertEquals("/rdate", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(7);
    assertEquals("rrule", f.getName());
    assertEquals("/rrule", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(8);
    assertEquals("category", f.getName());
    assertEquals("/category", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(9);
    assertEquals("description", f.getName());
    assertEquals("/description", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonComplexType) fields.get(10);
    assertEquals("geo", f.getName());
    assertEquals("/geo", f.getPath());
    assertEquals(FieldType.COMPLEX, f.getFieldType());
    List<JsonField> geofields = ((JsonComplexType) f).getJsonFields().getJsonField();
    f = geofields.get(0);
    assertEquals("latitude", f.getName());
    assertEquals("/geo/latitude", f.getPath());
    assertEquals(FieldType.NUMBER, f.getFieldType());
    f = geofields.get(1);
    assertEquals("longitude", f.getName());
    assertEquals("/geo/longitude", f.getPath());
    assertEquals(FieldType.NUMBER, f.getFieldType());
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonField(io.atlasmap.json.v2.JsonField) JsonDocument(io.atlasmap.json.v2.JsonDocument)

Example 17 with Fields

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

the class SchemaInspectorTest method inspectJsonSchemaAddress.

@Test
public void inspectJsonSchemaAddress() throws Exception {
    final String schema = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/address.json")));
    JsonDocument document = inspectionService.inspectJsonSchema(schema);
    List<Field> fields = document.getFields().getField();
    JsonField f = (JsonField) fields.get(0);
    assertEquals("post-office-box", f.getName());
    assertEquals("/post-office-box", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(1);
    assertEquals("extended-address", f.getName());
    assertEquals("/extended-address", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(2);
    assertEquals("street-address", f.getName());
    assertEquals("/street-address", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(3);
    assertEquals("locality", f.getName());
    assertEquals("/locality", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(4);
    assertEquals("region", f.getName());
    assertEquals("/region", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(5);
    assertEquals("postal-code", f.getName());
    assertEquals("/postal-code", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(6);
    assertEquals("country-name", f.getName());
    assertEquals("/country-name", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonField(io.atlasmap.json.v2.JsonField) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.Test)

Example 18 with Fields

use of io.atlasmap.v2.Fields 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 19 with Fields

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

Aggregations

Field (io.atlasmap.v2.Field)10 Test (org.junit.Test)6 JsonDocument (io.atlasmap.json.v2.JsonDocument)5 JsonField (io.atlasmap.json.v2.JsonField)5 Fields (io.atlasmap.v2.Fields)4 XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)3 XmlDocument (io.atlasmap.xml.v2.XmlDocument)3 XmlField (io.atlasmap.xml.v2.XmlField)3 File (java.io.File)3 Record (org.jooq.Record)3 DefaultDataTypeDefinition (org.jooq.util.DefaultDataTypeDefinition)3 Rdb$fields (org.jooq.util.firebird.rdb.tables.Rdb$fields)3 JavaField (io.atlasmap.java.v2.JavaField)2 AtlasMapping (io.atlasmap.v2.AtlasMapping)2 BaseMapping (io.atlasmap.v2.BaseMapping)2 Mapping (io.atlasmap.v2.Mapping)2 XmlFields (io.atlasmap.xml.v2.XmlFields)2 XmlNamespace (io.atlasmap.xml.v2.XmlNamespace)2 ArrayList (java.util.ArrayList)2 ColumnDefinition (org.jooq.util.ColumnDefinition)2