Search in sources :

Example 16 with DBOperator

use of net.heartsome.cat.database.DBOperator in project translationstudio8 by heartsome.

the class Utils method convertFile2TbModel.

public static Map<DatabaseModelBean, String> convertFile2TbModel(File f, boolean loadLang) throws Exception {
    String path = f.getParent();
    String name = f.getName();
    System.out.println(path + " " + name);
    DatabaseModelBean selectedVal = new DatabaseModelBean();
    selectedVal.setDbName(name);
    selectedVal.setDbType(Constants.DBTYPE_SQLITE);
    selectedVal.setItlDBLocation(path);
    String lang = "";
    DBOperator dbOp = DatabaseService.getDBOperator(selectedVal.toDbMetaData());
    Statement stmt = null;
    ResultSet rs = null;
    try {
        dbOp.start();
        stmt = dbOp.getConnection().createStatement();
        rs = stmt.executeQuery("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
        List<String> tables = new ArrayList<String>();
        while (rs.next()) {
            String tname = rs.getString(1);
            if (tname.toUpperCase().startsWith("MATRIX_")) {
                continue;
            } else {
                tables.add(tname);
            }
        }
        List<String> l = Arrays.asList("BATTRIBUTE", "BMARTIFHEADER", "BNODE", "BREFOBJECTLIST", "BTERMENTRY", "LANG", "MEXTRA", "MHEADER", "MHEADERNODE", "MNOTE", "MPROP", "MTU", "TEXTDATA");
        if (!tables.containsAll(l)) {
            String message = Messages.getString("tb.dialog.addFileTb.error.msg1");
            throw new Exception(message);
        }
        if (loadLang) {
            List<String> langs = dbOp.getLanguages();
            for (int j = 0; j < langs.size(); j++) {
                lang += langs.get(j);
                if (j != langs.size() - 1) {
                    lang += ",";
                }
            }
        }
        Map<DatabaseModelBean, String> result = new HashMap<DatabaseModelBean, String>();
        result.put(selectedVal, lang);
        return result;
    } catch (Exception ex) {
        LOGGER.error("", ex);
        String message = Messages.getString("tb.dialog.addFileTb.error.msg2");
        message = MessageFormat.format(message, name);
        throw new Exception(message);
    } finally {
        try {
            if (dbOp != null) {
                dbOp.end();
            }
        } catch (SQLException ex) {
            LOGGER.error("", ex);
        }
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Statement(java.sql.Statement) DatabaseModelBean(net.heartsome.cat.common.bean.DatabaseModelBean) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DBOperator(net.heartsome.cat.database.DBOperator) SQLException(java.sql.SQLException)

Example 17 with DBOperator

use of net.heartsome.cat.database.DBOperator in project translationstudio8 by heartsome.

the class Utils method convertFile2TmModel.

public static Map<DatabaseModelBean, String> convertFile2TmModel(File f, boolean loadLang) throws Exception {
    String path = f.getParent();
    String name = f.getName();
    DatabaseModelBean selectedVal = new DatabaseModelBean();
    selectedVal.setDbName(name);
    selectedVal.setDbType(Constants.DBTYPE_SQLITE);
    selectedVal.setItlDBLocation(path);
    DBOperator dbOp = DatabaseService.getDBOperator(selectedVal.toDbMetaData());
    String lang = "";
    try {
        dbOp.start();
        Statement stmt = dbOp.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
        List<String> tables = new ArrayList<String>();
        while (rs.next()) {
            String tname = rs.getString(1);
            if (tname.toUpperCase().startsWith("MATRIX_")) {
                continue;
            } else {
                tables.add(tname);
            }
        }
        List<String> l = Arrays.asList("BATTRIBUTE", "BMARTIFHEADER", "BNODE", "BREFOBJECTLIST", "BTERMENTRY", "LANG", "MEXTRA", "MHEADER", "MHEADERNODE", "MNOTE", "MPROP", "MTU", "TEXTDATA");
        if (!tables.containsAll(l)) {
            throw new Exception(Messages.getString("tm.dialog.addFileTm.error.msg1"));
        }
        if (loadLang) {
            List<String> langs = dbOp.getLanguages();
            for (int j = 0; j < langs.size(); j++) {
                lang += langs.get(j);
                if (j != langs.size() - 1) {
                    lang += ",";
                }
            }
        }
        Map<DatabaseModelBean, String> result = new HashMap<DatabaseModelBean, String>();
        result.put(selectedVal, lang);
        return result;
    } catch (Exception ex) {
        LOGGER.error("", ex);
        String message = Messages.getString("tm.dialog.addFileTm.error.msg2");
        message = MessageFormat.format(message, name);
        throw new Exception(message);
    } finally {
        try {
            if (dbOp != null) {
                dbOp.end();
            }
        } catch (SQLException ex) {
            LOGGER.error("", ex);
        }
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Statement(java.sql.Statement) DatabaseModelBean(net.heartsome.cat.common.bean.DatabaseModelBean) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DBOperator(net.heartsome.cat.database.DBOperator) SQLException(java.sql.SQLException)

Example 18 with DBOperator

use of net.heartsome.cat.database.DBOperator in project translationstudio8 by heartsome.

the class ConcordanceSearchDialog method initLanguageMenu.

/**
	 * 初始化语言菜单 ;
	 */
private void initLanguageMenu() {
    Set<String> set = new HashSet<String>();
    for (DatabaseModelBean model : lstDatabase) {
        MetaData metaData = model.toDbMetaData();
        DBOperator dbop = DatabaseService.getDBOperator(metaData);
        if (null == dbop) {
            continue;
        }
        try {
            dbop.start();
            set.addAll(dbop.getLanguages());
        } catch (SQLException e) {
            LOGGER.error(Messages.getString("dialog.ConcordanceSearchDialog.logger1"), e);
        } catch (ClassNotFoundException e) {
            LOGGER.error(Messages.getString("dialog.ConcordanceSearchDialog.logger1"), e);
        } finally {
            try {
                if (dbop != null) {
                    dbop.end();
                }
            } catch (SQLException e) {
                LOGGER.error("", e);
            }
        }
    }
    set.remove(strSrcLang);
    set.remove(strTgtLang);
    lstLangs = new ArrayList<String>(set);
    Collections.sort(lstLangs);
    // cmbLang.setItems((String[]) langs.toArray(new String[langs.size()]));
    menu = new Menu(getShell(), SWT.POP_UP);
    if (strTgtLang != null) {
        MenuItem itemTgt = new MenuItem(menu, SWT.CHECK);
        itemTgt.setText(strTgtLang);
        itemTgt.setSelection(true);
        itemTgt.setEnabled(false);
    }
    for (final String lang : lstLangs) {
        final MenuItem itemLang = new MenuItem(menu, SWT.CHECK);
        itemLang.setText(lang);
        itemLang.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event event) {
                ArrayList<IColumn> lstShowColumn = new ArrayList<IColumn>();
                // }
                if (itemLang.getSelection()) {
                    // int totalWidth = 0;
                    boolean blnIsResetWidth = false;
                    // tableModel.getColumnCount()
                    for (int index = 1; index < tableModel.getColumnCount(); index++) {
                        IColumn column = tableModel.getColumn(index);
                        int width = jTable.getTableViewState().getColumnWidth(column);
                        if (column.getHeaderLabel().equals(lang) && width == 0) {
                            jTable.getTableViewState().setColumnVisible(column, true);
                            jTable.updateColumnList();
                            jTable.redraw();
                            // jTable.getTableViewState().setColumnWidth(column, width0);
                            lstShowColumn.add(column);
                            blnIsResetWidth = true;
                        } else if (width > 0) {
                            // totalWidth += width;
                            lstShowColumn.add(column);
                        // jTable.columnWidthChanged(column, width0);
                        }
                    }
                    if (blnIsResetWidth) {
                        int width = totalWidth / lstShowColumn.size() + 1;
                        for (int colNum = 1; colNum < jTable.getColumnCount(); colNum++) {
                            jTable.getTableViewState().setColumnWidth(jTable.getColumn(colNum), width);
                        }
                    // for (IColumn column : lstShowColumn) {
                    // // jTable.getTableViewState().setColumnVisible(column, true);
                    // jTable.getTableViewState().setColumnWidth(column, width);
                    // }
                    }
                // initGroupIdAndSearch();
                } else {
                    // int totalWidth = 0;
                    IColumn deleteColumn = null;
                    for (int index = 1; index < jTable.getColumnCount(); index++) {
                        IColumn column = jTable.getColumn(index);
                        int width = jTable.getTableViewState().getColumnWidth(column);
                        if (width > 0) {
                            // totalWidth += width;
                            lstShowColumn.add(column);
                        }
                        if (column.getHeaderLabel().equals(lang)) {
                            deleteColumn = column;
                        // 将删除列中的数据清空,以保证行高正常调整
                        // for (GridItem item : tableModel.getRowCount()) {
                        // item.setText(index, "");
                        // }
                        }
                    }
                    // int width = (jTable.getTableViewState().getColumnWidth(deleteColumn) * lstShowColumn.size() -
                    // 100)
                    // / (lstShowColumn.size() - 1);
                    int width = totalWidth / (lstShowColumn.size() - 1) + 1;
                    jTable.getTableViewState().setColumnWidth(deleteColumn, 0);
                    jTable.getTableViewState().setColumnVisible(deleteColumn, false);
                    lstShowColumn.remove(deleteColumn);
                    for (IColumn column : lstShowColumn) {
                        jTable.getTableViewState().setColumnWidth(column, width);
                    }
                // initGroupIdAndSearch();
                }
            }
        });
    }
    btnSelectLang.addListener(SWT.Selection, new Listener() {

        public void handleEvent(Event event) {
            Rectangle rect = btnSelectLang.getBounds();
            Point pt = btnSelectLang.toDisplay(new Point(event.x, event.y + rect.height));
            // Point pt = btnSelectLang.toDisplay(new Point(event.x + rect.width, event.y + rect.height));
            menu.setOrientation(getShell().getOrientation());
            menu.setLocation(pt.x, pt.y);
            menu.setVisible(true);
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) SelectionListener(org.eclipse.swt.events.SelectionListener) Listener(org.eclipse.swt.widgets.Listener) IExpansionListener(org.eclipse.ui.forms.events.IExpansionListener) KeyListener(org.eclipse.swt.events.KeyListener) SQLException(java.sql.SQLException) DatabaseModelBean(net.heartsome.cat.common.bean.DatabaseModelBean) ArrayList(java.util.ArrayList) Rectangle(org.eclipse.swt.graphics.Rectangle) DBOperator(net.heartsome.cat.database.DBOperator) MenuItem(org.eclipse.swt.widgets.MenuItem) Point(org.eclipse.swt.graphics.Point) IColumn(de.jaret.util.ui.table.model.IColumn) MetaData(net.heartsome.cat.common.bean.MetaData) DisposeEvent(org.eclipse.swt.events.DisposeEvent) FocusEvent(org.eclipse.swt.events.FocusEvent) KeyEvent(org.eclipse.swt.events.KeyEvent) Event(org.eclipse.swt.widgets.Event) ExpansionEvent(org.eclipse.ui.forms.events.ExpansionEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) HashSet(java.util.HashSet)

Example 19 with DBOperator

use of net.heartsome.cat.database.DBOperator in project translationstudio8 by heartsome.

the class TermDbManagerDialog method searchCurrServerDatabase.

private List<DatabaseManagerDbListBean> searchCurrServerDatabase(SystemDBOperator sysDbOp, DatabaseModelBean currServer) {
    List<DatabaseManagerDbListBean> temp = new ArrayList<DatabaseManagerDbListBean>();
    // 检查是否创建了系统库
    if (!sysDbOp.checkSysDb()) {
        // MessageDialog.openInformation(getShell(), "提示信息",
        // "当前服务器上没有创建任何库");
        setLastSelectedServer(null);
        return null;
    }
    // 检查是否创建了库
    List<String> dbNames = sysDbOp.getSysDbNames(Constants.DB_TYPE_TB);
    if (dbNames.size() == 0) {
        // MessageDialog.openInformation(getShell(), "提示信息",
        // "当前服务器上没有创建任何库");
        setLastSelectedServer(null);
        return null;
    }
    // 获取数据库相关资料,封装了库名称和语言
    MetaData metaData = currServer.toDbMetaData();
    DBOperator dbop = DatabaseService.getDBOperator(metaData);
    for (int i = 0; i < dbNames.size(); i++) {
        DatabaseManagerDbListBean bean = new DatabaseManagerDbListBean();
        String dbName = dbNames.get(i);
        bean.setIndex(i + 1 + "");
        bean.setDbName(dbName);
        metaData.setDatabaseName(dbName);
        dbop.setMetaData(metaData);
        String lang = "";
        try {
            dbop.start();
            List<String> langs = dbop.getLanguages();
            for (int j = 0; j < langs.size(); j++) {
                lang += langs.get(j);
                if (j != langs.size() - 1) {
                    lang += ", ";
                }
            }
        } catch (Exception e1) {
            logger.error("", e1);
            continue;
        } finally {
            try {
                if (dbop != null) {
                    dbop.end();
                }
            } catch (SQLException e) {
                logger.error("", e);
            }
        }
        if (lang.equals("")) {
            bean.setLangs(Messages.getString("dialog.TermDbManagerDialog.msg10"));
        } else {
            bean.setLangs(lang);
        }
        temp.add(bean);
    }
    return temp;
}
Also used : SQLException(java.sql.SQLException) DatabaseManagerDbListBean(net.heartsome.cat.database.ui.bean.DatabaseManagerDbListBean) MetaData(net.heartsome.cat.common.bean.MetaData) ArrayList(java.util.ArrayList) SystemDBOperator(net.heartsome.cat.database.SystemDBOperator) DBOperator(net.heartsome.cat.database.DBOperator) Point(org.eclipse.swt.graphics.Point) SQLException(java.sql.SQLException)

Example 20 with DBOperator

use of net.heartsome.cat.database.DBOperator in project translationstudio8 by heartsome.

the class ConcordanceSearchDialog method search.

/**
	 * 搜索记忆库 ;
	 */
private boolean search() {
    String searchText = cmbSearch.getText();
    if (searchText == null || searchText.trim().equals("")) {
        txtPage.setText("");
        MessageDialog.openInformation(getShell(), Messages.getString("dialog.ConcordanceSearchDialog.msgTitle"), Messages.getString("dialog.ConcordanceSearchDialog.msg2"));
        return false;
    }
    StringBuffer searchTextBf = new StringBuffer(searchText);
    TreeMap<String, InnerTagBean> tags = InnerTagUtil.parseXmlToDisplayValue(searchTextBf, TagStyle.getDefault(false));
    searchText = InnerTagUtil.resolveTag(searchTextBf.toString());
    searchText = InnerTagUtil.escapeTag(searchText);
    searchText = InnerTagUtil.parseDisplayToXmlValue(tags, searchText);
    // 此处的判断已加在 ConcordanceSearchHandler 中
    // if (lstDatabase.size() == 0) {
    // txtPage.setText("");
    // MessageDialog.openInformation(getShell(), "提示", "请为项目指定翻译记忆库");
    // return false;
    // }
    ArrayList<String> lstSelLangs = new ArrayList<String>();
    lstSelLangs.add(strSrcLang);
    for (MenuItem item : menu.getItems()) {
        if (item.getSelection()) {
            lstSelLangs.add(item.getText());
        }
    }
    LinkedHashMap<MetaData, List<Integer>> mapSub = getCurPageMap();
    LinkedHashMap<MetaData, List<ConcordanceBean>> mapResult = new LinkedHashMap<MetaData, List<ConcordanceBean>>();
    Iterator<Entry<MetaData, List<Integer>>> iterator = mapSub.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<MetaData, List<Integer>> entry = (Entry<MetaData, List<Integer>>) iterator.next();
        MetaData metaData = entry.getKey();
        DBOperator dbop = DatabaseService.getDBOperator(metaData);
        try {
            dbop.start();
            List<ConcordanceBean> lstConcordance = dbop.getConcordanceSearchResult(cmbSearch.getText(), btnIsCaseSensitive.getSelection(), btnApplyRegularExpression.getSelection(), btnIsIgnoreMark.getSelection(), strSrcLang, lstSelLangs, entry.getValue());
            if (lstConcordance != null && lstConcordance.size() > 0) {
                mapResult.put(metaData, lstConcordance);
            }
        } catch (SQLException e1) {
            LOGGER.error(Messages.getString("dialog.ConcordanceSearchDialog.logger2"), e1);
        } catch (ClassNotFoundException e1) {
            LOGGER.error(Messages.getString("dialog.ConcordanceSearchDialog.logger3"), e1);
        } finally {
            try {
                if (dbop != null) {
                    dbop.end();
                }
            } catch (SQLException e) {
                LOGGER.error("", e);
            }
        }
    }
    if (mapResult.size() == 0) {
        clearTable();
        MessageDialog.openInformation(getShell(), Messages.getString("dialog.ConcordanceSearchDialog.msgTitle"), Messages.getString("dialog.ConcordanceSearchDialog.msg1"));
        txtPage.setText("");
        return false;
    }
    // 当有多个数据库的数据时要对所有数据按 changedate 重新排序,然后取前 spiResultCount.getSelection() 数量的记录
    int rowNum = 0;
    HashMap<IRow, ArrayList<int[]>> mapStyle = new HashMap<IRow, ArrayList<int[]>>();
    if (mapResult.size() > 1) {
        LinkedHashMap<String, Object[]> map = sortMap(mapResult);
        Iterator<Entry<String, Object[]>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Object[]> entry = it.next();
            Object[] arrObj = entry.getValue();
            createItem((MetaData) arrObj[0], (ConcordanceBean) arrObj[1], rowNum++, mapStyle);
        }
    } else {
        Iterator<Entry<MetaData, List<ConcordanceBean>>> it = mapResult.entrySet().iterator();
        while (it.hasNext()) {
            Entry<MetaData, List<ConcordanceBean>> entry = it.next();
            MetaData metaData2 = entry.getKey();
            List<ConcordanceBean> lstBean = entry.getValue();
            for (ConcordanceBean bean : lstBean) {
                createItem(metaData2, bean, rowNum++, mapStyle);
            }
            if (lstBean.size() < rowNumPerPage) {
                for (int row = lstBean.size(); row < rowNumPerPage; row++) {
                    if (rowNum < tableModel.getRowCount() && tableModel.getRow(rowNum) != null) {
                        tableModel.remRow(tableModel.getRow(rowNum));
                    }
                }
            }
        }
    }
    jTable.registerCellRenderer(tableModel.getColumn(1), new StyleTextCellRenderer(searchText, btnIsCaseSensitive.getSelection(), btnApplyRegularExpression.getSelection(), createResultsStyle()));
    jTable.registerCellRenderer(tableModel.getColumn(tableModel.getColumnCount() - 1), new AttributeTextCellRenderer(mapStyle, style));
    jTable.setTableModel(tableModel);
    return true;
}
Also used : SQLException(java.sql.SQLException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) InnerTagBean(net.heartsome.cat.common.innertag.InnerTagBean) LinkedHashMap(java.util.LinkedHashMap) ConcordanceBean(net.heartsome.cat.database.tmx.ConcordanceBean) Entry(java.util.Map.Entry) MetaData(net.heartsome.cat.common.bean.MetaData) List(java.util.List) ArrayList(java.util.ArrayList) DBOperator(net.heartsome.cat.database.DBOperator) MenuItem(org.eclipse.swt.widgets.MenuItem) Point(org.eclipse.swt.graphics.Point) IRow(de.jaret.util.ui.table.model.IRow) StyleTextCellRenderer(net.heartsome.cat.ts.ui.jaret.renderer.StyleTextCellRenderer)

Aggregations

DBOperator (net.heartsome.cat.database.DBOperator)29 SQLException (java.sql.SQLException)28 ArrayList (java.util.ArrayList)9 DatabaseModelBean (net.heartsome.cat.common.bean.DatabaseModelBean)8 MetaData (net.heartsome.cat.common.bean.MetaData)8 SystemDBOperator (net.heartsome.cat.database.SystemDBOperator)6 HashMap (java.util.HashMap)5 MenuItem (org.eclipse.swt.widgets.MenuItem)5 Point (org.eclipse.swt.graphics.Point)4 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 GridItem (org.eclipse.nebula.widgets.grid.GridItem)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2