Search in sources :

Example 11 with Batch

use of org.jumpmind.symmetric.io.data.Batch in project symmetric-ds by JumpMind.

the class DataExtractorService method cleanupIgnoredBatch.

protected void cleanupIgnoredBatch(Node sourceNode, Node targetNode, OutgoingBatch currentBatch, IDataWriter writer) {
    Batch batch = new Batch(BatchType.EXTRACT, currentBatch.getBatchId(), currentBatch.getChannelId(), symmetricDialect.getBinaryEncoding(), sourceNode.getNodeId(), currentBatch.getNodeId(), currentBatch.isCommonFlag());
    batch.setIgnored(true);
    try {
        IStagedResource resource = getStagedResource(currentBatch);
        if (resource != null) {
            resource.delete();
        }
        DataContext ctx = new DataContext(batch);
        ctx.put("targetNode", targetNode);
        ctx.put("sourceNode", sourceNode);
        writer.open(ctx);
        writer.start(batch);
        writer.end(batch, false);
    } finally {
        writer.close();
    }
}
Also used : DataContext(org.jumpmind.symmetric.io.data.DataContext) Batch(org.jumpmind.symmetric.io.data.Batch) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch) IStagedResource(org.jumpmind.symmetric.io.stage.IStagedResource)

Example 12 with Batch

use of org.jumpmind.symmetric.io.data.Batch in project symmetric-ds by JumpMind.

the class SymXmlDataReader method readNext.

protected Object readNext() {
    try {
        Map<String, String> rowData = new LinkedHashMap<String, String>();
        String columnName = null;
        Table lastTable = this.table;
        int eventType = parser.next();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch(eventType) {
                case XmlPullParser.TEXT:
                    if (columnName != null) {
                        rowData.put(columnName, parser.getText());
                        columnName = null;
                    }
                    break;
                case XmlPullParser.START_TAG:
                    String name = parser.getName();
                    if ("row".equalsIgnoreCase(name)) {
                        table = new Table();
                        data = new CsvData();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("entity")) {
                                table.setName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("catalog")) {
                                table.setCatalog(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("schema")) {
                                table.setSchema(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("dml")) {
                                if (attributeValue.equals("I")) {
                                    data.setDataEventType(DataEventType.INSERT);
                                } else if (attributeValue.equals("U")) {
                                    data.setDataEventType(DataEventType.UPDATE);
                                } else if (attributeValue.equals("D")) {
                                    data.setDataEventType(DataEventType.DELETE);
                                } else if (attributeValue.equals("C")) {
                                    data.setDataEventType(DataEventType.CREATE);
                                } else if (attributeValue.equals("S")) {
                                    data.setDataEventType(DataEventType.SQL);
                                } else if (attributeValue.equals("B")) {
                                    data.setDataEventType(DataEventType.BSH);
                                } else if (attributeValue.equals("R")) {
                                    data.setDataEventType(DataEventType.RELOAD);
                                }
                            }
                        }
                    } else if ("data".equalsIgnoreCase(name)) {
                        boolean nullValue = false;
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if ("key".equalsIgnoreCase(attributeName)) {
                                columnName = attributeValue;
                            } else if ("xsi:nil".equalsIgnoreCase(attributeName)) {
                                nullValue = true;
                            }
                        }
                        if (nullValue) {
                            rowData.put(columnName, null);
                            columnName = null;
                        }
                    } else if ("batch".equalsIgnoreCase(name)) {
                        batch = new Batch();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if ("binary".equalsIgnoreCase(attributeName)) {
                                batch.setBinaryEncoding(BinaryEncoding.valueOf(attributeValue));
                            } else if ("nodeid".equalsIgnoreCase(attributeName)) {
                                batch.setSourceNodeId(attributeValue);
                            }
                        }
                        return batch;
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if ("row".equalsIgnoreCase(name)) {
                        String[] columnNames = rowData.keySet().toArray(new String[rowData.keySet().size()]);
                        for (String colName : columnNames) {
                            table.addColumn(new Column(colName));
                        }
                        String[] columnValues = rowData.values().toArray(new String[rowData.values().size()]);
                        data.putParsedData(CsvData.ROW_DATA, columnValues);
                        rowData = new LinkedHashMap<String, String>();
                        if (lastTable == null || !lastTable.equals(table)) {
                            return table;
                        } else {
                            return data;
                        }
                    } else if ("data".equalsIgnoreCase(name)) {
                        columnName = null;
                    }
                    break;
            }
            eventType = parser.next();
        }
        return null;
    } catch (IOException ex) {
        throw new IoException(ex);
    } catch (XmlPullParserException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Table(org.jumpmind.db.model.Table) IOException(java.io.IOException) CsvData(org.jumpmind.symmetric.io.data.CsvData) LinkedHashMap(java.util.LinkedHashMap) Batch(org.jumpmind.symmetric.io.data.Batch) Column(org.jumpmind.db.model.Column) IoException(org.jumpmind.exception.IoException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 13 with Batch

use of org.jumpmind.symmetric.io.data.Batch in project symmetric-ds by JumpMind.

the class ProtocolDataReader method nextData.

public CsvData nextData() {
    if (next instanceof CsvData) {
        CsvData data = (CsvData) next;
        next = null;
        return data;
    } else {
        do {
            next = readNext();
            if (next instanceof CsvData) {
                CsvData data = (CsvData) next;
                next = null;
                return data;
            }
        } while (next != null && !(next instanceof Batch) && !(next instanceof Table));
    }
    return null;
}
Also used : Table(org.jumpmind.db.model.Table) Batch(org.jumpmind.symmetric.io.data.Batch) CsvData(org.jumpmind.symmetric.io.data.CsvData)

Example 14 with Batch

use of org.jumpmind.symmetric.io.data.Batch in project symmetric-ds by JumpMind.

the class ProtocolDataReader method nextTable.

public Table nextTable() {
    if (next instanceof Table) {
        Table table = (Table) next;
        context.setLastParsedTable(table);
        next = null;
        return table;
    } else {
        do {
            next = readNext();
            if (next instanceof Table) {
                Table table = (Table) next;
                context.setLastParsedTable(table);
                next = null;
                return table;
            }
        } while (next != null && !(next instanceof Batch));
    }
    return null;
}
Also used : Table(org.jumpmind.db.model.Table) Batch(org.jumpmind.symmetric.io.data.Batch)

Example 15 with Batch

use of org.jumpmind.symmetric.io.data.Batch in project symmetric-ds by JumpMind.

the class AbstractXmlPublisherExtensionPoint method resend.

@ManagedOperation(description = "Looks up rows in the database and resends them to the publisher")
@ManagedOperationParameters({ @ManagedOperationParameter(name = "args", description = "A pipe deliminated list of key values to use to look up the tables to resend") })
public boolean resend(String args) {
    try {
        String[] argArray = args != null ? args.split("\\|") : new String[0];
        DataContext context = new DataContext();
        IDatabasePlatform platform = engine.getDatabasePlatform();
        for (String tableName : tableNamesToPublishAsGroup) {
            Table table = platform.getTableFromCache(tableName, false);
            List<String[]> dataRowsForTable = readData(table, argArray);
            for (String[] values : dataRowsForTable) {
                Batch batch = new Batch();
                batch.setBinaryEncoding(engine.getSymmetricDialect().getBinaryEncoding());
                batch.setSourceNodeId("republish");
                context.setBatch(batch);
                CsvData data = new CsvData(DataEventType.INSERT);
                data.putParsedData(CsvData.ROW_DATA, values);
                Element xml = getXmlFromCache(context, context.getBatch().getBinaryEncoding(), table.getColumnNames(), data.getParsedData(CsvData.ROW_DATA), table.getPrimaryKeyColumnNames(), data.getParsedData(CsvData.PK_DATA));
                if (xml != null) {
                    toXmlElement(data.getDataEventType(), xml, table.getCatalog(), table.getSchema(), table.getName(), table.getColumnNames(), data.getParsedData(CsvData.ROW_DATA), table.getPrimaryKeyColumnNames(), data.getParsedData(CsvData.PK_DATA));
                }
            }
        }
        if (doesXmlExistToPublish(context)) {
            finalizeXmlAndPublish(context);
            return true;
        } else {
            log.warn(String.format("Failed to resend message for tables %s, columns %s, and args %s", tableNamesToPublishAsGroup, groupByColumnNames, args));
        }
    } catch (RuntimeException ex) {
        log.error(String.format("Failed to resend message for tables %s, columns %s, and args %s", tableNamesToPublishAsGroup, groupByColumnNames, args), ex);
    }
    return false;
}
Also used : DataContext(org.jumpmind.symmetric.io.data.DataContext) IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) Batch(org.jumpmind.symmetric.io.data.Batch) Element(org.jdom.Element) CsvData(org.jumpmind.symmetric.io.data.CsvData) ManagedOperationParameters(org.springframework.jmx.export.annotation.ManagedOperationParameters) ManagedOperation(org.springframework.jmx.export.annotation.ManagedOperation)

Aggregations

Batch (org.jumpmind.symmetric.io.data.Batch)16 Table (org.jumpmind.db.model.Table)9 CsvData (org.jumpmind.symmetric.io.data.CsvData)8 DataContext (org.jumpmind.symmetric.io.data.DataContext)8 Column (org.jumpmind.db.model.Column)4 IOException (java.io.IOException)3 IoException (org.jumpmind.exception.IoException)3 OutgoingBatch (org.jumpmind.symmetric.model.OutgoingBatch)3 LinkedHashMap (java.util.LinkedHashMap)2 DataProcessor (org.jumpmind.symmetric.io.data.DataProcessor)2 IStagedResource (org.jumpmind.symmetric.io.stage.IStagedResource)2 Node (org.jumpmind.symmetric.model.Node)2 Statistics (org.jumpmind.util.Statistics)2 Test (org.junit.Test)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Element (org.jdom.Element)1