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;
}
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();
}
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;
}
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);
}
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);
}
Aggregations