Search in sources :

Example 76 with Field

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

the class DefaultAtlasExpressionProcessor method processExpression.

/**
 * Processes the expression.
 * @param session session
 * @param expression expression
 */
public static void processExpression(DefaultAtlasSession session, String expression) {
    if (expression == null || expression.trim().isEmpty()) {
        return;
    }
    try {
        Map<String, Field> sourceFieldMap = new HashMap<>();
        Field parent = session.head().getSourceField();
        if (parent != null && !AtlasUtil.isEmpty(parent.getDocId()) && !AtlasUtil.isEmpty(parent.getPath())) {
            sourceFieldMap.put(parent.getDocId() + ":" + parent.getPath(), parent);
        }
        // Anonymous FieldGroup is just a wrapping, peel it off
        if (parent instanceof FieldGroup && AtlasUtil.isEmpty(parent.getPath())) {
            FieldGroup parentGroup = FieldGroup.class.cast(parent);
            for (Field child : parentGroup.getField()) {
                if (!(AtlasUtil.isEmpty(child.getDocId()) && AtlasUtil.isEmpty(child.getPath()))) {
                    sourceFieldMap.put(child.getDocId() + ":" + child.getPath(), child);
                }
            }
        }
        Expression parsedExpression = Expression.parse(expression, DefaultAtlasFunctionResolver.getInstance());
        Object answer = parsedExpression.evaluate((path) -> {
            if (path == null || path.isEmpty()) {
                return null;
            }
            try {
                Field f = sourceFieldMap.get(path);
                if (f == null) {
                    return null;
                }
                AtlasModule sourceModule;
                Map<String, AtlasModule> sourceModules = session.getAtlasContext().getSourceModules();
                if (f instanceof ConstantField) {
                    sourceModule = sourceModules.get(AtlasConstants.CONSTANTS_DOCUMENT_ID);
                } else if (f instanceof PropertyField) {
                    sourceModule = sourceModules.get(AtlasConstants.PROPERTIES_SOURCE_DOCUMENT_ID);
                } else {
                    String[] splitted = path.split(":", 2);
                    sourceModule = sourceModules.get(splitted[0]);
                }
                if (sourceModule == null) {
                    throw new ExpressionException(String.format("Module for the path '%s' is not found", path));
                }
                session.head().setSourceField(f);
                sourceModule.readSourceValue(session);
                return session.head().getSourceField();
            } catch (Exception e) {
                throw new ExpressionException(e);
            }
        });
        if (answer instanceof Field) {
            session.head().setSourceField((Field) answer);
        } else {
            Field from = session.head().getSourceField();
            SimpleField to = new SimpleField();
            AtlasModelFactory.copyField(from, to, false);
            to.setValue(answer);
            session.head().setSourceField(to);
        }
    } catch (Exception e) {
        AtlasUtil.addAudit(session, expression, String.format("Expression processing error [%s]: %s", expression, e.getMessage()), AuditStatus.ERROR, null);
        if (LOG.isDebugEnabled()) {
            LOG.debug("", e);
        }
    }
}
Also used : HashMap(java.util.HashMap) FieldGroup(io.atlasmap.v2.FieldGroup) ConstantField(io.atlasmap.v2.ConstantField) ExpressionException(io.atlasmap.expression.ExpressionException) ExpressionException(io.atlasmap.expression.ExpressionException) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) AtlasModule(io.atlasmap.spi.AtlasModule) PropertyField(io.atlasmap.v2.PropertyField) Expression(io.atlasmap.expression.Expression) SimpleField(io.atlasmap.v2.SimpleField)

Example 77 with Field

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

the class DefaultAtlasFieldActionService method extractNestedListValuesForExpressionAction.

private void extractNestedListValuesForExpressionAction(Field field, List<Object> fieldValues) {
    if (!(field instanceof FieldGroup)) {
        fieldValues.add(field.getValue());
        return;
    }
    FieldGroup fieldGroup = (FieldGroup) field;
    if (fieldGroup == null || fieldGroup.getField() == null || fieldGroup.getField().isEmpty()) {
        return;
    }
    List<Field> fields = null;
    // passed in to Expression.
    while (fieldGroup.getPath() == null || fieldGroup.getPath().isEmpty()) {
        if (fieldGroup.getField().size() == 1 && (fieldGroup.getField().get(0) instanceof FieldGroup)) {
            fieldGroup = (FieldGroup) fieldGroup.getField().get(0);
        } else {
            fields = fieldGroup.getField();
            break;
        }
    }
    if (fields == null) {
        fields = new LinkedList<>();
        fields.add(fieldGroup);
    }
    doExtractValuesForExpressionAction(fields, fieldValues);
}
Also used : Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) FieldGroup(io.atlasmap.v2.FieldGroup)

Example 78 with Field

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

the class DefaultAtlasFieldActionService method processActionForEachCollectionItem.

private FieldGroup processActionForEachCollectionItem(Action action, ActionProcessor processor, FieldType sourceType, FieldGroup fieldGroup) throws AtlasException {
    for (Field subField : fieldGroup.getField()) {
        if (subField instanceof FieldGroup) {
            processActionForEachCollectionItem(action, processor, sourceType, (FieldGroup) subField);
            continue;
        }
        Object value = subField.getValue();
        if (value != null && isAssignableFieldType(processor.getActionDetail().getSourceType(), sourceType)) {
            value = getConversionService().convertType(value, sourceType, processor.getActionDetail().getSourceType());
        }
        value = processor.process(action, value);
        subField.setValue(value);
    }
    return fieldGroup;
}
Also used : Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) FieldGroup(io.atlasmap.v2.FieldGroup)

Example 79 with Field

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

the class DefaultAtlasFieldActionService method processActions.

@Override
public Field processActions(AtlasInternalSession session, Field field) throws AtlasException {
    ArrayList<Action> actions = field.getActions();
    if (actions == null || actions.isEmpty()) {
        return field;
    }
    Field tmpSourceField = field;
    FieldType currentType = determineFieldType(field);
    for (Action action : actions) {
        ActionProcessor processor = findActionProcessor(action, currentType);
        if (processor == null) {
            AtlasUtil.addAudit(session, field, String.format("Couldn't find metadata for a FieldAction '%s', please make sure it's in the classpath, and also have a service declaration under META-INF/services. Ignoring...", action.getDisplayName()), AuditStatus.WARN, null);
            continue;
        }
        ActionDetail detail = processor.getActionDetail();
        if (detail == null) {
            AtlasUtil.addAudit(session, field, String.format("Couldn't find metadata for a FieldAction '%s', ignoring...", action.getDisplayName()), AuditStatus.WARN, null);
            continue;
        }
        tmpSourceField = processAction(action, processor, currentType, tmpSourceField);
        currentType = determineFieldType(tmpSourceField);
    }
    return tmpSourceField;
}
Also used : Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) CustomAction(io.atlasmap.v2.CustomAction) AtlasFieldAction(io.atlasmap.spi.AtlasFieldAction) Action(io.atlasmap.v2.Action) ActionDetail(io.atlasmap.v2.ActionDetail) ActionProcessor(io.atlasmap.spi.ActionProcessor) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor) FieldType(io.atlasmap.v2.FieldType)

Example 80 with Field

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

the class DefaultAtlasFieldActionService method doExtractValuesForExpressionAction.

private void doExtractValuesForExpressionAction(List<Field> fields, List<Object> values) {
    for (Field subField : fields) {
        Object subValue = null;
        if (subField instanceof FieldGroup) {
            List<Object> subValues = new ArrayList<>();
            doExtractValuesForExpressionAction(((FieldGroup) subField).getField(), subValues);
            subValue = subValues;
        } else {
            subValue = subField.getValue();
        }
        Integer index = subField.getIndex();
        if (index != null) {
            while (index >= values.size()) {
                values.add(null);
            }
            values.set(index, subValue);
        } else {
            values.add(subValue);
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList)

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