use of io.atlasmap.v2.Field in project atlasmap by atlasmap.
the class XmlFieldReader method read.
public void read(AtlasInternalSession session) throws AtlasException {
if (document == null) {
throw new AtlasException(new IllegalArgumentException("'document' cannot be null"));
}
Field field = session.head().getSourceField();
if (field == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
}
seedDocumentNamespaces(document);
XmlField xmlField = XmlField.class.cast(field);
if (LOG.isDebugEnabled()) {
LOG.debug("Reading source value for field: " + xmlField.getPath());
}
if (document == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'document' cannot be null"));
}
if (xmlField == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'xmlField' cannot be null"));
}
Element parentNode = document.getDocumentElement();
for (SegmentContext sc : new XmlPath(xmlField.getPath()).getSegmentContexts(false)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing segment: " + sc.getSegment());
LOG.debug("Parent element is currently: " + XmlIOHelper.writeDocumentToString(true, parentNode));
}
if (sc.getPrev() == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping root segment: " + sc);
}
// "/XOA/contact<>/firstName", skip.
continue;
}
if (!XmlPath.isAttributeSegment(sc.getSegment())) {
String childrenElementName = XmlPath.cleanPathSegment(sc.getSegment());
String namespaceAlias = XmlPath.getNamespace(sc.getSegment());
if (namespaceAlias != null && !"".equals(namespaceAlias)) {
childrenElementName = namespaceAlias + ":" + childrenElementName;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Looking for children elements with name: " + childrenElementName);
}
List<Element> children = XmlIOHelper.getChildrenWithName(childrenElementName, parentNode);
if (children == null || children.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, couldn't find children with name '" + childrenElementName + "', for segment: " + sc);
}
return;
}
parentNode = children.get(0);
if (XmlPath.isCollectionSegment(sc.getSegment())) {
int index = XmlPath.indexOfSegment(sc.getSegment());
if (index >= children.size()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, children list can't fit index " + index + ", children list size: " + children.size());
}
return;
}
parentNode = children.get(index);
}
}
if (sc.getNext() == null) {
// last segment.
String value = parentNode.getTextContent();
if (XmlPath.isAttributeSegment(sc.getSegment())) {
String attributeName = XmlPath.getAttribute(sc.getSegment());
value = parentNode.getAttribute(attributeName);
}
if (value == null) {
return;
}
if (xmlField.getFieldType() == null) {
xmlField.setValue(value);
xmlField.setFieldType(FieldType.STRING);
} else {
Object convertedValue;
try {
convertedValue = conversionService.convertType(value, xmlField.getFormat(), xmlField.getFieldType(), null);
xmlField.setValue(convertedValue);
} catch (AtlasConversionException e) {
AtlasUtil.addAudit(session, xmlField.getDocId(), String.format("Failed to convert field value '%s' into type '%s'", value, xmlField.getFieldType()), xmlField.getPath(), AuditStatus.ERROR, value);
}
}
}
}
}
use of io.atlasmap.v2.Field in project atlasmap by atlasmap.
the class JsonModule method processSourceFieldMapping.
@Override
public void processSourceFieldMapping(AtlasInternalSession session) throws AtlasException {
Field sourceField = session.head().getSourceField();
JsonFieldReader reader = session.getFieldReader(getDocId(), JsonFieldReader.class);
if (reader == null) {
AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Source document '%s' doesn't exist", getDocId()), sourceField.getPath(), AuditStatus.ERROR, null);
return;
}
reader.read(session);
if (sourceField.getActions() != null && sourceField.getActions().getActions() != null) {
getFieldActionService().processActions(sourceField.getActions(), sourceField);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{}: processSourceFieldMapping completed: SourceField:[docId={}, path={}, type={}, value={}]", getDocId(), sourceField.getDocId(), sourceField.getPath(), sourceField.getFieldType(), sourceField.getValue());
}
}
use of io.atlasmap.v2.Field in project atlasmap by atlasmap.
the class JsonModule method processTargetFieldMapping.
@Override
public void processTargetFieldMapping(AtlasInternalSession session) throws AtlasException {
Field sourceField = session.head().getSourceField();
Field targetField = session.head().getTargetField();
// Attempt to Auto-detect field type based on input value
if (targetField.getFieldType() == null && sourceField.getValue() != null) {
targetField.setFieldType(getConversionService().fieldTypeFromClass(sourceField.getValue().getClass()));
}
Object targetValue = null;
// Do auto-conversion
if (sourceField.getFieldType() != null && sourceField.getFieldType().equals(targetField.getFieldType())) {
targetValue = sourceField.getValue();
} else if (sourceField.getValue() != null) {
try {
targetValue = getConversionService().convertType(sourceField.getValue(), sourceField.getFormat(), targetField.getFieldType(), targetField.getFormat());
} catch (AtlasConversionException e) {
AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Unable to auto-convert for sT=%s tT=%s tF=%s msg=%s", sourceField.getFieldType(), targetField.getFieldType(), targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, null);
return;
}
}
targetField.setValue(targetValue);
LookupTable lookupTable = session.head().getLookupTable();
if (lookupTable != null) {
processLookupField(session, lookupTable, targetField.getValue(), targetField);
}
if (isAutomaticallyProcessOutputFieldActions() && targetField.getActions() != null && targetField.getActions().getActions() != null) {
getFieldActionService().processActions(targetField.getActions(), targetField);
}
JsonFieldWriter writer = session.getFieldWriter(getDocId(), JsonFieldWriter.class);
writer.write(session);
}
use of io.atlasmap.v2.Field in project atlasmap by atlasmap.
the class XmlFieldWriter method write.
@Override
public void write(AtlasInternalSession session) throws AtlasException {
Field targetField = session.head().getTargetField();
if (targetField == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
}
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing field p={} t={} v={}", targetField.getPath(), targetField.getFieldType(), targetField.getValue());
}
XmlPath path = new XmlPath(targetField.getPath());
String lastSegment = path.getLastSegment();
Element parentNode = null;
String parentSegment = null;
for (String segment : path.getSegments()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing segment: {}", segment);
LOG.debug("Parent element is currently: {}", XmlIOHelper.writeDocumentToString(true, parentNode));
}
if (parentNode == null) {
// processing root node
parentNode = document.getDocumentElement();
String cleanedSegment = XmlPath.cleanPathSegment(segment);
if (parentNode == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Creating root element with name: {}", cleanedSegment);
}
// no root node exists yet, create root node with this segment name;
Element rootNode = createElement(segment);
addNamespacesToElement(rootNode, namespaces);
document.appendChild(rootNode);
parentNode = rootNode;
} else if (!(parentNode.getNodeName().equals(segment))) {
// make sure root element's name matches.
throw new AtlasException(String.format("Root element name '%s' does not match expected name '%s' from path: %s", parentNode.getNodeName(), segment, targetField.getPath()));
}
parentSegment = segment;
} else {
if (LOG.isDebugEnabled()) {
if (segment.equals(lastSegment)) {
LOG.debug("Now processing field value segment: {}", segment);
} else {
LOG.debug("Now processing parent segment: {}", segment);
}
}
if (segment.equals(lastSegment) && targetField.getValue() == null) {
break;
}
if (!XmlPath.isAttributeSegment(segment)) {
// if current segment of path isn't attribute, it refers to a child element,
// find it or create it..
Element childNode = getChildNode(parentNode, parentSegment, segment);
if (childNode == null) {
childNode = createParentNode(parentNode, parentSegment, segment);
}
parentNode = childNode;
parentSegment = segment;
}
if (segment.equals(lastSegment)) {
writeValue(parentNode, segment, targetField);
}
}
}
}
use of io.atlasmap.v2.Field in project atlasmap by atlasmap.
the class SchemaInspectorTest method inspectFlatPrimitiveNoRoot.
@Test
public void inspectFlatPrimitiveNoRoot() throws Exception {
final String instance = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/flatprimitive-base-unrooted.json")));
JsonDocument document = inspectionService.inspectJsonSchema(instance);
assertNotNull(document);
assertEquals(5, document.getFields().getField().size());
List<Field> fields = document.getFields().getField();
JsonField field = (JsonField) fields.get(0);
assertEquals("booleanField", field.getName());
assertEquals("/booleanField", field.getPath());
assertEquals(FieldType.BOOLEAN, field.getFieldType());
assertEquals(FieldStatus.SUPPORTED, field.getStatus());
field = (JsonField) fields.get(1);
assertEquals("stringField", field.getName());
assertEquals("/stringField", field.getPath());
assertEquals(FieldType.STRING, field.getFieldType());
assertEquals(FieldStatus.SUPPORTED, field.getStatus());
field = (JsonField) fields.get(2);
assertEquals("numberField", field.getName());
assertEquals("/numberField", field.getPath());
assertEquals(FieldType.NUMBER, field.getFieldType());
assertEquals(FieldStatus.SUPPORTED, field.getStatus());
field = (JsonField) fields.get(3);
assertEquals("intField", field.getName());
assertEquals("/intField", field.getPath());
assertEquals(FieldType.INTEGER, field.getFieldType());
assertEquals(FieldStatus.SUPPORTED, field.getStatus());
field = (JsonField) fields.get(4);
assertEquals("nullField", field.getName());
assertEquals("/nullField", field.getPath());
assertEquals(FieldType.NONE, field.getFieldType());
assertEquals(FieldStatus.SUPPORTED, field.getStatus());
}
Aggregations