use of org.jumpmind.symmetric.io.data.transform.TransformColumn in project symmetric-ds by JumpMind.
the class JavaColumnTransformTest method testInnerClass.
@Test
public void testInnerClass() throws Exception {
String javaCode = "final DataContext ctx = context;" + "HashMap namedParams = new HashMap();" + "context.findTransaction().query(\"sql\", new ISqlRowMapper<Object>() {" + " public Object mapRow(Row row) {" + " ctx.put(\"a\", row.getString(\"b\"));" + " return null;" + " }" + "}, namedParams);" + "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 create.
protected List<TransformedData> create(DataContext context, DataEventType dataEventType, TransformTable transformation, Map<String, String> sourceKeyValues, Map<String, String> oldSourceValues, Map<String, String> sourceValues) throws IgnoreRowException {
List<TransformColumn> columns = transformation.getPrimaryKeyColumns();
if (columns == null || columns.size() == 0) {
log.error("No primary key defined for the transformation: {}", transformation.getTransformId());
return new ArrayList<TransformedData>(0);
} else {
List<TransformedData> datas = new ArrayList<TransformedData>();
TransformedData data = new TransformedData(transformation, dataEventType, sourceKeyValues, oldSourceValues, sourceValues);
datas.add(data);
DataEventType eventType = data.getSourceDmlType();
for (TransformColumn transformColumn : columns) {
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)) {
List<TransformedData> newDatas = null;
try {
Object columnValue = transformColumn(context, data, transformColumn, sourceValues, oldSourceValues);
if (columnValue instanceof List) {
@SuppressWarnings("unchecked") List<String> values = (List<String>) columnValue;
if (values.size() > 0) {
data.put(transformColumn, values.get(0), oldSourceValues != null ? values.get(0) : null, true);
if (values.size() > 1) {
if (newDatas == null) {
newDatas = new ArrayList<TransformedData>(values.size() - 1);
}
for (int i = 1; i < values.size(); i++) {
TransformedData newData = data.copy();
newData.put(transformColumn, values.get(i), oldSourceValues != null ? values.get(i) : null, true);
newDatas.add(newData);
}
}
} else {
throw new IgnoreRowException();
}
} else if (columnValue instanceof NewAndOldValue) {
data.put(transformColumn, ((NewAndOldValue) columnValue).getNewValue(), oldSourceValues != null ? ((NewAndOldValue) columnValue).getOldValue() : null, true);
} else {
data.put(transformColumn, (String) columnValue, oldSourceValues != null ? (String) columnValue : null, true);
}
} catch (IgnoreColumnException e) {
// Do nothing. We are suppose to ignore the column.
}
if (newDatas != null) {
datas.addAll(newDatas);
newDatas = null;
}
}
}
return datas;
}
}
Aggregations