use of org.jumpmind.symmetric.model.DataMetaData in project symmetric-ds by JumpMind.
the class XmlPublisherDataRouterTest method testPublishUpdateAsXml.
@Test
public void testPublishUpdateAsXml() {
Data data = new Data();
data.setDataEventType(DataEventType.UPDATE);
data.setRowData("2,updated data");
data.setTriggerHistory(new TriggerHistory(TABLE_NAME, "ID", "ID,DATA"));
data.setTableName(TABLE_NAME);
router.routeToNodes(context, new DataMetaData(data, table, null, null), null, false, false, null);
router.contextCommitted(context);
assertEquals(UPDATE_XML.trim(), output.toString().trim());
}
use of org.jumpmind.symmetric.model.DataMetaData in project symmetric-ds by JumpMind.
the class XmlPublisherDataRouterTest method testPublishInsertAsXml.
@Test
public void testPublishInsertAsXml() {
Data data = new Data();
data.setDataEventType(DataEventType.INSERT);
data.setRowData("1,new inserted data");
data.setTriggerHistory(new TriggerHistory(TABLE_NAME, "ID", "ID,DATA"));
data.setTableName(TABLE_NAME);
router.routeToNodes(context, new DataMetaData(data, table, null, null), null, false, false, null);
router.contextCommitted(context);
assertEquals(INSERT_XML.trim(), output.toString().trim());
}
use of org.jumpmind.symmetric.model.DataMetaData in project symmetric-ds by JumpMind.
the class XmlPublisherDataRouterTest method testPublishDeleteAsXml.
@Test
public void testPublishDeleteAsXml() {
Data data = new Data();
data.setDataEventType(DataEventType.DELETE);
data.setOldData("3,old deleted data");
data.setTriggerHistory(new TriggerHistory(TABLE_NAME, "ID", "ID,DATA"));
data.setTableName(TABLE_NAME);
router.routeToNodes(context, new DataMetaData(data, table, null, null), null, false, false, null);
router.contextCommitted(context);
assertEquals(DELETE_XML.trim(), output.toString().trim());
}
use of org.jumpmind.symmetric.model.DataMetaData in project symmetric-ds by JumpMind.
the class AbstractFileParsingRouter method deleteFileIfNecessary.
public void deleteFileIfNecessary(DataMetaData dataMetaData) {
Data data = dataMetaData.getData();
Table snapshotTable = dataMetaData.getTable();
if (data.getDataEventType() == DataEventType.INSERT || data.getDataEventType() == DataEventType.UPDATE) {
List<File> filesToDelete = new ArrayList<File>();
Map<String, String> columnData = data.toColumnNameValuePairs(snapshotTable.getColumnNames(), CsvData.ROW_DATA);
FileSnapshot fileSnapshot = new FileSnapshot();
fileSnapshot.setTriggerId(columnData.get("TRIGGER_ID"));
fileSnapshot.setRouterId(columnData.get("ROUTER_ID"));
fileSnapshot.setFileModifiedTime(Long.parseLong(columnData.get("FILE_MODIFIED_TIME")));
fileSnapshot.setFileName(columnData.get("FILE_NAME"));
fileSnapshot.setRelativeDir(columnData.get("RELATIVE_DIR"));
fileSnapshot.setLastEventType(LastEventType.fromCode(columnData.get("LAST_EVENT_TYPE")));
FileTriggerRouter triggerRouter = getEngine().getFileSyncService().getFileTriggerRouter(fileSnapshot.getTriggerId(), fileSnapshot.getRouterId());
if (triggerRouter != null) {
FileTrigger fileTrigger = triggerRouter.getFileTrigger();
if (fileTrigger.isDeleteAfterSync()) {
File file = fileTrigger.createSourceFile(fileSnapshot);
if (!file.isDirectory()) {
filesToDelete.add(file);
if (fileTrigger.isSyncOnCtlFile()) {
File ctlFile = getEngine().getFileSyncService().getControleFile(file);
filesToDelete.add(ctlFile);
}
}
} else if (getEngine().getParameterService().is(ParameterConstants.FILE_SYNC_DELETE_CTL_FILE_AFTER_SYNC, false)) {
File file = fileTrigger.createSourceFile(fileSnapshot);
if (!file.isDirectory()) {
if (fileTrigger.isSyncOnCtlFile()) {
File ctlFile = getEngine().getFileSyncService().getControleFile(file);
filesToDelete.add(ctlFile);
}
}
}
}
if (filesToDelete != null && filesToDelete.size() > 0) {
for (File file : filesToDelete) {
if (file != null && file.exists()) {
log.debug("Deleting the '{}' file", file.getAbsolutePath());
boolean deleted = FileUtils.deleteQuietly(file);
if (!deleted) {
log.warn("Failed to 'delete on sync' the {} file", file.getAbsolutePath());
}
}
file = null;
}
filesToDelete = null;
}
}
}
use of org.jumpmind.symmetric.model.DataMetaData in project symmetric-ds by JumpMind.
the class AbstractFileParsingRouter method routeToNodes.
@Override
public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
Map<String, String> newData = getNewDataAsString(null, dataMetaData, getEngine().getSymmetricDialect());
String targetTableName = dataMetaData.getRouter().getTargetTableName();
String fileName = newData.get("FILE_NAME");
String relativeDir = newData.get("RELATIVE_DIR");
String triggerId = newData.get("TRIGGER_ID");
String lastEventType = newData.get("LAST_EVENT_TYPE");
String routerExpression = dataMetaData.getRouter().getRouterExpression();
String channelId = "default";
String filePath = relativeDir + "/" + fileName;
IContextService contextService = getEngine().getContextService();
if (lastEventType.equals(DataEventType.DELETE.toString())) {
log.debug("File deleted (" + filePath + "), cleaning up context value.");
contextService.delete(filePath);
} else {
if (routerExpression != null) {
String[] keyValues = routerExpression.split(",");
if (keyValues.length > 0) {
for (int i = 0; i < keyValues.length; i++) {
String[] keyValue = keyValues[i].split("=");
if (keyValue.length > 1) {
if (ROUTER_EXPRESSION_CHANNEL_KEY.equals(keyValue[0])) {
channelId = keyValue[1];
}
}
}
}
}
if (triggerId != null) {
String baseDir = getEngine().getFileSyncService().getFileTrigger(triggerId).getBaseDir();
File file = createSourceFile(baseDir, relativeDir, fileName);
Integer lineNumber = contextService.getString(filePath) == null ? 0 : new Integer(contextService.getString(filePath));
List<String> dataRows = parse(file, lineNumber);
String columnNames = getColumnNames();
String nodeList = buildNodeList(nodes);
String externalData = new StringBuilder(EXTERNAL_DATA_TRIGGER_KEY).append("=").append(triggerId).append(",").append(EXTERNAL_DATA_ROUTER_KEY).append("=").append(dataMetaData.getRouter().getRouterId()).append(",").append(EXTERNAL_DATA_FILE_DATA_ID).append("=").append(dataMetaData.getData().getDataId()).toString();
for (String row : dataRows) {
Data data = new Data();
data.setChannelId(channelId);
data.setDataEventType(DataEventType.INSERT);
data.setRowData(row);
data.setTableName(targetTableName);
data.setNodeList(nodeList);
data.setTriggerHistory(getTriggerHistory(targetTableName, columnNames));
data.setExternalData(externalData);
data.setDataId(getEngine().getDataService().insertData(data));
lineNumber++;
}
if (!dataRows.isEmpty()) {
try {
contextService.save(filePath, lineNumber.toString());
deleteFileIfNecessary(dataMetaData);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return new HashSet<String>();
}
Aggregations