Search in sources :

Example 1 with LoadFilter

use of org.jumpmind.symmetric.model.LoadFilter in project symmetric-ds by JumpMind.

the class DynamicDatabaseWriterFilter method processLoadFilters.

protected boolean processLoadFilters(DataContext context, Table table, CsvData data, Exception error, WriteMethod writeMethod) {
    boolean writeRow = true;
    if (table != null) {
        List<LoadFilter> foundFilters = null;
        if (!table.getName().toLowerCase().startsWith(engine.getTablePrefix() + "_")) {
            foundFilters = lookupFilters(foundFilters, table.getCatalog(), table.getSchema(), FormatUtils.WILDCARD);
            foundFilters = lookupFilters(foundFilters, table.getCatalog(), FormatUtils.WILDCARD, FormatUtils.WILDCARD);
            foundFilters = lookupFilters(foundFilters, FormatUtils.WILDCARD, FormatUtils.WILDCARD, FormatUtils.WILDCARD);
        }
        foundFilters = lookupFilters(foundFilters, FormatUtils.WILDCARD, FormatUtils.WILDCARD, table.getName());
        foundFilters = lookupFilters(foundFilters, FormatUtils.WILDCARD, table.getSchema(), table.getName());
        foundFilters = lookupFilters(foundFilters, table.getCatalog(), FormatUtils.WILDCARD, table.getName());
        foundFilters = lookupFilters(foundFilters, table.getCatalog(), table.getSchema(), table.getName());
        if (foundFilters != null) {
            for (LoadFilter filter : foundFilters) {
                addBatchScriptsToContext(context, filter);
            }
            writeRow = processLoadFilters(context, table, data, error, writeMethod, foundFilters);
        }
    }
    return writeRow;
}
Also used : LoadFilter(org.jumpmind.symmetric.model.LoadFilter)

Example 2 with LoadFilter

use of org.jumpmind.symmetric.model.LoadFilter in project symmetric-ds by JumpMind.

the class JavaDatabaseWriterFilter method processLoadFilters.

@Override
protected boolean processLoadFilters(DataContext context, Table table, CsvData data, Exception error, WriteMethod writeMethod, List<LoadFilter> loadFiltersForTable) {
    boolean writeRow = true;
    LoadFilter currentFilter = null;
    try {
        for (LoadFilter filter : loadFiltersForTable) {
            currentFilter = filter;
            if (filter.isFilterOnDelete() && data.getDataEventType().equals(DataEventType.DELETE) || filter.isFilterOnInsert() && data.getDataEventType().equals(DataEventType.INSERT) || filter.isFilterOnUpdate() && data.getDataEventType().equals(DataEventType.UPDATE)) {
                if (writeMethod.equals(WriteMethod.BEFORE_WRITE) && filter.getBeforeWriteScript() != null) {
                    writeRow = getCompiledClass(filter.getBeforeWriteScript()).execute(context, table, data, error);
                } else if (writeMethod.equals(WriteMethod.AFTER_WRITE) && filter.getAfterWriteScript() != null) {
                    writeRow = getCompiledClass(filter.getAfterWriteScript()).execute(context, table, data, error);
                } else if (writeMethod.equals(WriteMethod.HANDLE_ERROR) && filter.getHandleErrorScript() != null) {
                    writeRow = getCompiledClass(filter.getHandleErrorScript()).execute(context, table, data, error);
                }
            }
        }
    } catch (Exception ex) {
        String formattedMessage = String.format("Error executing Java load filter %s on table %s. The error was: %s", new Object[] { currentFilter != null ? currentFilter.getLoadFilterId() : "N/A", table.getName(), ex.getMessage() });
        log.error(formattedMessage);
        if (currentFilter.isFailOnError()) {
            throw new SymmetricException(formattedMessage, ex);
        }
    }
    return writeRow;
}
Also used : LoadFilter(org.jumpmind.symmetric.model.LoadFilter) SymmetricException(org.jumpmind.symmetric.SymmetricException) SymmetricException(org.jumpmind.symmetric.SymmetricException)

Example 3 with LoadFilter

use of org.jumpmind.symmetric.model.LoadFilter in project symmetric-ds by JumpMind.

the class LoadFilterService method refreshCache.

protected void refreshCache() {
    // get the cache timeout
    long cacheTimeoutInMs = parameterService.getLong(ParameterConstants.CACHE_TIMEOUT_LOAD_FILTER_IN_MS);
    synchronized (this) {
        if (System.currentTimeMillis() - lastCacheTimeInMs >= cacheTimeoutInMs || loadFilterCacheByNodeGroupLink == null) {
            loadFilterCacheByNodeGroupLink = new HashMap<NodeGroupLink, Map<LoadFilterType, Map<String, List<LoadFilter>>>>();
            List<LoadFilterNodeGroupLink> loadFilters = getLoadFiltersFromDB();
            boolean ignoreCase = this.parameterService.is(ParameterConstants.DB_METADATA_IGNORE_CASE);
            for (LoadFilterNodeGroupLink loadFilter : loadFilters) {
                NodeGroupLink nodeGroupLink = loadFilter.getNodeGroupLink();
                if (nodeGroupLink != null) {
                    Map<LoadFilterType, Map<String, List<LoadFilter>>> loadFiltersByType = loadFilterCacheByNodeGroupLink.get(nodeGroupLink);
                    if (loadFiltersByType == null) {
                        loadFiltersByType = new HashMap<LoadFilterType, Map<String, List<LoadFilter>>>();
                        loadFilterCacheByNodeGroupLink.put(nodeGroupLink, loadFiltersByType);
                    }
                    Map<String, List<LoadFilter>> loadFiltersByTable = loadFiltersByType.get(loadFilter.getLoadFilterType());
                    if (loadFiltersByTable == null) {
                        loadFiltersByTable = new HashMap<String, List<LoadFilter>>();
                        loadFiltersByType.put(loadFilter.getLoadFilterType(), loadFiltersByTable);
                    }
                    String tableName = loadFilter.getTargetTableName();
                    if (StringUtils.isBlank(tableName)) {
                        tableName = FormatUtils.WILDCARD;
                    } else if (ignoreCase) {
                        tableName = tableName.toUpperCase();
                    }
                    String schemaName = loadFilter.getTargetSchemaName();
                    if (StringUtils.isBlank(schemaName)) {
                        schemaName = FormatUtils.WILDCARD;
                    } else if (ignoreCase) {
                        schemaName = schemaName.toUpperCase();
                    }
                    String catalogName = loadFilter.getTargetCatalogName();
                    if (StringUtils.isBlank(catalogName)) {
                        catalogName = FormatUtils.WILDCARD;
                    } else if (ignoreCase) {
                        catalogName = catalogName.toUpperCase();
                    }
                    String qualifiedName = Table.getFullyQualifiedTableName(catalogName, schemaName, tableName);
                    List<LoadFilter> loadFiltersForTable = loadFiltersByTable.get(qualifiedName);
                    if (loadFiltersForTable == null) {
                        loadFiltersForTable = new ArrayList<LoadFilter>();
                        loadFiltersByTable.put(qualifiedName, loadFiltersForTable);
                    }
                    loadFiltersForTable.add(loadFilter);
                }
            }
            lastCacheTimeInMs = System.currentTimeMillis();
        }
    }
}
Also used : LoadFilterType(org.jumpmind.symmetric.model.LoadFilter.LoadFilterType) LoadFilter(org.jumpmind.symmetric.model.LoadFilter) ArrayList(java.util.ArrayList) List(java.util.List) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with LoadFilter

use of org.jumpmind.symmetric.model.LoadFilter in project symmetric-ds by JumpMind.

the class BshDatabaseWriterFilter method processLoadFilters.

@Override
protected boolean processLoadFilters(DataContext context, Table table, CsvData data, Exception error, WriteMethod writeMethod, List<LoadFilter> loadFiltersForTable) {
    boolean writeRow = true;
    LoadFilter currentFilter = null;
    try {
        Interpreter interpreter = getInterpreter(context);
        bind(interpreter, context, table, data, error);
        for (LoadFilter filter : loadFiltersForTable) {
            currentFilter = filter;
            if (filter.isFilterOnDelete() && data.getDataEventType().equals(DataEventType.DELETE) || filter.isFilterOnInsert() && data.getDataEventType().equals(DataEventType.INSERT) || filter.isFilterOnUpdate() && data.getDataEventType().equals(DataEventType.UPDATE)) {
                Object result = null;
                if (writeMethod.equals(WriteMethod.BEFORE_WRITE) && filter.getBeforeWriteScript() != null) {
                    result = interpreter.eval(filter.getBeforeWriteScript());
                } else if (writeMethod.equals(WriteMethod.AFTER_WRITE) && filter.getAfterWriteScript() != null) {
                    result = interpreter.eval(filter.getAfterWriteScript());
                } else if (writeMethod.equals(WriteMethod.HANDLE_ERROR) && filter.getHandleErrorScript() != null) {
                    result = interpreter.eval(filter.getHandleErrorScript());
                }
                if (result != null && result.equals(Boolean.FALSE)) {
                    writeRow = false;
                }
            }
        }
    } catch (EvalError ex) {
        processError(currentFilter, table, ex);
    }
    return writeRow;
}
Also used : Interpreter(bsh.Interpreter) LoadFilter(org.jumpmind.symmetric.model.LoadFilter) EvalError(bsh.EvalError)

Example 5 with LoadFilter

use of org.jumpmind.symmetric.model.LoadFilter in project symmetric-ds by JumpMind.

the class SQLDatabaseWriterFilter method processLoadFilters.

@Override
protected boolean processLoadFilters(DataContext context, Table table, CsvData data, Exception error, WriteMethod writeMethod, List<LoadFilter> loadFiltersForTable) {
    boolean writeRow = true;
    LoadFilter currentFilter = null;
    List<Boolean> values = null;
    try {
        LinkedCaseInsensitiveMap<Object> namedParams = null;
        for (LoadFilter filter : loadFiltersForTable) {
            currentFilter = filter;
            values = null;
            if (filter.isFilterOnDelete() && data.getDataEventType().equals(DataEventType.DELETE) || filter.isFilterOnInsert() && data.getDataEventType().equals(DataEventType.INSERT) || filter.isFilterOnUpdate() && data.getDataEventType().equals(DataEventType.UPDATE)) {
                String sql = null;
                if (writeMethod.equals(WriteMethod.BEFORE_WRITE) && filter.getBeforeWriteScript() != null) {
                    sql = doTokenReplacementOnSql(context, filter.getBeforeWriteScript());
                } else if (writeMethod.equals(WriteMethod.AFTER_WRITE) && filter.getAfterWriteScript() != null) {
                    sql = doTokenReplacementOnSql(context, filter.getAfterWriteScript());
                } else if (writeMethod.equals(WriteMethod.HANDLE_ERROR) && filter.getHandleErrorScript() != null) {
                    sql = doTokenReplacementOnSql(context, filter.getHandleErrorScript());
                }
                if (sql != null && !sql.trim().isEmpty()) {
                    if (namedParams == null) {
                        namedParams = getVariablesMap(table, data);
                    }
                    ISqlTransaction transaction = context.findTransaction();
                    values = transaction.query(sql, lookupColumnRowMapper, namedParams);
                }
                if (values != null && values.size() > 0) {
                    writeRow = values.get(0);
                }
            }
        }
    } catch (Exception ex) {
        processError(currentFilter, table, ex);
    }
    return writeRow;
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) LoadFilter(org.jumpmind.symmetric.model.LoadFilter) SymmetricException(org.jumpmind.symmetric.SymmetricException)

Aggregations

LoadFilter (org.jumpmind.symmetric.model.LoadFilter)6 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SymmetricException (org.jumpmind.symmetric.SymmetricException)2 LoadFilterType (org.jumpmind.symmetric.model.LoadFilter.LoadFilterType)2 NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)2 EvalError (bsh.EvalError)1 Interpreter (bsh.Interpreter)1 List (java.util.List)1 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)1 IDataWriter (org.jumpmind.symmetric.io.data.IDataWriter)1 TransformTable (org.jumpmind.symmetric.io.data.transform.TransformTable)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 TransformWriter (org.jumpmind.symmetric.io.data.writer.TransformWriter)1 DynamicDatabaseWriterFilter (org.jumpmind.symmetric.load.DynamicDatabaseWriterFilter)1 ChannelMap (org.jumpmind.symmetric.model.ChannelMap)1 IncomingError (org.jumpmind.symmetric.model.IncomingError)1