Search in sources :

Example 11 with AtlasPath

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

the class ClassHelper method parentObjectForPath.

public static Object parentObjectForPath(Object targetObject, AtlasPath pathUtil, boolean skipCollectionWrapper) throws AtlasException {
    try {
        if (targetObject == null) {
            return null;
        }
        if (pathUtil == null) {
            return targetObject;
        }
        if (!pathUtil.hasParent() && !pathUtil.hasCollection()) {
            return targetObject;
        }
        Object parentObject = targetObject;
        AtlasPath parentPath = pathUtil.getLastSegmentParentPath();
        if (parentPath == null) {
            parentPath = pathUtil;
        }
        for (String segment : parentPath.getSegments()) {
            List<String> getters = getterMethodNames(AtlasPath.cleanPathSegment(segment));
            Method getterMethod = null;
            for (String getter : getters) {
                try {
                    getterMethod = detectGetterMethod(parentObject.getClass(), getter);
                    break;
                } catch (NoSuchMethodException e) {
                // exhaust options
                }
            }
            if (getterMethod == null) {
                throw new NoSuchMethodException("Unable to detect getter method for " + segment);
            }
            getterMethod.setAccessible(true);
            parentObject = getterMethod.invoke(parentObject);
            if (skipCollectionWrapper) {
                if (AtlasPath.isListSegment(segment) && pathUtil.isIndexedCollection()) {
                    int index = AtlasPath.indexOfSegment(segment);
                    parentObject = ((List<?>) parentObject).get(index);
                } else if (AtlasPath.isArraySegment(segment)) {
                    int index = AtlasPath.indexOfSegment(segment);
                    parentObject = Array.get(parentObject, index);
                }
            }
        }
        return parentObject;
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 12 with AtlasPath

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

the class BaseDocumentWriterTest method setupPath.

public void setupPath(String fieldPath) {
    this.segmentContexts = new AtlasPath(fieldPath).getSegmentContexts(true);
    for (SegmentContext ctx : this.segmentContexts) {
        addClassForFieldPath(ctx.getSegmentPath(), String.class);
    }
    this.lastSegmentContext = segmentContexts.get(segmentContexts.size() - 1);
    this.field = createField(fieldPath, DEFAULT_VALUE);
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasPath(io.atlasmap.core.AtlasPath)

Example 13 with AtlasPath

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

the class DocumentJavaFieldReader method populateSourceFieldValue.

private void populateSourceFieldValue(AtlasInternalSession session, Field field, Object source, Method m) throws Exception {
    Method getter = m;
    Object parentObject = source;
    AtlasPath atlasPath = new AtlasPath(field.getPath());
    Object sourceValue = null;
    if (atlasPath.isRoot()) {
        sourceValue = source;
    } else {
        if (atlasPath.hasParent()) {
            parentObject = ClassHelper.parentObjectForPath(source, atlasPath, true);
        }
        getter = (getter == null) ? resolveGetMethod(source, field) : getter;
        if (getter != null) {
            sourceValue = getter.invoke(parentObject);
        }
    }
    // TODO: support doing parent stuff at field level vs getter
    if (sourceValue == null) {
        String cleanedLastSegment = AtlasPath.cleanPathSegment(atlasPath.getLastSegment());
        sourceValue = getValueFromMemberField(session, parentObject, cleanedLastSegment);
    }
    if (sourceValue != null) {
        sourceValue = extractFromCollection(session, sourceValue, atlasPath);
        if (conversionService.isPrimitive(sourceValue.getClass()) || conversionService.isBoxedPrimitive(sourceValue.getClass())) {
            sourceValue = conversionService.copyPrimitive(sourceValue);
        }
    }
    field.setValue(sourceValue);
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath) Method(java.lang.reflect.Method)

Example 14 with AtlasPath

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

the class ClassHelperTest method testParentObjectForPathParamChecking.

@Test
public void testParentObjectForPathParamChecking() throws Exception {
    assertNull(ClassHelper.parentObjectForPath(null, null, true));
    assertNull(ClassHelper.parentObjectForPath(null, new AtlasPath("foo.bar"), true));
    SourceContact targetObject = new SourceContact();
    Object parentObject = ClassHelper.parentObjectForPath(targetObject, null, true);
    assertNotNull(parentObject);
    assertTrue(parentObject instanceof SourceContact);
    assertEquals(targetObject, parentObject);
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath) SourceContact(io.atlasmap.java.test.SourceContact) Test(org.junit.Test)

Example 15 with AtlasPath

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

the class ClassHelperTest method testParentObjectForPathList.

@Test
public void testParentObjectForPathList() throws Exception {
    SourceOrderList sourceOrderList = new SourceOrderList();
    List<BaseOrder> sourceOrders = new ArrayList<>();
    sourceOrderList.setOrders(sourceOrders);
    SourceAddress sourceAddress = new SourceAddress();
    SourceOrder sourceOrder = new SourceOrder();
    sourceOrder.setAddress(sourceAddress);
    sourceOrderList.getOrders().add(sourceOrder);
    Object parentObject = ClassHelper.parentObjectForPath(sourceOrderList, new AtlasPath("orders<>"), true);
    assertNotNull(parentObject);
    assertTrue(parentObject instanceof List<?>);
    assertEquals(sourceOrders, parentObject);
}
Also used : SourceAddress(io.atlasmap.java.test.SourceAddress) BaseOrder(io.atlasmap.java.test.BaseOrder) ArrayList(java.util.ArrayList) SourceOrder(io.atlasmap.java.test.SourceOrder) AtlasPath(io.atlasmap.core.AtlasPath) SourceOrderList(io.atlasmap.java.test.SourceOrderList) Test(org.junit.Test)

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