Search in sources :

Example 1 with ImportFileDescription

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

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

the class XLSImportFileHandler 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
    synchronized (this) {
        if (importFileDescription == null) {
            final List<String> colsLst = new ArrayList<String>();
            final List<Integer> itemsNumberOfSheets = new ArrayList<Integer>();
            importFileDescription = new ImportFileDescription(0, 0, colsLst);
            IRunnableWithProgress runnable = new IRunnableWithProgress() {

                public void run(final IProgressMonitor monitor) {
                    monitor.beginTask("", IProgressMonitor.UNKNOWN);
                    Workbook workbook = null;
                    int totalRowCount = 0;
                    int sheetNum = 0;
                    try {
                        if (fileCharset == null) {
                            workbook = Workbook.getWorkbook(new File(fileName));
                        } else {
                            WorkbookSettings workbookSettings = new WorkbookSettings();
                            workbookSettings.setEncoding(fileCharset);
                            workbook = Workbook.getWorkbook(new File(fileName), workbookSettings);
                        }
                        // get column count and total row count
                        sheetNum = workbook.getNumberOfSheets();
                        if (sheetNum > 0) {
                            int columnCount = workbook.getSheet(0).getColumns();
                            for (int j = 0; !monitor.isCanceled() && j < columnCount; j++) {
                                Cell cell = workbook.getSheet(0).getCell(j, 0);
                                //$NON-NLS-1$
                                colsLst.add(cell == null ? "" : cell.getContents());
                            }
                        }
                        for (int i = 0; !monitor.isCanceled() && i < sheetNum; i++) {
                            int rowsInSheet = workbook.getSheet(i).getRows();
                            itemsNumberOfSheets.add(Integer.valueOf(rowsInSheet));
                            totalRowCount += rowsInSheet;
                        }
                        if (monitor.isCanceled()) {
                            throw new InterruptedException();
                        }
                    } catch (Exception e) {
                        LOGGER.error(e.getMessage(), e);
                        throw new RuntimeException(e);
                    } finally {
                        importFileDescription.setSheetNum(sheetNum);
                        importFileDescription.setTotalCount(totalRowCount);
                        importFileDescription.setFirstRowCols(colsLst);
                        importFileDescription.setItemsNumberOfSheets(itemsNumberOfSheets);
                        if (workbook != null) {
                            workbook.close();
                        }
                        monitor.done();
                    }
                }
            };
            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
        }
        return importFileDescription;
    }
}
Also used : ArrayList(java.util.ArrayList) WorkbookSettings(jxl.WorkbookSettings) Workbook(jxl.Workbook) BiffException(jxl.read.biff.BiffException) 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) File(java.io.File) Cell(jxl.Cell)

Example 3 with ImportFileDescription

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

the class PstmtDataTask method executeFromXls.

/**
	 * Do with data from excel file
	 *
	 * @param monitor IProgressMonitor
	 */
private void executeFromXls(IProgressMonitor monitor) {
    // FIXME move this logic to core module
    try {
        XLSImportFileHandler fileHandler = (XLSImportFileHandler) importFileHandler;
        Sheet[] sheets = fileHandler.getSheets();
        ImportFileDescription fileDesc = fileHandler.getSourceFileInfo();
        int rowNum = 0;
        int currentRow = 0;
        for (int sheetNum = 0; sheetNum < sheets.length; sheetNum++) {
            int start = 0;
            int lastRowNum = rowNum;
            int rows = fileDesc.getItemsNumberOfSheets().get(sheetNum);
            if (isFirstRowAsColumn) {
                rowNum += rows - 1;
            } else {
                rowNum += rows;
            }
            if (startRow > rowNum) {
                continue;
            }
            if (lastRowNum >= startRow) {
                start = isFirstRowAsColumn ? 1 : 0;
            } else {
                start = startRow - lastRowNum + (isFirstRowAsColumn ? 1 : 0);
            }
            Sheet sheet = sheets[sheetNum];
            String content = null;
            String pattern = null;
            for (int i = start; i < rows && currentRow < rowCount; i++) {
                for (int j = 0; j < parameterList.size(); j++) {
                    PstmtParameter pstmtParameter = parameterList.get(j);
                    int column = Integer.parseInt(pstmtParameter.getStringParamValue());
                    Cell cell = sheet.getCell(column, i);
                    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();
                        }
                    }
                    String dataType = DataType.getRealType(pstmtParameter.getDataType());
                    content = FieldHandlerUtils.getRealValueForImport(dataType, content, parentFile);
                    FormatDataResult formatDataResult = null;
                    if (StringUtil.isEmpty(pattern)) {
                        formatDataResult = DBAttrTypeFormatter.format(dataType, content, false, dbCharset, true);
                    } else {
                        formatDataResult = DBAttrTypeFormatter.format(dataType, content, pattern, 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 {
                        dataTypeErrorHandling(getErrorMsg(i, 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 (BiffException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (DataFormatException ex) {
        throw new RuntimeException(ex);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } catch (OutOfMemoryError error) {
        throw new RuntimeException(error);
    }
}
Also used : BiffException(jxl.read.biff.BiffException) CellFormat(jxl.format.CellFormat) SQLException(java.sql.SQLException) EmptyCell(jxl.biff.EmptyCell) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) BiffException(jxl.read.biff.BiffException) IOException(java.io.IOException) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) XLSImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler) Sheet(jxl.Sheet) Cell(jxl.Cell) EmptyCell(jxl.biff.EmptyCell)

Example 4 with ImportFileDescription

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

the class ImportDataFromFileDialog method openFile.

/**
	 * Open the file
	 *
	 */
private void openFile() {
    String fileName = fileNameTxt.getText();
    if (fileName.trim().length() == 0) {
        return;
    }
    boolean isHaveException = false;
    String errorMsg = null;
    try {
        String fileCharset = fileCharsetCombo.getText();
        importFileHandler = ImportFileHandlerFactory.getHandler(fileName, fileCharset);
        ImportFileDescription ifd = importFileHandler.getSourceFileInfo();
        itemsNumberOfSheets = ifd.getItemsNumberOfSheets();
        firstRowColsLst.clear();
        firstRowColsLst.addAll(ifd.getFirstRowCols());
        totalLinesText.setText(String.valueOf(ifd.getTotalCount()));
        if (firstRowAsColumnBtn.getSelection()) {
            handleSelectEventForFirstRowAsColBtn();
        }
        File file = new File(fileName);
        CommonUIPlugin.putSettingValue(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();
    }
    if (errorMsg != null && errorMsg.trim().length() > 0) {
        CommonUITool.openErrorBox(errorMsg);
    }
    if (isHaveException) {
        fileNameTxt.setText("");
        totalLinesText.setText("");
    }
}
Also used : ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with ImportFileDescription

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

ImportFileDescription (com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription)9 File (java.io.File)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Cell (jxl.Cell)3 BiffException (jxl.read.biff.BiffException)3 ImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.ImportFileHandler)2 XLSImportFileHandler (com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler)2 FileInputStream (java.io.FileInputStream)2 FileReader (java.io.FileReader)2 InputStreamReader (java.io.InputStreamReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SQLException (java.sql.SQLException)2 Sheet (jxl.Sheet)2 EmptyCell (jxl.biff.EmptyCell)2 CellFormat (jxl.format.CellFormat)2 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)1