Search in sources :

Example 11 with AtlasException

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

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

the class JavaModule method processPreSourceExecution.

@Override
public void processPreSourceExecution(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());
    }
    Object sourceDocument = atlasSession.getSourceDocument(getDocId());
    if (sourceDocument == null) {
        AtlasUtil.addAudit(atlasSession, getDocId(), String.format("Null source document: docId='%s'", getDocId()), null, AuditStatus.WARN, null);
    } else {
        DocumentJavaFieldReader reader = new DocumentJavaFieldReader();
        reader.setConversionService(getConversionService());
        reader.setDocument(sourceDocument);
        atlasSession.setFieldReader(getDocId(), reader);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("{}: processPreSourceExcution completed", getDocId());
    }
}
Also used : DocumentJavaFieldReader(io.atlasmap.java.core.DocumentJavaFieldReader) ClassInspectionService(io.atlasmap.java.inspect.ClassInspectionService) AtlasException(io.atlasmap.api.AtlasException)

Example 13 with AtlasException

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

the class DocumentJavaFieldReader method read.

@Override
public void read(AtlasInternalSession session) throws AtlasException {
    try {
        Field sourceField = session.head().getSourceField();
        Method getter = null;
        if (sourceField.getFieldType() == null && (sourceField instanceof JavaField || sourceField instanceof JavaEnumField)) {
            getter = resolveGetMethod(sourceDocument, sourceField);
            if (getter == null) {
                AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Unable to auto-detect sourceField type path=%s docId=%s", sourceField.getPath(), sourceField.getDocId()), sourceField.getPath(), AuditStatus.WARN, null);
                return;
            }
            Class<?> returnType = getter.getReturnType();
            sourceField.setFieldType(conversionService.fieldTypeFromClass(returnType));
            if (LOG.isTraceEnabled()) {
                LOG.trace("Auto-detected sourceField type p=" + sourceField.getPath() + " t=" + sourceField.getFieldType());
            }
        }
        populateSourceFieldValue(session, sourceField, sourceDocument, getter);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Processed input field sPath=" + sourceField.getPath() + " sV=" + sourceField.getValue() + " sT=" + sourceField.getFieldType() + " docId: " + sourceField.getDocId());
        }
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 14 with AtlasException

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

the class DocumentJavaFieldWriter method getClassForField.

private Class<?> getClassForField(Field field, SegmentContext segmentContext, Object parentObject, boolean unwrapCollectionType) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Looking up class to use for segment: " + segmentContext + "\n\tparentObject: " + parentObject);
    }
    Class<?> clz = null;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Looking for configured class for field: " + field + ".");
    }
    String className = null;
    if (field instanceof JavaField) {
        className = ((JavaField) field).getClassName();
    } else if (field instanceof JavaEnumField) {
        className = ((JavaEnumField) field).getClassName();
    }
    if (className != null) {
        try {
            clz = className == null ? null : Class.forName(className);
        } catch (Exception e) {
            throw new AtlasException("Could not find class for '" + className + "', for segment: " + segmentContext + ", on field: " + field, e);
        }
    }
    if (clz == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Couldn't find class on field. Looking for configured class for segment: " + segmentContext + ".");
        }
        String normalizedSegment = AtlasPath.removeCollectionIndexes(segmentContext.getSegmentPath());
        clz = this.classesForFields.get(normalizedSegment);
    }
    Type clzType = null;
    if (clz == null) {
        // attempt to determine it from the parent object.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Couldn't find configured class for segment: " + segmentContext + ", looking up getter method.");
        }
        Method m = null;
        try {
            String methodName = "get" + JavaWriterUtil.capitalizeFirstLetter(AtlasPath.cleanPathSegment(segmentContext.getSegment()));
            m = ClassHelper.detectGetterMethod(parentObject.getClass(), methodName);
        } catch (NoSuchMethodException e) {
            // it's ok, we didnt find a getter.
            if (LOG.isDebugEnabled()) {
                LOG.debug("Couldn't find getter method for segment: " + segmentContext, e);
            }
        }
        clz = m == null ? null : m.getReturnType();
        clzType = m.getGenericReturnType();
    }
    if (clz == null) {
        throw new AtlasException("Could not create object, can't find class to instantiate for segment: " + segmentContext);
    }
    if (unwrapCollectionType) {
        clz = unwrapCollectionType(field, segmentContext, parentObject, clz, clzType);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Found class '" + clz.getName() + "' to use for segment: " + segmentContext);
    }
    return clz;
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) FieldType(io.atlasmap.v2.FieldType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JavaField(io.atlasmap.java.v2.JavaField) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 15 with AtlasException

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

the class DocumentJavaFieldWriter method expandCollectionToFitItem.

@SuppressWarnings({ "rawtypes", "unchecked" })
private Object expandCollectionToFitItem(Field field, Object obj, SegmentContext segmentContext, Object parentObject) throws AtlasException {
    Object collectionObject = obj;
    String segment = segmentContext.getSegment();
    if (!collectionHasRoomForIndex(collectionObject, segmentContext)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Collection is not large enough for segment '" + segment + "', expanding the collection.");
        }
        int index = AtlasPath.indexOfSegment(segment);
        if (collectionObject instanceof List) {
            List list = (List) collectionObject;
            while (list.size() < (index + 1)) {
                list.add(null);
            }
        } else if (collectionObject instanceof Map) {
            throw new AtlasException("FIXME: Cannot yet handle adding children to maps");
        } else if (collectionObject.getClass().isArray()) {
            if (Array.getLength(collectionObject) < (index + 1)) {
                // resize the array to fit the item
                Object newArray = createObject(field, segmentContext, parentObject, true);
                // copy pre-existing items over to new array
                for (int i = 0; i < Array.getLength(collectionObject); i++) {
                    Array.set(newArray, i, Array.get(collectionObject, i));
                }
                collectionObject = newArray;
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished expanding collection: " + collectionObject);
        }
    }
    return collectionObject;
}
Also used : List(java.util.List) LinkedList(java.util.LinkedList) AtlasException(io.atlasmap.api.AtlasException) HashMap(java.util.HashMap) Map(java.util.Map)

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