Search in sources :

Example 1 with XLSXImportFileHandler

use of com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler in project cubrid-manager by CUBRID.

the class ImportDataFromFileDialog method finish.

/**
	 *
	 * After task finished, call it
	 *
	 *
	 *
	 */
private void finish() {
    if (getStartShowResult()) {
        return;
    } else {
        setStartShowResult(true);
    }
    if (importFileHandler instanceof XLSImportFileHandler) {
        XLSImportFileHandler xlsHandler = (XLSImportFileHandler) importFileHandler;
        xlsHandler.dispose();
    } else if (importFileHandler instanceof XLSXImportFileHandler) {
        XLSXImportFileHandler xlsHandler = (XLSXImportFileHandler) importFileHandler;
        xlsHandler.dispose();
    }
    long endTimestamp = System.currentTimeMillis();
    String spendTime = calcSpendTime(beginTimestamp, endTimestamp);
    List<Status> allStatusList = new ArrayList<Status>();
    int commitedCount = 0;
    boolean isHasError = false;
    boolean isCancel = false;
    List<String> errorList = new ArrayList<String>();
    int totalErrorCount = 0;
    for (PstmtDataTask task : taskList) {
        commitedCount += task.getCommitedCount();
        isCancel = isCancel || task.isCancel();
        if (!task.isSuccess() && task.getErrorMsg() != null) {
            Status status = new Status(IStatus.ERROR, CommonUIPlugin.PLUGIN_ID, task.getErrorMsg());
            allStatusList.add(status);
            isHasError = true;
        }
        errorList.addAll(task.getErrorMsgList());
        totalErrorCount = totalErrorCount + task.getTotalErrorCount();
        if (!errorList.isEmpty()) {
            isHasError = true;
        }
    }
    if (!isHasError && isCancel) {
        String msg = Messages.bind(Messages.msgCancelExecSql, new String[] { String.valueOf(commitedCount), spendTime });
        CommonUITool.openInformationBox(getShell(), Messages.titleExecuteResult, msg);
        close();
        deleteLog();
        return;
    }
    String successMsg = Messages.bind(Messages.msgSuccessExecSql, new String[] { String.valueOf(commitedCount), spendTime });
    if (!isHasError) {
        CommonUITool.openInformationBox(getShell(), Messages.titleExecuteResult, successMsg);
        close();
        deleteLog();
        return;
    }
    if (errorList.isEmpty()) {
        // break
        Status status = new Status(IStatus.INFO, CommonUIPlugin.PLUGIN_ID, successMsg + "\r\n");
        allStatusList.add(0, status);
        IStatus[] errors = new IStatus[allStatusList.size()];
        allStatusList.toArray(errors);
        MultiStatus multiStatus = new MultiStatus(CommonUIPlugin.PLUGIN_ID, IStatus.OK, errors, Messages.msgDetailCause, null);
        String errorMsg = Messages.bind(Messages.errExecSql, new String[] { String.valueOf(commitedCount), spendTime });
        ErrorDialog errorDialog = new ErrorDialog(getShell(), Messages.titleExecuteResult, errorMsg, multiStatus, IStatus.INFO | IStatus.ERROR);
        errorDialog.open();
        if (isHasError) {
            getShell().setVisible(true);
        } else {
            close();
        }
    } else {
        //ignore the error
        String msg = Messages.bind(Messages.importColumnNOTotal, String.valueOf(totalErrorCount)) + "\r\n" + successMsg;
        ImportResultDialog dialog = new ImportResultDialog(getShell(), msg, errorList, errorLogDir);
        dialog.open();
        close();
    }
    deleteLog();
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) XLSXImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler) IStatus(org.eclipse.core.runtime.IStatus) ArrayList(java.util.ArrayList) MultiStatus(org.eclipse.core.runtime.MultiStatus) ErrorDialog(org.eclipse.jface.dialogs.ErrorDialog) XLSImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler)

Example 2 with XLSXImportFileHandler

use of com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler 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 3 with XLSXImportFileHandler

use of com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler in project cubrid-manager by CUBRID.

the class ImportDataFromFileDialog method showResultSet.

/**
	 *
	 * Show the result set by query editor
	 *
	 * @param parameterList List<PstmtParameter>
	 */
private void showResultSet(List<PstmtParameter> parameterList) {
    // FIXME move this logic to core module
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window == null) {
        return;
    }
    String showedSql = sqlTxt.getText();
    String executedSql = showedSql;
    while (executedSql.endsWith(";")) {
        executedSql = executedSql.substring(0, executedSql.length() - 1);
    }
    executedSql += ";";
    int threadCount = threadCountSpinner.getSelection();
    String fileName = fileNameTxt.getText();
    String charset = fileCharsetCombo.getText();
    int totalLine = Integer.parseInt(totalLinesText.getText());
    int commitedLineOnce = commitLineSpinner.getSelection();
    ImportFileHandler handler = ImportFileHandlerFactory.getHandler(fileName, charset);
    PstmtDataTask task = new PstmtDataTask(sqlTxt.getText(), database, fileName, parameterList, 0, totalLine, commitedLineOnce, charset, firstRowAsColumnBtn.getSelection(), false, null, handler);
    List<List<PstmtParameter>> rowParameterList = null;
    try {
        rowParameterList = task.getRowParameterList();
    } catch (RuntimeException ex) {
        String errorMsg = null;
        if (ex.getCause() == null) {
            errorMsg = ex.getMessage();
        } else {
            if (ex.getCause() instanceof OutOfMemoryError) {
                errorMsg = Messages.errNoAvailableMemory;
            } else {
                errorMsg = ex.getCause().getMessage();
            }
        }
        errorMsg = errorMsg == null || errorMsg.trim().length() == 0 ? "Unknown error." : errorMsg;
        CommonUITool.openErrorBox(getShell(), errorMsg);
        return;
    } catch (Exception ex) {
        String errorMsg = ex.getMessage();
        errorMsg = errorMsg == null || errorMsg.trim().length() == 0 ? "Unknown error." : errorMsg;
        CommonUITool.openErrorBox(getShell(), errorMsg);
        return;
    } finally {
        if (importFileHandler instanceof XLSImportFileHandler) {
            XLSImportFileHandler xlsHandler = (XLSImportFileHandler) importFileHandler;
            xlsHandler.dispose();
        } else if (importFileHandler instanceof XLSXImportFileHandler) {
            XLSXImportFileHandler xlsHandler = (XLSXImportFileHandler) importFileHandler;
            xlsHandler.dispose();
        }
    }
    close();
    int rows = rowParameterList == null ? 0 : rowParameterList.size();
    int rowsOfThread = rows;
    if (threadCount > 1) {
        rowsOfThread = rows / threadCount;
    }
    int rowsOfLastThread = rowsOfThread + (rows % threadCount == 0 ? 0 : rows % threadCount);
    int currentRow = 0;
    for (int i = 0; i < threadCount; i++) {
        QueryUnit editorInput = new QueryUnit();
        IEditorPart editor = null;
        try {
            editor = window.getActivePage().openEditor(editorInput, QueryEditorPart.ID);
        } catch (PartInitException e) {
            editor = null;
        }
        if (editor != null) {
            QueryEditorPart queryEditor = ((QueryEditorPart) editor);
            int endRow = currentRow + rowsOfThread;
            if (i == threadCount - 1) {
                endRow = currentRow + rowsOfLastThread;
            }
            List<List<PstmtParameter>> paraList = new ArrayList<List<PstmtParameter>>();
            StringBuffer showedSqlBuffer = new StringBuffer();
            StringBuffer executeSqlBuffer = new StringBuffer();
            for (int j = currentRow; j < endRow; j++) {
                showedSqlBuffer.append(getCommentSqlValue(rowParameterList.get(j)));
                paraList.add(rowParameterList.get(j));
                executeSqlBuffer.append(executedSql);
            }
            showedSqlBuffer.append(showedSql);
            currentRow = endRow;
            if (!queryEditor.isConnected() && database != null) {
                queryEditor.connect(database);
            }
            if (queryEditor.isConnected()) {
                queryEditor.setQuery(showedSqlBuffer.toString(), executedSql, rowParameterList, true, true, false);
            } else {
                queryEditor.setQuery(showedSqlBuffer.toString(), true, false, false);
            }
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) XLSXImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler) XLSImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler) ImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileHandler) XLSXImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler) ArrayList(java.util.ArrayList) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) QueryEditorPart(com.cubrid.common.ui.query.editor.QueryEditorPart) QueryUnit(com.cubrid.common.ui.query.editor.QueryUnit) List(java.util.List) ArrayList(java.util.ArrayList) XLSImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler) PartInitException(org.eclipse.ui.PartInitException)

Example 4 with XLSXImportFileHandler

use of com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler in project cubrid-manager by CUBRID.

the class PstmtDataTask method executeFromXlsx.

/**
	 *
	 * Do with data from excel file
	 *
	 * @param monitor IProgressMonitor
	 * @throws Exception the Exception
	 */
private void executeFromXlsx(final IProgressMonitor monitor) throws Exception {
    // FIXME move this logic to core module
    XlsxReaderHandler xlsxReader = new XlsxReaderHandler((XLSXImportFileHandler) importFileHandler) {

        private SimpleDateFormat datetimeSdf;

        private SimpleDateFormat timestampSdf;

        private SimpleDateFormat dateSdf;

        private SimpleDateFormat timeSdf;

        @Override
        public void operateRows(int sheetIndex, List<String> rowlist) throws SQLException, DataFormatException {
            if (currentRow == getTitleRow()) {
                return;
            }
            XLSXImportFileHandler fileHandler = (XLSXImportFileHandler) importFileHandler;
            List<Integer> itemsNumberOfSheets = null;
            try {
                itemsNumberOfSheets = fileHandler.getSourceFileInfo().getItemsNumberOfSheets();
            } catch (Exception ex) {
                LOGGER.error(ex.getMessage());
                return;
            }
            int rowFromStart = 0;
            for (int i = 0; i < sheetIndex; i++) {
                rowFromStart += itemsNumberOfSheets.get(i);
            }
            rowFromStart += currentRow;
            int absoluteStartRow = getAbsoluteRowNum(startRow, itemsNumberOfSheets);
            if (absoluteStartRow > rowFromStart) {
                return;
            }
            int absoluteEndRow = getAbsoluteRowNum(startRow + rowCount, itemsNumberOfSheets);
            int rowCountIncludingTitle = absoluteEndRow - absoluteStartRow;
            int relativeRow = rowFromStart - absoluteStartRow;
            if (relativeRow > rowCountIncludingTitle - 1) {
                return;
            }
            for (int i = 0; i < parameterList.size(); i++) {
                PstmtParameter pstmtParameter = parameterList.get(i);
                int column = Integer.parseInt(pstmtParameter.getStringParamValue());
                String dataType = DataType.getRealType(pstmtParameter.getDataType());
                String cellContent = rowlist.get(column);
                int cellType = FieldHandlerUtils.getCellType(dataType, cellContent);
                boolean isHaveError = false;
                try {
                    double value;
                    Date dateCon;
                    switch(cellType) {
                        case -1:
                            cellContent = DataType.NULL_EXPORT_FORMAT;
                            break;
                        case 0:
                            if (cellContent.contains(".")) {
                                cellContent = cellContent.substring(0, cellContent.indexOf('.'));
                            }
                            break;
                        case 2:
                            value = Double.parseDouble(cellContent.trim());
                            dateCon = DateUtil.getJavaDate(value);
                            cellContent = datetimeSdf.format(dateCon);
                            break;
                        case 3:
                            value = Double.parseDouble(cellContent.trim());
                            dateCon = DateUtil.getJavaDate(value);
                            cellContent = timestampSdf.format(dateCon);
                            break;
                        case 4:
                            value = Double.parseDouble(cellContent.trim());
                            dateCon = DateUtil.getJavaDate(value);
                            cellContent = dateSdf.format(dateCon);
                            break;
                        case 5:
                            value = Double.parseDouble(cellContent.trim());
                            dateCon = DateUtil.getJavaDate(value);
                            cellContent = timeSdf.format(dateCon);
                            break;
                        default:
                            break;
                    }
                } catch (NumberFormatException e) {
                    isHaveError = true;
                }
                String content = FieldHandlerUtils.getRealValueForImport(dataType, cellContent, 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 {
                    isHaveError = true;
                    PstmtParameter parameter = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), null);
                    parameter.setCharSet(fileCharset);
                    FieldHandlerUtils.setPreparedStatementValue(parameter, pStmt, dbCharset);
                }
                if (isHaveError) {
                    dataTypeErrorHandling(getErrorMsg(i, column, dataType));
                }
            }
            if (pStmt != null) {
                pStmt.addBatch();
                monitor.worked(PROGRESS_ROW);
                workedProgress += PROGRESS_ROW;
            }
            int importedRow = relativeRow + 1;
            if (importedRow > 0 && importedRow % commitLineCountOnce == 0) {
                commit(monitor, importedRow);
            } else {
                if (importedRow == rowCount && importedRow % commitLineCountOnce != 0) {
                    commit(monitor, importedRow);
                }
            }
            if (isCancel) {
                return;
            }
        }

        public void startDocument() {
            if (isFirstRowAsColumn) {
                setTitleRow(0);
            }
            datetimeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault());
            timestampSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            dateSdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            timeSdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        }

        private int getAbsoluteRowNum(int value, List<Integer> itemsNumberOfSheets) {
            if (itemsNumberOfSheets == null || itemsNumberOfSheets.isEmpty()) {
                return value;
            }
            if (getTitleRow() == -1) {
                return value;
            }
            int upLimit = 0;
            int downLimit = 0;
            int absoluteVal = value;
            for (int i = 0; i < itemsNumberOfSheets.size(); i++) {
                int absoluteValIncldingTitle = value + i + 1;
                upLimit += itemsNumberOfSheets.get(i);
                if (i == 0) {
                    if (absoluteValIncldingTitle <= upLimit) {
                        absoluteVal = absoluteValIncldingTitle;
                        break;
                    }
                } else {
                    downLimit += itemsNumberOfSheets.get(i - 1);
                    if (absoluteValIncldingTitle > downLimit && absoluteValIncldingTitle <= upLimit) {
                        absoluteVal = absoluteValIncldingTitle;
                        break;
                    }
                }
            }
            return absoluteVal;
        }
    };
    xlsxReader.process(fileName);
}
Also used : XLSXImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler) XlsxReaderHandler(com.cubrid.common.ui.cubrid.table.control.XlsxReaderHandler) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) BiffException(jxl.read.biff.BiffException) IOException(java.io.IOException) Date(java.util.Date) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) List(java.util.List) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

XLSXImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler)4 ArrayList (java.util.ArrayList)4 List (java.util.List)3 XlsxReaderHandler (com.cubrid.common.ui.cubrid.table.control.XlsxReaderHandler)2 XLSImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SQLException (java.sql.SQLException)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 ImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.ImportFileHandler)1 QueryEditorPart (com.cubrid.common.ui.query.editor.QueryEditorPart)1 QueryUnit (com.cubrid.common.ui.query.editor.QueryUnit)1 FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1