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);
}
}
}
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;
}
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"));
}
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;
}
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);
}
Aggregations