use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.
the class RowDetailDialog method validate.
/**
* Validate the data validation
*
* @return boolean
*/
private boolean validate() {
ColumnInfo columnInfo = columnInfoList.get(selComboIndex);
String value = columnValueText.getText();
if (DataType.NULL_EXPORT_FORMAT.equals(value)) {
return true;
}
String completedType = columnInfo.getComleteType();
boolean isMuchValue = DBAttrTypeFormatter.isMuchValueType(completedType, -1);
boolean isValid = false;
if (isMuchValue && DBAttrTypeFormatter.isFilePath(value)) {
isValid = true;
} else {
FormatDataResult result = DBAttrTypeFormatter.format(completedType, value, false, dbCharset, true);
isValid = result.isSuccess();
}
if (!isValid) {
String errMsg = Messages.bind(com.cubrid.common.ui.cubrid.table.Messages.errTextTypeNotMatch, completedType);
CommonUITool.openErrorBox(getShell(), errMsg);
return false;
}
if (fileCharsetCombo.isEnabled()) {
String charsetName = fileCharsetCombo.getText();
try {
"".getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
CommonUITool.openErrorBox(getShell(), com.cubrid.common.ui.cubrid.table.Messages.errUnsupportedCharset);
return false;
}
}
return true;
}
use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.
the class RowDetailDialog method export.
/**
* Export data to file
*
* @param filePath String
* @param oidStr String
* @param value String
* @param fileCharset String
* @throws IOException The exception
* @throws SQLException The exception
*/
private void export(final String filePath, String oidStr, String value, String fileCharset) throws IOException, SQLException {
// FIXME move this logic to core module
OutputStream fs = null;
Writer writer = null;
InputStream in = null;
Reader reader = null;
ResultSet rs = null;
try {
ColumnInfo columnInfo = columnInfoList.get(selComboIndex);
String type = columnInfo.getType();
String completedType = columnInfo.getComleteType();
String colName = columnInfo.getName();
if (DataType.DATATYPE_BLOB.equals(type) && DataType.BLOB_EXPORT_FORMAT.equals(value)) {
CUBRIDOIDProxy oidPxory = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) qe.getQueryEditor().getConnection().checkAndConnect(), oidStr);
rs = oidPxory.getValues(new String[] { colName });
rs.next();
Blob blob = rs.getBlob(colName);
in = blob.getBinaryStream();
fs = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bArr = new byte[512];
int count = in.read(bArr);
while (count > 0) {
fs.write(bArr, 0, count);
count = in.read(bArr);
}
fs.flush();
} else {
if (DataType.DATATYPE_CLOB.equals(type) && DataType.CLOB_EXPORT_FORMAT.equals(value)) {
CUBRIDOIDProxy oidPxory = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) qe.getQueryEditor().getConnection().checkAndConnect(), oidStr);
rs = oidPxory.getValues(new String[] { colName });
rs.next();
Clob clob = rs.getClob(colName);
reader = clob.getCharacterStream();
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), fileCharset));
char[] charArr = new char[512];
int count = reader.read(charArr);
while (count > 0) {
writer.write(charArr, 0, count);
count = reader.read(charArr);
}
writer.flush();
} else if (DataType.DATATYPE_BIT.equals(type) || DataType.DATATYPE_BIT_VARYING.equals(type)) {
byte[] bArr = null;
if (DataType.BIT_EXPORT_FORMAT.equals(value)) {
CUBRIDOIDProxy oidPxory = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) qe.getQueryEditor().getConnection().checkAndConnect(), oidStr);
rs = oidPxory.getValues(new String[] { colName });
rs.next();
bArr = rs.getBytes(colName);
} else {
FormatDataResult result = new FormatDataResult();
DBAttrTypeFormatter.formatBit(completedType, value, result, dbCharset);
bArr = (byte[]) result.getFormatedJavaObj();
}
fs = new BufferedOutputStream(new FileOutputStream(filePath));
fs.write(bArr);
fs.flush();
} else {
byte[] bArr = value.getBytes(fileCharset);
fs = new BufferedOutputStream(new FileOutputStream(filePath));
fs.write(bArr);
fs.flush();
}
}
} finally {
if (fs != null) {
try {
fs.close();
fs = null;
} catch (IOException e) {
LOGGER.error("", e);
}
}
if (writer != null) {
try {
writer.close();
writer = null;
} catch (IOException e) {
LOGGER.error("", e);
}
}
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
LOGGER.error("", e);
}
}
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e) {
LOGGER.error("", e);
}
}
if (reader != null) {
try {
reader.close();
reader = 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 getRowParameterListFromXlsx.
/**
* Get the row parameter list from excel 2007
*
* @return List<List<PstmtParameter>>
*/
private List<List<PstmtParameter>> getRowParameterListFromXlsx() {
// FIXME move this logic to core module
final List<List<PstmtParameter>> rowParaList = new ArrayList<List<PstmtParameter>>();
XlsxReaderHandler xlsxReader = new XlsxReaderHandler((XLSXImportFileHandler) importFileHandler) {
private SimpleDateFormat datetimeSdf;
private SimpleDateFormat timestampSdf;
private SimpleDateFormat dateSdf;
private SimpleDateFormat timeSdf;
@Override
public void operateRows(int sheetIndex, List<String> rowlist) throws SQLException, DataFormatException {
if (currentRow == getTitleRow()) {
return;
}
int rowFromStart = sheetIndex * (ImportFileConstants.XLSX_ROW_LIMIT + 1 - getTitleRow()) + currentRow;
if (startRow > rowFromStart) {
return;
}
int lastRow = startRow + rowCount + getTitleRow() + 1;
if (lastRow <= rowFromStart) {
return;
}
List<PstmtParameter> paraList = new ArrayList<PstmtParameter>();
for (int i = 0; i < parameterList.size(); i++) {
PstmtParameter pstmtParameter = parameterList.get(i);
PstmtParameter newParam = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), null);
int column = Integer.parseInt(pstmtParameter.getStringParamValue());
String dataType = DataType.getRealType(pstmtParameter.getDataType());
String cellContent = rowlist.get(column).trim();
int cellType = FieldHandlerUtils.getCellType(dataType, cellContent);
double value;
Date dateCon;
switch(cellType) {
case -1:
cellContent = DataType.NULL_EXPORT_FORMAT;
break;
case 2:
try {
value = Double.parseDouble(cellContent);
dateCon = DateUtil.getJavaDate(value);
cellContent = datetimeSdf.format(dateCon);
} catch (NumberFormatException e) {
cellContent = DataType.NULL_EXPORT_FORMAT;
}
break;
case 3:
try {
value = Double.parseDouble(cellContent);
dateCon = DateUtil.getJavaDate(value);
cellContent = timestampSdf.format(dateCon);
} catch (Exception e) {
cellContent = DataType.NULL_EXPORT_FORMAT;
}
break;
case 4:
try {
value = Double.parseDouble(cellContent);
dateCon = DateUtil.getJavaDate(value);
cellContent = dateSdf.format(dateCon);
} catch (Exception e) {
cellContent = DataType.NULL_EXPORT_FORMAT;
}
break;
case 5:
try {
value = Double.parseDouble(cellContent);
dateCon = DateUtil.getJavaDate(value);
cellContent = timeSdf.format(dateCon);
} catch (Exception e) {
cellContent = DataType.NULL_EXPORT_FORMAT;
}
break;
default:
break;
}
String content = FieldHandlerUtils.getRealValueForImport(dataType, cellContent, parentFile);
FormatDataResult formatDataResult = DBAttrTypeFormatter.format(dataType, content, 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);
}
public void startDocument() {
if (isFirstRowAsColumn) {
setTitleRow(0);
}
datetimeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault());
timestampSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
dateSdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
timeSdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
}
};
try {
xlsxReader.process(fileName);
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return rowParaList;
}
use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.
the class PstmtDataTask method executeFromXlsx.
/**
*
* Do with data from excel file
*
* @param monitor IProgressMonitor
* @throws Exception the Exception
*/
private void executeFromXlsx(final IProgressMonitor monitor) throws Exception {
// FIXME move this logic to core module
XlsxReaderHandler xlsxReader = new XlsxReaderHandler((XLSXImportFileHandler) importFileHandler) {
private SimpleDateFormat datetimeSdf;
private SimpleDateFormat timestampSdf;
private SimpleDateFormat dateSdf;
private SimpleDateFormat timeSdf;
@Override
public void operateRows(int sheetIndex, List<String> rowlist) throws SQLException, DataFormatException {
if (currentRow == getTitleRow()) {
return;
}
XLSXImportFileHandler fileHandler = (XLSXImportFileHandler) importFileHandler;
List<Integer> itemsNumberOfSheets = null;
try {
itemsNumberOfSheets = fileHandler.getSourceFileInfo().getItemsNumberOfSheets();
} catch (Exception ex) {
LOGGER.error(ex.getMessage());
return;
}
int rowFromStart = 0;
for (int i = 0; i < sheetIndex; i++) {
rowFromStart += itemsNumberOfSheets.get(i);
}
rowFromStart += currentRow;
int absoluteStartRow = getAbsoluteRowNum(startRow, itemsNumberOfSheets);
if (absoluteStartRow > rowFromStart) {
return;
}
int absoluteEndRow = getAbsoluteRowNum(startRow + rowCount, itemsNumberOfSheets);
int rowCountIncludingTitle = absoluteEndRow - absoluteStartRow;
int relativeRow = rowFromStart - absoluteStartRow;
if (relativeRow > rowCountIncludingTitle - 1) {
return;
}
for (int i = 0; i < parameterList.size(); i++) {
PstmtParameter pstmtParameter = parameterList.get(i);
int column = Integer.parseInt(pstmtParameter.getStringParamValue());
String dataType = DataType.getRealType(pstmtParameter.getDataType());
String cellContent = rowlist.get(column);
int cellType = FieldHandlerUtils.getCellType(dataType, cellContent);
boolean isHaveError = false;
try {
double value;
Date dateCon;
switch(cellType) {
case -1:
cellContent = DataType.NULL_EXPORT_FORMAT;
break;
case 0:
if (cellContent.contains(".")) {
cellContent = cellContent.substring(0, cellContent.indexOf('.'));
}
break;
case 2:
value = Double.parseDouble(cellContent.trim());
dateCon = DateUtil.getJavaDate(value);
cellContent = datetimeSdf.format(dateCon);
break;
case 3:
value = Double.parseDouble(cellContent.trim());
dateCon = DateUtil.getJavaDate(value);
cellContent = timestampSdf.format(dateCon);
break;
case 4:
value = Double.parseDouble(cellContent.trim());
dateCon = DateUtil.getJavaDate(value);
cellContent = dateSdf.format(dateCon);
break;
case 5:
value = Double.parseDouble(cellContent.trim());
dateCon = DateUtil.getJavaDate(value);
cellContent = timeSdf.format(dateCon);
break;
default:
break;
}
} catch (NumberFormatException e) {
isHaveError = true;
}
String content = FieldHandlerUtils.getRealValueForImport(dataType, cellContent, 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 {
isHaveError = true;
PstmtParameter parameter = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), null);
parameter.setCharSet(fileCharset);
FieldHandlerUtils.setPreparedStatementValue(parameter, pStmt, dbCharset);
}
if (isHaveError) {
dataTypeErrorHandling(getErrorMsg(i, column, dataType));
}
}
if (pStmt != null) {
pStmt.addBatch();
monitor.worked(PROGRESS_ROW);
workedProgress += PROGRESS_ROW;
}
int importedRow = relativeRow + 1;
if (importedRow > 0 && importedRow % commitLineCountOnce == 0) {
commit(monitor, importedRow);
} else {
if (importedRow == rowCount && importedRow % commitLineCountOnce != 0) {
commit(monitor, importedRow);
}
}
if (isCancel) {
return;
}
}
public void startDocument() {
if (isFirstRowAsColumn) {
setTitleRow(0);
}
datetimeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault());
timestampSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
dateSdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
timeSdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
}
private int getAbsoluteRowNum(int value, List<Integer> itemsNumberOfSheets) {
if (itemsNumberOfSheets == null || itemsNumberOfSheets.isEmpty()) {
return value;
}
if (getTitleRow() == -1) {
return value;
}
int upLimit = 0;
int downLimit = 0;
int absoluteVal = value;
for (int i = 0; i < itemsNumberOfSheets.size(); i++) {
int absoluteValIncldingTitle = value + i + 1;
upLimit += itemsNumberOfSheets.get(i);
if (i == 0) {
if (absoluteValIncldingTitle <= upLimit) {
absoluteVal = absoluteValIncldingTitle;
break;
}
} else {
downLimit += itemsNumberOfSheets.get(i - 1);
if (absoluteValIncldingTitle > downLimit && absoluteValIncldingTitle <= upLimit) {
absoluteVal = absoluteValIncldingTitle;
break;
}
}
}
return absoluteVal;
}
};
xlsxReader.process(fileName);
}
use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult 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);
}
}
Aggregations