Search in sources :

Example 11 with AtlasModule

use of io.atlasmap.spi.AtlasModule in project atlasmap by atlasmap.

the class AtlasField method write.

/**
 * Wr8ites the field.
 * @param docId Document ID
 * @param path path
 * @throws AtlasException unexpected error
 */
public void write(String docId, String path) throws AtlasException {
    AtlasModule module = session.resolveModule(docId);
    if (module == null) {
        throw new AtlasException(String.format("Target document '%s' doesn't exist", docId));
    }
    if (module.getMode() != AtlasModuleMode.TARGET) {
        throw new AtlasException(String.format("Unable to write to %s Document '%s'", module.getMode(), docId));
    }
    Field f = module.createField();
    f.setDocId(docId);
    f.setPath(path);
    session.head().setSourceField(getRawField());
    session.head().setTargetField(f);
    module.populateTargetField(session);
    module.writeTargetValue(session);
}
Also used : AtlasModule(io.atlasmap.spi.AtlasModule) PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) AtlasException(io.atlasmap.api.AtlasException)

Example 12 with AtlasModule

use of io.atlasmap.spi.AtlasModule in project atlasmap by atlasmap.

the class DefaultAtlasContext method processTargetFieldMapping.

private void processTargetFieldMapping(DefaultAtlasSession session, Mapping mapping) {
    MappingType mappingType = mapping.getMappingType();
    List<Field> sourceFields = mapping.getInputField();
    List<Field> targetFields = mapping.getOutputField();
    AtlasModule module = null;
    Field targetField = null;
    if (mappingType == null || mappingType == MappingType.LOOKUP || mappingType == MappingType.MAP) {
        Field sourceField = session.head().getSourceField();
        FieldGroup sourceFieldGroup = null;
        if (sourceField instanceof FieldGroup) {
            sourceFieldGroup = unwrapNestedGroup((FieldGroup) sourceField);
        }
        for (Field f : targetFields) {
            targetField = f;
            module = resolveModule(FieldDirection.TARGET, targetField);
            if (!auditTargetFieldType(session, module, targetField)) {
                continue;
            }
            session.head().setTargetField(targetField);
            if (sourceFieldGroup != null) {
                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);
                        return;
                    }
                    session.head().setSourceField(sourceFieldGroup);
                } else if (index == null) {
                    if (sourceFieldGroup.getField().size() > 0) {
                        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;
                    }
                }
            }
            try {
                module.populateTargetField(session);
            } catch (Exception e) {
                AtlasUtil.addAudit(session, targetField, "Failed to populate target field: " + e.getMessage(), AuditStatus.ERROR, null);
                if (LOG.isDebugEnabled()) {
                    LOG.error(String.format("populateTargetField() failed for %s:%s", targetField.getDocId(), targetField.getPath()), e);
                }
                return;
            }
            Field processed = applyFieldActions(session, session.head().getTargetField());
            session.head().setTargetField(processed);
            try {
                module.writeTargetValue(session);
            } catch (Exception e) {
                AtlasUtil.addAudit(session, targetField, "Failed to write field value into target document: " + e.getMessage(), AuditStatus.ERROR, null);
                if (LOG.isDebugEnabled()) {
                    LOG.error(String.format("writeTargetValue() failed for %s:%s", targetField.getDocId(), targetField.getPath()), e);
                }
                return;
            }
        }
        return;
    } else if (mappingType == MappingType.COMBINE) {
        targetField = targetFields.get(0);
        module = resolveModule(FieldDirection.TARGET, targetField);
        if (!auditTargetFieldType(session, module, targetField)) {
            return;
        }
        Field sourceField = processCombineField(session, mapping, sourceFields, targetField);
        session.head().setSourceField(sourceField).setTargetField(targetField);
        try {
            module.populateTargetField(session);
        } catch (Exception e) {
            AtlasUtil.addAudit(session, targetField, "Failed to populate target field: " + e.getMessage(), AuditStatus.ERROR, null);
            return;
        }
        applyFieldActions(session, session.head().getTargetField());
        try {
            module.writeTargetValue(session);
        } catch (Exception e) {
            AtlasUtil.addAudit(session, targetField, "Failed to write field value into target document: " + e.getMessage(), AuditStatus.ERROR, null);
            return;
        }
        return;
    } else if (mappingType == MappingType.SEPARATE) {
        List<Field> separatedFields = null;
        try {
            separatedFields = processSeparateField(session, mapping, sourceFields.get(0));
        } catch (Exception e) {
            AtlasUtil.addAudit(session, targetField, "Failed to process separate mode: " + e.getMessage(), AuditStatus.ERROR, null);
            return;
        }
        if (separatedFields == null) {
            return;
        }
        for (Field f : targetFields) {
            targetField = f;
            module = resolveModule(FieldDirection.TARGET, targetField);
            if (!auditTargetFieldType(session, module, targetField)) {
                continue;
            }
            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;
            }
            session.head().setSourceField(separatedFields.get(targetField.getIndex())).setTargetField(targetField);
            try {
                module.populateTargetField(session);
            } catch (Exception e) {
                AtlasUtil.addAudit(session, targetField, "Failed to populate target field: " + e.getMessage(), AuditStatus.ERROR, null);
                return;
            }
            Field processed = applyFieldActions(session, session.head().getTargetField());
            session.head().setTargetField(processed);
            try {
                module.writeTargetValue(session);
            } catch (Exception e) {
                AtlasUtil.addAudit(session, targetField, "Failed to write field value into target document: " + e.getMessage(), AuditStatus.ERROR, null);
                return;
            }
        }
        return;
    }
    AtlasUtil.addAudit(session, (String) null, String.format("Unsupported mappingType=%s detected", mapping.getMappingType()), AuditStatus.ERROR, null);
}
Also used : MappingType(io.atlasmap.v2.MappingType) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) PropertyField(io.atlasmap.v2.PropertyField) ConstantField(io.atlasmap.v2.ConstantField) AtlasModule(io.atlasmap.spi.AtlasModule) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasException(io.atlasmap.api.AtlasException)

Example 13 with AtlasModule

use of io.atlasmap.spi.AtlasModule in project atlasmap by atlasmap.

the class DefaultAtlasContext method processValidation.

@Override
public void processValidation(AtlasSession userSession) throws AtlasException {
    if (!(userSession instanceof DefaultAtlasSession)) {
        throw new AtlasException(String.format("Unsupported session class '%s'", userSession.getClass().getName()));
    }
    if (!this.equals(userSession.getAtlasContext())) {
        throw new AtlasException("Cannot execute AtlasSession created by the other AtlasContext");
    }
    DefaultAtlasSession session = (DefaultAtlasSession) userSession;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Begin processValidation {}", session);
    }
    List<Validation> validations = getContextFactory().getValidationService().validateMapping(session.getMapping());
    if (validations != null && !validations.isEmpty()) {
        session.getValidations().getValidation().addAll(validations);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Detected {} core validation notices", validations == null ? 0 : validations.size());
    }
    for (AtlasModule module : getSourceModules().values()) {
        module.processPreValidation(session);
    }
    for (AtlasModule module : getTargetModules().values()) {
        module.processPreValidation(session);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("End processValidation {}", session);
    }
}
Also used : Validation(io.atlasmap.v2.Validation) AtlasModule(io.atlasmap.spi.AtlasModule) AtlasException(io.atlasmap.api.AtlasException)

Example 14 with AtlasModule

use of io.atlasmap.spi.AtlasModule in project atlasmap by atlasmap.

the class DefaultAtlasContext method resolveModule.

/**
 * Resolves {@link AtlasModule} for the field.
 * @param direction source or target
 * @param field field
 * @return module
 */
protected AtlasModule resolveModule(FieldDirection direction, Field field) {
    String docId = field.getDocId();
    if (docId == null || docId.isEmpty()) {
        docId = direction == FieldDirection.SOURCE ? AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID : AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID;
    }
    Map<String, AtlasModule> modules = direction == FieldDirection.SOURCE ? sourceModules : targetModules;
    if (direction == FieldDirection.SOURCE && field instanceof ConstantField) {
        AtlasModule answer = sourceModules.get(AtlasConstants.CONSTANTS_DOCUMENT_ID);
        if (!modules.containsKey(docId)) {
            modules.put(docId, answer);
        }
        return answer;
    }
    if (field instanceof PropertyField) {
        AtlasModule answer = modules.get(direction == FieldDirection.SOURCE ? AtlasConstants.PROPERTIES_SOURCE_DOCUMENT_ID : AtlasConstants.PROPERTIES_TARGET_DOCUMENT_ID);
        if (!modules.containsKey(docId)) {
            modules.put(docId, answer);
        }
        return answer;
    }
    return modules.get(docId);
}
Also used : AtlasModule(io.atlasmap.spi.AtlasModule) PropertyField(io.atlasmap.v2.PropertyField) ConstantField(io.atlasmap.v2.ConstantField)

Aggregations

AtlasModule (io.atlasmap.spi.AtlasModule)14 PropertyField (io.atlasmap.v2.PropertyField)9 Field (io.atlasmap.v2.Field)8 AtlasException (io.atlasmap.api.AtlasException)7 ConstantField (io.atlasmap.v2.ConstantField)7 SimpleField (io.atlasmap.v2.SimpleField)6 FieldGroup (io.atlasmap.v2.FieldGroup)3 AtlasConversionException (io.atlasmap.api.AtlasConversionException)2 AtlasModuleInfo (io.atlasmap.spi.AtlasModuleInfo)2 AtlasMapping (io.atlasmap.v2.AtlasMapping)2 BaseMapping (io.atlasmap.v2.BaseMapping)2 DataSource (io.atlasmap.v2.DataSource)2 Mapping (io.atlasmap.v2.Mapping)2 MappingType (io.atlasmap.v2.MappingType)2 Validation (io.atlasmap.v2.Validation)2 Expression (io.atlasmap.expression.Expression)1 ExpressionException (io.atlasmap.expression.ExpressionException)1 AtlasModuleInfoRegistry (io.atlasmap.spi.AtlasModuleInfoRegistry)1 Collection (io.atlasmap.v2.Collection)1 CustomMapping (io.atlasmap.v2.CustomMapping)1