Search in sources :

Example 1 with LookupTable

use of io.atlasmap.v2.LookupTable in project atlasmap by atlasmap.

the class DefaultAtlasContext method init.

/**
 * TODO: For dynamic re-load. This needs lock()
 *
 * @throws AtlasException
 */
protected void init() throws AtlasException {
    registerJmx(this);
    if (this.atlasMappingUri != null) {
        this.mappingDefinition = factory.getMappingService().loadMapping(this.atlasMappingUri, atlasMappingFormat);
    }
    sourceModules.clear();
    ConstantModule constant = new ConstantModule();
    constant.setConversionService(factory.getConversionService());
    constant.setFieldActionService(factory.getFieldActionService());
    sourceModules.put(CONSTANTS_DOCUMENT_ID, constant);
    PropertyModule propSource = new PropertyModule(factory.getPropertyStrategy());
    propSource.setMode(AtlasModuleMode.SOURCE);
    propSource.setConversionService(factory.getConversionService());
    propSource.setFieldActionService(factory.getFieldActionService());
    sourceModules.put(PROPERTIES_DOCUMENT_ID, propSource);
    targetModules.clear();
    lookupTables.clear();
    if (mappingDefinition.getLookupTables() != null && mappingDefinition.getLookupTables().getLookupTable() != null) {
        for (LookupTable table : mappingDefinition.getLookupTables().getLookupTable()) {
            lookupTables.put(table.getName(), table);
        }
    }
    AtlasModuleInfoRegistry moduleInfoRegistry = factory.getModuleInfoRegistry();
    for (DataSource ds : mappingDefinition.getDataSource()) {
        AtlasModuleInfo moduleInfo = moduleInfoRegistry.lookupByUri(ds.getUri());
        if (moduleInfo == null) {
            LOG.error("Cannot find module info for the DataSource uri '{}'", ds.getUri());
            continue;
        }
        if (ds.getDataSourceType() != DataSourceType.SOURCE && ds.getDataSourceType() != DataSourceType.TARGET) {
            LOG.error("Unsupported DataSource type '{}'", ds.getDataSourceType());
            continue;
        }
        String docId = ds.getId();
        if (docId == null || docId.isEmpty()) {
            docId = ds.getDataSourceType() == DataSourceType.SOURCE ? AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID : AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID;
        }
        if (ds.getDataSourceType() == DataSourceType.SOURCE && sourceModules.containsKey(docId)) {
            LOG.error("Duplicated {} DataSource ID '{}' was detected, ignoring...", ds.getDataSourceType(), ds.getId());
            continue;
        }
        if (ds.getDataSourceType() == DataSourceType.TARGET && targetModules.containsKey(docId)) {
            LOG.error("Duplicated {} DataSource ID '{}' was detected, ignoring...", ds.getDataSourceType(), docId);
            continue;
        }
        try {
            AtlasModule module = moduleInfo.getModuleClass().newInstance();
            module.setConversionService(factory.getConversionService());
            module.setFieldActionService(factory.getFieldActionService());
            module.setUri(ds.getUri());
            if (ds.getDataSourceType() == DataSourceType.SOURCE) {
                module.setMode(AtlasModuleMode.SOURCE);
                getSourceModules().put(docId, module);
            } else if (ds.getDataSourceType() == DataSourceType.TARGET) {
                module.setMode(AtlasModuleMode.TARGET);
                getTargetModules().put(docId, module);
            }
            module.setDocId(docId);
            module.init();
        } catch (Throwable t) {
            LOG.error("Unable to initialize {} module: {}", ds.getDataSourceType(), moduleInfo.toString());
            LOG.error(t.getMessage(), t);
            throw new AtlasException(String.format("Unable to initialize %s module: %s", ds.getDataSourceType(), moduleInfo.toString()), t);
        }
    }
}
Also used : AtlasModule(io.atlasmap.spi.AtlasModule) AtlasModuleInfo(io.atlasmap.spi.AtlasModuleInfo) AtlasModuleInfoRegistry(io.atlasmap.spi.AtlasModuleInfoRegistry) LookupTable(io.atlasmap.v2.LookupTable) AtlasException(io.atlasmap.api.AtlasException) DataSource(io.atlasmap.v2.DataSource)

Example 2 with LookupTable

use of io.atlasmap.v2.LookupTable in project atlasmap by atlasmap.

the class LookupTableNameValidator method findDuplicatedName.

private String findDuplicatedName(List<LookupTable> tables) {
    List<String> names = new ArrayList<>();
    for (LookupTable table : tables) {
        names.add(table.getName());
    }
    Set<String> uniqueSet = new HashSet<>(names);
    for (String s : uniqueSet) {
        if (Collections.frequency(names, s) > 1) {
            return s;
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) LookupTable(io.atlasmap.v2.LookupTable) HashSet(java.util.HashSet)

Example 3 with LookupTable

use of io.atlasmap.v2.LookupTable in project atlasmap by atlasmap.

the class DefaultAtlasContextTest method testLookupTable.

@Test
public void testLookupTable() throws Exception {
    LookupTable table = new LookupTable();
    table.setName("table");
    LookupEntry e = new LookupEntry();
    e.setSourceValue("foo");
    e.setTargetValue("bar");
    table.getLookupEntry().add(e);
    context.getLookupTables().put(table.getName(), table);
    Mapping m = (Mapping) AtlasModelFactory.createMapping(MappingType.LOOKUP);
    mapping.getMappings().getMapping().add(m);
    m.setLookupTableName("table");
    populateSourceField(m, FieldType.STRING, "foo");
    prepareTargetField(m, "/target");
    context.process(session);
    Assert.assertFalse(printAudit(session), session.hasErrors());
    Assert.assertEquals("bar", writer.targets.get("/target"));
}
Also used : LookupEntry(io.atlasmap.v2.LookupEntry) LookupTable(io.atlasmap.v2.LookupTable) BaseMapping(io.atlasmap.v2.BaseMapping) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) Test(org.junit.Test)

Example 4 with LookupTable

use of io.atlasmap.v2.LookupTable in project atlasmap by atlasmap.

the class BaseValidatorTest method getAtlasMappingWithLookupTables.

protected AtlasMapping getAtlasMappingWithLookupTables(String... names) {
    AtlasMapping mapping = this.getAtlasMappingFullValid();
    LookupTables lookupTables = new LookupTables();
    mapping.setLookupTables(lookupTables);
    for (String name : names) {
        LookupTable lookupTable = new LookupTable();
        lookupTable.setName(name);
        lookupTable.setDescription("desc_".concat(name));
        lookupTables.getLookupTable().add(lookupTable);
        Mapping lookupFieldMapping = AtlasModelFactory.createMapping(MappingType.LOOKUP);
        lookupFieldMapping.setDescription("field_desc_".concat(name));
        lookupFieldMapping.setLookupTableName(name);
        Field inputField = createInputJavaField("inputName");
        Field outputField = createInputJavaField("outputName");
        lookupFieldMapping.getInputField().add(inputField);
        lookupFieldMapping.getOutputField().add(outputField);
        mapping.getMappings().getMapping().add(lookupFieldMapping);
    }
    return mapping;
}
Also used : MockField(io.atlasmap.v2.MockField) Field(io.atlasmap.v2.Field) AtlasMapping(io.atlasmap.v2.AtlasMapping) LookupTables(io.atlasmap.v2.LookupTables) LookupTable(io.atlasmap.v2.LookupTable) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping)

Example 5 with LookupTable

use of io.atlasmap.v2.LookupTable 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);
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonFieldWriter(io.atlasmap.json.core.JsonFieldWriter) AtlasConversionException(io.atlasmap.api.AtlasConversionException) LookupTable(io.atlasmap.v2.LookupTable)

Aggregations

LookupTable (io.atlasmap.v2.LookupTable)16 Field (io.atlasmap.v2.Field)7 AtlasMapping (io.atlasmap.v2.AtlasMapping)6 LookupEntry (io.atlasmap.v2.LookupEntry)6 LookupTables (io.atlasmap.v2.LookupTables)6 AtlasException (io.atlasmap.api.AtlasException)5 Mapping (io.atlasmap.v2.Mapping)5 AtlasConversionException (io.atlasmap.api.AtlasConversionException)4 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)3 JavaField (io.atlasmap.java.v2.JavaField)3 FieldType (io.atlasmap.v2.FieldType)3 Test (org.junit.Test)3 BaseMapping (io.atlasmap.v2.BaseMapping)2 MockField (io.atlasmap.v2.MockField)2 Validation (io.atlasmap.v2.Validation)2 BaseValidatorTest (io.atlasmap.validators.BaseValidatorTest)2 AtlasFieldActionService (io.atlasmap.api.AtlasFieldActionService)1 AtlasPath (io.atlasmap.core.AtlasPath)1 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)1 TargetAddress (io.atlasmap.java.test.TargetAddress)1