use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent 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);
}
}
}
}
use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent 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);
}
}
use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent in project cubrid-manager by CUBRID.
the class AbsImportRunnable method commit.
/**
*
* Commit the data
*
*
* @param monitor IProgressMonitor
* @param currentRow int
* @throws SQLException The exception
*/
protected void commit(List<ImportRowData> rowList) throws SQLException {
if (pStmt == null) {
return;
}
try {
pStmt.executeBatch();
QueryUtil.commit(conn);
handleEvent(new ImportDataSuccessEvent(tableName, rowList.size()));
LOGGER.debug("Committed : " + rowList.size());
} catch (SQLException ex) {
QueryUtil.rollback(conn);
ImportDataFailedEvent failedEvt = new ImportDataFailedEvent(tableName, rowList.size(), "", ex.getMessage());
handleEvent(failedEvt);
LOGGER.debug("Failed : " + rowList.size());
writeErrorLog(rowList);
} finally {
rowList.clear();
try {
pStmt.clearBatch();
} catch (SQLException e) {
LOGGER.error(e.getMessage());
processSQLException(e);
}
}
}
use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent in project cubrid-manager by CUBRID.
the class ImportFromSQLRunnable method commit.
/**
* Commit the data
*
* @param importDataEventHandler IImportDataEventHandler
* @param rowList List<RowData>
*/
protected void commit(List<ImportRowData> rowList) {
// FIXME move this logic to core module
if (stmt == null || conn == null) {
return;
}
String sql = "";
int importCount = 0;
long currentRunCount = 0;
int totalWorkedSize = 0;
List<ImportRowData> batchDataList = new ArrayList<ImportRowData>();
for (ImportRowData rowData : rowList) {
batchDataList.add(rowData);
sql = rowData.getSql();
try {
importCount++;
currentRunCount++;
totalWorkedSize += rowData.getWorkSize();
stmt.execute(sql);
if (importCount % commitCount == 0) {
QueryUtil.commit(conn);
ImportDataSuccessEvent successEvt = new ImportDataSuccessEvent(fileName, importCount);
successEvt.setWorkedSize(totalWorkedSize);
handleEvent(successEvt);
LOGGER.debug("Committed : currentRunCount={}, commitCount={}", currentRunCount, importCount);
importCount = 0;
totalWorkedSize = 0;
batchDataList.clear();
}
} catch (Exception e) {
for (ImportRowData batchData : batchDataList) {
String tempSql = batchData.getSql();
String errMessage = Messages.msgFailedByRollback;
if (StringUtil.isEqual(tempSql, sql)) {
errMessage = e.getMessage();
}
ImportDataFailedEvent failedEvt = new ImportDataFailedEvent(fileName, 1, tempSql, errMessage);
failedEvt.setWorkedSize(batchData.getWorkSize());
handleEvent(failedEvt);
writeErrorLog(batchData);
}
QueryUtil.rollback(conn);
LOGGER.debug("Execute SQL from SQL file sql : {}, error message: {}", sql, e);
importCount = 0;
totalWorkedSize = 0;
batchDataList.clear();
}
}
if (importCount > 0) {
QueryUtil.commit(conn);
ImportDataSuccessEvent successEvt = new ImportDataSuccessEvent(fileName, importCount);
successEvt.setWorkedSize(totalWorkedSize);
handleEvent(successEvt);
LOGGER.debug("Committed : currentRunCount={}, commitCount={}", currentRunCount, importCount);
importCount = 0;
totalWorkedSize = 0;
batchDataList.clear();
}
}
use of com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent 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);
}
}
Aggregations