Search in sources :

Example 26 with Collection

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

the class JavaFieldWriter method doCommitWriting.

private void doCommitWriting(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    AtlasPath path = new AtlasPath(targetField.getPath());
    Object parentObject = this.pathParentQueue.get(targetField.getPath());
    this.pathParentQueue.remove(targetField.getPath());
    converter.convertTargetValue(session, parentObject, targetField);
    SegmentContext lastSegment = path.getLastSegment();
    if (path.isRoot()) {
        if (lastSegment.getCollectionType() == CollectionType.NONE) {
            this.rootObject = targetField.getValue();
        } else {
            Object adjusted = writerUtil.adjustCollectionSize(this.rootObject, lastSegment);
            if (adjusted != this.rootObject) {
                this.rootObject = adjusted;
            }
            writerUtil.setCollectionItem(adjusted, targetField.getValue(), lastSegment);
        }
        return;
    }
    if (parentObject == null) {
        return;
    }
    String targetClassName = null;
    if (targetField instanceof JavaField) {
        targetClassName = ((JavaField) targetField).getClassName();
    } else if (targetField instanceof JavaEnumField) {
        targetClassName = ((JavaEnumField) targetField).getClassName();
    }
    if (lastSegment.getCollectionType() == CollectionType.NONE) {
        // Don't handle null for JavaEnumField complex type using complex child object
        if (targetField.getFieldType() == FieldType.COMPLEX && !(targetField instanceof JavaEnumField) && targetField.getValue() == null) {
            if (targetClassName != null && !targetClassName.isEmpty()) {
                writerUtil.createComplexChildObject(parentObject, lastSegment, writerUtil.loadClass(targetClassName));
            } else {
                writerUtil.createComplexChildObject(parentObject, lastSegment);
            }
        } else {
            writerUtil.setChildObject(parentObject, targetField.getValue(), lastSegment);
        }
    } else {
        Object collection = writerUtil.getChildObject(parentObject, lastSegment);
        if (collection == null) {
            collection = writerUtil.createComplexChildObject(parentObject, lastSegment);
        }
        if (lastSegment.getCollectionIndex() == null) {
            // Collection field without index - just create collection object and keep it empty
            return;
        }
        Object adjusted = writerUtil.adjustCollectionSize(collection, lastSegment);
        if (adjusted != collection) {
            writerUtil.setChildObject(parentObject, adjusted, lastSegment);
        }
        if (targetField.getFieldType() == FieldType.COMPLEX && targetField.getValue() == null) {
            if (targetClassName != null && !targetClassName.isEmpty()) {
                writerUtil.createComplexChildObject(parentObject, lastSegment, writerUtil.loadClass(targetClassName));
            } else {
                writerUtil.createComplexChildObject(parentObject, lastSegment);
            }
        } else {
            writerUtil.setCollectionItem(adjusted, targetField.getValue(), lastSegment);
        }
    }
}
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) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) AtlasPath(io.atlasmap.core.AtlasPath)

Example 27 with Collection

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

the class BaseMarshallerTest method generateCollectionMapping.

protected AtlasMapping generateCollectionMapping() {
    AtlasMapping innerMapping1 = generateAtlasMapping();
    AtlasMapping innerMapping2 = generateAtlasMapping();
    Collection cMapping = (Collection) AtlasModelFactory.createMapping(MappingType.COLLECTION);
    cMapping.setMappings(new Mappings());
    cMapping.getMappings().getMapping().addAll(innerMapping1.getMappings().getMapping());
    cMapping.getMappings().getMapping().addAll(innerMapping2.getMappings().getMapping());
    cMapping.setCollectionType(CollectionType.LIST);
    cMapping.setCollectionSize(new BigInteger("2"));
    cMapping.setAlias("alias");
    cMapping.setDescription("description");
    AtlasMapping mapping = generateAtlasMapping();
    mapping.getMappings().getMapping().clear();
    mapping.getMappings().getMapping().add(cMapping);
    return mapping;
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) Mappings(io.atlasmap.v2.Mappings) Collection(io.atlasmap.v2.Collection) BigInteger(java.math.BigInteger)

Example 28 with Collection

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

the class DefaultAtlasContextCollectionExpansionTest method shouldNotExponentialyGrowExpandedCollectionMappings.

@Test
public void shouldNotExponentialyGrowExpandedCollectionMappings() throws Exception {
    final AtlasMapping mapping = new AtlasMapping();
    final DefaultAtlasContext context = new DefaultAtlasContext(DefaultAtlasContextFactory.getInstance(), mapping) {

        protected void init() {
        // hijack initialization
        }
    };
    context.setSourceModules(Collections.singletonMap("source", new JsonModule()));
    final DefaultAtlasSession session = (DefaultAtlasSession) context.createSession();
    session.setSourceDocument("source", "{ \"array\": [ { \"property\": 1 }, { \"property\": 2 }, { \"property\": 3 } ] }");
    final Collection baseMapping = new Collection();
    baseMapping.setMappingType(MappingType.COLLECTION);
    final Mappings mappings = new Mappings();
    final Mapping singleMapping = new Mapping();
    singleMapping.setMappingType(MappingType.MAP);
    final JsonField nestedArrayField = new JsonField();
    nestedArrayField.setDocId("source");
    nestedArrayField.setPath("/array<>/property");
    singleMapping.getInputField().add(nestedArrayField);
    mappings.getMapping().add(singleMapping);
    baseMapping.setMappings(mappings);
    Method m = DefaultAtlasContext.class.getDeclaredMethod("unwrapCollectionMappings", new Class[] { DefaultAtlasSession.class, BaseMapping.class });
    m.setAccessible(true);
    assertEquals(1, List.class.cast(m.invoke(context, new Object[] { session, baseMapping })).size());
    assertEquals(1, List.class.cast(m.invoke(context, new Object[] { session, baseMapping })).size());
    assertEquals(1, List.class.cast(m.invoke(context, new Object[] { session, baseMapping })).size());
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) AtlasMapping(io.atlasmap.v2.AtlasMapping) DefaultAtlasContext(io.atlasmap.core.DefaultAtlasContext) Mappings(io.atlasmap.v2.Mappings) Collection(io.atlasmap.v2.Collection) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) BaseMapping(io.atlasmap.v2.BaseMapping) Method(java.lang.reflect.Method) JsonModule(io.atlasmap.json.module.JsonModule) DefaultAtlasSession(io.atlasmap.core.DefaultAtlasSession) Test(org.junit.jupiter.api.Test)

Example 29 with Collection

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

the class NestedCollectionXmlTest method processXmlNestedCollection.

private String processXmlNestedCollection(List<String> mappingsToProcess, boolean assertNoWarnings) throws AtlasException, IOException, URISyntaxException {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mappings/atlasmapping-nested-collection-xml.json");
    ADMArchiveHandler admHandler = new ADMArchiveHandler(Thread.currentThread().getContextClassLoader());
    admHandler.load(AtlasContextFactory.Format.JSON, in);
    AtlasMapping mapping = admHandler.getMappingDefinition();
    mapping.getMappings().getMapping().removeIf(m -> !mappingsToProcess.contains(((Mapping) m).getId()));
    AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
    AtlasSession session = context.createSession();
    String source = new String(Files.readAllBytes(Paths.get(Thread.currentThread().getContextClassLoader().getResource("mappings/document-nested-collection.xml").toURI())));
    session.setSourceDocument("XMLInstanceNestedCollection", source);
    context.process(session);
    assertFalse(session.hasErrors(), TestHelper.printAudit(session));
    if (assertNoWarnings) {
        assertFalse(session.hasWarns(), TestHelper.printAudit(session));
    }
    Object output = session.getTargetDocument("XMLInstanceNestedCollection");
    return (String) output;
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) InputStream(java.io.InputStream) AtlasContext(io.atlasmap.api.AtlasContext) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) AtlasSession(io.atlasmap.api.AtlasSession)

Example 30 with Collection

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

the class NestedCollectionJsonTest method testAsymmetricSingleTarget.

@Test
public void testAsymmetricSingleTarget() throws Exception {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mappings/atlasmapping-nested-collection-asymmetric.json");
    ADMArchiveHandler admHandler = new ADMArchiveHandler(Thread.currentThread().getContextClassLoader());
    admHandler.load(AtlasContextFactory.Format.JSON, in);
    AtlasMapping mapping = admHandler.getMappingDefinition();
    mapping.getMappings().getMapping().removeIf(m -> !"3-1".equals(((Mapping) m).getId()));
    AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
    AtlasSession session = context.createSession();
    String source = new String(Files.readAllBytes(Paths.get(Thread.currentThread().getContextClassLoader().getResource("mappings/document-nested-collection.json").toURI())));
    session.setSourceDocument("JSONInstanceNestedCollection", source);
    context.process(session);
    assertFalse(session.hasErrors(), TestHelper.printAudit(session));
    assertTrue(session.hasWarns(), TestHelper.printAudit(session));
    Object output = session.getTargetDocument("JSONInstanceNestedCollection");
    assertEquals(String.class, output.getClass());
    JsonNode outputJson = mapper.readTree((String) output);
    String prettyPrinted = mapper.writeValueAsString(outputJson);
    ArrayNode firstArray = (ArrayNode) outputJson.get("firstArray");
    assertEquals(10, firstArray.size(), prettyPrinted);
    assertEquals("thirdArrayValue0-0-0", firstArray.get(0).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(0).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue0-0-1", firstArray.get(1).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(1).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue0-1-0", firstArray.get(2).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(2).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue0-1-1", firstArray.get(3).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(3).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue0-1-2", firstArray.get(4).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(4).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-0-0", firstArray.get(5).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(5).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-0-1", firstArray.get(6).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(6).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-0-2", firstArray.get(7).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(7).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-1-0", firstArray.get(8).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(8).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-1-1", firstArray.get(9).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(9).get("secondArray"), prettyPrinted);
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) InputStream(java.io.InputStream) AtlasContext(io.atlasmap.api.AtlasContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AtlasSession(io.atlasmap.api.AtlasSession) Test(org.junit.jupiter.api.Test)

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