Search in sources :

Example 81 with Field

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

the class DefaultAtlasFieldActionService method loadFieldActions.

protected void loadFieldActions() {
    ClassLoader classLoader = this.getClass().getClassLoader();
    final ServiceLoader<AtlasFieldAction> fieldActionServiceLoader = ServiceLoader.load(AtlasFieldAction.class, classLoader);
    for (final AtlasFieldAction atlasFieldAction : fieldActionServiceLoader) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Loading FieldAction class: " + atlasFieldAction.getClass().getCanonicalName());
        }
        Class<?> clazz = atlasFieldAction.getClass();
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            AtlasFieldActionInfo annotation = method.getAnnotation(AtlasFieldActionInfo.class);
            if (annotation != null) {
                ActionDetail det = new ActionDetail();
                det.setClassName(clazz.getName());
                det.setMethod(method.getName());
                det.setName(annotation.name());
                det.setSourceType(annotation.sourceType());
                det.setTargetType(annotation.targetType());
                det.setSourceCollectionType(annotation.sourceCollectionType());
                det.setTargetCollectionType(annotation.targetCollectionType());
                try {
                    det.setParameters(detectFieldActionParameters("io.atlasmap.v2." + annotation.name()));
                } catch (ClassNotFoundException e) {
                    LOG.error(String.format("Error detecting parameters for field action=%s msg=%s", annotation.name(), e.getMessage()), e);
                }
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Loaded FieldAction: " + det.getName());
                }
                listActionDetails().add(det);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Loaded %s Field Actions", listActionDetails().size()));
    }
}
Also used : ActionDetail(io.atlasmap.v2.ActionDetail) AtlasFieldAction(io.atlasmap.api.AtlasFieldAction) Method(java.lang.reflect.Method) AtlasFieldActionInfo(io.atlasmap.spi.AtlasFieldActionInfo)

Example 82 with Field

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

the class DefaultAtlasFieldActionService method processAction.

protected Object processAction(Action action, ActionDetail actionDetail, Object sourceObject) throws AtlasException {
    Object targetObject = null;
    if (actionDetail != null) {
        Object actionObject = null;
        try {
            Class<?> actionClazz = Class.forName(actionDetail.getClassName());
            actionObject = actionClazz.newInstance();
            Method method = null;
            if (actionDetail.getSourceType() != null) {
                switch(actionDetail.getSourceType()) {
                    case ANY:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Object.class);
                        break;
                    case BIG_INTEGER:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, BigInteger.class);
                        break;
                    case BOOLEAN:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Boolean.class);
                        break;
                    case BYTE:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Byte.class);
                        break;
                    case BYTE_ARRAY:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Byte[].class);
                        break;
                    case CHAR:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Character.class);
                        break;
                    case DATE:
                    case DATE_TIME:
                    case DATE_TZ:
                    case TIME_TZ:
                    case DATE_TIME_TZ:
                    case ANY_DATE:
                        if (sourceObject instanceof Calendar) {
                            sourceObject = DateTimeHelper.toZonedDateTime((Calendar) sourceObject);
                        } else if (sourceObject instanceof Date) {
                            sourceObject = DateTimeHelper.toZonedDateTime((Date) sourceObject, null);
                        } else if (sourceObject instanceof LocalDate) {
                            sourceObject = DateTimeHelper.toZonedDateTime((LocalDate) sourceObject, null);
                        } else if (sourceObject instanceof LocalTime) {
                            sourceObject = DateTimeHelper.toZonedDateTime((LocalTime) sourceObject, null);
                        } else if (sourceObject instanceof LocalDateTime) {
                            sourceObject = DateTimeHelper.toZonedDateTime((LocalDateTime) sourceObject, null);
                        } else if (!(sourceObject instanceof ZonedDateTime)) {
                            LOG.warn(String.format("Unsupported sourceObject type=%s in actionClass=%s", sourceObject.getClass(), actionDetail.getClassName()));
                            break;
                        }
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, ZonedDateTime.class);
                        break;
                    case DECIMAL:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, BigDecimal.class);
                        break;
                    case DOUBLE:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Double.class);
                        break;
                    case FLOAT:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Float.class);
                        break;
                    case INTEGER:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Integer.class);
                        break;
                    case LONG:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Long.class);
                        break;
                    case NUMBER:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Number.class);
                        break;
                    case SHORT:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Short.class);
                        break;
                    case STRING:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, String.class);
                        break;
                    default:
                        LOG.warn(String.format("Unsupported sourceType=%s in actionClass=%s", actionDetail.getSourceType().value(), actionDetail.getClassName()));
                        break;
                }
            }
            if (method == null) {
                throw new AtlasException(String.format("Unable to locate field action className=%s method=%s sourceType=%s", actionDetail.getClassName(), actionDetail.getMethod(), actionDetail.getSourceType()));
            }
            if (Modifier.isStatic(method.getModifiers())) {
                targetObject = method.invoke(null, action, sourceObject);
            } else {
                targetObject = method.invoke(actionObject, action, sourceObject);
            }
        } catch (Throwable e) {
            throw new AtlasException(String.format("Error processing action %s", actionDetail.getName()), e);
        }
        return targetObject;
    }
    return sourceObject;
}
Also used : LocalDateTime(java.time.LocalDateTime) AtlasFieldAction(io.atlasmap.api.AtlasFieldAction) Action(io.atlasmap.v2.Action) LocalTime(java.time.LocalTime) Calendar(java.util.Calendar) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) ZonedDateTime(java.time.ZonedDateTime) BigInteger(java.math.BigInteger)

Example 83 with Field

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

the class JsonValidationServiceTest method testValidateMappingInvalidCombineInputFieldType.

@Test
public void testValidateMappingInvalidCombineInputFieldType() {
    AtlasMapping atlasMapping = getAtlasMappingFullValid();
    Mapping combineFieldMapping = AtlasModelFactory.createMapping(MappingType.COMBINE);
    combineFieldMapping.setId("combine.firstName.lastName");
    JsonField bIJavaField = jsonModelFactory.createJsonField();
    bIJavaField.setFieldType(FieldType.STRING);
    bIJavaField.setValue(Boolean.TRUE);
    bIJavaField.setPath("firstName");
    combineFieldMapping.getInputField().add(bIJavaField);
    JsonField sOJavaField = jsonModelFactory.createJsonField();
    sOJavaField.setFieldType(FieldType.BOOLEAN);
    sOJavaField.setPath("lastName");
    sOJavaField.setIndex(0);
    combineFieldMapping.getOutputField().add(sOJavaField);
    atlasMapping.getMappings().getMapping().add(combineFieldMapping);
    validations.addAll(sourceValidationService.validateMapping(atlasMapping));
    validations.addAll(targetValidationService.validateMapping(atlasMapping));
    assertTrue(validationHelper.hasErrors());
    assertFalse(validationHelper.hasWarnings());
    assertFalse(validationHelper.hasInfos());
    assertEquals(new Integer(1), new Integer(validationHelper.getCount()));
    Validation validation = validations.get(0);
    assertNotNull(validation);
    assertEquals(ValidationScope.MAPPING, validation.getScope());
    assertEquals("combine.firstName.lastName", validation.getId());
    assertEquals("Output field 'lastName' must be of type 'STRING' for a Combine Mapping", validation.getMessage());
    assertEquals(ValidationStatus.ERROR, validation.getStatus());
}
Also used : Validation(io.atlasmap.v2.Validation) JsonField(io.atlasmap.json.v2.JsonField) AtlasMapping(io.atlasmap.v2.AtlasMapping) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) Test(org.junit.Test)

Example 84 with Field

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

the class XmlFieldWriter method convertValue.

private String convertValue(Field field) {
    FieldType type = field.getFieldType();
    Object originalValue = field.getValue();
    String value = originalValue != null ? String.valueOf(originalValue) : null;
    if (LOG.isDebugEnabled()) {
        String valueClass = originalValue == null ? "null" : originalValue.getClass().getName();
        LOG.debug("Converted field value. Type: {}, originalValue: {}({}), to: '{}", type, originalValue, valueClass, value);
    }
    return value;
}
Also used : FieldType(io.atlasmap.v2.FieldType)

Example 85 with Field

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

the class SchemaInspectorTest method inspectJsonSchemaGeo.

// examples from json-schema.org
@Test
public void inspectJsonSchemaGeo() throws Exception {
    final String schema = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/geo.json")));
    JsonDocument document = inspectionService.inspectJsonSchema(schema);
    List<Field> fields = document.getFields().getField();
    JsonField f = (JsonField) fields.get(0);
    assertEquals("latitude", f.getName());
    assertEquals("/latitude", f.getPath());
    assertEquals(FieldType.NUMBER, f.getFieldType());
    f = (JsonField) fields.get(1);
    assertEquals("longitude", f.getName());
    assertEquals("/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) Test(org.junit.Test)

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