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