Search in sources :

Example 6 with ColumnInfo

use of com.cubrid.common.ui.query.control.ColumnInfo in project cubrid-manager by CUBRID.

the class ExportToXlsHandler method exportFromCache.

public void exportFromCache(String tableName) throws IOException {
    if (StringUtil.isEmpty(tableName)) {
        // FIXME move this logic to core module
        return;
    }
    WritableWorkbook workbook = null;
    int workbookNum = 0;
    int cellCharacterLimit = ImportFileConstants.XLSX_CELL_CHAR_LIMIT;
    // 65536: limit xls row number.
    int rowLimit = ImportFileConstants.XLS_ROW_LIMIT;
    boolean isInitedColumnTitles = false;
    List<String> columnTitles = new ArrayList<String>();
    try {
        int sheetNum = 0;
        int xlsRecordNum = 0;
        workbook = createWorkbook(exportConfig.getDataFilePath(tableName), workbookNum++);
        WritableSheet sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
        sheetNum++;
        int exportedCount = 0;
        try {
            ResultSetDataCache resultSetDataCache = exportConfig.getResultSetDataCache();
            List<ArrayList<Object>> datas = resultSetDataCache.getDatas();
            List<ColumnInfo> columnInfos = resultSetDataCache.getColumnInfos();
            int colCount = columnInfos.size();
            if (!isInitedColumnTitles) {
                isInitedColumnTitles = true;
                for (ColumnInfo column : columnInfos) {
                    columnTitles.add(column.getName());
                }
                if (isExit) {
                    return;
                }
                // first line add column name
                if (exportConfig.isFirstRowAsColumnName()) {
                    writeHeader(sheet, columnTitles);
                    xlsRecordNum++;
                }
            }
            for (ArrayList<Object> rowData : datas) {
                //Check memory
                if (!CommonUITool.isAvailableMemory(REMAINING_MEMORY_SIZE)) {
                    closeWorkbook(workbook);
                    workbook = null;
                    System.gc();
                    workbook = createWorkbook(exportConfig.getDataFilePath(tableName), workbookNum++);
                    sheetNum = 0;
                    sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
                    sheetNum++;
                    xlsRecordNum = 0;
                    // first line add column name
                    if (exportConfig.isFirstRowAsColumnName()) {
                        writeHeader(sheet, columnTitles);
                        xlsRecordNum++;
                    }
                }
                for (int j = 1; j <= colCount; j++) {
                    String colType = columnInfos.get(j - 1).getType();
                    int precision = columnInfos.get(j - 1).getPrecision();
                    setIsHasBigValue(colType, precision);
                    Object cellValue = rowData.get(j - 1);
                    // We need judge the CLOB/BLOD data by column type
                    if (DataType.DATATYPE_BLOB.equals(colType) || DataType.DATATYPE_CLOB.equals(colType)) {
                        if (DataType.DATATYPE_BLOB.equals(colType)) {
                            String fileName = exportBlobData(tableName, (Blob) cellValue);
                            String dataCellValue = DataType.NULL_EXPORT_FORMAT;
                            if (StringUtil.isNotEmpty(fileName)) {
                                dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + BLOB_FOLDER_POSTFIX + File.separator + fileName;
                            }
                            sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                        } else {
                            String fileName = exportClobData(tableName, (Clob) cellValue);
                            String dataCellValue = DataType.NULL_EXPORT_FORMAT;
                            if (StringUtil.isNotEmpty(fileName)) {
                                dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + CLOB_FOLDER_POSTFIX + File.separator + fileName;
                            }
                            sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                        }
                    } else if (cellValue instanceof Long) {
                        sheet.addCell(new Number(j - 1, xlsRecordNum, (Long) cellValue));
                    } else if (cellValue instanceof Double) {
                        sheet.addCell(new Number(j - 1, xlsRecordNum, (Double) cellValue));
                    } else if (cellValue instanceof Timestamp) {
                        String dataCellValue = FieldHandlerUtils.formatDateTime((Timestamp) cellValue);
                        sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                    } else if (cellValue instanceof java.sql.Time) {
                        String dataCellValue = DateUtil.getDatetimeString(((java.sql.Time) cellValue).getTime(), FieldHandlerUtils.FORMAT_TIME);
                        sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                    } else if (cellValue instanceof java.sql.Date) {
                        String dataCellValue = DateUtil.getDatetimeString(((java.sql.Date) cellValue).getTime(), FieldHandlerUtils.FORMAT_DATE);
                        sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                    } else {
                        sheet.addCell(new Label(j - 1, xlsRecordNum, cellValue.toString().length() > cellCharacterLimit ? cellValue.toString().substring(0, cellCharacterLimit) : cellValue.toString()));
                    }
                }
                xlsRecordNum++;
                if ((xlsRecordNum + 1) % rowLimit == 0) {
                    xlsRecordNum = 0;
                    sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
                    sheetNum++;
                    // first line add column name
                    if (exportConfig.isFirstRowAsColumnName()) {
                        writeHeader(sheet, columnTitles);
                        xlsRecordNum++;
                    }
                }
                exportedCount++;
                if (exportedCount >= COMMIT_LINES) {
                    exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(tableName, exportedCount));
                    exportedCount = 0;
                }
                if (stop) {
                    break;
                }
            }
            exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(tableName, exportedCount));
            exportedCount = 0;
        } catch (Exception e) {
            LOGGER.error("", e);
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
        }
        System.gc();
    } catch (Exception e) {
        LOGGER.error("", e);
    } finally {
        closeWorkbook(workbook);
    }
}
Also used : ArrayList(java.util.ArrayList) Label(jxl.write.Label) ColumnInfo(com.cubrid.common.ui.query.control.ColumnInfo) Timestamp(java.sql.Timestamp) ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) Number(jxl.write.Number) ResultSetDataCache(com.cubrid.common.ui.cubrid.table.export.ResultSetDataCache) WritableSheet(jxl.write.WritableSheet) WriteException(jxl.write.WriteException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RowsExceededException(jxl.write.biff.RowsExceededException) WritableWorkbook(jxl.write.WritableWorkbook) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent)

Example 7 with ColumnInfo

use of com.cubrid.common.ui.query.control.ColumnInfo in project cubrid-manager by CUBRID.

the class QueryRecordListComparator method createResultColumn.

private void createResultColumn(final TableViewer tableViewer, final QueryRecord queryRecord) {
    if (queryRecord != null) {
        TableColumn[] tblColumn = new TableColumn[(queryRecord.getColumnInfoList() == null ? 0 : queryRecord.getColumnInfoList().size()) + 1];
        tblColumn[0] = new TableColumn(tableViewer.getTable(), SWT.NONE);
        tblColumn[0].setText("NO");
        tblColumn[0].setWidth(40);
        if (queryRecord.getColumnInfoList() == null) {
            return;
        }
        final Map<String, ColumnComparator> colComparatorMap = new HashMap<String, ColumnComparator>();
        for (int j = 0; j < queryRecord.getColumnInfoList().size(); j++) {
            tblColumn[j + 1] = new TableColumn(tableViewer.getTable(), SWT.NONE);
            ColumnInfo columnInfo = (ColumnInfo) queryRecord.getColumnInfoList().get(j);
            String name = columnInfo.getName();
            String type = columnInfo.getType();
            tblColumn[j + 1].setText(name);
            tblColumn[j + 1].setToolTipText(columnInfo.getComleteType());
            tblColumn[j + 1].setData(columnInfo);
            tblColumn[j + 1].pack();
            ColumnComparator comparator = new ColumnComparator(columnInfo.getIndex(), type, true);
            if (colComparatorMap != null) {
                colComparatorMap.put(columnInfo.getIndex(), comparator);
            }
            tblColumn[j + 1].addSelectionListener(new SelectionListener() {

                @SuppressWarnings("unchecked")
                public void widgetSelected(SelectionEvent event) {
                    TableColumn column = (TableColumn) event.widget;
                    if (column == null || column.getText() == null || column.getText().trim().length() == 0) {
                        return;
                    }
                    TableColumn sortedColumn = tableViewer.getTable().getSortColumn();
                    int width = column.getWidth();
                    ColumnInfo columnInfo = (ColumnInfo) column.getData();
                    ColumnComparator comparator = colComparatorMap.get(columnInfo.getIndex());
                    tableViewer.getTable().setSortColumn(column);
                    tableViewer.getTable().setSortDirection(comparator.isAsc() ? SWT.UP : SWT.DOWN);
                    Collections.sort(queryRecord.getPageData(), comparator);
                    comparator.setAsc(!comparator.isAsc());
                    column.pack();
                    if (column.equals(sortedColumn)) {
                        column.setWidth(width);
                    } else {
                        column.setWidth(width + 25);
                    }
                    tableViewer.refresh();
                }

                public void widgetDefaultSelected(SelectionEvent event) {
                }
            });
        }
    }
}
Also used : HashMap(java.util.HashMap) ColumnComparator(com.cubrid.common.ui.query.control.ColumnComparator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ColumnInfo(com.cubrid.common.ui.query.control.ColumnInfo) TableColumn(org.eclipse.swt.widgets.TableColumn) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 8 with ColumnInfo

use of com.cubrid.common.ui.query.control.ColumnInfo 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;
}
Also used : FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) ColumnInfo(com.cubrid.common.ui.query.control.ColumnInfo) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 9 with ColumnInfo

use of com.cubrid.common.ui.query.control.ColumnInfo in project cubrid-manager by CUBRID.

the class RowDetailDialog method update.

/**
	 * Update data to db and update ui.
	 *
	 * @param columnValue String
	 * @param fileCharset String
	 * @param oid String
	 * @return error message.
	 */
private String update(String columnValue, String fileCharset, String oid, boolean updateDB) {
    final ColumnInfo columnInfo = columnInfoList.get(selComboIndex);
    final String[] cols = new String[1];
    final String[] vals = new String[1];
    cols[0] = columnInfo.getName();
    vals[0] = columnValue;
    final Object realObj;
    boolean isMuchValue = DBAttrTypeFormatter.isMuchValueType(columnInfo.getComleteType(), -1);
    if (DataType.NULL_EXPORT_FORMAT.equals(vals[0])) {
        realObj = null;
    } else if (isMuchValue) {
        realObj = DBAttrTypeFormatter.formatMuchValue(vals[0], columnInfo.getComleteType(), qe.getQueryEditor().getConnection().checkAndConnectQuietly(), dbCharset, fileCharset, true);
    } else {
        realObj = DBAttrTypeFormatter.format(columnInfo.getComleteType(), vals[0], false, dbCharset, true).getFormatedJavaObj();
    }
    if (realObj instanceof Exception) {
        return ((Exception) realObj).getMessage();
    } else {
        try {
            Object[] values = null;
            if (realObj instanceof Object[]) {
                Object[] objCollection = DataType.getCollectionValues(columnInfo.getComleteType(), (Object[]) realObj, true);
                values = new Object[] { objCollection };
            } else {
                values = new Object[] { realObj };
            }
            if (updateDB) {
                qe.updateValue(oid, cols, values);
            }
        } catch (SQLException e) {
            LOGGER.error("", e);
            return Messages.bind(com.cubrid.common.ui.common.Messages.errCommonTip, e.getErrorCode(), e.getMessage());
        }
    }
    Display.getDefault().syncExec(new Runnable() {

        public void run() {
            String type = columnInfo.getType();
            if (DataType.NULL_EXPORT_FORMAT.equals(vals[0])) {
                dataItem.setData("" + (selComboIndex + 1), DataType.VALUE_NULL);
                dataItem.setText(selComboIndex + 1, DataType.NULL_EXPORT_FORMAT);
                dataMap.put(columnInfo.getIndex(), null);
                return;
            }
            String showValue = vals[0];
            if (DataType.DATATYPE_BLOB.equals(type)) {
                showValue = DataType.BLOB_EXPORT_FORMAT;
            } else if (DataType.DATATYPE_CLOB.equals(type)) {
                showValue = DataType.CLOB_EXPORT_FORMAT;
            } else if (DataType.DATATYPE_BIT.equals(type) || DataType.DATATYPE_BIT_VARYING.equals(type)) {
                byte[] bArr = (byte[]) realObj;
                if (bArr.length > FieldHandlerUtils.BIT_TYPE_MUCH_VALUE_LENGTH) {
                    showValue = DataType.BIT_EXPORT_FORMAT;
                } else {
                    showValue = "X'" + DBAttrTypeFormatter.getHexString(bArr) + "'";
                }
            }
            dataItem.setText(selComboIndex + 1, showValue);
            dataMap.put(columnInfo.getIndex(), new CellValue(showValue, showValue));
            columnValueText.setText(showValue);
            boolean isNull = DataType.VALUE_NULL.equals(dataItem.getData("" + (selComboIndex + 1)));
            if (isNull) {
                dataItem.setData("" + (selComboIndex + 1), "");
            }
        }
    });
    return null;
}
Also used : SQLException(java.sql.SQLException) ColumnInfo(com.cubrid.common.ui.query.control.ColumnInfo) CellValue(com.cubrid.common.ui.spi.table.CellValue) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 10 with ColumnInfo

use of com.cubrid.common.ui.query.control.ColumnInfo in project cubrid-manager by CUBRID.

the class RowDetailDialog method initial.

/**
	 * initializes some values
	 */
private void initial() {
    int index = 0;
    for (int i = 0; i < columnInfoList.size(); i++) {
        ColumnInfo columnInfo = columnInfoList.get(i);
        columnCombo.add(columnInfo.getName());
        if (columnInfo.getName().equals(columnName)) {
            index = i;
        }
    }
    columnCombo.select(index);
    boolean isNull = DataType.VALUE_NULL.equals(dataItem.getData("" + (index + 1)));
    if (isNull) {
        columnValueText.setText(DataType.NULL_EXPORT_FORMAT);
    } else {
        columnValueText.setText(dataItem.getText(index + 1));
    }
    selComboIndex = index;
}
Also used : ColumnInfo(com.cubrid.common.ui.query.control.ColumnInfo)

Aggregations

ColumnInfo (com.cubrid.common.ui.query.control.ColumnInfo)18 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)6 ExportDataFailedOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent)4 ExportDataSuccessEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent)4 ResultSetDataCache (com.cubrid.common.ui.cubrid.table.export.ResultSetDataCache)4 HashMap (java.util.HashMap)4 BufferedWriter (java.io.BufferedWriter)3 CellValue (com.cubrid.common.ui.spi.table.CellValue)2 FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)2 CUBRIDOIDProxy (com.cubrid.jdbc.proxy.driver.CUBRIDOIDProxy)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Blob (java.sql.Blob)2 Clob (java.sql.Clob)2 Map (java.util.Map)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 MenuItem (org.eclipse.swt.widgets.MenuItem)2 TableColumn (org.eclipse.swt.widgets.TableColumn)2