use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class DefaultAtlasPreviewContext method processPreview.
/**
* Process single mapping entry in preview mode. Since modules don't participate
* in preview mode, any document format specific function won't be applied.
*
* @param mapping A @link{Mapping} entry to process
*/
@Override
public Audits processPreview(Mapping mapping) throws AtlasException {
DefaultAtlasSession session = new DefaultAtlasSession(this);
this.originalMapping = mapping;
Mapping cloned;
try {
byte[] serialized = jsonMapper.writeValueAsBytes(mapping);
cloned = jsonMapper.readValue(serialized, Mapping.class);
} catch (Exception e) {
throw new AtlasException(e);
}
session.head().setMapping(cloned);
MappingType mappingType = cloned.getMappingType();
String expression = cloned.getExpression();
FieldGroup sourceFieldGroup = cloned.getInputFieldGroup();
List<Field> sourceFields = cloned.getInputField();
List<Field> targetFields = cloned.getOutputField();
targetFields.forEach(tf -> tf.setValue(null));
if ((sourceFieldGroup == null && sourceFields.isEmpty()) || targetFields.isEmpty()) {
return session.getAudits();
}
if (sourceFieldGroup != null) {
sourceFields = sourceFieldGroup.getField();
}
for (Field sf : sourceFields) {
if (sf.getFieldType() == null || sf.getValue() == null) {
continue;
}
if (sf.getValue() instanceof String && ((String) sf.getValue()).isEmpty()) {
continue;
}
if (!restoreSourceFieldType(session, sf)) {
return session.getAudits();
}
}
processSourceFieldMapping(session);
if (session.hasErrors()) {
return session.getAudits();
}
Field sourceField = session.head().getSourceField();
Field targetField;
if (mappingType == null || mappingType == MappingType.MAP) {
sourceFieldGroup = sourceField instanceof FieldGroup ? (FieldGroup) sourceField : null;
for (int i = 0; i < targetFields.size(); i++) {
targetField = targetFields.get(i);
session.head().setTargetField(targetField);
if (sourceFieldGroup != null) {
if (sourceFieldGroup.getField().size() == 0) {
AtlasUtil.addAudit(session, targetField, String.format("Skipping empty source group field '%s:%s'", sourceField.getDocId(), sourceField.getPath()), AuditStatus.INFO, null);
continue;
}
Integer index = targetField.getIndex();
AtlasPath targetPath = new AtlasPath(targetField.getPath());
if (targetPath.hasCollection() && !targetPath.isIndexedCollection()) {
if (targetFields.size() > 1) {
AtlasUtil.addAudit(session, targetField, "It's not yet supported to have a collection field as a part of multiple target fields in a same mapping", AuditStatus.ERROR, null);
session.getAudits().getAudit().addAll(session.head().getAudits());
return session.getAudits();
}
if (index != null) {
LOG.warn("Field index '{}' is detected on target field '{}:{}' while there's only one target field, ignoring", index, targetField.getDocId(), targetField.getPath());
targetField.setIndex(null);
}
FieldGroup targetFieldGroup = targetField instanceof FieldGroup ? (FieldGroup) targetField : AtlasModelFactory.createFieldGroupFrom(targetField, true);
targetFields.set(i, targetFieldGroup);
Field previousTargetField = null;
for (Field subSourceField : sourceFieldGroup.getField()) {
Field subTargetField = AtlasModelFactory.cloneFieldToSimpleField(targetFieldGroup);
targetFieldGroup.getField().add(subTargetField);
collectionHelper.copyCollectionIndexes(sourceFieldGroup, subSourceField, subTargetField, previousTargetField);
previousTargetField = subTargetField;
if (!convertSourceToTarget(session, subSourceField, subTargetField)) {
session.getAudits().getAudit().addAll(session.head().getAudits());
return session.getAudits();
}
;
Field processed = subTargetField;
if (expression == null || expression.isEmpty()) {
processed = applyFieldActions(session, subTargetField);
}
subTargetField.setValue(processed.getValue());
}
continue;
} else if (index == null) {
session.head().setSourceField(sourceFieldGroup.getField().get(sourceFieldGroup.getField().size() - 1));
} else {
if (sourceFieldGroup.getField().size() > index) {
session.head().setSourceField(sourceFieldGroup.getField().get(index));
} else {
AtlasUtil.addAudit(session, targetField, String.format("The number of source fields '%s' is fewer than expected via target field index '%s'", sourceFieldGroup.getField().size(), targetField.getIndex()), AuditStatus.WARN, null);
continue;
}
}
}
if (session.hasErrors()) {
session.getAudits().getAudit().addAll(session.head().getAudits());
return session.getAudits();
}
if (!convertSourceToTarget(session, session.head().getSourceField(), targetField)) {
session.getAudits().getAudit().addAll(session.head().getAudits());
return session.getAudits();
}
Field processed = targetField;
if (expression == null || expression.isEmpty()) {
processed = applyFieldActions(session, targetField);
}
targetField.setValue(processed.getValue());
}
} else if (mappingType == MappingType.COMBINE) {
targetField = targetFields.get(0);
Field combined = processCombineField(session, cloned, sourceFields, targetField);
if (!convertSourceToTarget(session, combined, targetField)) {
session.getAudits().getAudit().addAll(session.head().getAudits());
return session.getAudits();
}
applyFieldActions(session, targetField);
} else if (mappingType == MappingType.SEPARATE) {
List<Field> separatedFields;
try {
separatedFields = processSeparateField(session, cloned, sourceField);
} catch (AtlasException e) {
AtlasUtil.addAudit(session, sourceField, String.format("Failed to separate field: %s", AtlasUtil.getChainedMessage(e)), AuditStatus.ERROR, null);
if (LOG.isDebugEnabled()) {
LOG.error("", e);
}
session.getAudits().getAudit().addAll(session.head().getAudits());
return session.getAudits();
}
if (separatedFields == null) {
session.getAudits().getAudit().addAll(session.head().getAudits());
return session.getAudits();
}
for (Field f : targetFields) {
targetField = f;
if (targetField.getIndex() == null || targetField.getIndex() < 0) {
AtlasUtil.addAudit(session, targetField, String.format("Separate requires zero or positive Index value to be set on targetField targetField.path=%s", targetField.getPath()), AuditStatus.WARN, null);
continue;
}
if (separatedFields.size() <= targetField.getIndex()) {
String errorMessage = String.format("Separate returned fewer segments count=%s when targetField.path=%s requested index=%s", separatedFields.size(), targetField.getPath(), targetField.getIndex());
AtlasUtil.addAudit(session, targetField, errorMessage, AuditStatus.WARN, null);
break;
}
if (!convertSourceToTarget(session, separatedFields.get(targetField.getIndex()), targetField)) {
break;
}
applyFieldActions(session, targetField);
}
} else {
AtlasUtil.addAudit(session, (String) null, String.format("Unsupported mappingType=%s detected", cloned.getMappingType()), AuditStatus.ERROR, null);
}
mapping.getOutputField().clear();
mapping.getOutputField().addAll(cloned.getOutputField());
session.getAudits().getAudit().addAll(session.head().getAudits());
return session.getAudits();
}
use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.
the class StringComplexFieldActions method replaceAll.
/**
* Replaces all hits with the regular expression specified as a parameter.
* @param replaceAll action model
* @param input source
* @return processed
*/
@AtlasActionProcessor
public static String replaceAll(ReplaceAll replaceAll, String input) {
if (replaceAll == null || replaceAll.getMatch() == null || replaceAll.getMatch().isEmpty()) {
throw new IllegalArgumentException("ReplaceAll action must be specified with a non-empty old string");
}
String match = replaceAll.getMatch();
String newString = replaceAll.getNewString();
return input == null ? null : input.replaceAll(match, newString == null ? "" : newString);
}
use of io.atlasmap.v2.Expression in project shifu by ShifuML.
the class JexlTest method testJavaNull.
@Test
public void testJavaNull() {
JexlEngine jexl = new JexlEngine();
String jexlExp = "is_bad_new != null";
String jexlExpEqual = "is_bad_new == null";
Expression e = jexl.createExpression(jexlExp);
Expression exp = jexl.createExpression(jexlExpEqual);
JexlContext jc = new MapContext();
jc.set("is_bad_new", null);
Assert.assertEquals(Boolean.FALSE, e.evaluate(jc));
Assert.assertEquals(Boolean.TRUE, exp.evaluate(jc));
jc.set("is_bad_new", new Object());
Assert.assertEquals(Boolean.TRUE, e.evaluate(jc));
Assert.assertEquals(Boolean.FALSE, exp.evaluate(jc));
}
use of io.atlasmap.v2.Expression in project shifu by ShifuML.
the class JexlTest method testMathMethod.
@Test
public void testMathMethod() {
JexlEngine jexl = new JexlEngine();
String jexlExp = "NumberUtils.max(a, b, c)";
Expression e = jexl.createExpression(jexlExp);
JexlContext jc = new MapContext();
jc.set("NumberUtils", new NumberUtils());
jc.set("a", 7);
jc.set("b", 5);
jc.set("c", 9);
Assert.assertEquals(9, e.evaluate(jc));
}
use of io.atlasmap.v2.Expression in project shifu by ShifuML.
the class JexlTest method testJavaExpressionString.
@Test
public void testJavaExpressionString() {
JexlEngine jexl = new JexlEngine();
String jexlExp = "name == \"user_a\"";
Expression e = jexl.createExpression(jexlExp);
JexlContext jc = new MapContext();
jc.set("name", "user_a");
// Now evaluate the expression, getting the result
Boolean isEqual = (Boolean) e.evaluate(jc);
Assert.assertTrue(isEqual);
jc.set("name", "user_b");
isEqual = (Boolean) e.evaluate(jc);
Assert.assertFalse(isEqual);
}
Aggregations