Search in sources :

Example 31 with AtlasException

use of io.atlasmap.api.AtlasException 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)

Example 32 with AtlasException

use of io.atlasmap.api.AtlasException 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 33 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class BaseDocumentWriterTest method reset.

@Before
public void reset() {
    classLoader = Thread.currentThread().getContextClassLoader();
    writer = new DocumentJavaFieldWriter(conversionService);
    writer.setTargetValueConverter(new TargetValueConverter(classLoader, conversionService) {

        public Object convert(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
            return targetField.getValue();
        }
    });
    field = null;
    segmentContexts = new LinkedList<>();
    targetTestClassInstance = new TargetTestClass();
    targetTestClassInstance.setContact(new TargetContact());
    targetTestClassInstance.setAddress(new TargetAddress());
    targetOrderListInstance = new TestListOrders();
    targetOrderListInstance.setOrders(new LinkedList<>());
    targetOrderListInstance.getOrders().add(new TargetOrder());
    targetOrderListInstance.getOrders().add(new TargetOrder());
    targetTestClassInstance.setListOrders(targetOrderListInstance);
    targetOrderArrayInstance = new TargetOrderArray();
    targetOrderArrayInstance.setOrders(new BaseOrder[2]);
    targetOrderArrayInstance.getOrders()[0] = new TargetOrder();
    targetOrderArrayInstance.getOrders()[1] = new TargetOrder();
    targetTestClassInstance.setOrderArray(targetOrderArrayInstance);
}
Also used : AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) TargetContact(io.atlasmap.java.test.TargetContact) TargetAddress(io.atlasmap.java.test.TargetAddress) AtlasException(io.atlasmap.api.AtlasException) TargetOrderArray(io.atlasmap.java.test.TargetOrderArray) Field(io.atlasmap.v2.Field) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) TestListOrders(io.atlasmap.java.test.TestListOrders) LookupTable(io.atlasmap.v2.LookupTable) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetTestClass(io.atlasmap.java.test.TargetTestClass) Before(org.junit.Before)

Example 34 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class DocumentJavaFieldWriter method writeDocumentToString.

private String writeDocumentToString(boolean stripSpaces, Object object) throws AtlasException {
    try {
        if (object == null) {
            return "";
        }
        String result = object.toString();
        if (stripSpaces) {
            result = result.replaceAll("\n|\r", "");
            result = result.replaceAll("> *?<", "><");
        }
        return result;
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Aggregations

AtlasException (io.atlasmap.api.AtlasException)34 AtlasPath (io.atlasmap.core.AtlasPath)8 Method (java.lang.reflect.Method)7 AtlasConversionException (io.atlasmap.api.AtlasConversionException)6 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)6 JavaField (io.atlasmap.java.v2.JavaField)6 Field (io.atlasmap.v2.Field)6 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 TargetAddress (io.atlasmap.java.test.TargetAddress)3 TargetOrder (io.atlasmap.java.test.TargetOrder)3 TargetOrderArray (io.atlasmap.java.test.TargetOrderArray)3 TargetTestClass (io.atlasmap.java.test.TargetTestClass)3 TestListOrders (io.atlasmap.java.test.TestListOrders)3 AtlasModule (io.atlasmap.spi.AtlasModule)3 FieldType (io.atlasmap.v2.FieldType)3 LookupTable (io.atlasmap.v2.LookupTable)3 List (java.util.List)3 Element (org.w3c.dom.Element)3 JsonFactory (com.fasterxml.jackson.core.JsonFactory)2