Search in sources :

Example 11 with Trigger

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

the class ConfigurationChangedDataRouter method queueSyncTriggers.

@SuppressWarnings("unchecked")
protected void queueSyncTriggers(SimpleRouterContext routingContext, DataMetaData dataMetaData, Map<String, String> columnValues) {
    if ((tableMatches(dataMetaData, TableConstants.SYM_TRIGGER) || tableMatches(dataMetaData, TableConstants.SYM_TRIGGER_ROUTER))) {
        Object needResync = routingContext.get(CTX_KEY_RESYNC_NEEDED);
        if (needResync == null || needResync instanceof Set) {
            if (needResync == null) {
                needResync = new HashSet<Trigger>();
                routingContext.put(CTX_KEY_RESYNC_NEEDED, needResync);
            }
            ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
            boolean refreshCache = false;
            if (routingContext.get(CTX_KEY_FLUSHED_TRIGGER_ROUTERS) == null) {
                triggerRouterService.clearCache();
                refreshCache = true;
                routingContext.put(CTX_KEY_FLUSHED_TRIGGER_ROUTERS, true);
            }
            Trigger trigger = null;
            Date lastUpdateTime = null;
            String triggerId = columnValues.get("TRIGGER_ID");
            if (tableMatches(dataMetaData, TableConstants.SYM_TRIGGER_ROUTER)) {
                String routerId = columnValues.get("ROUTER_ID");
                TriggerRouter tr = triggerRouterService.findTriggerRouterById(triggerId, routerId, refreshCache);
                if (tr != null) {
                    trigger = tr.getTrigger();
                    lastUpdateTime = tr.getLastUpdateTime();
                }
            } else {
                trigger = triggerRouterService.getTriggerById(triggerId, refreshCache);
                if (trigger != null) {
                    lastUpdateTime = trigger.getLastUpdateTime();
                }
            }
            if (trigger != null) {
                List<TriggerHistory> histories = triggerRouterService.getActiveTriggerHistories(trigger);
                boolean sync = false;
                if (histories != null && histories.size() > 0) {
                    for (TriggerHistory triggerHistory : histories) {
                        if (triggerHistory.getCreateTime().before(lastUpdateTime)) {
                            sync = true;
                        }
                    }
                } else {
                    sync = true;
                }
                if (sync) {
                    ((Set<Trigger>) needResync).add(trigger);
                }
            }
        }
    } else if (tableMatches(dataMetaData, TableConstants.SYM_ROUTER) || tableMatches(dataMetaData, TableConstants.SYM_NODE_GROUP_LINK)) {
        routingContext.put(CTX_KEY_RESYNC_NEEDED, Boolean.TRUE);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Trigger(org.jumpmind.symmetric.model.Trigger) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Date(java.util.Date)

Example 12 with Trigger

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

the class TriggerRouterService method syncTriggers.

public void syncTriggers(Table table, boolean force) {
    boolean ignoreCase = this.parameterService.is(ParameterConstants.DB_METADATA_IGNORE_CASE);
    if (table == null) {
        throw new SymmetricException("'table' cannot be null, check that the table exists.");
    }
    /* Re-lookup just in case the table was just altered */
    platform.resetCachedTableModel();
    table = platform.getTableFromCache(table.getCatalog(), table.getSchema(), table.getName(), true);
    List<Trigger> triggersForCurrentNode = getTriggersForCurrentNode();
    List<TriggerHistory> activeTriggerHistories = getActiveTriggerHistories();
    for (Trigger trigger : triggersForCurrentNode) {
        if (trigger.matches(table, platform.getDefaultCatalog(), platform.getDefaultSchema(), ignoreCase)) {
            log.info("Synchronizing triggers for {}", table.getFullyQualifiedTableName());
            updateOrCreateDatabaseTriggers(trigger, table, null, force, true, activeTriggerHistories);
            log.info("Done synchronizing triggers for {}", table.getFullyQualifiedTableName());
        }
    }
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) SymmetricException(org.jumpmind.symmetric.SymmetricException)

Example 13 with Trigger

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

the class TriggerRouterService method buildTriggerRoutersForSymmetricTables.

public List<TriggerRouter> buildTriggerRoutersForSymmetricTables(String version, NodeGroupLink nodeGroupLink, String... tablesToExclude) {
    int initialLoadOrder = 1;
    List<Trigger> triggers = buildTriggersForSymmetricTables(version, tablesToExclude);
    List<TriggerRouter> triggerRouters = new ArrayList<TriggerRouter>(triggers.size());
    for (int j = 0; j < triggers.size(); j++) {
        Trigger trigger = triggers.get(j);
        TriggerRouter triggerRouter = buildTriggerRoutersForSymmetricTables(version, trigger, nodeGroupLink);
        triggerRouter.setInitialLoadOrder(initialLoadOrder++);
        triggerRouters.add(triggerRouter);
    }
    return triggerRouters;
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) ArrayList(java.util.ArrayList) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter)

Example 14 with Trigger

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

the class TriggerRouterService method getTriggers.

public List<Trigger> getTriggers(boolean replaceTokens) {
    List<Trigger> triggers = sqlTemplate.query("select " + getSql("selectTriggersColumnList", "selectTriggersSql"), new TriggerMapper());
    if (replaceTokens) {
        @SuppressWarnings({ "rawtypes", "unchecked" }) Map<String, String> replacements = (Map) parameterService.getAllParameters();
        for (Trigger trigger : triggers) {
            trigger.setSourceCatalogName(FormatUtils.replaceTokens(trigger.getSourceCatalogName(), replacements, true));
            trigger.setSourceSchemaName(FormatUtils.replaceTokens(trigger.getSourceSchemaName(), replacements, true));
            trigger.setSourceTableName(FormatUtils.replaceTokens(trigger.getSourceTableName(), replacements, true));
        }
    }
    return triggers;
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) Map(java.util.Map) HashMap(java.util.HashMap)

Example 15 with Trigger

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

the class TriggerRouterService method createTriggersOnChannelForTables.

public void createTriggersOnChannelForTables(String channelId, String catalogName, String schemaName, List<String> tables, String lastUpdateBy) {
    List<Trigger> createdTriggers = new ArrayList<Trigger>();
    List<Trigger> existingTriggers = getTriggers();
    for (String table : tables) {
        Trigger trigger = new Trigger();
        trigger.setChannelId(channelId);
        trigger.setSourceCatalogName(catalogName);
        trigger.setSourceSchemaName(schemaName);
        trigger.setSourceTableName(table);
        String triggerId = table;
        if (table.length() > 50) {
            triggerId = table.substring(0, 13) + "_" + UUID.randomUUID().toString();
        }
        boolean uniqueNameCreated = false;
        int suffix = 0;
        while (!uniqueNameCreated) {
            String triggerIdPriorToCheck = triggerId;
            for (Trigger existingTrigger : existingTriggers) {
                if (triggerId.equals(existingTrigger.getTriggerId())) {
                    String suffixString = "_" + suffix;
                    if (suffix == 0) {
                        triggerId = triggerId + suffixString;
                    } else {
                        triggerId = triggerId.substring(0, triggerId.length() - ("_" + (suffix - 1)).length()) + suffixString;
                    }
                    suffix++;
                }
            }
            if (triggerId.equals(triggerIdPriorToCheck)) {
                uniqueNameCreated = true;
            }
        }
        trigger.setTriggerId(triggerId);
        trigger.setLastUpdateBy(lastUpdateBy);
        trigger.setLastUpdateTime(new Date());
        trigger.setCreateTime(new Date());
        saveTrigger(trigger);
        createdTriggers.add(trigger);
    }
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

Trigger (org.jumpmind.symmetric.model.Trigger)42 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)20 ArrayList (java.util.ArrayList)17 TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)14 Router (org.jumpmind.symmetric.model.Router)13 ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)11 Test (org.junit.Test)11 Date (java.util.Date)5 Table (org.jumpmind.db.model.Table)5 CsvData (org.jumpmind.symmetric.io.data.CsvData)5 Data (org.jumpmind.symmetric.model.Data)5 Node (org.jumpmind.symmetric.model.Node)5 HashSet (java.util.HashSet)4 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)4 HashMap (java.util.HashMap)3 Set (java.util.Set)3 ISymmetricDialect (org.jumpmind.symmetric.db.ISymmetricDialect)3 NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)3 INodeService (org.jumpmind.symmetric.service.INodeService)3 List (java.util.List)2