Search in sources :

Example 1 with FormatDataResult

use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.

the class SetPstmtValueDialog method validate.

/**
	 * 
	 * Validate the data
	 * 
	 * @return boolean
	 */
private boolean validate() {
    setErrorMessage(null);
    if (inputTextBtn.getSelection()) {
        if (paraType == null) {
            paraType = item.getText(1);
        }
        String data = paraValueText.getText();
        if (!setNullBtn.getSelection() && data.length() > 0) {
            FormatDataResult formatDataResult = DBAttrTypeFormatter.format(DataType.getRealType(paraType), data, false, database.getDatabaseInfo().getCharSet(), true);
            if (!formatDataResult.isSuccess()) {
                setErrorMessage(Messages.bind(Messages.errTextTypeNotMatch, paraType));
                return false;
            }
        }
    } else {
        String filePath = filePathText.getText();
        if (filePath.trim().length() == 0) {
            setErrorMessage(Messages.msgSelectFile);
            return false;
        }
        String charsetName = fileCharsetCombo.getText();
        try {
            "".getBytes(charsetName);
        } catch (UnsupportedEncodingException e) {
            setErrorMessage(Messages.errUnsupportedCharset);
            return false;
        }
    }
    if (getButton(IDialogConstants.OK_ID) != null) {
        getButton(IDialogConstants.OK_ID).setEnabled(true);
    }
    return true;
}
Also used : FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with FormatDataResult

use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.

the class PstmtDataTask method getRowParameterListFromCSV.

/**
	 *
	 * Get the parameter of PSTMT list from CSV file
	 *
	 * @return List<List<PstmtParameter>>
	 */
private List<List<PstmtParameter>> getRowParameterListFromCSV() {
    // FIXME move this logic to core module
    List<List<PstmtParameter>> rowParaList = new ArrayList<List<PstmtParameter>>();
    File file = new File(fileName);
    CSVReader csvReader = null;
    try {
        if (fileCharset == null || fileCharset.trim().length() == 0) {
            csvReader = new CSVReader(new FileReader(file));
        } else {
            csvReader = new CSVReader(new InputStreamReader(new FileInputStream(file), fileCharset));
        }
        int currentRow = 0;
        if (isFirstRowAsColumn) {
            csvReader.readNext();
            currentRow++;
        }
        String[] cvsRow;
        while ((cvsRow = csvReader.readNext()) != null) {
            List<PstmtParameter> paraList = new ArrayList<PstmtParameter>();
            for (int j = 0; j < parameterList.size(); j++) {
                PstmtParameter pstmtParameter = parameterList.get(j);
                PstmtParameter newParam = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), null);
                int column = Integer.parseInt(pstmtParameter.getStringParamValue());
                String content = null;
                if (cvsRow.length > column) {
                    content = cvsRow[column];
                }
                String dataType = DataType.getRealType(pstmtParameter.getDataType());
                content = FieldHandlerUtils.getRealValueForImport(dataType, content, parentFile);
                FormatDataResult formatDataResult = DBAttrTypeFormatter.format(DataType.getRealType(pstmtParameter.getDataType()), content, false, dbCharset, true);
                if (formatDataResult.isSuccess()) {
                    newParam.setParamValue(content);
                    newParam.setCharSet(fileCharset);
                } else {
                    dataTypeErrorHandling(getErrorMsg(currentRow, column, dataType));
                    newParam.setParamValue(null);
                    newParam.setCharSet(fileCharset);
                }
                paraList.add(newParam);
            }
            rowParaList.add(paraList);
            currentRow++;
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (DataFormatException ex) {
        throw new RuntimeException(ex);
    } catch (OutOfMemoryError error) {
        throw new RuntimeException(error);
    } finally {
        if (csvReader != null) {
            try {
                csvReader.close();
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
    }
    return rowParaList;
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.cubrid.common.core.reader.CSVReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) List(java.util.List) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) File(java.io.File)

Example 3 with FormatDataResult

use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.

the class PstmtDataTask method executeFromCSV.

/**
	 *
	 * Do with data from CSV file
	 *
	 * @param monitor IProgressMonitor
	 */
private void executeFromCSV(IProgressMonitor monitor) {
    // FIXME move this logic to core module
    File file = new File(fileName);
    CSVReader csvReader = null;
    try {
        if (fileCharset == null || fileCharset.trim().length() == 0) {
            csvReader = new CSVReader(new FileReader(file));
        } else {
            csvReader = new CSVReader(new InputStreamReader(new FileInputStream(file), fileCharset));
        }
        if (isFirstRowAsColumn) {
            csvReader.readNext();
        }
        int currentRow = 0;
        int rowNum = 0;
        String[] cvsRow;
        while ((cvsRow = csvReader.readNext()) != null && currentRow < rowCount) {
            rowNum++;
            if (startRow >= rowNum) {
                continue;
            }
            for (int j = 0; j < parameterList.size(); j++) {
                PstmtParameter pstmtParameter = parameterList.get(j);
                int column = Integer.parseInt(pstmtParameter.getStringParamValue());
                String content = null;
                if (cvsRow.length > column) {
                    content = cvsRow[column];
                }
                String dataType = DataType.getRealType(pstmtParameter.getDataType());
                content = FieldHandlerUtils.getRealValueForImport(dataType, content, 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 {
                    int row = isFirstRowAsColumn ? currentRow + 1 : currentRow;
                    dataTypeErrorHandling(getErrorMsg(row, 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 (IOException ex) {
        throw new RuntimeException(ex);
    } catch (DataFormatException ex) {
        throw new RuntimeException(ex);
    } catch (OutOfMemoryError error) {
        throw new RuntimeException(error);
    } finally {
        if (csvReader != null) {
            try {
                csvReader.close();
                csvReader = null;
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.cubrid.common.core.reader.CSVReader) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) FileReader(java.io.FileReader) File(java.io.File)

Example 4 with FormatDataResult

use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.

the class PstmtDataTask method getRowParameterListFromExcel.

/**
	 *
	 * Get the parameter of PSTMT list from excel file
	 *
	 * @return List<List<PstmtParameter>>
	 */
private List<List<PstmtParameter>> getRowParameterListFromExcel() {
    // FIXME move this logic to core module
    File file = new File(fileName);
    Workbook workbook = null;
    List<List<PstmtParameter>> rowParaList = new ArrayList<List<PstmtParameter>>();
    try {
        if (fileCharset == null) {
            workbook = Workbook.getWorkbook(file);
        } else {
            WorkbookSettings workbookSettings = new WorkbookSettings();
            workbookSettings.setEncoding(fileCharset);
            workbook = Workbook.getWorkbook(file, workbookSettings);
        }
        Sheet[] sheets = workbook.getSheets();
        for (int sheetNum = 0; sheetNum < sheets.length; sheetNum++) {
            Sheet sheet = sheets[sheetNum];
            int rows = sheet.getRows();
            int start = 0;
            if (isFirstRowAsColumn) {
                start = 1;
            }
            for (int i = start; i < rows; i++) {
                List<PstmtParameter> paraList = new ArrayList<PstmtParameter>();
                for (int j = 0; j < parameterList.size(); j++) {
                    PstmtParameter pstmtParameter = parameterList.get(j);
                    PstmtParameter newParam = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), null);
                    int column = Integer.parseInt(pstmtParameter.getStringParamValue());
                    Cell cell = sheet.getCell(column, i);
                    String content = null;
                    String 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()) {
                        newParam.setCharSet(fileCharset);
                        newParam.setParamValue(content);
                    } else {
                        dataTypeErrorHandling(getErrorMsg(i, column, dataType));
                        newParam.setCharSet(fileCharset);
                        newParam.setParamValue(null);
                    }
                    paraList.add(newParam);
                }
                rowParaList.add(paraList);
            }
        }
    } catch (BiffException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (DataFormatException ex) {
        throw new RuntimeException(ex);
    } catch (OutOfMemoryError error) {
        throw new RuntimeException(error);
    } finally {
        if (workbook != null) {
            workbook.close();
        }
    }
    return rowParaList;
}
Also used : BiffException(jxl.read.biff.BiffException) CellFormat(jxl.format.CellFormat) ArrayList(java.util.ArrayList) EmptyCell(jxl.biff.EmptyCell) WorkbookSettings(jxl.WorkbookSettings) IOException(java.io.IOException) Workbook(jxl.Workbook) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) Sheet(jxl.Sheet) Cell(jxl.Cell) EmptyCell(jxl.biff.EmptyCell)

Example 5 with FormatDataResult

use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.

the class PstmtDataTask method executeFromTxt.

/**
	 *
	 * Do with data from Txt file
	 *
	 * @param monitor IProgressMonitor
	 */
private void executeFromTxt(IProgressMonitor monitor) {
    // FIXME move this logic to core module
    File file = new File(fileName);
    TxtReader txtReader = null;
    try {
        if (fileCharset == null || fileCharset.trim().length() == 0) {
            txtReader = new TxtReader(new FileReader(file), separator);
        } else {
            txtReader = new TxtReader(new InputStreamReader(new FileInputStream(file), fileCharset), separator);
        }
        if (isFirstRowAsColumn) {
            txtReader.readNext();
        }
        int currentRow = 0;
        int rowNum = 0;
        String[] cvsRow;
        while ((cvsRow = txtReader.readNext()) != null && currentRow < rowCount) {
            rowNum++;
            if (startRow >= rowNum) {
                continue;
            }
            for (int j = 0; j < parameterList.size(); j++) {
                PstmtParameter pstmtParameter = parameterList.get(j);
                int column = Integer.parseInt(pstmtParameter.getStringParamValue());
                String content = null;
                if (cvsRow.length > column) {
                    content = cvsRow[column];
                }
                String dataType = DataType.getRealType(pstmtParameter.getDataType());
                content = FieldHandlerUtils.getRealValueForImport(dataType, content, 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 {
                    int row = isFirstRowAsColumn ? currentRow + 1 : currentRow;
                    dataTypeErrorHandling(getErrorMsg(row, 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 (IOException ex) {
        throw new RuntimeException(ex);
    } catch (DataFormatException 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);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) IOException(java.io.IOException) TxtReader(com.cubrid.common.core.reader.TxtReader) FileInputStream(java.io.FileInputStream) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)20 SQLException (java.sql.SQLException)10 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 CellValue (com.cubrid.common.ui.spi.table.CellValue)4 File (java.io.File)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 BiffException (jxl.read.biff.BiffException)4 PstmtParameter (com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter)3 ParamSetException (com.cubrid.common.ui.spi.util.paramSetter.ParamSetException)3 ParamSetter (com.cubrid.common.ui.spi.util.paramSetter.ParamSetter)3 DBConnection (com.cubrid.cubridmanager.core.common.jdbc.DBConnection)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 FileReader (java.io.FileReader)3 InputStreamReader (java.io.InputStreamReader)3 Connection (java.sql.Connection)3