Search in sources :

Example 1 with TransformTable

use of org.jumpmind.symmetric.io.data.transform.TransformTable 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);
}
Also used : JavaColumnTransform(org.jumpmind.symmetric.io.data.transform.JavaColumnTransform) HashMap(java.util.HashMap) TransformedData(org.jumpmind.symmetric.io.data.transform.TransformedData) TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable) TransformColumn(org.jumpmind.symmetric.io.data.transform.TransformColumn) Test(org.junit.Test)

Example 2 with TransformTable

use of org.jumpmind.symmetric.io.data.transform.TransformTable in project symmetric-ds by JumpMind.

the class TransformWriter method start.

@Override
public boolean start(Table table) {
    List<TransformTable> activeTransformsTemp = transformsBySourceTable.get(table.getFullyQualifiedTableNameLowerCase());
    if (activeTransformsTemp != null && activeTransformsTemp.size() > 0) {
        this.sourceTable = table;
        activeTransforms = new ArrayList<TransformTable>(activeTransformsTemp.size());
        for (TransformTable transformation : activeTransformsTemp) {
            activeTransforms.add(transformation.enhanceWithImpliedColumns(this.sourceTable.getPrimaryKeyColumnNames(), this.sourceTable.getColumnNames()));
        }
        return true;
    } else {
        this.sourceTable = null;
        return super.start(table);
    }
}
Also used : TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable)

Example 3 with TransformTable

use of org.jumpmind.symmetric.io.data.transform.TransformTable in project symmetric-ds by JumpMind.

the class TransformWriter method write.

public void write(CsvData data) {
    DataEventType eventType = data.getDataEventType();
    if (activeTransforms != null && activeTransforms.size() > 0 && isTransformable(eventType)) {
        if (data.requiresTable() && sourceTable == null && context.getLastParsedTable() != null) {
            // if we cross batches and the table isn't specified, then
            // use the last table we used
            start(context.getLastParsedTable());
        }
        long ts = System.currentTimeMillis();
        Map<String, String> sourceValues = data.toColumnNameValuePairs(this.sourceTable.getColumnNames(), CsvData.ROW_DATA);
        Map<String, String> oldSourceValues = null;
        if (data.contains(CsvData.OLD_DATA)) {
            oldSourceValues = data.toColumnNameValuePairs(this.sourceTable.getColumnNames(), CsvData.OLD_DATA);
        }
        Map<String, String> sourceKeyValues = null;
        if (data.contains(CsvData.PK_DATA)) {
            sourceKeyValues = data.toKeyColumnValuePairs(this.sourceTable);
        }
        if (eventType == DataEventType.DELETE) {
            sourceValues = oldSourceValues;
            if (sourceValues == null || sourceValues.size() == 0) {
                sourceValues = sourceKeyValues;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("{} transformation(s) started because of {} on {}.  The original row data was: {}", new Object[] { activeTransforms.size(), eventType.toString(), this.sourceTable.getFullyQualifiedTableName(), sourceValues });
        }
        List<TransformedData> dataThatHasBeenTransformed = new ArrayList<TransformedData>();
        TransformTable[] transformTables = activeTransforms.toArray(new TransformTable[activeTransforms.size()]);
        if (eventType == DataEventType.DELETE) {
            CollectionUtils.reverseArray(transformTables);
        }
        for (TransformTable transformation : transformTables) {
            if (eventType == DataEventType.INSERT && transformation.isUpdateFirst()) {
                eventType = DataEventType.UPDATE;
            }
            dataThatHasBeenTransformed.addAll(transform(eventType, context, transformation, sourceKeyValues, oldSourceValues, sourceValues));
        }
        for (TransformedData transformedData : dataThatHasBeenTransformed) {
            Table transformedTable = transformedData.buildTargetTable();
            CsvData csvData = transformedData.buildTargetCsvData();
            long transformTimeInMs = System.currentTimeMillis() - ts;
            boolean processData = true;
            if (lastTransformedTable == null || !lastTransformedTable.equals(transformedTable)) {
                if (lastTransformedTable != null) {
                    this.nestedWriter.end(lastTransformedTable);
                }
                processData = this.nestedWriter.start(transformedTable);
                if (!processData) {
                    lastTransformedTable = null;
                } else {
                    lastTransformedTable = transformedTable;
                }
            }
            if (processData || !csvData.requiresTable()) {
                this.nestedWriter.write(csvData);
            }
            Statistics stats = this.nestedWriter.getStatistics().get(batch);
            if (stats != null) {
                stats.increment(DataWriterStatisticConstants.TRANSFORMMILLIS, transformTimeInMs);
            }
            ts = System.currentTimeMillis();
        }
    } else {
        if (sourceTable != null) {
            super.start(sourceTable);
        }
        super.write(data);
        if (sourceTable != null) {
            super.end(sourceTable);
        }
    }
}
Also used : TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable) Table(org.jumpmind.db.model.Table) TransformedData(org.jumpmind.symmetric.io.data.transform.TransformedData) ArrayList(java.util.ArrayList) DataEventType(org.jumpmind.symmetric.io.data.DataEventType) TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable) Statistics(org.jumpmind.util.Statistics) CsvData(org.jumpmind.symmetric.io.data.CsvData)

Example 4 with TransformTable

use of org.jumpmind.symmetric.io.data.transform.TransformTable in project symmetric-ds by JumpMind.

the class TransformWriter method toMap.

protected Map<String, List<TransformTable>> toMap(TransformTable[] transforms) {
    Map<String, List<TransformTable>> transformsByTable = new HashMap<String, List<TransformTable>>();
    if (transforms != null) {
        for (TransformTable transformTable : transforms) {
            if (transformPoint == transformTable.getTransformPoint()) {
                String sourceTableName = transformTable.getFullyQualifiedSourceTableName().toLowerCase();
                List<TransformTable> tables = transformsByTable.get(sourceTableName);
                if (tables == null) {
                    tables = new ArrayList<TransformTable>();
                    transformsByTable.put(sourceTableName, tables);
                }
                tables.add(transformTable);
            }
        }
    }
    return transformsByTable;
}
Also used : HashMap(java.util.HashMap) TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with TransformTable

use of org.jumpmind.symmetric.io.data.transform.TransformTable in project symmetric-ds by JumpMind.

the class DataLoaderService method buildDataWriter.

protected IDataWriter buildDataWriter(ProcessInfo processInfo, String sourceNodeId, String channelId, long batchId, boolean isRetry) {
    TransformTable[] transforms = null;
    NodeGroupLink link = null;
    List<ResolvedData> resolvedDatas = new ArrayList<ResolvedData>();
    List<IDatabaseWriterFilter> filters = extensionService.getExtensionPointList(IDatabaseWriterFilter.class);
    List<IDatabaseWriterFilter> dynamicFilters = filters;
    List<IDatabaseWriterErrorHandler> errorHandlers = extensionService.getExtensionPointList(IDatabaseWriterErrorHandler.class);
    List<IDatabaseWriterErrorHandler> dynamicErrorHandlers = errorHandlers;
    if (sourceNodeId != null) {
        Node sourceNode = nodeService.findNode(sourceNodeId, true);
        if (sourceNode != null) {
            link = new NodeGroupLink(sourceNode.getNodeGroupId(), parameterService.getNodeGroupId());
        }
        Map<LoadFilterType, Map<String, List<LoadFilter>>> loadFilters = loadFilterService.findLoadFiltersFor(link, true);
        List<DynamicDatabaseWriterFilter> databaseWriterFilters = DynamicDatabaseWriterFilter.getDatabaseWriterFilters(engine, loadFilters);
        if (loadFilters != null && loadFilters.size() > 0) {
            dynamicFilters = new ArrayList<IDatabaseWriterFilter>(filters.size() + 1);
            dynamicFilters.addAll(filters);
            dynamicFilters.addAll(databaseWriterFilters);
            dynamicErrorHandlers = new ArrayList<IDatabaseWriterErrorHandler>(errorHandlers.size() + 1);
            dynamicErrorHandlers.addAll(errorHandlers);
            dynamicErrorHandlers.addAll(databaseWriterFilters);
        }
        List<TransformTableNodeGroupLink> transformsList = transformService.findTransformsFor(link, TransformPoint.LOAD);
        transforms = transformsList != null ? transformsList.toArray(new TransformTable[transformsList.size()]) : null;
        if (isRetry) {
            List<IncomingError> incomingErrors = getIncomingErrors(batchId, sourceNodeId);
            for (IncomingError incomingError : incomingErrors) {
                if (incomingError.isResolveIgnore() || StringUtils.isNotBlank(incomingError.getResolveData())) {
                    resolvedDatas.add(new ResolvedData(incomingError.getFailedRowNumber(), incomingError.getResolveData(), incomingError.isResolveIgnore()));
                }
            }
        }
    }
    TransformWriter transformWriter = new TransformWriter(platform, TransformPoint.LOAD, null, transformService.getColumnTransforms(), transforms);
    IDataWriter targetWriter = getFactory(channelId).getDataWriter(sourceNodeId, symmetricDialect, transformWriter, dynamicFilters, dynamicErrorHandlers, getConflictSettingsNodeGroupLinks(link, false), resolvedDatas);
    transformWriter.setNestedWriter(new ProcessInfoDataWriter(targetWriter, processInfo));
    return transformWriter;
}
Also used : ResolvedData(org.jumpmind.symmetric.io.data.writer.ResolvedData) Node(org.jumpmind.symmetric.model.Node) ArrayList(java.util.ArrayList) TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable) DynamicDatabaseWriterFilter(org.jumpmind.symmetric.load.DynamicDatabaseWriterFilter) IncomingError(org.jumpmind.symmetric.model.IncomingError) IDatabaseWriterErrorHandler(org.jumpmind.symmetric.io.data.writer.IDatabaseWriterErrorHandler) TransformTableNodeGroupLink(org.jumpmind.symmetric.service.impl.TransformService.TransformTableNodeGroupLink) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) IDatabaseWriterFilter(org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter) ProcessInfoDataWriter(org.jumpmind.symmetric.model.ProcessInfoDataWriter) LoadFilterType(org.jumpmind.symmetric.model.LoadFilter.LoadFilterType) LoadFilter(org.jumpmind.symmetric.model.LoadFilter) TransformWriter(org.jumpmind.symmetric.io.data.writer.TransformWriter) ChannelMap(org.jumpmind.symmetric.model.ChannelMap) Map(java.util.Map) HashMap(java.util.HashMap) TransformTableNodeGroupLink(org.jumpmind.symmetric.service.impl.TransformService.TransformTableNodeGroupLink) IDataWriter(org.jumpmind.symmetric.io.data.IDataWriter)

Aggregations

TransformTable (org.jumpmind.symmetric.io.data.transform.TransformTable)7 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 TransformedData (org.jumpmind.symmetric.io.data.transform.TransformedData)3 JavaColumnTransform (org.jumpmind.symmetric.io.data.transform.JavaColumnTransform)2 TransformColumn (org.jumpmind.symmetric.io.data.transform.TransformColumn)2 TransformWriter (org.jumpmind.symmetric.io.data.writer.TransformWriter)2 NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)2 TransformTableNodeGroupLink (org.jumpmind.symmetric.service.impl.TransformService.TransformTableNodeGroupLink)2 Test (org.junit.Test)2 List (java.util.List)1 Map (java.util.Map)1 Table (org.jumpmind.db.model.Table)1 CsvData (org.jumpmind.symmetric.io.data.CsvData)1 DataEventType (org.jumpmind.symmetric.io.data.DataEventType)1 IDataWriter (org.jumpmind.symmetric.io.data.IDataWriter)1 IDatabaseWriterErrorHandler (org.jumpmind.symmetric.io.data.writer.IDatabaseWriterErrorHandler)1 IDatabaseWriterFilter (org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter)1 ResolvedData (org.jumpmind.symmetric.io.data.writer.ResolvedData)1 DynamicDatabaseWriterFilter (org.jumpmind.symmetric.load.DynamicDatabaseWriterFilter)1