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;
}
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);
}
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());
}
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);
}
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());
}
}
Aggregations