use of io.atlasmap.java.core.JavaFieldWriter in project atlasmap by atlasmap.
the class JavaModule method populateTargetField.
@Override
public void populateTargetField(AtlasInternalSession session) throws AtlasException {
Field sourceField = session.head().getSourceField();
Field targetField = session.head().getTargetField();
AtlasPath path = new AtlasPath(targetField.getPath());
FieldGroup targetFieldGroup = null;
if (path.hasCollection() && !path.isIndexedCollection()) {
targetFieldGroup = AtlasModelFactory.createFieldGroupFrom(targetField, true);
session.head().setTargetField(targetFieldGroup);
}
JavaFieldWriter writer = session.getFieldWriter(getDocId(), JavaFieldWriter.class);
if (targetFieldGroup == null) {
if (sourceField instanceof FieldGroup) {
List<Field> subFields = ((FieldGroup) sourceField).getField();
if (subFields == null || subFields.size() == 0) {
return;
}
Integer index = targetField.getIndex();
if (index != null) {
if (subFields.size() > index) {
sourceField = subFields.get(index);
} else {
AtlasUtil.addAudit(session, getDocId(), String.format("The number of source fields (%s) is smaller than target index (%s) - ignoring", subFields.size(), index), AuditStatus.WARN, null);
return;
}
} else {
// The last one wins for compatibility
sourceField = subFields.get(subFields.size() - 1);
}
session.head().setSourceField(sourceField);
}
/* Lazy parent instantiation, unless specific mapping defined for a complex type (Example json -> java)
Only instantiate the parent if there is a child value to avoid null src class -> empty dst class mapping
This will ensure null src class maps to null destination class
*/
Object parentObject = null;
if (null != sourceField.getValue() || (null == sourceField.getValue() && targetField.getFieldType() == FieldType.COMPLEX && !(targetField instanceof JavaEnumField))) {
parentObject = writer.prepareParentObject(session);
}
if (parentObject != null) {
writer.populateTargetFieldValue(session, parentObject);
writer.enqueueFieldAndParent(targetField, parentObject);
}
} else if (sourceField instanceof FieldGroup) {
if (((FieldGroup) sourceField).getField().size() == 0) {
Object parentObject = writer.prepareParentObject(session);
if (parentObject != null) {
writer.enqueueFieldAndParent(targetFieldGroup, parentObject);
}
}
Field previousTargetSubField = null;
for (int i = 0; i < ((FieldGroup) sourceField).getField().size(); i++) {
Field sourceSubField = ((FieldGroup) sourceField).getField().get(i);
Field targetSubField = targetField instanceof JavaEnumField ? new JavaEnumField() : new JavaField();
AtlasJavaModelFactory.copyField(targetField, targetSubField, false);
getCollectionHelper().copyCollectionIndexes(sourceField, sourceSubField, targetSubField, previousTargetSubField);
previousTargetSubField = targetSubField;
targetFieldGroup.getField().add(targetSubField);
session.head().setSourceField(sourceSubField);
session.head().setTargetField(targetSubField);
Object parentObject = writer.prepareParentObject(session);
if (parentObject != null) {
writer.populateTargetFieldValue(session, parentObject);
writer.enqueueFieldAndParent(targetSubField, parentObject);
}
}
session.head().setSourceField(sourceField);
session.head().setTargetField(targetFieldGroup);
} else {
Field targetSubField = targetField instanceof JavaEnumField ? new JavaEnumField() : new JavaField();
AtlasJavaModelFactory.copyField(targetField, targetSubField, false);
path.setVacantCollectionIndex(0);
targetSubField.setPath(path.toString());
targetFieldGroup.getField().add(targetSubField);
session.head().setTargetField(targetSubField);
Object parentObject = writer.prepareParentObject(session);
if (parentObject != null) {
writer.populateTargetFieldValue(session, parentObject);
writer.enqueueFieldAndParent(targetSubField, parentObject);
}
session.head().setTargetField(targetFieldGroup);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{}: processTargetFieldMapping completed: SourceField:[docId={}, path={}, type={}, value={}], TargetField:[docId={}, path={}, type={}, value={}]", getDocId(), sourceField.getDocId(), sourceField.getPath(), sourceField.getFieldType(), sourceField.getValue(), targetField.getDocId(), targetField.getPath(), targetField.getFieldType(), targetField.getValue());
}
}
use of io.atlasmap.java.core.JavaFieldWriter in project atlasmap by atlasmap.
the class JavaModule method processPreTargetExecution.
@Override
public void processPreTargetExecution(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");
}
Object rootObject;
String targetClassName = AtlasUtil.unescapeFromUri(AtlasUtil.getUriParameterValue(getUri(), "className"));
String collectionTypeStr = AtlasUtil.unescapeFromUri(AtlasUtil.getUriParameterValue(getUri(), "collectionType"));
CollectionType collectionType = collectionTypeStr != null ? CollectionType.fromValue(collectionTypeStr) : CollectionType.NONE;
String collectionClassName = AtlasUtil.unescapeFromUri(AtlasUtil.getUriParameterValue(getUri(), "collectionClassName"));
JavaFieldWriter writer = new JavaFieldWriter(this.writerUtil);
Class<?> clazz = writerUtil.loadClass(targetClassName);
if (collectionType == CollectionType.ARRAY) {
rootObject = Array.newInstance(clazz, 0);
} else if (collectionType != CollectionType.NONE) {
if (collectionClassName != null) {
rootObject = writerUtil.instantiateObject(writerUtil.loadClass(collectionClassName));
} else {
rootObject = writerUtil.instantiateObject(writerUtil.getDefaultCollectionImplClass(collectionType));
}
writer.setCollectionItemClass(clazz);
} else {
rootObject = writerUtil.instantiateObject(clazz);
}
writer.setRootObject(rootObject);
writer.setTargetValueConverter(targetValueConverter);
atlasSession.setFieldWriter(getDocId(), writer);
if (LOG.isDebugEnabled()) {
LOG.debug("{}: processPreTargetExcution completed", getDocId());
}
}
use of io.atlasmap.java.core.JavaFieldWriter in project atlasmap by atlasmap.
the class JavaModule method processPostTargetExecution.
@Override
public void processPostTargetExecution(AtlasInternalSession session) throws AtlasException {
JavaFieldWriter writer = session.getFieldWriter(getDocId(), JavaFieldWriter.class);
if (writer != null && writer.getRootObject() != null) {
session.setTargetDocument(getDocId(), writer.getRootObject());
} else {
AtlasUtil.addAudit(session, getDocId(), String.format("No target document created for DataSource '%s'", getDocId()), AuditStatus.WARN, null);
}
session.removeFieldWriter(getDocId());
if (LOG.isDebugEnabled()) {
LOG.debug("{}: processPostTargetExecution completed", getDocId());
}
}
use of io.atlasmap.java.core.JavaFieldWriter in project atlasmap by atlasmap.
the class JavaModule method writeTargetValue.
@Override
public void writeTargetValue(AtlasInternalSession session) throws AtlasException {
JavaFieldWriter writer = session.getFieldWriter(getDocId(), JavaFieldWriter.class);
writer.commitWriting(session);
}
Aggregations