Search in sources :

Example 1 with ImportFileHandler

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

the class FileToTableMappingComposite method parseData.

@SuppressWarnings("unchecked")
private void parseData(final Map<String, String> tableToFile) throws Exception {
    allTableList = (List<ICubridNode>) treeViewer.getInput();
    final List<ICubridNode> newMappedNodeList = new ArrayList<ICubridNode>();
    final ImportConfig importConfig = importSettingPage.getImportDataWizard().getImportConfig();
    ProgressMonitorDialog progress = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
    progress.setCancelable(true);
    progress.run(true, true, new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) throws InvocationTargetException {
            monitor.beginTask(Messages.taskParsingData, tableToFile.size());
            for (Entry<String, String> entry : tableToFile.entrySet()) {
                // FIXME move this logic to core module
                String tableName = entry.getKey();
                String filePath = entry.getValue();
                File file = new File(filePath);
                String fileName = file.getName();
                monitor.subTask(Messages.bind(Messages.taskParsingFile, fileName));
                List<String> fileColumnList = new ArrayList<String>();
                List<String> firstRowColsLst = new ArrayList<String>();
                List<String> colNameList = new ArrayList<String>();
                List<String> colTypeList = new ArrayList<String>();
                int totalLine = 0;
                List<Integer> itemsNumberOfSheets = null;
                /*Find the class node*/
                ICubridNode classNode = getTableNode(tableName);
                /*Process file*/
                ImportFileHandler importFileHandler = ImportFileHandlerFactory.getHandler(file.getAbsolutePath(), importConfig);
                ImportFileDescription ifd = getFileDescription(importFileHandler);
                firstRowColsLst.addAll(ifd.getFirstRowCols());
                totalLine = ifd.getTotalCount();
                itemsNumberOfSheets = ifd.getItemsNumberOfSheets();
                boolean isFirstLineAsColumnName = isFirstLineAsColumnName(tableName);
                fillInFromList(fileColumnList, firstRowColsLst, isFirstLineAsColumnName);
                if (isFirstLineAsColumnName) {
                    handleSelectEventForFirstRowAsColBtn(itemsNumberOfSheets, fileColumnList, firstRowColsLst, totalLine, isFirstLineAsColumnName);
                }
                if (classNode != null) {
                    Object isNew = classNode.getData(ImportObjectLabelProvider.IS_NEW);
                    if (isNew == null || !(Boolean) isNew) {
                        SchemaInfo schemaInfo = database.getDatabaseInfo().getSchemaInfo(tableName);
                        List<DBAttribute> attributes = schemaInfo == null ? null : schemaInfo.getAttributes();
                        if (attributes == null) {
                            return;
                        }
                        for (DBAttribute attr : attributes) {
                            String column = attr.getName();
                            String dataType = attr.getType();
                            colNameList.add(column);
                            colTypeList.add(dataType);
                        }
                    } else {
                        removeClassNode(classNode);
                        classNode = createClassNode(tableName, isFirstLineAsColumnName(tableName), firstRowColsLst, colNameList, colTypeList);
                    }
                } else if (importConfig.isCreateTableAccordingData()) {
                    classNode = createClassNode(tableName, isFirstLineAsColumnName(tableName), firstRowColsLst, colNameList, colTypeList);
                } else {
                    failedFileList.add(file.getAbsolutePath());
                    continue;
                }
                if (fileColumnList.size() == colNameList.size()) {
                    classNode.setData(ImportObjectLabelProvider.IS_MAPPED, true);
                    classNode.setData(ImportObjectLabelProvider.FILE_PAHT, fileName);
                    classNode.setData(ImportObjectLabelProvider.ROW_COUNT, totalLine);
                    List<PstmtParameter> parameterList = new ArrayList<PstmtParameter>();
                    for (int i = 0; i < fileColumnList.size(); i++) {
                        PstmtParameter pstmtParameter = new PstmtParameter(colNameList.get(i), i + 1, colTypeList.get(i), String.valueOf(i));
                        pstmtParameter.setFileColumnName(fileColumnList.get(i));
                        parameterList.add(pstmtParameter);
                    }
                    TableConfig tableConfig = new TableConfig(classNode.getName());
                    tableConfig.setPstmList(parameterList);
                    tableConfig.setFilePath(file.getAbsolutePath());
                    tableConfig.setInsertDML(getInsertDML(classNode, colNameList, colTypeList));
                    tableConfig.setLineCount(totalLine);
                    tableConfig.setMapped(true);
                    Object isNew = classNode.getData(ImportObjectLabelProvider.IS_NEW);
                    if (isNew != null && (Boolean) isNew) {
                        tableConfig.setCreateDDL(classNode.getData(ImportObjectLabelProvider.CREATE_DDL).toString());
                    }
                    importConfig.addTableConfig(tableConfig);
                    newMappedNodeList.add(classNode);
                } else {
                    classNode.setData(ImportObjectLabelProvider.IS_MAPPED, false);
                    classNode.setData(ImportObjectLabelProvider.FILE_PAHT, fileName);
                    classNode.setData(ImportObjectLabelProvider.ROW_COUNT, totalLine);
                    TableConfig tableConfig = importConfig.getSelectedMap().get(classNode.getName());
                    if (tableConfig != null) {
                        tableConfig.setMapped(false);
                        tableConfig.setFilePath(filePath);
                        tableConfig.setLineCount(totalLine);
                        tableConfig.getPstmList().clear();
                        tableConfig.setInsertDML("");
                    }
                    failedFileList.add(fileName);
                }
                monitor.worked(1);
                if (monitor.isCanceled()) {
                    return;
                }
            }
        }
    });
    for (ICubridNode node : allTableList) {
        if (newMappedNodeList.contains(node)) {
            treeViewer.setChecked(node, true);
        }
    }
    treeViewer.refresh();
}
Also used : ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) ImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileHandler) ArrayList(java.util.ArrayList) ICubridNode(com.cubrid.common.ui.spi.model.ICubridNode) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Entry(java.util.Map.Entry) PstmtParameter(com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 2 with ImportFileHandler

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

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

the class AddTableFileDialog method openFile.

/**
	 * Open the file
	 */
private void openFile() {
    if (getButton(IDialogConstants.OK_ID) != null) {
        getButton(IDialogConstants.OK_ID).setEnabled(false);
    }
    String filePath = fileNameTxt.getText();
    if (filePath.trim().length() == 0) {
        return;
    }
    isOpenedFile = true;
    firstRowAsColumnBtn.setEnabled(isOpenedFile);
    boolean isHaveException = false;
    String errorMsg = null;
    try {
        ImportFileHandler importFileHandler = ImportFileHandlerFactory.getHandler(filePath, configModel);
        ImportFileDescription ifd = importFileHandler.getSourceFileInfo();
        firstRowColsLst.clear();
        firstRowColsLst.addAll(ifd.getFirstRowCols());
        totalLine = ifd.getTotalCount();
        columnCount = firstRowColsLst.size();
        itemsNumberOfSheets = ifd.getItemsNumberOfSheets();
        if (firstRowAsColumnBtn.getSelection()) {
            handleSelectEventForFirstRowAsColBtn();
        }
        File file = new File(filePath);
        CommonUIPlugin.putSettingValue(ImportDataWizard.SESSION_IMPORT_KEY, file.getParent());
    } catch (InvocationTargetException ex) {
        isHaveException = true;
        Throwable targetException = ((InvocationTargetException) ex).getTargetException();
        if (targetException instanceof OutOfMemoryError) {
            CommonUITool.openErrorBox(Messages.errNoAvailableMemory);
        } else {
            errorMsg = ex.getMessage();
        }
    } catch (InterruptedException ex) {
        isHaveException = true;
        errorMsg = ex.getMessage();
    } catch (Exception ex) {
        isHaveException = true;
        errorMsg = ex.getMessage();
    }
    String supportExtString = null;
    if (configModel.getImportType() == ImportConfig.IMPORT_FROM_EXCEL) {
        supportExtString = "csv, xls";
    } else if (configModel.getImportType() == ImportConfig.IMPORT_FROM_TXT) {
        supportExtString = "txt";
    } else if (configModel.getImportType() == ImportConfig.IMPORT_FROM_SQL) {
        supportExtString = "sql";
    } else if (configModel.getImportType() == ImportConfig.IMPORT_FROM_LOADDB) {
        supportExtString = "LoadDB";
    }
    if (errorMsg != null && errorMsg.trim().length() > 0) {
        String msg = Messages.errorOpenFile;
        if (supportExtString != null) {
            msg += Messages.bind(Messages.errorOpenFileDetail, supportExtString);
        }
        msg += StringUtil.NEWLINE + StringUtil.NEWLINE + errorMsg;
        CommonUITool.openErrorBox(msg);
    }
    if (isHaveException) {
        fileNameTxt.setText("");
    }
}
Also used : ImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileHandler) ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.ImportFileHandler)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ImportFileDescription (com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)1 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)1 PstmtParameter (com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter)1 XLSImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler)1 XLSXImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler)1 QueryEditorPart (com.cubrid.common.ui.query.editor.QueryEditorPart)1 QueryUnit (com.cubrid.common.ui.query.editor.QueryUnit)1 ICubridNode (com.cubrid.common.ui.spi.model.ICubridNode)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Entry (java.util.Map.Entry)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 IEditorPart (org.eclipse.ui.IEditorPart)1