Search in sources :

Example 1 with TxtReader

use of com.cubrid.common.core.reader.TxtReader in project cubrid-manager by CUBRID.

the class PstmtDataTask method executeFromTxt.

/**
	 *
	 * Do with data from Txt file
	 *
	 * @param monitor IProgressMonitor
	 */
private void executeFromTxt(IProgressMonitor monitor) {
    // FIXME move this logic to core module
    File file = new File(fileName);
    TxtReader txtReader = null;
    try {
        if (fileCharset == null || fileCharset.trim().length() == 0) {
            txtReader = new TxtReader(new FileReader(file), separator);
        } else {
            txtReader = new TxtReader(new InputStreamReader(new FileInputStream(file), fileCharset), separator);
        }
        if (isFirstRowAsColumn) {
            txtReader.readNext();
        }
        int currentRow = 0;
        int rowNum = 0;
        String[] cvsRow;
        while ((cvsRow = txtReader.readNext()) != null && currentRow < rowCount) {
            rowNum++;
            if (startRow >= rowNum) {
                continue;
            }
            for (int j = 0; j < parameterList.size(); j++) {
                PstmtParameter pstmtParameter = parameterList.get(j);
                int column = Integer.parseInt(pstmtParameter.getStringParamValue());
                String content = null;
                if (cvsRow.length > column) {
                    content = cvsRow[column];
                }
                String dataType = DataType.getRealType(pstmtParameter.getDataType());
                content = FieldHandlerUtils.getRealValueForImport(dataType, content, parentFile);
                FormatDataResult formatDataResult = DBAttrTypeFormatter.format(dataType, content, false, dbCharset, true);
                if (formatDataResult.isSuccess()) {
                    PstmtParameter parameter = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), content);
                    parameter.setCharSet(fileCharset);
                    FieldHandlerUtils.setPreparedStatementValue(parameter, pStmt, dbCharset);
                } else {
                    int row = isFirstRowAsColumn ? currentRow + 1 : currentRow;
                    dataTypeErrorHandling(getErrorMsg(row, column, dataType));
                    PstmtParameter parameter = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), null);
                    parameter.setCharSet(fileCharset);
                    FieldHandlerUtils.setPreparedStatementValue(parameter, pStmt, dbCharset);
                }
            }
            if (pStmt != null) {
                pStmt.addBatch();
                monitor.worked(PROGRESS_ROW);
                workedProgress += PROGRESS_ROW;
            }
            currentRow++;
            if (currentRow > 0 && currentRow % commitLineCountOnce == 0) {
                commit(monitor, currentRow);
            }
            if (isCancel) {
                return;
            }
        }
        if (currentRow > 0 && currentRow % commitLineCountOnce > 0) {
            commit(monitor, currentRow);
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (DataFormatException 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 : InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) IOException(java.io.IOException) TxtReader(com.cubrid.common.core.reader.TxtReader) FileInputStream(java.io.FileInputStream) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) FileReader(java.io.FileReader) File(java.io.File)

Example 2 with TxtReader

use of com.cubrid.common.core.reader.TxtReader 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 TxtReader

use of com.cubrid.common.core.reader.TxtReader in project cubrid-manager by CUBRID.

the class TxtImportFileHandler method getSourceFileInfo.

/**
	 * Get the source file information
	 *
	 * @return ImportFileDescription
	 * @throws Exception in process.
	 */
public ImportFileDescription getSourceFileInfo() throws Exception {
    // FIXME move this logic to core module
    final List<String> colsList = new ArrayList<String>();
    final List<Integer> itemsNumberOfSheets = new ArrayList<Integer>();
    final ImportFileDescription importFileDescription = new ImportFileDescription(0, 1, colsList);
    importFileDescription.setItemsNumberOfSheets(itemsNumberOfSheets);
    IRunnableWithProgress runnable = new IRunnableWithProgress() {

        public void run(final IProgressMonitor monitor) {
            monitor.beginTask("", IProgressMonitor.UNKNOWN);
            int totalRowCount = 0;
            TxtReader txtReader = null;
            try {
                if (fileCharset == null || fileCharset.trim().length() == 0) {
                    txtReader = new TxtReader(new FileReader(fileName), separator, rowSeparator);
                } else {
                    txtReader = new TxtReader(new InputStreamReader(new FileInputStream(fileName), fileCharset), separator, rowSeparator);
                }
                String[] txtRow = txtReader.readNextRow();
                if (txtRow != null) {
                    totalRowCount++;
                    for (String title : txtRow) {
                        colsList.add(title);
                    }
                }
                while (!monitor.isCanceled() && txtReader.readNextRow() != null) {
                    totalRowCount++;
                }
                itemsNumberOfSheets.add(Integer.valueOf(totalRowCount));
                if (monitor.isCanceled()) {
                    throw new InterruptedException();
                }
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
                throw new RuntimeException(e);
            } finally {
                importFileDescription.setTotalCount(totalRowCount);
                importFileDescription.setFirstRowCols(colsList);
                importFileDescription.setItemsNumberOfSheets(itemsNumberOfSheets);
                closeFile(txtReader);
                monitor.done();
            }
        }
    };
    PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
    return importFileDescription;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) TxtReader(com.cubrid.common.core.reader.TxtReader) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) FileReader(java.io.FileReader)

Aggregations

TxtReader (com.cubrid.common.core.reader.TxtReader)3 FileInputStream (java.io.FileInputStream)3 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 File (java.io.File)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 ImportDataFailedEvent (com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent)1 ImportDataTableFailedEvent (com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent)1 ImportRowData (com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportRowData)1 ImportFileDescription (com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription)1 FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1