Search in sources :

Example 11 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class DatabaseXmlUtil method nextTable.

public static Table nextTable(XmlPullParser parser, String catalog, String schema) {
    try {
        Table table = null;
        ForeignKey fk = null;
        IIndex index = null;
        boolean done = false;
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT && !done) {
            switch(eventType) {
                case XmlPullParser.START_TAG:
                    String name = parser.getName();
                    if (name.equalsIgnoreCase("table")) {
                        table = new Table();
                        table.setCatalog(catalog);
                        table.setSchema(schema);
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("name")) {
                                table.setName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("description")) {
                                table.setDescription(attributeValue);
                            }
                        }
                    } else if (name.equalsIgnoreCase("column")) {
                        Column column = new Column();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("name")) {
                                column.setName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("primaryKey")) {
                                column.setPrimaryKey(FormatUtils.toBoolean(attributeValue));
                            } else if (attributeName.equalsIgnoreCase("required")) {
                                column.setRequired(FormatUtils.toBoolean(attributeValue));
                            } else if (attributeName.equalsIgnoreCase("type")) {
                                column.setMappedType(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("size")) {
                                column.setSize(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("default")) {
                                if (StringUtils.isNotBlank(attributeValue)) {
                                    column.setDefaultValue(attributeValue);
                                }
                            } else if (attributeName.equalsIgnoreCase("autoIncrement")) {
                                column.setAutoIncrement(FormatUtils.toBoolean(attributeValue));
                            } else if (attributeName.equalsIgnoreCase("javaName")) {
                                column.setJavaName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("description")) {
                                column.setDescription(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("unique")) {
                                column.setUnique(FormatUtils.toBoolean(attributeValue));
                            }
                        }
                        if (table != null) {
                            table.addColumn(column);
                        }
                    } else if (name.equalsIgnoreCase("platform-column")) {
                        PlatformColumn platformColumn = new PlatformColumn();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("name")) {
                                platformColumn.setName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("type")) {
                                platformColumn.setType(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("default")) {
                                platformColumn.setDefaultValue(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("size")) {
                                if (isNotBlank(attributeValue)) {
                                    platformColumn.setSize(Integer.parseInt(attributeValue));
                                }
                            } else if (attributeName.equalsIgnoreCase("decimalDigits")) {
                                if (isNotBlank(attributeValue)) {
                                    platformColumn.setDecimalDigits(Integer.parseInt(attributeValue));
                                }
                            }
                        }
                        if (table != null && table.getColumnCount() > 0) {
                            table.getColumn(table.getColumnCount() - 1).addPlatformColumn(platformColumn);
                        }
                    } else if (name.equalsIgnoreCase("foreign-key")) {
                        fk = new ForeignKey();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("name")) {
                                fk.setName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("foreignTable")) {
                                fk.setForeignTableName(attributeValue);
                            }
                        }
                        table.addForeignKey(fk);
                    } else if (name.equalsIgnoreCase("reference")) {
                        Reference ref = new Reference();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("local")) {
                                ref.setLocalColumnName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("foreign")) {
                                ref.setForeignColumnName(attributeValue);
                            }
                        }
                        fk.addReference(ref);
                    } else if (name.equalsIgnoreCase("index") || name.equalsIgnoreCase("unique")) {
                        if (name.equalsIgnoreCase("index")) {
                            index = new NonUniqueIndex();
                        } else {
                            index = new UniqueIndex();
                        }
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("name")) {
                                index.setName(attributeValue);
                            }
                        }
                        table.addIndex(index);
                    } else if (name.equalsIgnoreCase("index-column") || name.equalsIgnoreCase("unique-column")) {
                        IndexColumn indexColumn = new IndexColumn();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("name")) {
                                indexColumn.setName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("size")) {
                                indexColumn.setSize(attributeValue);
                            }
                        }
                        indexColumn.setColumn(table.getColumnWithName(indexColumn.getName()));
                        if (index != null) {
                            index.addColumn(indexColumn);
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("table")) {
                        done = true;
                    } else if (name.equalsIgnoreCase("index") || name.equalsIgnoreCase("unique")) {
                        index = null;
                    } else if (name.equalsIgnoreCase("table")) {
                        table = null;
                    } else if (name.equalsIgnoreCase("foreign-key")) {
                        fk = null;
                    }
                    break;
            }
            if (!done) {
                eventType = parser.next();
            }
        }
        return table;
    } catch (XmlPullParserException e) {
        throw new IoException(e);
    } catch (IOException e) {
        throw new IoException(e);
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Table(org.jumpmind.db.model.Table) NonUniqueIndex(org.jumpmind.db.model.NonUniqueIndex) Reference(org.jumpmind.db.model.Reference) IOException(java.io.IOException) ForeignKey(org.jumpmind.db.model.ForeignKey) PlatformColumn(org.jumpmind.db.model.PlatformColumn) IndexColumn(org.jumpmind.db.model.IndexColumn) Column(org.jumpmind.db.model.Column) IndexColumn(org.jumpmind.db.model.IndexColumn) PlatformColumn(org.jumpmind.db.model.PlatformColumn) IoException(org.jumpmind.exception.IoException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NonUniqueIndex(org.jumpmind.db.model.NonUniqueIndex) UniqueIndex(org.jumpmind.db.model.UniqueIndex)

Example 12 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class DataProcessor method forEachDataInTable.

protected int forEachDataInTable(DataContext context, boolean processTable, Batch batch) {
    int dataRow = 0;
    IgnoreBatchException ignore = null;
    long startTime = System.currentTimeMillis();
    long ts = System.currentTimeMillis();
    do {
        batch.startTimer(STAT_READ_DATA);
        currentData = dataReader.nextData();
        context.setData(currentData);
        batch.incrementDataReadMillis(batch.endTimer(STAT_READ_DATA));
        if (currentData != null) {
            dataRow++;
            if (processTable || !currentData.requiresTable()) {
                try {
                    batch.startTimer(STAT_WRITE_DATA);
                    batch.incrementLineCount();
                    context.getWriter().write(currentData);
                    batch.incrementDataWriteMillis(batch.endTimer(STAT_WRITE_DATA));
                } catch (IgnoreBatchException ex) {
                    ignore = ex;
                    processTable = false;
                }
            }
        }
        if (System.currentTimeMillis() - ts > 60000 && context.getWriter() != null) {
            Statistics stats = context.getWriter().getStatistics().get(batch);
            if (stats != null) {
                log.info("Batch '{}', for node '{}', for process '{}' has been processing for {} seconds.  The following stats have been gathered: {}", new Object[] { batch.getBatchId(), batch.getTargetNodeId(), name, (System.currentTimeMillis() - startTime) / 1000, stats.toString() });
            }
            ts = System.currentTimeMillis();
        }
        if (Thread.currentThread().isInterrupted()) {
            throw new IoException("This thread was interrupted");
        }
    } while (currentData != null);
    if (ignore != null) {
        throw ignore;
    }
    return dataRow;
}
Also used : IoException(org.jumpmind.exception.IoException) IgnoreBatchException(org.jumpmind.symmetric.io.data.writer.IgnoreBatchException) Statistics(org.jumpmind.util.Statistics)

Example 13 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class AbstractDataReader method toReader.

protected static Reader toReader(File file) {
    try {
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader in = new InputStreamReader(fis, IoConstants.ENCODING);
        return new BufferedReader(in);
    } catch (IOException ex) {
        throw new IoException(ex);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 14 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class DbImport method importTables.

public void importTables(String importData, String tableName) {
    try {
        ByteArrayInputStream in = new ByteArrayInputStream(importData.getBytes());
        importTables(in, tableName);
        in.close();
    } catch (IOException e) {
        throw new IoException("Failed to read '" + importData + "' for table '" + tableName + "'", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Example 15 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class FtpDataWriter method println.

protected void println(String... data) {
    FileInfo fileInfo = fileInfoByTable.get(table.getFullyQualifiedTableName());
    if (fileInfo != null) {
        try {
            StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < data.length; i++) {
                if (i != 0) {
                    buffer.append(",");
                }
                buffer.append(data[i]);
            }
            buffer.append("\n");
            fileInfo.outputFileWriter.write(buffer.toString());
            long byteCount = buffer.length();
            statistics.get(batch).increment(DataWriterStatisticConstants.BYTECOUNT, byteCount);
        } catch (IOException e) {
            throw new IoException(e);
        }
    }
}
Also used : IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Aggregations

IoException (org.jumpmind.exception.IoException)48 IOException (java.io.IOException)41 File (java.io.File)8 Table (org.jumpmind.db.model.Table)8 BufferedReader (java.io.BufferedReader)6 Column (org.jumpmind.db.model.Column)6 BufferedWriter (java.io.BufferedWriter)5 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)4 FileWriter (java.io.FileWriter)4 OutputStreamWriter (java.io.OutputStreamWriter)4 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)4 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 SymmetricException (org.jumpmind.symmetric.SymmetricException)3 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)3 ProcessInfoKey (org.jumpmind.symmetric.model.ProcessInfoKey)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 BufferedInputStream (java.io.BufferedInputStream)2