Search in sources :

Example 1 with ImportDataTableFailedEvent

use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent in project cubrid-manager by CUBRID.

the class AbsImportRunnable method run.

public void run() {
    handleEvent(new ImportDataBeginOneTableEvent(tableName));
    try {
        performCreateTable(tableName);
    } catch (Exception e) {
        writeErrorLog("Create table failed:" + e.getMessage());
        LOGGER.error("", e);
    }
    DataType.setNULLValuesForImport(importConfig.getNullValueList().toArray(new String[importConfig.getNullValueList().size()]));
    try {
        initConnection();
        doRun();
    } catch (final Exception e) {
        handleEvent(new ImportDataTableFailedEvent(tableName));
        LOGGER.error("", e);
        Display.getDefault().syncExec(new Runnable() {

            public void run() {
                String msg = Messages.errorOpenFile + StringUtil.NEWLINE + tableConfig.getFilePath() + StringUtil.NEWLINE + StringUtil.NEWLINE + e.getMessage();
                CommonUITool.openErrorBox(msg);
            }
        });
    } finally {
        finish();
    }
    handleEvent(new ImportDataFinishOneTableEvent(tableName));
    if (progressManager != null) {
        progressManager.taskFinished(this);
    }
}
Also used : ImportDataTableFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent) ImportDataBeginOneTableEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataBeginOneTableEvent) ImportDataFinishOneTableEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFinishOneTableEvent) SQLException(java.sql.SQLException) DataFormatException(com.cubrid.common.ui.cubrid.table.dialog.DataFormatException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParamSetException(com.cubrid.common.ui.spi.util.paramSetter.ParamSetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with ImportDataTableFailedEvent

use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent in project cubrid-manager by CUBRID.

the class ImportFromTxtRunnable method doRun.

/* (non-Javadoc)
	 * @see com.cubrid.common.ui.cubrid.table.dialog.imp.progress.AbsImportDataThread#doRun()
	 */
@Override
protected void doRun() throws Exception {
    // FIXME move this logic to core module
    if (pStmt == null) {
        handleEvent(new ImportDataFailedEvent(tableName, tableConfig.getLineCount(), tableConfig.getInsertDML(), "Invalid parameters."));
        return;
    }
    File file = new File(tableConfig.getFilePath());
    boolean isFirstRowAsCoulmn = tableConfig.isFirstRowAsColumn();
    File parentFile;
    if (file.exists()) {
        parentFile = file.getParentFile();
    } else {
        parentFile = null;
    }
    int currentRow = 0;
    TxtReader txtReader = null;
    List<ImportRowData> rowList = new ArrayList<ImportRowData>();
    try {
        if (importConfig.getFilesCharset() == null || importConfig.getFilesCharset().trim().length() == 0) {
            txtReader = new TxtReader(new FileReader(file), importConfig.getColumnDelimiter(), importConfig.getRowDelimiter());
        } else {
            txtReader = new TxtReader(new InputStreamReader(new FileInputStream(file), importConfig.getFilesCharset()), importConfig.getColumnDelimiter(), importConfig.getRowDelimiter());
        }
        if (isFirstRowAsCoulmn) {
            txtReader.readNextRow();
            currentRow++;
        }
        String[] txtRow;
        ImportRowData rowData = null;
        while ((txtRow = txtReader.readNextRow()) != null) {
            boolean isSuccess = true;
            try {
                if (txtRow == null || txtRow.length == 0) {
                    continue;
                }
                /*Process the row data*/
                rowData = processRowData(txtRow, null, currentRow, parentFile);
                rowList.add(rowData);
                pStmt.addBatch();
                currentRow++;
                /*Process commit*/
                if (rowList.size() >= importConfig.getCommitLine()) {
                    commit(rowList);
                }
                if (isCanceled) {
                    return;
                }
            } catch (SQLException ex) {
                isSuccess = false;
                LOGGER.debug(ex.getMessage());
            } catch (StopPerformException ex) {
                isSuccess = false;
                handleEvent(new ImportDataTableFailedEvent(tableName));
                LOGGER.debug("Stop import by user setting.");
                break;
            } catch (OutOfMemoryError error) {
                throw new RuntimeException(error);
            } finally {
                if (!isSuccess) {
                    rowData.setStatus(ImportStatus.STATUS_COMMIT_FAILED);
                    writeErrorLog(rowData);
                }
            }
        }
        if (rowList.size() > 0) {
            commit(rowList);
            rowList = null;
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (OutOfMemoryError error) {
        throw new RuntimeException(error);
    } finally {
        if (txtReader != null) {
            try {
                txtReader.close();
                txtReader = null;
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
    }
}
Also used : ImportRowData(com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportRowData) InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TxtReader(com.cubrid.common.core.reader.TxtReader) FileInputStream(java.io.FileInputStream) ImportDataFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent) ImportDataTableFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent) FileReader(java.io.FileReader) File(java.io.File)

Example 3 with ImportDataTableFailedEvent

use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent in project cubrid-manager by CUBRID.

the class ImportFromXlsxRunnable method doRun.

/* (non-Javadoc)
	 * @see com.cubrid.common.ui.cubrid.table.dialog.imp.progress.AbsImportDataThread#doRun()
	 */
@Override
protected void doRun() throws Exception {
    // FIXME move this logic to core module
    if (pStmt == null) {
        handleEvent(new ImportDataFailedEvent(tableName, tableConfig.getLineCount(), tableConfig.getInsertDML(), "Invalid parameters."));
        return;
    }
    String fileName = tableConfig.getFilePath();
    final File parentFile;
    File file = new File(fileName);
    if (file.exists()) {
        parentFile = file.getParentFile();
    } else {
        parentFile = null;
    }
    final XLSXImportFileHandler importFileHandler = (XLSXImportFileHandler) ImportFileHandlerFactory.getHandler(fileName, importConfig);
    final List<ImportRowData> rowList = new ArrayList<ImportRowData>();
    XlsxReaderHandler xlsxReader = new XlsxReaderHandler((XLSXImportFileHandler) importFileHandler) {

        boolean isFirstRowAsColumn = tableConfig.isFirstRowAsColumn();

        private String[] rowContentArray;

        private ImportRowData rowData = null;

        private boolean isFailed = false;

        public void operateRows(int sheetIndex, List<String> rowContentlist) {
            if (isFailed) {
                return;
            }
            if (currentRow == getTitleRow()) {
                return;
            }
            if (rowContentlist == null) {
                return;
            }
            rowContentArray = new String[rowContentlist.size()];
            rowContentlist.toArray(rowContentArray);
            boolean isSuccess = true;
            try {
                /*Process the row data*/
                rowData = processRowData(rowContentArray, null, currentRow, parentFile);
                rowList.add(rowData);
                pStmt.addBatch();
                importedRow++;
                if (rowList.size() >= importConfig.getCommitLine()) {
                    commit(rowList);
                }
                if (isCanceled) {
                    return;
                }
            } catch (SQLException ex) {
                isSuccess = false;
                LOGGER.debug(ex.getMessage());
            } catch (StopPerformException ex) {
                isSuccess = false;
                handleEvent(new ImportDataTableFailedEvent(tableName));
                LOGGER.debug("Stoped by user setting.");
                isFailed = true;
            } catch (OutOfMemoryError error) {
                throw new RuntimeException(error);
            } finally {
                if (!isSuccess) {
                    rowData.setStatus(ImportStatus.STATUS_COMMIT_FAILED);
                    writeErrorLog(rowData);
                }
            }
        }

        public void startDocument() {
            if (isFirstRowAsColumn) {
                setTitleRow(0);
            }
        }
    };
    xlsxReader.process(fileName);
    if (rowList.size() > 0) {
        commit(rowList);
    }
}
Also used : XLSXImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler) ImportRowData(com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportRowData) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) XlsxReaderHandler(com.cubrid.common.ui.cubrid.table.control.XlsxReaderHandler) ImportDataFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent) ImportDataTableFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 4 with ImportDataTableFailedEvent

use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent in project cubrid-manager by CUBRID.

the class ImportFromXlsRunnable method doRun.

/* (non-Javadoc)
	 * @see com.cubrid.common.ui.cubrid.table.dialog.imp.progress.AbsImportDataThread#doRun()
	 */
@Override
protected void doRun() throws Exception {
    // FIXME move this logic to core module
    if (pStmt == null) {
        handleEvent(new ImportDataFailedEvent(tableName, tableConfig.getLineCount(), tableConfig.getInsertDML(), "Invalid parameters."));
        return;
    }
    String fileName = tableConfig.getFilePath();
    boolean isFirstRowAsColumn = tableConfig.isFirstRowAsColumn();
    File parentFile;
    File file = new File(fileName);
    if (file.exists()) {
        parentFile = file.getParentFile();
    } else {
        parentFile = null;
    }
    int start = 0;
    if (isFirstRowAsColumn) {
        start = 1;
    }
    try {
        XLSImportFileHandler fileHandler = (XLSImportFileHandler) ImportFileHandlerFactory.getHandler(fileName, importConfig);
        Sheet[] sheets = fileHandler.getSheets();
        ImportFileDescription fileDesc = getFileDescription(fileHandler);
        int currentRow = 0;
        List<ImportRowData> rowList = new ArrayList<ImportRowData>();
        for (int sheetNum = 0; sheetNum < sheets.length; sheetNum++) {
            int rows = fileDesc.getItemsNumberOfSheets().get(sheetNum);
            Sheet sheet = sheets[sheetNum];
            String[] rowContent = null;
            String[] patterns = null;
            ImportRowData rowData = null;
            String content = null;
            String pattern = null;
            for (int i = start; i < rows; i++) {
                boolean isSuccess = true;
                try {
                    int columns = sheet.getColumns();
                    for (int j = 0; j < columns; j++) {
                        rowContent = new String[columns];
                        patterns = new String[columns];
                        Cell[] cells = sheet.getRow(i);
                        for (int k = 0; k < cells.length; k++) {
                            Cell cell = cells[k];
                            content = null;
                            pattern = null;
                            if (cell == null) {
                                content = null;
                            } else if (cell instanceof EmptyCell) {
                                content = null;
                            } else {
                                content = cell.getContents();
                                CellFormat format = cell.getCellFormat();
                                if (format != null && format.getFormat() != null) {
                                    pattern = format.getFormat().getFormatString();
                                }
                            }
                            rowContent[k] = content;
                            patterns[k] = pattern;
                        }
                    }
                    /*Process the row data*/
                    rowData = processRowData(rowContent, patterns, currentRow, parentFile);
                    pStmt.addBatch();
                    rowList.add(rowData);
                    currentRow++;
                    /*Process commit*/
                    if (rowList.size() >= importConfig.getCommitLine()) {
                        commit(rowList);
                    }
                    if (isCanceled) {
                        return;
                    }
                } catch (SQLException ex) {
                    isSuccess = false;
                    LOGGER.debug(ex.getMessage());
                } catch (StopPerformException ex) {
                    isSuccess = false;
                    handleEvent(new ImportDataTableFailedEvent(tableName));
                    LOGGER.debug("Stop import by user setting.");
                    break;
                } catch (OutOfMemoryError error) {
                    throw new RuntimeException(error);
                } finally {
                    if (!isSuccess) {
                        rowData.setStatus(ImportStatus.STATUS_COMMIT_FAILED);
                        writeErrorLog(rowData);
                    }
                }
            }
        }
        if (rowList.size() > 0) {
            commit(rowList);
        }
    } catch (BiffException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } catch (OutOfMemoryError error) {
        throw new RuntimeException(error);
    }
}
Also used : CellFormat(jxl.format.CellFormat) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ImportDataTableFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent) ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) XLSImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler) Cell(jxl.Cell) EmptyCell(jxl.biff.EmptyCell) BiffException(jxl.read.biff.BiffException) ImportRowData(com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportRowData) EmptyCell(jxl.biff.EmptyCell) IOException(java.io.IOException) BiffException(jxl.read.biff.BiffException) IOException(java.io.IOException) SQLException(java.sql.SQLException) ImportDataFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent) File(java.io.File) Sheet(jxl.Sheet)

Example 5 with ImportDataTableFailedEvent

use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent in project cubrid-manager by CUBRID.

the class ImportFromCsvRunnable method doRun.

/* (non-Javadoc)
	 * @see com.cubrid.common.ui.cubrid.table.dialog.imp.progress.AbsImportDataThread#doRun()
	 */
protected void doRun() throws Exception {
    // FIXME move this logic to core module
    if (pStmt == null) {
        handleEvent(new ImportDataFailedEvent(tableName, tableConfig.getLineCount(), tableConfig.getInsertDML(), "Invalid parameters."));
        return;
    }
    File file = new File(tableConfig.getFilePath());
    boolean isFirstRowAsColumn = tableConfig.isFirstRowAsColumn();
    String fileCharset = importConfig.getFilesCharset();
    File parentFile;
    if (file.exists()) {
        parentFile = file.getParentFile();
    } else {
        parentFile = null;
    }
    CSVReader csvReader = null;
    int currentRow = 0;
    List<ImportRowData> rowList = new ArrayList<ImportRowData>();
    try {
        if (fileCharset == null || fileCharset.trim().length() == 0) {
            csvReader = new CSVReader(new FileReader(file));
        } else {
            csvReader = new CSVReader(new InputStreamReader(new FileInputStream(file), fileCharset));
        }
        if (isFirstRowAsColumn) {
            csvReader.readNext();
            currentRow++;
        }
        String[] cvsRow;
        ImportRowData rowData = null;
        while ((cvsRow = csvReader.readNext()) != null) {
            boolean isSuccess = true;
            try {
                /*Process the row data*/
                rowData = processRowData(cvsRow, null, currentRow, parentFile);
                rowList.add(rowData);
                pStmt.addBatch();
                currentRow++;
                /*Process commit*/
                if (rowList.size() >= importConfig.getCommitLine()) {
                    commit(rowList);
                }
                if (isCanceled) {
                    return;
                }
            } catch (SQLException ex) {
                isSuccess = false;
                LOGGER.debug(ex.getMessage());
            } catch (StopPerformException ex) {
                isSuccess = false;
                handleEvent(new ImportDataTableFailedEvent(tableName));
                break;
            } finally {
                if (!isSuccess) {
                    rowData.setStatus(ImportStatus.STATUS_COMMIT_FAILED);
                    writeErrorLog(rowData);
                }
            }
        }
        if (rowList.size() > 0) {
            commit(rowList);
            rowList = null;
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (OutOfMemoryError error) {
        throw new RuntimeException(error);
    } finally {
        if (csvReader != null) {
            try {
                csvReader.close();
                csvReader = null;
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
    }
}
Also used : ImportRowData(com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportRowData) InputStreamReader(java.io.InputStreamReader) CSVReader(com.cubrid.common.core.reader.CSVReader) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ImportDataFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent) ImportDataTableFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

ImportDataTableFailedEvent (com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent)5 SQLException (java.sql.SQLException)5 ImportDataFailedEvent (com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent)4 ImportRowData (com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportRowData)4 File (java.io.File)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 FileInputStream (java.io.FileInputStream)2 FileReader (java.io.FileReader)2 InputStreamReader (java.io.InputStreamReader)2 CSVReader (com.cubrid.common.core.reader.CSVReader)1 TxtReader (com.cubrid.common.core.reader.TxtReader)1 XlsxReaderHandler (com.cubrid.common.ui.cubrid.table.control.XlsxReaderHandler)1 DataFormatException (com.cubrid.common.ui.cubrid.table.dialog.DataFormatException)1 ImportDataBeginOneTableEvent (com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataBeginOneTableEvent)1 ImportDataFinishOneTableEvent (com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFinishOneTableEvent)1 ImportFileDescription (com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription)1 XLSImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler)1 XLSXImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler)1 ParamSetException (com.cubrid.common.ui.spi.util.paramSetter.ParamSetException)1