use of org.jumpmind.symmetric.io.data.transform.TransformColumn in project symmetric-ds by JumpMind.
the class JavaColumnTransformTest method testSimple.
@Test
public void testSimple() throws Exception {
String javaCode = "return \"transValue\";";
TransformColumn column = new TransformColumn("sColumn", "tColumn", false, "java", javaCode);
TransformTable table = new TransformTable("sTable", "tTable", TransformPoint.LOAD, column);
Map<String, String> sourceKeyValues = new HashMap<String, String>();
Map<String, String> sourceValues = new HashMap<String, String>();
sourceValues.put("sColumn", "aNewValue");
Map<String, String> oldSourceValues = new HashMap<String, String>();
oldSourceValues.put("sColumn", "anOldValue");
TransformedData data = new TransformedData(table, DataEventType.INSERT, sourceKeyValues, oldSourceValues, sourceValues);
JavaColumnTransform transform = new JavaColumnTransform(extensionService);
String out = transform.transform(platform, context, column, data, sourceValues, "aNewValue", "anOldValue");
assertEquals("transValue", out);
}
use of org.jumpmind.symmetric.io.data.transform.TransformColumn in project symmetric-ds by JumpMind.
the class TransformWriter method perform.
protected boolean perform(DataContext context, TransformedData data, TransformTable transformation, Map<String, String> sourceValues, Map<String, String> oldSourceValues) throws IgnoreRowException {
boolean persistData = false;
try {
DataEventType eventType = data.getSourceDmlType();
for (TransformColumn transformColumn : transformation.getTransformColumns()) {
if (!transformColumn.isPk()) {
IncludeOnType includeOn = transformColumn.getIncludeOn();
if (includeOn == IncludeOnType.ALL || (includeOn == IncludeOnType.INSERT && eventType == DataEventType.INSERT) || (includeOn == IncludeOnType.UPDATE && eventType == DataEventType.UPDATE) || (includeOn == IncludeOnType.DELETE && eventType == DataEventType.DELETE)) {
if (StringUtils.isBlank(transformColumn.getSourceColumnName()) || sourceValues.containsKey(transformColumn.getSourceColumnName())) {
try {
Object value = transformColumn(context, data, transformColumn, sourceValues, oldSourceValues);
if (value instanceof NewAndOldValue) {
data.put(transformColumn, ((NewAndOldValue) value).getNewValue(), oldSourceValues != null ? ((NewAndOldValue) value).getOldValue() : null, false);
} else if (value == null || value instanceof String) {
data.put(transformColumn, (String) value, null, false);
} else if (value instanceof List) {
throw new IllegalStateException(String.format("Column transform failed %s.%s. Transforms that multiply rows must be marked as part of the primary key", transformColumn.getTransformId(), transformColumn.getTargetColumnName()));
} else {
throw new IllegalStateException(String.format("Column transform failed %s.%s. It returned an unexpected type of %s", transformColumn.getTransformId(), transformColumn.getTargetColumnName(), value.getClass().getSimpleName()));
}
} catch (IgnoreColumnException e) {
// Do nothing. We are ignoring the column
if (log.isDebugEnabled()) {
log.debug("A transform indicated we should ignore the target column {}", transformColumn.getTargetColumnName());
}
}
} else {
if (eventType != DataEventType.DELETE) {
log.warn("Could not find a source column of {} for the transformation: {}", transformColumn.getSourceColumnName(), transformation.getTransformId());
} else {
log.debug("Could not find a source column of {} for the transformation: {}. This is probably because this was a DELETE event and no old data was captured.", transformColumn.getSourceColumnName(), transformation.getTransformId());
}
}
}
}
}
// transformation
if (data.getColumnNames().length > 0) {
TargetDmlAction targetAction = null;
switch(data.getTargetDmlType()) {
case INSERT:
targetAction = TargetDmlAction.INS_ROW;
break;
case UPDATE:
targetAction = transformation.evaluateTargetDmlAction(context, data);
break;
case DELETE:
targetAction = transformation.getDeleteAction();
break;
default:
persistData = true;
}
if (targetAction != null) {
// how to handle the update/delete action on target..
switch(targetAction) {
case DEL_ROW:
data.setTargetDmlType(DataEventType.DELETE);
persistData = true;
break;
case UPDATE_COL:
case UPD_ROW:
data.setTargetDmlType(DataEventType.UPDATE);
persistData = true;
break;
case INS_ROW:
data.setTargetDmlType(DataEventType.INSERT);
persistData = true;
break;
case NONE:
default:
if (log.isDebugEnabled()) {
log.debug("The {} transformation is not configured to delete row. Not sending the delete through.", transformation.getTransformId());
}
break;
}
}
}
} catch (IgnoreRowException ex) {
// ignore this row
if (log.isDebugEnabled()) {
log.debug("Transform indicated that the target row should be ignored with a target key of: {}", ArrayUtils.toString(data.getKeyValues()));
}
}
return persistData;
}
use of org.jumpmind.symmetric.io.data.transform.TransformColumn in project symmetric-ds by JumpMind.
the class DbCompareTables method applyColumnMappingsFromTransform.
protected void applyColumnMappingsFromTransform() {
for (Column sourceColumn : sourceTable.getColumns()) {
List<TransformColumn> sourceTransformColumns = transform.getTransformColumnFor(sourceColumn.getName());
if (!sourceTransformColumns.isEmpty()) {
TransformColumn transformColumn = sourceTransformColumns.get(0);
Column targetColumn = targetTable.getColumnWithName(transformColumn.getTargetColumnName());
if (transformColumn.isPk()) {
sourceColumn.setPrimaryKey(true);
}
columnMapping.put(sourceColumn, targetColumn);
} else {
if (transform.getColumnPolicy() == ColumnPolicy.SPECIFIED) {
sourceTable.removeColumn(sourceColumn);
} else {
mapColumnDefault(sourceColumn);
}
}
}
}
use of org.jumpmind.symmetric.io.data.transform.TransformColumn in project symmetric-ds by JumpMind.
the class TransformService method saveTransformTable.
public void saveTransformTable(TransformTableNodeGroupLink transformTable, boolean saveTransformColumns) {
ISqlTransaction transaction = null;
try {
transaction = sqlTemplate.startSqlTransaction();
transformTable.setLastUpdateTime(new Date());
if (transaction.prepareAndExecute(getSql("updateTransformTableSql"), transformTable.getNodeGroupLink().getSourceNodeGroupId(), transformTable.getNodeGroupLink().getTargetNodeGroupId(), transformTable.getSourceCatalogName(), transformTable.getSourceSchemaName(), transformTable.getSourceTableName(), transformTable.getTargetCatalogName(), transformTable.getTargetSchemaName(), transformTable.getTargetTableName(), transformTable.getTransformPoint().toString(), transformTable.isUpdateFirst() ? 1 : 0, transformTable.getDeleteAction().toString(), transformTable.getUpdateAction(), transformTable.getTransformOrder(), transformTable.getColumnPolicy().toString(), transformTable.getLastUpdateTime(), transformTable.getLastUpdateBy(), transformTable.getTransformId()) == 0) {
transformTable.setCreateTime(new Date());
transaction.prepareAndExecute(getSql("insertTransformTableSql"), transformTable.getNodeGroupLink().getSourceNodeGroupId(), transformTable.getNodeGroupLink().getTargetNodeGroupId(), transformTable.getSourceCatalogName(), transformTable.getSourceSchemaName(), transformTable.getSourceTableName(), transformTable.getTargetCatalogName(), transformTable.getTargetSchemaName(), transformTable.getTargetTableName(), transformTable.getTransformPoint().toString(), transformTable.isUpdateFirst() ? 1 : 0, transformTable.getDeleteAction().toString(), transformTable.getUpdateAction(), transformTable.getTransformOrder(), transformTable.getColumnPolicy().toString(), transformTable.getLastUpdateTime(), transformTable.getLastUpdateBy(), transformTable.getCreateTime(), transformTable.getTransformId());
}
if (saveTransformColumns) {
deleteTransformColumns(transaction, transformTable.getTransformId());
List<TransformColumn> columns = transformTable.getTransformColumns();
if (columns != null) {
for (TransformColumn transformColumn : columns) {
saveTransformColumn(transaction, transformColumn);
}
}
}
transaction.commit();
} catch (Error ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} catch (RuntimeException ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} finally {
close(transaction);
clearCache();
}
}
use of org.jumpmind.symmetric.io.data.transform.TransformColumn in project symmetric-ds by JumpMind.
the class TransformService method getTransformTablesFromDB.
private List<TransformTableNodeGroupLink> getTransformTablesFromDB(boolean includeColumns, boolean replaceTokens) {
List<TransformTableNodeGroupLink> transforms = sqlTemplate.query(getSql("selectTransformTable"), new TransformTableMapper());
if (includeColumns) {
List<TransformColumn> columns = getTransformColumnsFromDB();
for (TransformTableNodeGroupLink transformTable : transforms) {
for (TransformColumn column : columns) {
if (column.getTransformId().equals(transformTable.getTransformId())) {
transformTable.addTransformColumn(column);
}
}
}
}
if (replaceTokens) {
@SuppressWarnings({ "rawtypes", "unchecked" }) Map<String, String> replacements = (Map) parameterService.getAllParameters();
for (TransformTableNodeGroupLink transform : transforms) {
transform.setSourceCatalogName(FormatUtils.replaceTokens(transform.getSourceCatalogName(), replacements, true));
transform.setSourceSchemaName(FormatUtils.replaceTokens(transform.getSourceSchemaName(), replacements, true));
transform.setSourceTableName(FormatUtils.replaceTokens(transform.getSourceTableName(), replacements, true));
transform.setTargetCatalogName(FormatUtils.replaceTokens(transform.getTargetCatalogName(), replacements, true));
transform.setTargetSchemaName(FormatUtils.replaceTokens(transform.getTargetSchemaName(), replacements, true));
transform.setTargetTableName(FormatUtils.replaceTokens(transform.getTargetTableName(), replacements, true));
}
}
return transforms;
}
Aggregations