Search in sources :

Example 31 with MetaData

use of net.heartsome.cat.common.bean.MetaData in project translationstudio8 by heartsome.

the class ConcordanceSearchDialog method getCurPageMap.

private LinkedHashMap<MetaData, List<Integer>> getCurPageMap() {
    int startIndex = (curPageNum - 1) * rowNumPerPage;
    int endIndex = curPageNum * rowNumPerPage;
    if (curPageNum == amountPage) {
        endIndex = size;
    }
    Iterator<Entry<MetaData, ArrayList<Integer>>> it = mapGroupId.entrySet().iterator();
    int index = 0;
    LinkedHashMap<MetaData, List<Integer>> mapSub = new LinkedHashMap<MetaData, List<Integer>>();
    while (it.hasNext()) {
        Entry<MetaData, ArrayList<Integer>> entry = (Entry<MetaData, ArrayList<Integer>>) it.next();
        ArrayList<Integer> lstGroupId = entry.getValue();
        if (index <= startIndex && index + lstGroupId.size() >= startIndex) {
            List<Integer> subList = null;
            if (index + lstGroupId.size() - 1 >= endIndex) {
                subList = lstGroupId.subList(startIndex - index, endIndex - index);
                mapSub.put(entry.getKey(), subList);
                break;
            } else {
                subList = lstGroupId.subList(startIndex - index, lstGroupId.size());
                index += lstGroupId.size();
                startIndex += lstGroupId.size();
                mapSub.put(entry.getKey(), subList);
            }
        } else {
            index += lstGroupId.size();
        }
    }
    return mapSub;
}
Also used : Entry(java.util.Map.Entry) MetaData(net.heartsome.cat.common.bean.MetaData) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) LinkedHashMap(java.util.LinkedHashMap)

Example 32 with MetaData

use of net.heartsome.cat.common.bean.MetaData in project translationstudio8 by heartsome.

the class TermBaseSearchDialog method search.

/**
	 * 搜索术语库 ;
	 */
@SuppressWarnings("unchecked")
public void search() {
    updateHistory(cmbSearch, lstSearchHistory);
    String searchText = cmbSearch.getText();
    searchText = cleanString(searchText);
    if (searchText == null || searchText.trim().equals("")) {
        MessageDialog.openInformation(getShell(), Messages.getString("dialog.TermBaseSearchDialog.msgTitle"), Messages.getString("dialog.TermBaseSearchDialog.msg1"));
        return;
    }
    if (lstDatabase.size() == 0) {
        MessageDialog.openInformation(getShell(), Messages.getString("dialog.TermBaseSearchDialog.msgTitle"), Messages.getString("dialog.TermBaseSearchDialog.msg2"));
        return;
    }
    ArrayList<String> lstSelLangs = new ArrayList<String>();
    lstSelLangs.add(strSrcLang);
    for (MenuItem item : menu.getItems()) {
        if (item.getSelection()) {
            lstSelLangs.add(item.getText());
        }
    }
    LinkedHashMap<MetaData, HashMap<String, IdentityHashMap<String, String>>> mapResult = new LinkedHashMap<MetaData, HashMap<String, IdentityHashMap<String, String>>>();
    ArrayList<DatabaseModelBean> lstDB = new ArrayList<DatabaseModelBean>();
    if (cmbDatabase.getSelectionIndex() == 0) {
        lstDB.addAll(lstDatabase);
    } else {
        DatabaseModelBean model = lstDatabase.get(cmbDatabase.getSelectionIndex() - 1);
        lstDB.add(model);
    }
    for (DatabaseModelBean model : lstDB) {
        MetaData metaData = model.toDbMetaData();
        DBOperator dbop = DatabaseService.getDBOperator(metaData);
        try {
            dbop.start();
            HashMap<String, IdentityHashMap<String, String>> mapTermBase = dbop.getTermBaseResult(searchText, !(btnIsCaseSensitive.getSelection()), btnApplyRegularExpression.getSelection(), btnIsIgnoreMark.getSelection(), strSrcLang, lstSelLangs, spiMatchQuality.getSelection());
            if (mapTermBase != null && mapTermBase.size() > 0) {
                mapResult.put(metaData, mapTermBase);
            }
        } catch (SQLException e1) {
            LOGGER.error(Messages.getString("dialog.TermBaseSearchDialog.logger2"), e1);
        } catch (ClassNotFoundException e1) {
            LOGGER.error(Messages.getString("dialog.TermBaseSearchDialog.logger3"), e1);
        } finally {
            try {
                if (dbop != null) {
                    dbop.end();
                }
            } catch (SQLException e) {
                LOGGER.error("", e);
            }
        }
    }
    Rectangle rect = grid.getBounds();
    grid.removeAll();
    grid.pack();
    grid.setHeaderVisible(true);
    grid.setBounds(rect);
    if (mapResult.size() == 0) {
        MessageDialog.openInformation(getShell(), Messages.getString("dialog.TermBaseSearchDialog.msgTitle"), Messages.getString("dialog.TermBaseSearchDialog.msg3"));
        return;
    }
    srcCellRenderer.setStrText(searchText);
    srcCellRenderer.setBlnIsCaseSensitive(btnIsCaseSensitive.getSelection());
    srcCellRenderer.setBlnIsApplyRegular(btnApplyRegularExpression.getSelection());
    if (mapResult.size() > 0) {
        if (btnApplyRegularExpression.getSelection()) {
            Iterator<Entry<MetaData, HashMap<String, IdentityHashMap<String, String>>>> iterator = mapResult.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<MetaData, HashMap<String, IdentityHashMap<String, String>>> entry = iterator.next();
                MetaData metaData1 = entry.getKey();
                HashMap<String, IdentityHashMap<String, String>> map = entry.getValue();
                Iterator<Entry<String, IdentityHashMap<String, String>>> it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, IdentityHashMap<String, String>> e = it.next();
                    createGridItem(metaData1, e.getValue());
                }
            }
        } else {
            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();
                createGridItem((MetaData) arrObj[0], (IdentityHashMap<String, String>) arrObj[1]);
            }
        }
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) IdentityHashMap(java.util.IdentityHashMap) DatabaseModelBean(net.heartsome.cat.common.bean.DatabaseModelBean) ArrayList(java.util.ArrayList) Rectangle(org.eclipse.swt.graphics.Rectangle) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) MetaData(net.heartsome.cat.common.bean.MetaData) DBOperator(net.heartsome.cat.database.DBOperator) MenuItem(org.eclipse.swt.widgets.MenuItem)

Example 33 with MetaData

use of net.heartsome.cat.common.bean.MetaData in project translationstudio8 by heartsome.

the class TermBaseSearchDialog 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.TermBaseSearchDialog.logger1"), e);
        } catch (ClassNotFoundException e) {
            LOGGER.error(Messages.getString("dialog.TermBaseSearchDialog.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<GridColumn> lstShowColumn = new ArrayList<GridColumn>();
                // 每增加一列,除标记列外的其他列的和加100,然后平均分配给各个语言列,删除一列则做相反的操作
                if (itemLang.getSelection()) {
                    int totalWidth = 0;
                    boolean blnIsResetWidth = false;
                    for (int index = 0; index < grid.getColumnCount(); index++) {
                        GridColumn column = grid.getColumn(index);
                        if (column.getText().equals(lang) && column.getWidth() == 0) {
                            lstShowColumn.add(column);
                            blnIsResetWidth = true;
                        } else if (column.getWidth() > 0) {
                            totalWidth += column.getWidth();
                            lstShowColumn.add(column);
                        }
                    }
                    if (blnIsResetWidth) {
                        int width = (totalWidth + 100) / lstShowColumn.size();
                        for (GridColumn column : lstShowColumn) {
                            column.setWidth(width);
                        }
                    }
                // if (grid.getItemCount() > 0) {
                // search();
                // }
                } else {
                    GridColumn deleteColumn = null;
                    for (int index = 0; index < grid.getColumnCount(); index++) {
                        GridColumn column = grid.getColumn(index);
                        if (column.getWidth() > 0) {
                            lstShowColumn.add(column);
                        }
                        if (column.getText().equals(lang)) {
                            deleteColumn = column;
                            // 将删除列中的数据清空,以保证行高正常调整
                            for (GridItem item : grid.getItems()) {
                                item.setText(index, "");
                            }
                        }
                    }
                    int width = (deleteColumn.getWidth() * lstShowColumn.size() - 100) / (lstShowColumn.size() - 1);
                    deleteColumn.setWidth(0);
                    lstShowColumn.remove(deleteColumn);
                    for (GridColumn column : lstShowColumn) {
                        column.setWidth(width);
                    }
                // search();
                }
            }
        });
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) SelectionListener(org.eclipse.swt.events.SelectionListener) Listener(org.eclipse.swt.widgets.Listener) KeyListener(org.eclipse.swt.events.KeyListener) SQLException(java.sql.SQLException) DatabaseModelBean(net.heartsome.cat.common.bean.DatabaseModelBean) ArrayList(java.util.ArrayList) DBOperator(net.heartsome.cat.database.DBOperator) MenuItem(org.eclipse.swt.widgets.MenuItem) GridItem(org.eclipse.nebula.widgets.grid.GridItem) MetaData(net.heartsome.cat.common.bean.MetaData) DisposeEvent(org.eclipse.swt.events.DisposeEvent) KeyEvent(org.eclipse.swt.events.KeyEvent) Event(org.eclipse.swt.widgets.Event) SelectionEvent(org.eclipse.swt.events.SelectionEvent) GridColumn(org.eclipse.nebula.widgets.grid.GridColumn) Menu(org.eclipse.swt.widgets.Menu) HashSet(java.util.HashSet)

Example 34 with MetaData

use of net.heartsome.cat.common.bean.MetaData in project translationstudio8 by heartsome.

the class OperateSystemDBImpl method addSysDb.

/**
	 * 插入系统库
	 * @param dbName
	 *            数据库名称
	 * @param quality
	 *            是否优化
	 */
public void addSysDb(String dbName, String quality, int type) {
    MetaData data = null;
    try {
        data = (MetaData) metaData.clone();
        data.setDatabaseName(Constants.HSSYSDB);
    } catch (CloneNotSupportedException e) {
        logger.warn("", e);
        return;
    }
    String driver = dbConfig.getDriver();
    String url = Utils.replaceParams(dbConfig.getDbURL(), data);
    Properties prop = Utils.replaceParams(dbConfig.getConfigProperty(), data);
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = getConnection(driver, url, prop);
        String insertDb = Utils.replaceParams(dbConfig.getInsertSysDb(), data);
        stmt = conn.prepareStatement(insertDb);
        stmt.setString(1, dbName);
        stmt.setString(2, quality);
        stmt.setInt(3, type);
        stmt.execute();
    } catch (ClassNotFoundException e) {
        logger.warn("", e);
    } catch (SQLException e) {
        logger.warn("", e);
    } finally {
        Statement _stmt = null;
        try {
            _stmt = conn.createStatement();
            _stmt.executeUpdate("SHUTDOWN;");
        } catch (SQLException e) {
            logger.error("", e);
        } finally {
            if (_stmt != null) {
                try {
                    _stmt.close();
                } catch (SQLException e) {
                    logger.error("", e);
                }
            }
        }
        freeConnection(stmt, conn);
    }
}
Also used : SQLException(java.sql.SQLException) MetaData(net.heartsome.cat.common.bean.MetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties)

Example 35 with MetaData

use of net.heartsome.cat.common.bean.MetaData in project translationstudio8 by heartsome.

the class OperateSystemDBImpl method checkDbExistOnServer.

/**
	 * (non-Javadoc)
	 * @see net.heartsome.cat.database.SystemDBOperator#checkDbExistOnServer()
	 */
@Override
public boolean checkDbExistOnServer() {
    Connection conn = null;
    MetaData data = null;
    try {
        data = (MetaData) metaData.clone();
    } catch (CloneNotSupportedException e) {
        logger.warn("", e);
        return false;
    }
    try {
        String url = Utils.replaceParams(dbConfig.getDbURL(), data);
        String driver = dbConfig.getDriver();
        Properties prop = Utils.replaceParams(dbConfig.getConfigProperty(), data);
        prop.setProperty("ifexists", "true");
        conn = getConnection(driver, url, prop);
    } catch (ClassNotFoundException e) {
        logger.error(Messages.getString("hsql.OperateSystemDBImpl.logger3"), e);
        return false;
    } catch (SQLException e) {
        // Just check ,so don't need record the exception
        return false;
    } finally {
        if (conn != null) {
            try {
                Statement stmt = conn.createStatement();
                stmt.executeUpdate("SHUTDOWN;");
                freeConnection(stmt, conn);
            } catch (SQLException e) {
                logger.error("", e);
            }
        }
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) MetaData(net.heartsome.cat.common.bean.MetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) Properties(java.util.Properties)

Aggregations

MetaData (net.heartsome.cat.common.bean.MetaData)40 SQLException (java.sql.SQLException)21 ArrayList (java.util.ArrayList)14 Properties (java.util.Properties)13 Connection (java.sql.Connection)11 PreparedStatement (java.sql.PreparedStatement)11 DatabaseModelBean (net.heartsome.cat.common.bean.DatabaseModelBean)8 DBOperator (net.heartsome.cat.database.DBOperator)8 Statement (java.sql.Statement)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 Entry (java.util.Map.Entry)5 Point (org.eclipse.swt.graphics.Point)5 MenuItem (org.eclipse.swt.widgets.MenuItem)5 List (java.util.List)4 SystemDBOperator (net.heartsome.cat.database.SystemDBOperator)4 File (java.io.File)2 HashSet (java.util.HashSet)2 IdentityHashMap (java.util.IdentityHashMap)2