Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed Dec 24, 2007
1 parent fd0137f commit de86413
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 20 deletions.
Expand Up @@ -32,6 +32,14 @@ public class HsqlDbDialect extends AbstractDbDialect implements IDbDialect {
static final String TRANSACTION_ID_FUNCTION_NAME = "fn_transaction_id";

static final String SYNC_TRIGGERS_DISABLED_USER_VARIABLE = "";

ThreadLocal<Boolean> syncEnabled = new ThreadLocal<Boolean>() {
@Override
protected Boolean initialValue() {
return Boolean.TRUE;
}

};

protected void initForSpecificDialect() {
}
Expand Down Expand Up @@ -66,11 +74,17 @@ public boolean isBlobSyncSupported() {
public boolean isClobSyncSupported() {
return true;
}

public boolean isSyncEnabled() {
return syncEnabled.get();
}

public void disableSyncTriggers() {
syncEnabled.set(Boolean.FALSE);
}

public void enableSyncTriggers() {
syncEnabled.set(Boolean.TRUE);
}

public String getSyncTriggersExpression() {
Expand Down
Expand Up @@ -19,18 +19,128 @@
*/
package org.jumpmind.symmetric.db.hsqldb;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hsqldb.Trigger;
import org.jumpmind.symmetric.SymmetricEngine;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.model.Data;
import org.jumpmind.symmetric.model.DataEventAction;
import org.jumpmind.symmetric.model.DataEventType;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.Trigger;
import org.jumpmind.symmetric.model.TriggerHistory;
import org.jumpmind.symmetric.service.IConfigurationService;
import org.jumpmind.symmetric.service.IDataService;
import org.jumpmind.symmetric.service.INodeService;

public class HsqlDbTrigger implements Trigger {
public class HsqlDbTrigger implements org.hsqldb.Trigger {

static final Log logger = LogFactory.getLog(HsqlDbTrigger.class);

public void fire(int type, String triggerName, String tableName, Object[] oldRow,
Object[] newRolw) {
logger.info("trigger " + triggerName + " fired for " + tableName);


public void fire(int type, String triggerName, String tableName,
Object[] oldRow, Object[] newRow) {
try {
logger.info("trigger " + triggerName + " fired for " + tableName);
SymmetricEngine engine = getEngine(triggerName);
IConfigurationService configService = getConfigurationService(engine);
IDataService dataService = getDataService(engine);
INodeService nodeService = getNodeService(engine);
TriggerHistory history = configService
.getHistoryRecordFor(getTriggerHistoryId(triggerName));
Trigger trigger = configService.getTriggerById(history
.getTriggerId());
HsqlDbDialect dialect = getDbDialect(engine);
if (trigger.isSyncOnIncomingBatch() || dialect.isSyncEnabled()) {
DataEventType eventType = getDataEventType(type);
Data data = new Data(trigger.getChannelId(), tableName,
eventType, formatRowData(eventType, oldRow, newRow),
formatPkRowData(eventType, oldRow, newRow), history);

// select nodes from sym_node

DataEventAction action = configService
.getDataEventActionsByGroupId(trigger
.getSourceGroupId(), trigger.getTargetGroupId());
List<Node> nodes = null;

if (action != null) {
switch (action) {
case PUSH:
//nodes = nodeService.findNodesToPushTo();
break;
case WAIT_FOR_POLL:
//nodes = nodeService.findNodesToPull();
}
}
if (nodes != null) {
dataService.insertDataEvent(data, nodes);
}

}
} catch (RuntimeException ex) {
logger.error(ex, ex);
throw ex;
}
}

private String formatRowData(DataEventType type, Object[] oldRow,
Object[] newRow) {
return null;
}

private String formatPkRowData(DataEventType type, Object[] oldRow,
Object[] newRow) {
return null;
}

private DataEventType getDataEventType(int type) {
switch (type) {

case org.hsqldb.Trigger.INSERT_AFTER_ROW:
return DataEventType.INSERT;
case org.hsqldb.Trigger.UPDATE_AFTER_ROW:
return DataEventType.UPDATE;
case org.hsqldb.Trigger.DELETE_AFTER_ROW:
return DataEventType.DELETE;
default:
throw new IllegalStateException("Unexpected trigger type: " + type);
}
}

private int getTriggerHistoryId(String triggerName) {
return Integer.parseInt(triggerName.substring(triggerName
.lastIndexOf("_") + 1));
}

private HsqlDbDialect getDbDialect(SymmetricEngine engine) {
return (HsqlDbDialect) engine.getApplicationContext().getBean(
Constants.DB_DIALECT);
}

private IConfigurationService getConfigurationService(SymmetricEngine engine) {
return (IConfigurationService) engine.getApplicationContext().getBean(
Constants.CONFIG_SERVICE);
}

private INodeService getNodeService(SymmetricEngine engine) {
return (INodeService) engine.getApplicationContext().getBean(
Constants.NODE_SERVICE);
}

private IDataService getDataService(SymmetricEngine engine) {
return (IDataService) engine.getApplicationContext().getBean(
Constants.DATA_SERVICE);
}

private SymmetricEngine getEngine(String triggerName) {
String minusTriggerId = triggerName.substring(0, triggerName
.lastIndexOf("_"));
String engineName = minusTriggerId.substring(minusTriggerId
.lastIndexOf("_") + 1);
return SymmetricEngine.findEngineByName(engineName.toLowerCase());
}

}
Expand Up @@ -45,7 +45,7 @@ public interface IConfigurationService {

public void initSystemChannels();

public Map<String, DataEventAction> getDataEventActionsByGroupId(String groupId);
public DataEventAction getDataEventActionsByGroupId(String sourceGroupId, String targetGroupId);

public Map<String, List<Trigger>> getTriggersByChannelFor(
String configurationTypeId);
Expand Down
Expand Up @@ -199,16 +199,12 @@ private boolean isSet(Object value) {
}

@SuppressWarnings("unchecked")
public Map<String, DataEventAction> getDataEventActionsByGroupId(
String nodeGroupId) {
Map<String, String> results = (Map<String, String>) jdbcTemplate
.queryForMap(selectDataEventActionsByIdSql,
new Object[] { nodeGroupId });
Map<String, DataEventAction> retMap = new HashMap<String, DataEventAction>();
for (String key : results.keySet()) {
retMap.put(key, DataEventAction.fromCode(results.get(key)));
}
return retMap;
public DataEventAction getDataEventActionsByGroupId(String sourceGroupId, String targetGroupId) {
String code = (String) jdbcTemplate
.queryForObject(selectDataEventActionsByIdSql,
new Object[] { sourceGroupId, targetGroupId }, String.class);

return DataEventAction.fromCode(code);
}

@SuppressWarnings("unchecked")
Expand Down
4 changes: 2 additions & 2 deletions symmetric/src/main/resources/symmetric-services.xml
Expand Up @@ -69,8 +69,8 @@
</property>
<property name="selectDataEventActionsByIdSql">
<value>
select source_node_group_id, data_event_action from
${sync.table.prefix}_node_group_link where source_node_group_id = ?
select data_event_action from
${sync.table.prefix}_node_group_link where source_node_group_id = ? and target_node_group_id = ?
</value>
</property>
<property name="selectChannelsSql">
Expand Down

0 comments on commit de86413

Please sign in to comment.