Search in sources :

Example 1 with DataMetaData

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());
}
Also used : TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Data(org.jumpmind.symmetric.model.Data) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) Test(org.junit.Test)

Example 2 with DataMetaData

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());
}
Also used : TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Data(org.jumpmind.symmetric.model.Data) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) Test(org.junit.Test)

Example 3 with DataMetaData

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());
}
Also used : TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Data(org.jumpmind.symmetric.model.Data) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) Test(org.junit.Test)

Example 4 with DataMetaData

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;
        }
    }
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) Table(org.jumpmind.db.model.Table) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) FileTrigger(org.jumpmind.symmetric.model.FileTrigger) ArrayList(java.util.ArrayList) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) File(java.io.File)

Example 5 with DataMetaData

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>();
}
Also used : IContextService(org.jumpmind.symmetric.service.IContextService) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

DataMetaData (org.jumpmind.symmetric.model.DataMetaData)16 Data (org.jumpmind.symmetric.model.Data)15 TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)13 Table (org.jumpmind.db.model.Table)12 Test (org.junit.Test)12 HashSet (java.util.HashSet)11 Node (org.jumpmind.symmetric.model.Node)9 NodeChannel (org.jumpmind.symmetric.model.NodeChannel)9 Router (org.jumpmind.symmetric.model.Router)9 File (java.io.File)2 CsvData (org.jumpmind.symmetric.io.data.CsvData)2 ArrayList (java.util.ArrayList)1 SymmetricException (org.jumpmind.symmetric.SymmetricException)1 FileSnapshot (org.jumpmind.symmetric.model.FileSnapshot)1 FileTrigger (org.jumpmind.symmetric.model.FileTrigger)1 FileTriggerRouter (org.jumpmind.symmetric.model.FileTriggerRouter)1 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)1 DelayRoutingException (org.jumpmind.symmetric.route.DelayRoutingException)1 IDataRouter (org.jumpmind.symmetric.route.IDataRouter)1 IContextService (org.jumpmind.symmetric.service.IContextService)1