Search in sources :

Example 16 with AtlasException

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

the class DocumentJavaFieldWriter method addChildObject.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void addChildObject(Field field, SegmentContext segmentContext, Object parentObject, Object childObject) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Adding child object for segment: " + segmentContext + "\n\tparentObject: " + parentObject + "\n\tchild: " + childObject);
    }
    if (this.rootObject == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Setting root object: " + childObject);
        }
        this.rootObject = childObject;
        return;
    }
    boolean parentIsCollection = (parentObject instanceof Collection) || (parentObject.getClass().isArray());
    if (parentIsCollection) {
        String segment = segmentContext.getSegment();
        int index = AtlasPath.indexOfSegment(segment);
        if (parentObject instanceof List) {
            List list = (List) parentObject;
            if (index >= list.size()) {
                throw new AtlasException("Cannot fit item in list, list size: " + list.size() + ", item index: " + index + ", segment: " + segmentContext);
            }
            list.set(index, childObject);
        } else if (parentObject instanceof Map) {
            throw new AtlasException("FIXME: Cannot yet handle adding children to maps");
        } else if (parentObject.getClass().isArray()) {
            if (index >= Array.getLength(parentObject)) {
                throw new AtlasException("Cannot fit item in array, array size: " + Array.getLength(parentObject) + ", item index: " + index + ", segment: " + segmentContext);
            }
            try {
                Array.set(parentObject, index, childObject);
            } catch (Exception e) {
                String parentClass = parentObject == null ? null : parentObject.getClass().getName();
                String childClass = childObject == null ? null : childObject.getClass().getName();
                throw new AtlasException("Could not set child class '" + childClass + "' on parent '" + parentClass + "' for: " + segmentContext, e);
            }
        } else {
            throw new AtlasException("Cannot determine collection type for: " + parentObject);
        }
    } else {
        writerUtil.setObjectOnParent(field, segmentContext, parentObject, childObject);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Finished adding child object for segment: " + segmentContext + "\n\tparentObject: " + parentObject + "\n\t: " + childObject);
    }
}
Also used : Collection(java.util.Collection) List(java.util.List) LinkedList(java.util.LinkedList) AtlasException(io.atlasmap.api.AtlasException) HashMap(java.util.HashMap) Map(java.util.Map) AtlasException(io.atlasmap.api.AtlasException)

Example 17 with AtlasException

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

the class DocumentJavaFieldWriter method unwrapCollectionType.

private Class<?> unwrapCollectionType(Field field, SegmentContext segmentContext, Object parentObject, Class<?> clz, Type clzType) throws AtlasException {
    Class<?> answer = clz;
    if (answer.isArray()) {
        Class<?> oldClass = answer;
        answer = answer.getComponentType();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unwrapped type '" + answer.getName() + "' from wrapper array type '" + oldClass.getName() + "'.");
        }
    } else if (Collection.class.isAssignableFrom(answer)) {
        Class<?> oldClass = answer;
        answer = null;
        String cleanedSegment = AtlasPath.cleanPathSegment(segmentContext.getSegment());
        // From return type of getter method
        if (clzType instanceof Class) {
            // No type parameter, use Object
            answer = Object.class;
        } else if (clzType instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) clzType;
            String typeName = pt.getActualTypeArguments()[0].getTypeName();
            try {
                answer = typeName == null ? null : Class.forName(typeName);
            } catch (Exception e) {
                throw new AtlasException("Could not find class for '" + typeName + "', for segment: " + segmentContext + ", on field: " + field, e);
            }
        // No getter found - check fields of parent object
        } else if (answer == null) {
            Class<?> parentClass = parentObject.getClass();
            while (parentClass != Object.class && answer == null) {
                answer = findClassOfNamedField(parentClass, cleanedSegment);
                parentClass = parentClass.getSuperclass();
            }
        }
        if (answer == null) {
            throw new AtlasException("Could not unwrap list collection's generic type for segment: " + segmentContext);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unwrapped type '" + answer.getName() + "' from wrapper list type '" + oldClass.getName() + "'.");
        }
    }
    return answer;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Collection(java.util.Collection) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 18 with AtlasException

use of io.atlasmap.api.AtlasException 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);
    }
}
Also used : AtlasPath(io.atlasmap.core.AtlasPath) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 19 with AtlasException

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

the class JavaWriterUtil method getObjectFromParent.

/**
 * Retrieve a child object (which may be a complex class or collection class)
 * from the given parentObject.
 *
 * @param field
 *            - provided for convenience, probably not needed here
 * @param ParentObject
 *            - the object to find the child on
 * @param segmentContext
 *            - the segment of the field's path that references the child object
 */
public Object getObjectFromParent(Field field, Object parentObject, SegmentContext segmentContext) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Retrieving child '" + segmentContext.getSegmentPath() + "'.\n\tparentObject: " + parentObject);
    }
    if (parentObject == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Cannot find child '" + segmentContext.getSegmentPath() + "', parent is null.");
        }
        return null;
    }
    // clean up our segment from something like "@addressLine1" to "addressLine1".
    // collection segments like "orders[4]" will be cleaned to "orders"
    String cleanedSegment = AtlasPath.cleanPathSegment(segmentContext.getSegment());
    // FIXME: this doesn't work if there isn't a getter but there is a private
    // member variable
    List<String> getters = ClassHelper.getterMethodNames(cleanedSegment);
    Method getterMethod = null;
    for (String getter : getters) {
        try {
            getterMethod = ClassHelper.detectGetterMethod(parentObject.getClass(), getter);
            break;
        } catch (NoSuchMethodException e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Looking for getter for '" + segmentContext.getSegmentPath() + " on this class: " + parentObject.getClass().getName(), e);
            }
        }
    }
    if (getterMethod == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unable to detect getter method for: " + segmentContext.getSegment() + " from " + segmentContext.getSegmentPath() + " on parent: " + parentObject);
        }
        return null;
    }
    getterMethod.setAccessible(true);
    Object childObject;
    try {
        childObject = getterMethod.invoke(parentObject);
    } catch (Exception e) {
        throw new AtlasException(e);
    }
    if (LOG.isDebugEnabled()) {
        if (childObject == null) {
            LOG.debug("Could not find child object for path: " + segmentContext.getSegmentPath());
        } else {
            LOG.debug("Found child object for path '" + segmentContext.getSegmentPath() + "': " + childObject);
        }
    }
    return childObject;
}
Also used : Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 20 with AtlasException

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

the class JavaModule method processPreTargetExecution.

@Override
public void processPreTargetExecution(AtlasInternalSession atlasSession) throws AtlasException {
    if (atlasSession == null || atlasSession.getMapping() == null || atlasSession.getMapping().getMappings() == null || atlasSession.getMapping().getMappings().getMapping() == null) {
        throw new AtlasException("AtlasSession not properly intialized with a mapping that contains field mappings");
    }
    if (javaInspectionService == null) {
        javaInspectionService = new ClassInspectionService();
        javaInspectionService.setConversionService(getConversionService());
    }
    List<BaseMapping> mapping = atlasSession.getMapping().getMappings().getMapping();
    Object rootObject;
    String targetClassName = AtlasUtil.getUriParameterValue(getUri(), "className");
    JavaClass inspectClass = getJavaInspectionService().inspectClass(targetClassName);
    merge(inspectClass, mapping);
    List<String> targetPaths = AtlasModuleSupport.listTargetPaths(mapping);
    try {
        rootObject = getJavaConstructService().constructClass(inspectClass, targetPaths);
    } catch (Exception e) {
        throw new AtlasException(e);
    }
    DocumentJavaFieldWriter writer = new DocumentJavaFieldWriter(getConversionService());
    writer.setRootObject(rootObject);
    writer.setTargetValueConverter(targetValueConverter);
    atlasSession.setFieldWriter(getDocId(), writer);
    if (LOG.isDebugEnabled()) {
        LOG.debug("{}: processPreTargetExcution completed", getDocId());
    }
}
Also used : JavaClass(io.atlasmap.java.v2.JavaClass) DocumentJavaFieldWriter(io.atlasmap.java.core.DocumentJavaFieldWriter) ClassInspectionService(io.atlasmap.java.inspect.ClassInspectionService) AtlasException(io.atlasmap.api.AtlasException) BaseMapping(io.atlasmap.v2.BaseMapping) AtlasValidationException(io.atlasmap.api.AtlasValidationException) 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