Search in sources :

Example 1 with AtlasPath

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

the class JsonModule method getCollectionSize.

@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
    // TODO could this use FieldReader?
    Object document = session.getSourceDocument(getDocId());
    // make this a JSON document
    JsonFactory jsonFactory = new JsonFactory();
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        JsonParser parser = jsonFactory.createParser(document.toString());
        JsonNode rootNode = objectMapper.readTree(parser);
        ObjectNode parentNode = (ObjectNode) rootNode;
        String parentSegment = "[root node]";
        for (SegmentContext sc : new AtlasPath(field.getPath()).getSegmentContexts(false)) {
            JsonNode currentNode = JsonFieldWriter.getChildNode(parentNode, parentSegment, sc.getSegment());
            if (currentNode == null) {
                return 0;
            }
            if (AtlasPath.isCollectionSegment(sc.getSegment())) {
                if (currentNode != null && currentNode.isArray()) {
                    return currentNode.size();
                }
                return 0;
            }
            parentNode = (ObjectNode) currentNode;
        }
    } catch (IOException e) {
        throw new AtlasException(e.getMessage(), e);
    }
    return 0;
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) AtlasException(io.atlasmap.api.AtlasException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 2 with AtlasPath

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

the class JavaModule method getCollectionSize.

@SuppressWarnings("rawtypes")
@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
    // TODO could this use FieldReader?
    Object document = session.getSourceDocument(getDocId());
    Object collectionObject = ClassHelper.parentObjectForPath(document, new AtlasPath(field.getPath()), false);
    if (collectionObject == null) {
        throw new AtlasException(String.format("Cannot find collection on sourceObject %s for path: %s", document.getClass().getName(), field.getPath()));
    }
    if (collectionObject.getClass().isArray()) {
        return Array.getLength(collectionObject);
    }
    return ((List) collectionObject).size();
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath) List(java.util.List) AtlasException(io.atlasmap.api.AtlasException)

Example 3 with AtlasPath

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

the class DocumentJavaFieldReader method resolveGetMethod.

private Method resolveGetMethod(Object sourceObject, Field field) throws AtlasException {
    Object parentObject = sourceObject;
    AtlasPath atlasPath = new AtlasPath(field.getPath());
    Method getter = null;
    if (atlasPath.hasParent()) {
        parentObject = ClassHelper.parentObjectForPath(sourceObject, atlasPath, true);
    }
    if (parentObject == null) {
        return null;
    }
    List<Class<?>> classTree = resolveMappableClasses(parentObject.getClass());
    for (Class<?> clazz : classTree) {
        try {
            if (field instanceof JavaField && ((JavaField) field).getGetMethod() != null) {
                getter = clazz.getMethod(((JavaField) field).getGetMethod());
                getter.setAccessible(true);
                return getter;
            }
        } catch (NoSuchMethodException e) {
        // no getter method specified in mapping file
        }
        for (String m : Arrays.asList("get", "is")) {
            String cleanedLastSegment = AtlasPath.cleanPathSegment(atlasPath.getLastSegment());
            String getterMethod = m + capitalizeFirstLetter(cleanedLastSegment);
            try {
                getter = clazz.getMethod(getterMethod);
                getter.setAccessible(true);
                return getter;
            } catch (NoSuchMethodException e) {
            // method does not exist
            }
        }
    }
    return null;
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) AtlasPath(io.atlasmap.core.AtlasPath) Method(java.lang.reflect.Method)

Example 4 with AtlasPath

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

the class ClassHelperTest method testParentObjectForPathGrandParent.

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

Example 5 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)

Aggregations

AtlasPath (io.atlasmap.core.AtlasPath)49 Field (io.atlasmap.v2.Field)27 FieldGroup (io.atlasmap.v2.FieldGroup)26 AtlasException (io.atlasmap.api.AtlasException)17 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)14 JavaField (io.atlasmap.java.v2.JavaField)12 ArrayList (java.util.ArrayList)12 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)11 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)11 JsonField (io.atlasmap.json.v2.JsonField)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 JsonEnumField (io.atlasmap.json.v2.JsonEnumField)4 Method (java.lang.reflect.Method)4 List (java.util.List)4 Test (org.junit.Test)4 SourceAddress (io.atlasmap.java.test.SourceAddress)3 SourceOrder (io.atlasmap.java.test.SourceOrder)3 KafkaConnectEnumField (io.atlasmap.kafkaconnect.v2.KafkaConnectEnumField)3 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)3 Head (io.atlasmap.spi.AtlasInternalSession.Head)3