Search in sources :

Example 6 with ImportFileDescription

use of com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription 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);
    }
}
Also used : CellFormat(jxl.format.CellFormat) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ImportDataTableFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataTableFailedEvent) ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) XLSImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler) Cell(jxl.Cell) EmptyCell(jxl.biff.EmptyCell) BiffException(jxl.read.biff.BiffException) ImportRowData(com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportRowData) EmptyCell(jxl.biff.EmptyCell) IOException(java.io.IOException) BiffException(jxl.read.biff.BiffException) IOException(java.io.IOException) SQLException(java.sql.SQLException) ImportDataFailedEvent(com.cubrid.common.ui.cubrid.table.dialog.imp.event.ImportDataFailedEvent) File(java.io.File) Sheet(jxl.Sheet)

Example 7 with ImportFileDescription

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

the class CSVImportFileHandler 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;
            CSVReader csvReader = null;
            try {
                if (fileCharset == null || fileCharset.trim().length() == 0) {
                    csvReader = new CSVReader(new FileReader(fileName));
                } else {
                    csvReader = new CSVReader(new InputStreamReader(new FileInputStream(fileName), fileCharset));
                }
                String[] cvsRow = csvReader.readNext();
                if (cvsRow != null) {
                    totalRowCount++;
                    for (String title : cvsRow) {
                        colsList.add(title);
                    }
                }
                while (!monitor.isCanceled() && csvReader.readNext() != 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(csvReader);
                monitor.done();
            }
        }
    };
    PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
    return importFileDescription;
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.cubrid.common.core.reader.CSVReader) ArrayList(java.util.ArrayList) 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)

Example 8 with ImportFileDescription

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

Example 9 with ImportFileDescription

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

the class XLSXImportFileHandler method getSourceFileInfo.

/**
	 * Get the source file information
	 *
	 * @return ImportFileDescription
	 * @throws InvocationTargetException Exception in process
	 * @throws InterruptedException The exception
	 */
public ImportFileDescription getSourceFileInfo() throws InvocationTargetException, InterruptedException {
    // FIXME move this logic to core module
    synchronized (this) {
        if (importFileDescription == null) {
            final XlsxRowNumberHandler xlsxRowNumberHandler = new XlsxRowNumberHandler(fileName);
            IRunnableWithProgress runnable = new IRunnableWithProgress() {

                public void run(final IProgressMonitor monitor) {
                    monitor.beginTask("", IProgressMonitor.UNKNOWN);
                    Thread thread = new //$NON-NLS-1$
                    Thread(//$NON-NLS-1$
                    "Monitoring cancel") {

                        public void run() {
                            while (monitor != null && !monitor.isCanceled() && !xlsxRowNumberHandler.isEnd()) {
                                try {
                                    sleep(100);
                                } catch (InterruptedException e) {
                                    LOGGER.error(e.getMessage(), e);
                                }
                            }
                            if (monitor != null && monitor.isCanceled()) {
                                xlsxRowNumberHandler.setCancel(true);
                                monitor.done();
                            }
                        }
                    };
                    thread.start();
                    xlsxRowNumberHandler.process();
                    monitor.done();
                }
            };
            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
            List<Integer> itemsNumberOfSheets = null;
            int totalRowCount = 0;
            List<String> colsLst = xlsxRowNumberHandler.getHeadInfo();
            if (xlsxRowNumberHandler.isCancel()) {
                throw new InterruptedException();
            } else {
                totalRowCount = xlsxRowNumberHandler.getNumberOfAllRow();
                itemsNumberOfSheets = xlsxRowNumberHandler.getItemsNumberOfSheets();
            }
            int sheetNum = 0;
            if (itemsNumberOfSheets != null) {
                sheetNum = itemsNumberOfSheets.size();
            }
            importFileDescription = new ImportFileDescription(totalRowCount, sheetNum, colsLst);
            importFileDescription.setItemsNumberOfSheets(itemsNumberOfSheets);
        }
        return importFileDescription;
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) XlsxRowNumberHandler(com.cubrid.common.ui.cubrid.table.control.XlsxRowNumberHandler) ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

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