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