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;
}
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;
}
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);
}
}
}
}
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;
}
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);
}
}
}
}
Aggregations