Search in sources :

Example 6 with ITriggerRouterService

use of org.jumpmind.symmetric.service.ITriggerRouterService in project symmetric-ds by JumpMind.

the class DataService method reloadTable.

public String reloadTable(String nodeId, String catalogName, String schemaName, String tableName, String overrideInitialLoadSelect) {
    Node sourceNode = engine.getNodeService().findIdentity();
    Node targetNode = engine.getNodeService().findNode(nodeId);
    if (targetNode == null) {
        return "Unknown node " + nodeId;
    }
    ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
    List<TriggerHistory> triggerHistories = triggerRouterService.findTriggerHistories(catalogName, schemaName, tableName);
    Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId = triggerRouterService.fillTriggerRoutersByHistIdAndSortHist(sourceNode.getNodeGroupId(), targetNode.getNodeGroupId(), triggerHistories);
    int eventCount = 0;
    ISqlTransaction transaction = null;
    try {
        transaction = sqlTemplate.startSqlTransaction();
        for (TriggerHistory triggerHistory : triggerHistories) {
            List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
            if (triggerRouters != null && triggerRouters.size() > 0) {
                for (TriggerRouter triggerRouter : triggerRouters) {
                    eventCount++;
                    insertReloadEvent(transaction, targetNode, triggerRouter, triggerHistory, overrideInitialLoadSelect, false, -1, "reloadTable", Status.NE);
                }
            }
        }
        transaction.commit();
    } catch (Error ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } catch (RuntimeException ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } finally {
        close(transaction);
    }
    if (eventCount > 0) {
        return "Successfully created " + (eventCount > 1 ? eventCount + " events" : "event") + " to reload table " + tableName + " for node " + targetNode.getNodeId();
    } else {
        return "Trigger for table " + tableName + " does not exist for source node group of " + sourceNode.getNodeGroupId();
    }
}
Also used : ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) Node(org.jumpmind.symmetric.model.Node) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with ITriggerRouterService

use of org.jumpmind.symmetric.service.ITriggerRouterService in project symmetric-ds by JumpMind.

the class DataService method insertReloadEvent.

public boolean insertReloadEvent(TableReloadRequest request, boolean deleteAtClient) {
    boolean successful = false;
    if (request != null && request.isReloadEnabled()) {
        ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
        INodeService nodeService = engine.getNodeService();
        Node targetNode = nodeService.findNode(request.getTargetNodeId());
        if (targetNode != null) {
            TriggerRouter triggerRouter = triggerRouterService.getTriggerRouterForCurrentNode(request.getTriggerId(), request.getRouterId(), false);
            if (triggerRouter != null) {
                Trigger trigger = triggerRouter.getTrigger();
                Router router = triggerRouter.getRouter();
                NodeGroupLink link = router.getNodeGroupLink();
                Node me = nodeService.findIdentity();
                if (link.getSourceNodeGroupId().equals(me.getNodeGroupId())) {
                    if (link.getTargetNodeGroupId().equals(targetNode.getNodeGroupId())) {
                        TriggerHistory triggerHistory = lookupTriggerHistory(trigger);
                        ISqlTransaction transaction = null;
                        try {
                            transaction = sqlTemplate.startSqlTransaction();
                            if (parameterService.is(ParameterConstants.INITIAL_LOAD_DELETE_BEFORE_RELOAD)) {
                                String overrideDeleteStatement = StringUtils.isNotBlank(request.getReloadDeleteStmt()) ? request.getReloadDeleteStmt() : null;
                                insertPurgeEvent(transaction, targetNode, triggerRouter, triggerHistory, false, overrideDeleteStatement, -1, null);
                            }
                            insertReloadEvent(transaction, targetNode, triggerRouter, triggerHistory, request.getReloadSelect(), false, -1, null, Status.NE);
                            if (!targetNode.requires13Compatiblity() && deleteAtClient) {
                                insertSqlEvent(transaction, triggerHistory, trigger.getChannelId(), targetNode, String.format("delete from %s where target_node_id='%s' and source_node_id='%s' and trigger_id='%s' and router_id='%s'", TableConstants.getTableName(tablePrefix, TableConstants.SYM_TABLE_RELOAD_REQUEST), request.getTargetNodeId(), request.getSourceNodeId(), request.getTriggerId(), request.getRouterId()), false, -1, null);
                            }
                            deleteTableReloadRequest(transaction, request);
                            transaction.commit();
                        } catch (Error ex) {
                            if (transaction != null) {
                                transaction.rollback();
                            }
                            throw ex;
                        } catch (RuntimeException ex) {
                            if (transaction != null) {
                                transaction.rollback();
                            }
                            throw ex;
                        } finally {
                            close(transaction);
                        }
                    } else {
                        log.error("Could not reload table for node {} because the router {} target node group id {} did not match", new Object[] { request.getTargetNodeId(), request.getRouterId(), link.getTargetNodeGroupId() });
                    }
                } else {
                    log.error("Could not reload table for node {} because the router {} source node group id {} did not match", new Object[] { request.getTargetNodeId(), request.getRouterId(), link.getSourceNodeGroupId() });
                }
            } else {
                log.error("Could not reload table for node {} because the trigger router ({}, {}) could not be found", new Object[] { request.getTargetNodeId(), request.getTriggerId(), request.getRouterId() });
            }
        } else {
            log.error("Could not reload table for node {} because the node could not be found", request.getTargetNodeId());
        }
    }
    return successful;
}
Also used : ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) Node(org.jumpmind.symmetric.model.Node) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Router(org.jumpmind.symmetric.model.Router) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) Trigger(org.jumpmind.symmetric.model.Trigger) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) INodeService(org.jumpmind.symmetric.service.INodeService) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink)

Example 8 with ITriggerRouterService

use of org.jumpmind.symmetric.service.ITriggerRouterService in project symmetric-ds by JumpMind.

the class DataService method insertFileSyncBatchForReload.

private void insertFileSyncBatchForReload(Node targetNode, long loadId, String createBy, boolean transactional, ISqlTransaction transaction) {
    if (parameterService.is(ParameterConstants.FILE_SYNC_ENABLE) && !Constants.DEPLOYMENT_TYPE_REST.equals(targetNode.getDeploymentType())) {
        ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
        IFileSyncService fileSyncService = engine.getFileSyncService();
        if (fileSyncService.getFileTriggerRoutersForCurrentNode().size() > 0) {
            TriggerHistory fileSyncSnapshotHistory = triggerRouterService.findTriggerHistory(null, null, TableConstants.getTableName(tablePrefix, TableConstants.SYM_FILE_SNAPSHOT));
            String routerid = triggerRouterService.buildSymmetricTableRouterId(fileSyncSnapshotHistory.getTriggerId(), parameterService.getNodeGroupId(), targetNode.getNodeGroupId());
            TriggerRouter fileSyncSnapshotTriggerRouter = triggerRouterService.getTriggerRouterForCurrentNode(fileSyncSnapshotHistory.getTriggerId(), routerid, true);
            List<Channel> channels = engine.getConfigurationService().getFileSyncChannels();
            for (Channel channel : channels) {
                if (channel.isReloadFlag()) {
                    insertReloadEvent(transaction, targetNode, fileSyncSnapshotTriggerRouter, fileSyncSnapshotHistory, "reload_channel_id='" + channel.getChannelId() + "'", true, loadId, createBy, Status.NE, channel.getChannelId());
                    if (!transactional) {
                        transaction.commit();
                    }
                }
            }
        }
    }
}
Also used : ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) IFileSyncService(org.jumpmind.symmetric.service.IFileSyncService) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Channel(org.jumpmind.symmetric.model.Channel) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter)

Example 9 with ITriggerRouterService

use of org.jumpmind.symmetric.service.ITriggerRouterService 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();
            String triggerId = columnValues.get("TRIGGER_ID");
            Trigger trigger = triggerRouterService.getTriggerById(true, triggerId);
            if (trigger != null) {
                ((Set<Trigger>) needResync).add(trigger);
            } else {
                routingContext.put(CTX_KEY_RESYNC_NEEDED, Boolean.TRUE);
            }
        }
    } 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)

Example 10 with ITriggerRouterService

use of org.jumpmind.symmetric.service.ITriggerRouterService in project symmetric-ds by JumpMind.

the class AbstractDataExtractorServiceTest method setupForTest.

@Before
public void setupForTest() {
    ITriggerRouterService triggerRouterService = getTriggerRouterService();
    TriggerRouter triggerRouter = triggerRouterService.findTriggerRouterById(true, TEST_TABLE, TestConstants.ROUTER_ID_ROOT_2_TEST);
    if (triggerRouter == null) {
        triggerRouter = new TriggerRouter(new Trigger(TEST_TABLE, TestConstants.TEST_CHANNEL_ID), new Router(TestConstants.ROUTER_ID_ROOT_2_TEST, TestConstants.ROOT_2_TEST));
        triggerRouterService.saveTriggerRouter(triggerRouter);
        triggerRouterService.syncTriggers();
        getDbDialect().truncateTable(TEST_TABLE);
    }
    resetBatches();
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Router(org.jumpmind.symmetric.model.Router) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Before(org.junit.Before)

Aggregations

ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)27 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)12 Trigger (org.jumpmind.symmetric.model.Trigger)11 TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)9 Node (org.jumpmind.symmetric.model.Node)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)6 Router (org.jumpmind.symmetric.model.Router)5 Table (org.jumpmind.db.model.Table)4 INodeService (org.jumpmind.symmetric.service.INodeService)4 File (java.io.File)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 ISymmetricDialect (org.jumpmind.symmetric.db.ISymmetricDialect)3 NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 IoException (org.jumpmind.exception.IoException)2