Search in sources :

Example 21 with Collection

use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.

the class DefaultAtlasContext method extractCollectionMappings.

private List<Mapping> extractCollectionMappings(DefaultAtlasSession session, BaseMapping baseMapping) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Generating Source Mappings from mapping: {}", baseMapping);
    }
    if (!baseMapping.getMappingType().equals(MappingType.COLLECTION)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Mapping is not a collection mapping, not cloning: {}", baseMapping);
        }
        return Arrays.asList((Mapping) baseMapping);
    }
    List<Mapping> mappings = new LinkedList<>();
    for (BaseMapping m : ((Collection) baseMapping).getMappings().getMapping()) {
        Mapping mapping = (Mapping) m;
        Field sourceField = mapping.getInputField().get(0);
        boolean sourceIsCollection = AtlasPath.isCollection(sourceField.getPath());
        if (!sourceIsCollection) {
            // just copy it over
            if (LOG.isDebugEnabled()) {
                LOG.debug("Internal mapping's source field is not a collection, not cloning: {}", mapping);
            }
            // output object to be created for our copied firstName value
            for (Field f : mapping.getOutputField()) {
                f.setPath(AtlasPath.overwriteCollectionIndex(f.getPath(), 0));
            }
            mappings.add(mapping);
            continue;
        }
        AtlasModule module = resolveModule(FieldDirection.SOURCE, sourceField);
        int sourceCollectionSize = module.getCollectionSize(session, sourceField);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Internal mapping's source field is a collection. Cloning it for each item ({} clones): {}", sourceCollectionSize, mapping);
        }
        for (int i = 0; i < sourceCollectionSize; i++) {
            Mapping cloneMapping = (Mapping) AtlasModelFactory.cloneMapping(mapping, false);
            for (Field f : mapping.getInputField()) {
                Field clonedField = module.cloneField(f);
                clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
                cloneMapping.getInputField().add(clonedField);
            }
            for (Field f : mapping.getOutputField()) {
                Field clonedField = module.cloneField(f);
                if (AtlasPath.isCollection(clonedField.getPath())) {
                    clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
                }
                cloneMapping.getOutputField().add(clonedField);
            }
            mappings.add(cloneMapping);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Generated {} mappings from mapping: {}", mappings.size(), baseMapping);
    }
    ((Collection) baseMapping).getMappings().getMapping().clear();
    ((Collection) baseMapping).getMappings().getMapping().addAll(mappings);
    return mappings;
}
Also used : PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) AtlasModule(io.atlasmap.spi.AtlasModule) Collection(io.atlasmap.v2.Collection) BaseMapping(io.atlasmap.v2.BaseMapping) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) LinkedList(java.util.LinkedList) BaseMapping(io.atlasmap.v2.BaseMapping)

Example 22 with Collection

use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.

the class SchemaInspector method printAttributes.

private void printAttributes(XSComplexType xsComplexType, String rootName, XmlComplexType xmlComplexType) {
    Collection<? extends XSAttributeUse> c = xsComplexType.getDeclaredAttributeUses();
    for (XSAttributeUse aC : c) {
        XmlField xmlField = AtlasXmlModelFactory.createXmlField();
        XSAttributeDecl attributeDecl = aC.getDecl();
        xmlField.setName(getNameNS(attributeDecl));
        if (attributeDecl.getDefaultValue() != null) {
            xmlField.setValue(attributeDecl.getDefaultValue().value);
        } else if (attributeDecl.getFixedValue() != null) {
            xmlField.setValue(attributeDecl.getFixedValue().value);
        }
        xmlField.setPath(rootName + "/" + "@" + getNameNS(attributeDecl));
        FieldType attrType = getFieldType(attributeDecl.getType().getName());
        xmlField.setFieldType(attrType);
        if (xmlField.getFieldType() == null) {
            // check the simple types in the schema....
            XSSimpleType simpleType = xsComplexType.getRoot().getSimpleType(xsComplexType.getTargetNamespace(), attributeDecl.getType().getName());
            if (simpleType != null) {
                FieldType fieldType = getFieldType(simpleType.getBaseType().getName());
                xmlField.setFieldType(fieldType);
                xmlField.setTypeName(attributeDecl.getType().getName());
                if (simpleType.asRestriction() != null) {
                    mapRestrictions(xmlField, simpleType.asRestriction());
                }
            } else {
                // cannot figure it out....
                xmlField.setFieldType(FieldType.UNSUPPORTED);
            }
        }
        xmlComplexType.getXmlFields().getXmlField().add(xmlField);
    }
}
Also used : XSSimpleType(com.sun.xml.xsom.XSSimpleType) XmlField(io.atlasmap.xml.v2.XmlField) XSAttributeDecl(com.sun.xml.xsom.XSAttributeDecl) XSAttributeUse(com.sun.xml.xsom.XSAttributeUse) FieldType(io.atlasmap.v2.FieldType)

Example 23 with Collection

use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.

the class DocumentJavaFieldReader method extractFromCollection.

private Object extractFromCollection(AtlasInternalSession session, Object source, AtlasPath atlasPath) {
    if (source == null) {
        return null;
    }
    String lastSegment = atlasPath.getLastSegment();
    if (!AtlasPath.isCollectionSegment(lastSegment) || !atlasPath.isIndexedCollection()) {
        return source;
    }
    Integer index = atlasPath.getCollectionIndex(atlasPath.getLastSegment());
    if (AtlasPath.isArraySegment(lastSegment)) {
        return Array.get(source, index);
    } else if (AtlasPath.isListSegment(lastSegment)) {
        return Collection.class.cast(source).toArray()[index];
    } else if (AtlasPath.isMapSegment(lastSegment)) {
        // TODO support map key
        String key = index.toString();
        return Map.class.cast(source).get(key);
    } else {
        Field sourceField = session.head().getSourceField();
        AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Ignoring unknown collection type in path '%s'", sourceField.getPath()), sourceField.getPath(), AuditStatus.WARN, null);
        return source;
    }
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) Collection(java.util.Collection) Map(java.util.Map)

Example 24 with Collection

use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.

the class JavaFieldWriterUtil method resolveCollectionItemClass.

/**
 * Resolves collection item class.
 * @param parentObject parent
 * @param segmentContext segment
 * @return class
 * @throws AtlasException unexpected error
 */
public Class<?> resolveCollectionItemClass(Object parentObject, SegmentContext segmentContext) throws AtlasException {
    Class<?> itemType = null;
    Method getterMethod = resolveGetterMethod(parentObject.getClass(), segmentContext.getName());
    try {
        Type genericType = null;
        if (getterMethod != null) {
            genericType = getterMethod.getGenericReturnType();
        } else {
            java.lang.reflect.Field field = resolveField(parentObject.getClass(), segmentContext.getName());
            if (field == null) {
                throw new AtlasException(String.format("Failed to create a collection item, parent class={}, field name={}", parentObject.getClass(), segmentContext.getName()));
            }
            genericType = field.getGenericType();
        }
        if (genericType instanceof Class) {
            if (((Class<?>) genericType).isArray()) {
                itemType = ((Class<?>) genericType).getComponentType();
            } else {
                itemType = Object.class;
            }
        } else if (genericType instanceof ParameterizedType && ((ParameterizedType) genericType).getActualTypeArguments().length > 0) {
            String typeArg = ((ParameterizedType) genericType).getActualTypeArguments()[0].getTypeName();
            itemType = classLoader.loadClass(typeArg);
        } else {
            itemType = Object.class;
        }
    } catch (Throwable t) {
        throw new AtlasException(String.format("Failed to resolve collection item class, parent class={}, field name={}", parentObject.getClass(), segmentContext.getName()), t);
    }
    return itemType;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CollectionType(io.atlasmap.v2.CollectionType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException)

Example 25 with Collection

use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.

the class JavaFieldWriter method prepareParentObject.

/**
 * Prepares the parent object.
 * @param session session
 * @return prepared
 * @throws AtlasException unexpected error
 */
public Object prepareParentObject(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    if (targetField == null) {
        throw new AtlasException("Target field cannot be null");
    }
    try {
        AtlasPath path = new AtlasPath(targetField.getPath());
        if (path.isRoot()) {
            return null;
        }
        if (rootObject == null) {
            throw new IllegalArgumentException("A root object must be set before process");
        }
        SegmentContext rootSegment = path.getRootSegment();
        Object parentObject = this.rootObject;
        if (rootSegment.getCollectionType() != CollectionType.NONE) {
            if (collectionItemClass == null) {
                throw new AtlasException(String.format("Collection item class must be specified to handle topmost collection, path=", path.toString()));
            }
            if (rootSegment.getCollectionIndex() == null) {
                // Cannot proceed from collection segment without index
                return null;
            }
            parentObject = writerUtil.getCollectionItem(rootObject, rootSegment);
            if (parentObject == null) {
                this.rootObject = writerUtil.adjustCollectionSize(this.rootObject, rootSegment);
                parentObject = writerUtil.createComplexCollectionItem(this.rootObject, collectionItemClass, rootSegment);
            }
        }
        for (SegmentContext segmentContext : path.getSegments(false)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing segment: " + segmentContext);
                LOG.debug("Parent object is currently: " + writeDocumentToString(false, parentObject));
            }
            if (segmentContext == path.getLastSegment()) {
                return parentObject;
            }
            Object childObject = writerUtil.getChildObject(parentObject, segmentContext);
            if (childObject == null) {
                childObject = writerUtil.createComplexChildObject(parentObject, segmentContext);
            }
            if (segmentContext.getCollectionType() != CollectionType.NONE) {
                if (segmentContext.getCollectionIndex() == null) {
                    // Cannot proceed from collection segment without index
                    return null;
                }
                Object item = writerUtil.getCollectionItem(childObject, segmentContext);
                if (item == null) {
                    Object adjusted = writerUtil.adjustCollectionSize(childObject, segmentContext);
                    if (adjusted != childObject) {
                        writerUtil.setChildObject(parentObject, adjusted, segmentContext);
                    }
                    item = writerUtil.createComplexCollectionItem(parentObject, adjusted, segmentContext);
                }
                childObject = item;
            }
            parentObject = childObject;
        }
        return null;
    } catch (Throwable t) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error occured while preparing parent object for: " + targetField.getPath(), t);
        }
        if (t instanceof AtlasException) {
            throw (AtlasException) t;
        }
        throw new AtlasException(t);
    }
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasPath(io.atlasmap.core.AtlasPath) AtlasException(io.atlasmap.api.AtlasException)

Aggregations

Field (io.atlasmap.v2.Field)25 FieldGroup (io.atlasmap.v2.FieldGroup)15 AtlasPath (io.atlasmap.core.AtlasPath)14 AtlasMapping (io.atlasmap.v2.AtlasMapping)14 AtlasException (io.atlasmap.api.AtlasException)11 ArrayList (java.util.ArrayList)10 Mapping (io.atlasmap.v2.Mapping)9 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)8 Collection (io.atlasmap.v2.Collection)7 SimpleField (io.atlasmap.v2.SimpleField)7 List (java.util.List)7 Test (org.junit.jupiter.api.Test)7 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)6 JavaField (io.atlasmap.java.v2.JavaField)6 LinkedList (java.util.LinkedList)6 AtlasContext (io.atlasmap.api.AtlasContext)5 AtlasSession (io.atlasmap.api.AtlasSession)5 ADMArchiveHandler (io.atlasmap.core.ADMArchiveHandler)5 ConstantField (io.atlasmap.v2.ConstantField)5 InputStream (java.io.InputStream)5