Search in sources :

Example 51 with Field

use of com.google.firestore.admin.v1.Field in project atlasmap by atlasmap.

the class JavaFieldReader method getFieldsForPath.

private List<Field> getFieldsForPath(AtlasInternalSession session, Object source, Field field, AtlasPath path, int depth) throws AtlasException {
    List<Field> fields = new ArrayList<>();
    List<SegmentContext> segments = path.getSegments(true);
    if (source == null) {
        return fields;
    }
    if (segments.size() < depth) {
        throw new AtlasException(String.format("depth '%s' exceeds segment size '%s'", depth, segments.size()));
    }
    if (segments.size() == depth) {
        Field newField;
        if (field instanceof FieldGroup && field.getFieldType() == FieldType.COMPLEX) {
            FieldGroup group = (FieldGroup) field;
            populateChildFields(source, group, path);
            newField = group;
        } else {
            newField = AtlasJavaModelFactory.cloneJavaField(field, true);
            if (source != null && (conversionService.isPrimitive(source.getClass()) || conversionService.isBoxedPrimitive(source.getClass()))) {
                source = conversionService.copyPrimitive(source);
            }
            newField.setValue(source);
            // reset index for subfields
            newField.setIndex(null);
        }
        fields.add(newField);
        return fields;
    }
    // segments.size() > depth) {
    SegmentContext segmentContext = segments.get(depth);
    if (depth == 0 && segments.size() > 1 && segmentContext.getCollectionType() == CollectionType.NONE) {
        depth++;
        segmentContext = segments.get(depth);
    }
    JavaChildAccessor childAccessor = getAccessorForSegment(session, source, field, path, segmentContext);
    if (childAccessor == null || childAccessor.getRawValue() == null) {
        return fields;
    }
    if (segmentContext.getCollectionType() == CollectionType.NONE) {
        List<Field> childFields = getFieldsForPath(session, childAccessor.getValue(), field, path, depth + 1);
        fields.addAll(childFields);
        return fields;
    }
    // collection
    if (segmentContext.getCollectionIndex() != null) {
        Object indexItem = childAccessor.getValueAt(segmentContext.getCollectionIndex());
        List<Field> childFields = getFieldsForPath(session, indexItem, field, path, depth + 1);
        fields.addAll(childFields);
    } else {
        List<?> items = childAccessor.getCollectionValues();
        for (int i = 0; i < items.size(); i++) {
            // include the array index within the path
            Field itemField;
            if (field instanceof FieldGroup) {
                itemField = AtlasJavaModelFactory.cloneFieldGroup((FieldGroup) field);
                AtlasPath.setCollectionIndexRecursively((FieldGroup) itemField, depth, i);
            } else {
                itemField = AtlasJavaModelFactory.cloneJavaField(field, false);
                AtlasPath itemPath = new AtlasPath(field.getPath());
                itemPath.setCollectionIndex(depth, i);
                itemField.setPath(itemPath.toString());
            }
            List<Field> arrayFields = getFieldsForPath(session, items.get(i), itemField, new AtlasPath(itemField.getPath()), depth + 1);
            fields.addAll(arrayFields);
        }
    }
    return fields;
}
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) JavaChildAccessor(io.atlasmap.java.core.accessor.JavaChildAccessor) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList) AtlasPath(io.atlasmap.core.AtlasPath) AtlasException(io.atlasmap.api.AtlasException)

Example 52 with Field

use of com.google.firestore.admin.v1.Field in project atlasmap by atlasmap.

the class JavaFieldReader method populateChildFields.

private void populateChildFields(Object source, FieldGroup fieldGroup, AtlasPath path) throws AtlasException {
    List<Field> newChildren = new ArrayList<>();
    for (Field child : fieldGroup.getField()) {
        AtlasPath childPath = new AtlasPath(child.getPath());
        JavaChildAccessor accessor = ClassHelper.lookupAccessor(source, childPath.getLastSegment().getName());
        if (childPath.getLastSegment().getCollectionType() != CollectionType.NONE) {
            FieldGroup childGroup = populateCollectionItems(accessor, child);
            newChildren.add(childGroup);
        } else {
            if (child instanceof FieldGroup) {
                populateChildFields(accessor.getValue(), (FieldGroup) child, childPath);
            } else {
                child.setValue(accessor.getValue());
            }
            newChildren.add(child);
        }
    }
    fieldGroup.getField().clear();
    fieldGroup.getField().addAll(newChildren);
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) JavaChildAccessor(io.atlasmap.java.core.accessor.JavaChildAccessor) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList) AtlasPath(io.atlasmap.core.AtlasPath)

Example 53 with Field

use of com.google.firestore.admin.v1.Field in project atlasmap by atlasmap.

the class JavaFieldReaderTest method testReadComplexListNullItem.

@Test
public void testReadComplexListNullItem() throws Exception {
    SourceCollectionsClass root = new SourceCollectionsClass();
    List<SourceContact> contactList = new ArrayList<>();
    root.setContactList(contactList);
    for (int i = 0; i < 3; i++) {
        if (i == 1) {
            contactList.add(null);
        } else {
            SourceContact c = new SourceContact();
            c.setFirstName("firstName" + i);
            contactList.add(c);
        }
    }
    reader.setDocument(root);
    readGroup("/contactList<>/firstName", FieldType.STRING);
    assertEquals(0, audits.size());
    assertEquals(2, fieldGroup.getField().size());
    Field f = fieldGroup.getField().get(0);
    assertEquals("/contactList<0>/firstName", f.getPath());
    assertEquals("firstName0", f.getValue());
    f = fieldGroup.getField().get(1);
    assertEquals("/contactList<2>/firstName", f.getPath());
    assertEquals("firstName2", f.getValue());
}
Also used : Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) SourceCollectionsClass(io.atlasmap.java.test.SourceCollectionsClass) ArrayList(java.util.ArrayList) SourceContact(io.atlasmap.java.test.SourceContact) Test(org.junit.jupiter.api.Test)

Example 54 with Field

use of com.google.firestore.admin.v1.Field in project atlasmap by atlasmap.

the class CsvFieldWriter method write.

/**
 * Write is not performed until after the whole target document is ready and toCsv is called.
 *
 * @param session {@link AtlasInternalSession}
 * @throws AtlasException unexpected error
 */
@Override
public void write(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    Field sourceField = session.head().getSourceField();
    if (sourceField instanceof FieldGroup) {
        FieldGroup targetFieldGroup = AtlasModelFactory.createFieldGroupFrom(targetField, true);
        for (Field sourceSubField : ((FieldGroup) sourceField).getField()) {
            CsvField targetCsvField = (CsvField) targetField;
            CsvField targetCsvSubField = new CsvField();
            AtlasModelFactory.copyField(targetField, targetCsvSubField, false);
            targetCsvSubField.setColumn(targetCsvField.getColumn());
            targetCsvSubField.setValue(sourceSubField.getValue());
            targetFieldGroup.getField().add(targetCsvSubField);
        }
        targetField = targetFieldGroup;
        session.head().setTargetField(targetFieldGroup);
    } else {
        targetField.setValue(sourceField.getValue());
    }
    document.getFields().getField().add(targetField);
}
Also used : CsvField(io.atlasmap.csv.v2.CsvField) Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField)

Example 55 with Field

use of com.google.firestore.admin.v1.Field in project atlasmap by atlasmap.

the class CsvModule method readSourceValue.

@Override
public void readSourceValue(AtlasInternalSession session) throws AtlasException {
    Field sourceField = session.head().getSourceField();
    CsvFieldReader reader = session.getFieldReader(getDocId(), CsvFieldReader.class);
    if (reader == null) {
        AtlasUtil.addAudit(session, sourceField, String.format("Source document '%s' doesn't exist", getDocId()), AuditStatus.ERROR, null);
        return;
    }
    reader.read(session);
    if (LOG.isDebugEnabled()) {
        LOG.debug("{}: processSourceFieldMapping completed: SourceField:[docId={}, path={}, type={}, value={}]", getDocId(), sourceField.getDocId(), sourceField.getPath(), sourceField.getFieldType(), sourceField.getValue());
    }
}
Also used : CsvField(io.atlasmap.csv.v2.CsvField) Field(io.atlasmap.v2.Field) CsvFieldReader(io.atlasmap.csv.core.CsvFieldReader)

Aggregations

Field (io.atlasmap.v2.Field)221 FieldGroup (io.atlasmap.v2.FieldGroup)86 Test (org.junit.jupiter.api.Test)67 SimpleField (io.atlasmap.v2.SimpleField)62 Field (org.apache.tapestry5.Field)46 JavaField (io.atlasmap.java.v2.JavaField)45 ArrayList (java.util.ArrayList)42 Mapping (io.atlasmap.v2.Mapping)39 Test (org.testng.annotations.Test)39 AtlasPath (io.atlasmap.core.AtlasPath)29 ConstantField (io.atlasmap.v2.ConstantField)29 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)26 AtlasException (io.atlasmap.api.AtlasException)25 JsonField (io.atlasmap.json.v2.JsonField)25 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)25 PropertyField (io.atlasmap.v2.PropertyField)22 AtlasMapping (io.atlasmap.v2.AtlasMapping)21 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)19 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)19 Head (io.atlasmap.spi.AtlasInternalSession.Head)18