Search in sources :

Example 6 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class ClassHelperTest method testParentObjectForPath.

@Test
public void testParentObjectForPath() throws Exception {
    SourceAddress sourceAddress = new SourceAddress();
    SourceOrder sourceOrder = new SourceOrder();
    sourceOrder.setAddress(sourceAddress);
    Object parentObject = ClassHelper.parentObjectForPath(sourceOrder, new AtlasPath("/address/city"), true);
    assertNotNull(parentObject);
    assertTrue(parentObject instanceof SourceAddress);
    assertEquals(sourceAddress, parentObject);
}
Also used : SourceAddress(io.atlasmap.java.test.SourceAddress) SourceOrder(io.atlasmap.java.test.SourceOrder) AtlasPath(io.atlasmap.core.AtlasPath) Test(org.junit.Test)

Example 7 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class JavaWriterUtil method setObjectOnParent.

/**
 * Set the given object within the parentObject.
 *
 * @param field
 *            - provided if we need it, I don't think we will since we already
 *            have the value in hand?
 * @param segmentContext
 *            - current segment for the field's path, this will be the last
 *            segment in the path.
 * @param parentObject
 *            - the object we're setting the value in
 * @param childObject
 *            - the childObject to set
 */
public void setObjectOnParent(Field javaField, SegmentContext segmentContext, Object parentObject, Object childObject) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Setting object for path:'" + javaField.getPath() + "'.\n\tchildObject: " + childObject + "\n\tparentObject: " + parentObject);
    }
    AtlasPath pathUtil = new AtlasPath(javaField.getPath());
    try {
        Class<?> childClass = childObject == null ? null : childObject.getClass();
        Method targetMethod = resolveSetMethod(parentObject, segmentContext, childClass);
        Object targetObject = parentObject;
        // We already know we have a 1 paramter setter here
        if (childObject == null && conversionService.isPrimitive(targetMethod.getParameterTypes()[0])) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Not setting null value for primitive method paramter for path:'" + javaField.getPath() + "'.\n\tchildObject: " + childObject + "\n\tparentObject: " + parentObject);
            }
            return;
        }
        if (targetMethod != null) {
            targetMethod.invoke(targetObject, childObject);
            javaField.setValue(childObject);
        } else {
            try {
                java.lang.reflect.Field field = targetObject.getClass().getField(pathUtil.getLastSegment());
                field.setAccessible(true);
                field.set(targetObject, childObject);
                javaField.setValue(field.get(targetObject));
            } catch (NoSuchFieldException nsfe) {
                throw new AtlasException("Unable to find matting setter method or field for path: " + javaField.getPath() + " on parentObject: " + parentObject.getClass().getName());
            }
        }
    } catch (Exception e) {
        String parentClassName = parentObject == null ? null : parentObject.getClass().getName();
        String childClassName = childObject == null ? null : childObject.getClass().getName();
        throw new AtlasException("Unable to set value for path: " + javaField.getPath() + " parentObject: " + parentClassName + " childObject: " + childClassName, e);
    }
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 8 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class JsonFieldWriter method write.

@Override
public void write(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    if (targetField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'jsonField' cannot be null"));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Field: " + AtlasModelFactory.toString(targetField));
        LOG.debug("Field type=" + targetField.getFieldType() + " path=" + targetField.getPath() + " v=" + targetField.getValue());
    }
    AtlasPath path = new AtlasPath(targetField.getPath());
    String lastSegment = path.getLastSegment();
    ObjectNode parentNode = this.rootNode;
    String parentSegment = null;
    for (String segment : path.getSegments()) {
        if (!segment.equals(lastSegment)) {
            // this is a parent node.
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing parent segment: " + segment);
            }
            JsonNode childNode = getChildNode(parentNode, parentSegment, segment);
            if (childNode == null) {
                childNode = createParentNode(parentNode, parentSegment, segment);
            } else if (childNode instanceof ArrayNode) {
                int index = AtlasPath.indexOfSegment(segment);
                ArrayNode arrayChild = (ArrayNode) childNode;
                if (arrayChild.size() < (index + 1)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Object Array is too small, resizing to accomodate index: " + index + ", current array: " + arrayChild);
                    }
                    // index available
                    while (arrayChild.size() < (index + 1)) {
                        arrayChild.addObject();
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Object Array after resizing: " + arrayChild);
                    }
                }
                childNode = arrayChild.get(index);
            }
            parentNode = (ObjectNode) childNode;
            parentSegment = segment;
        } else {
            // this is the last segment of the path, write the value
            if (targetField.getFieldType() == FieldType.COMPLEX) {
                createParentNode(parentNode, parentSegment, segment);
                return;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing field value segment: " + segment);
            }
            writeValue(parentNode, parentSegment, segment, targetField);
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AtlasException(io.atlasmap.api.AtlasException)

Example 9 with AtlasPath

use of io.atlasmap.core.AtlasPath 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 10 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class TargetValueConverter method resolveTargetSetMethod.

private Method resolveTargetSetMethod(Object sourceObject, Field field, Class<?> targetType) throws AtlasException {
    AtlasPath atlasPath = new AtlasPath(field.getPath());
    Object parentObject = sourceObject;
    List<Class<?>> classTree = resolveMappableClasses(parentObject.getClass());
    if (field instanceof JavaField) {
        JavaField javaField = (JavaField) field;
        for (Class<?> clazz : classTree) {
            try {
                String setterMethodName = javaField.getSetMethod();
                if (setterMethodName == null) {
                    setterMethodName = "set" + capitalizeFirstLetter(atlasPath.getLastSegment());
                }
                return ClassHelper.detectSetterMethod(clazz, setterMethodName, targetType);
            } catch (NoSuchMethodException e) {
            // method does not exist
            }
            // Try the boxUnboxed version
            if (conversionService.isPrimitive(targetType) || conversionService.isBoxedPrimitive(targetType)) {
                try {
                    String setterMethodName = javaField.getSetMethod();
                    if (setterMethodName == null) {
                        setterMethodName = "set" + capitalizeFirstLetter(atlasPath.getLastSegment());
                    }
                    return ClassHelper.detectSetterMethod(clazz, setterMethodName, conversionService.boxOrUnboxPrimitive(targetType));
                } catch (NoSuchMethodException e) {
                // method does not exist
                }
            }
        }
    } else if (field instanceof JavaEnumField) {
        for (Class<?> clazz : classTree) {
            try {
                String setterMethodName = "set" + capitalizeFirstLetter(atlasPath.getLastSegment());
                return ClassHelper.detectSetterMethod(clazz, setterMethodName, targetType);
            } catch (NoSuchMethodException e) {
            // method does not exist
            }
        }
    }
    throw new AtlasException(String.format("Unable to resolve setter for path=%s", field.getPath()));
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) AtlasPath(io.atlasmap.core.AtlasPath) AtlasException(io.atlasmap.api.AtlasException)

Aggregations

AtlasPath (io.atlasmap.core.AtlasPath)15 AtlasException (io.atlasmap.api.AtlasException)8 Method (java.lang.reflect.Method)4 Test (org.junit.Test)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)3 SourceAddress (io.atlasmap.java.test.SourceAddress)3 SourceOrder (io.atlasmap.java.test.SourceOrder)3 JavaField (io.atlasmap.java.v2.JavaField)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)2 Field (io.atlasmap.v2.Field)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 AtlasConversionException (io.atlasmap.api.AtlasConversionException)1 BaseOrder (io.atlasmap.java.test.BaseOrder)1 SourceContact (io.atlasmap.java.test.SourceContact)1 SourceOrderList (io.atlasmap.java.test.SourceOrderList)1