Search in sources :

Example 6 with AtlasModule

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

the class DefaultAtlasContextTest method testInit.

// (expected = AtlasException.class)
@Test
public void testInit() throws AtlasException {
    File file = Paths.get("src" + File.separator + "test" + File.separator + "resources" + File.separator + "atlasmapping.json").toFile();
    DefaultAtlasContext ctx = new DefaultAtlasContext(DefaultAtlasContextFactory.getInstance(), file.toURI());
    ctx.init();
    DataSource dataSource = new DataSource();
    dataSource.setUri("URI");
    mapping.getDataSource().add(dataSource);
    dataSource = new DataSource();
    dataSource.setUri(null);
    mapping.getDataSource().add(dataSource);
    dataSource = new DataSource();
    dataSource.setUri("java:source");
    dataSource.setDataSourceType(DataSourceType.SOURCE);
    dataSource.setId("io.atlasmap.core.DefaultAtlasContext.constants.docId");
    mapping.getDataSource().add(dataSource);
    dataSource = new DataSource();
    dataSource.setUri("java:target");
    dataSource.setDataSourceType(DataSourceType.TARGET);
    dataSource.setId("io.atlasmap.core.DefaultAtlasContext.constants.docId");
    mapping.getDataSource().add(dataSource);
    dataSource = new DataSource();
    dataSource.setUri("java:target");
    dataSource.setDataSourceType(DataSourceType.TARGET);
    dataSource.setId("io.atlasmap.core.DefaultAtlasContext.constants.docId");
    mapping.getDataSource().add(dataSource);
    ctx = new DefaultAtlasContext(DefaultAtlasContextFactory.getInstance(), mapping);
    ctx.getTargetModules().put("io.atlasmap.core.DefaultAtlasContext.constants.docId", new ConstantModule());
    ctx.init();
    @SuppressWarnings("unchecked") Map<String, AtlasModule> targetModules = spy(Map.class);
    when(targetModules.put(any(String.class), any(AtlasModule.class))).thenThrow(new RuntimeException("mockException"));
    ctx.setTargetModules(targetModules);
    ctx.init();
}
Also used : AtlasModule(io.atlasmap.spi.AtlasModule) File(java.io.File) DataSource(io.atlasmap.v2.DataSource) Test(org.junit.jupiter.api.Test)

Example 7 with AtlasModule

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

the class DefaultAtlasContext method processSourceFieldMappings.

private void processSourceFieldMappings(DefaultAtlasSession session, List<Field> sourceFields) throws AtlasException {
    for (Field sourceField : sourceFields) {
        session.head().setSourceField(sourceField);
        AtlasModule module = resolveModule(FieldDirection.SOURCE, sourceField);
        if (module == null) {
            AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Module not found for docId '%s'", sourceField.getDocId()), sourceField.getPath(), AuditStatus.ERROR, null);
            return;
        }
        if (!module.isSupportedField(sourceField)) {
            AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Unsupported source field type '%s' for DataSource '%s'", sourceField.getClass().getName(), module.getUri()), sourceField.getPath(), AuditStatus.ERROR, null);
            return;
        }
        module.processSourceFieldMapping(session);
    }
}
Also used : PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) AtlasModule(io.atlasmap.spi.AtlasModule)

Example 8 with AtlasModule

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

the class DefaultAtlasContext method processTargetFieldMappings.

private void processTargetFieldMappings(DefaultAtlasSession session, Mapping mapping) throws AtlasException {
    MappingType mappingType = mapping.getMappingType();
    List<Field> sourceFields = mapping.getInputField();
    List<Field> targetFields = mapping.getOutputField();
    AtlasModule module = null;
    Field targetField = null;
    switch(mappingType) {
        case LOOKUP:
        case MAP:
            targetField = targetFields.get(0);
            module = resolveModule(FieldDirection.TARGET, targetField);
            if (!auditTargetFieldType(session, module, targetField)) {
                return;
            }
            session.head().setTargetField(targetField);
            module.processTargetFieldMapping(session);
            return;
        case 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);
            module.processTargetFieldMapping(session);
            return;
        case SEPARATE:
            Field sourceFieldsep = sourceFields.get(0);
            if ((sourceFieldsep.getFieldType() != null && !FieldType.STRING.equals(sourceFieldsep.getFieldType()) || (sourceFieldsep.getValue() == null || !sourceFieldsep.getValue().getClass().isAssignableFrom(String.class)))) {
                AtlasUtil.addAudit(session, sourceFieldsep.getDocId(), String.format("Separate requires String field type for sourceField.path=%s", sourceFieldsep.getPath()), sourceFieldsep.getPath(), AuditStatus.WARN, null);
                return;
            }
            List<Field> separatedFields = processSeparateField(session, mapping, sourceFields.get(0));
            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.getDocId(), String.format("Separate requires zero or positive Index value to be set on targetField targetField.path=%s", targetField.getPath()), 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.getDocId(), errorMessage, targetField.getPath(), AuditStatus.WARN, null);
                    break;
                }
                session.head().setSourceField(separatedFields.get(targetField.getIndex())).setTargetField(targetField);
                module.processTargetFieldMapping(session);
            }
            return;
        default:
            AtlasUtil.addAudit(session, null, String.format("Unsupported mappingType=%s detected", mapping.getMappingType()), null, AuditStatus.ERROR, null);
    }
}
Also used : MappingType(io.atlasmap.v2.MappingType) PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) AtlasModule(io.atlasmap.spi.AtlasModule)

Example 9 with AtlasModule

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

the class DefaultAtlasContext method extractCollectionMappings.

private List<Mapping> extractCollectionMappings(DefaultAtlasSession session, BaseMapping baseMapping) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Generating Source Mappings from mapping: {}", baseMapping);
    }
    if (!baseMapping.getMappingType().equals(MappingType.COLLECTION)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Mapping is not a collection mapping, not cloning: {}", baseMapping);
        }
        return Arrays.asList((Mapping) baseMapping);
    }
    List<Mapping> mappings = new LinkedList<>();
    for (BaseMapping m : ((Collection) baseMapping).getMappings().getMapping()) {
        Mapping mapping = (Mapping) m;
        Field sourceField = mapping.getInputField().get(0);
        boolean sourceIsCollection = AtlasPath.isCollection(sourceField.getPath());
        if (!sourceIsCollection) {
            // just copy it over
            if (LOG.isDebugEnabled()) {
                LOG.debug("Internal mapping's source field is not a collection, not cloning: {}", mapping);
            }
            // output object to be created for our copied firstName value
            for (Field f : mapping.getOutputField()) {
                f.setPath(AtlasPath.overwriteCollectionIndex(f.getPath(), 0));
            }
            mappings.add(mapping);
            continue;
        }
        AtlasModule module = resolveModule(FieldDirection.SOURCE, sourceField);
        int sourceCollectionSize = module.getCollectionSize(session, sourceField);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Internal mapping's source field is a collection. Cloning it for each item ({} clones): {}", sourceCollectionSize, mapping);
        }
        for (int i = 0; i < sourceCollectionSize; i++) {
            Mapping cloneMapping = (Mapping) AtlasModelFactory.cloneMapping(mapping, false);
            for (Field f : mapping.getInputField()) {
                Field clonedField = module.cloneField(f);
                clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
                cloneMapping.getInputField().add(clonedField);
            }
            for (Field f : mapping.getOutputField()) {
                Field clonedField = module.cloneField(f);
                if (AtlasPath.isCollection(clonedField.getPath())) {
                    clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
                }
                cloneMapping.getOutputField().add(clonedField);
            }
            mappings.add(cloneMapping);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Generated {} mappings from mapping: {}", mappings.size(), baseMapping);
    }
    ((Collection) baseMapping).getMappings().getMapping().clear();
    ((Collection) baseMapping).getMappings().getMapping().addAll(mappings);
    return mappings;
}
Also used : PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) SimpleField(io.atlasmap.v2.SimpleField) ConstantField(io.atlasmap.v2.ConstantField) AtlasModule(io.atlasmap.spi.AtlasModule) Collection(io.atlasmap.v2.Collection) BaseMapping(io.atlasmap.v2.BaseMapping) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) LinkedList(java.util.LinkedList) BaseMapping(io.atlasmap.v2.BaseMapping)

Example 10 with AtlasModule

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

the class AtlasField method read.

/**
 * Reads the field from the source Document.
 * @param docId Document ID
 * @param path field path
 * @return read field
 * @throws AtlasException unexpected error
 */
public AtlasField read(String docId, String path) throws AtlasException {
    AtlasModule module = session.resolveModule(docId);
    if (module == null) {
        throw new AtlasException(String.format("Source document '%s' doesn't exist", docId));
    }
    if (module.getMode() != AtlasModuleMode.SOURCE) {
        throw new AtlasException(String.format("Unable to read from %s Document '%s'", module.getMode(), docId));
    }
    Field sourceField = module.createField();
    sourceField.setDocId(docId);
    sourceField.setPath(path);
    session.head().setSourceField(sourceField);
    module.readSourceValue(session);
    setRawField(sourceField);
    return this;
}
Also used : AtlasModule(io.atlasmap.spi.AtlasModule) PropertyField(io.atlasmap.v2.PropertyField) Field(io.atlasmap.v2.Field) AtlasException(io.atlasmap.api.AtlasException)

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