Search in sources :

Example 6 with DBOperator

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

the class ExportTmxImpl method executeExport.

/**
	 * (non-Javadoc)
	 * @see net.heartsome.cat.document.ExportAbstract#executeExport()
	 */
@Override
public String executeExport(IProgressMonitor monitor) {
    String mTuFilter = "";
    String textDataFilter = "";
    String mNoteFilter = "";
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.beginTask("", dbList.size());
    for (Iterator<ExportDatabaseBean> iterator = dbList.iterator(); iterator.hasNext(); ) {
        ExportDatabaseBean db = iterator.next();
        String srcLang = db.getSrcLang();
        DBOperator dbOp = DatabaseService.getDBOperator(db.getDbBean());
        // 过滤条件
        if (this.filterBean != null) {
            mTuFilter = dbOp.generationExportTMXFilter("MTU", this.filterBean);
            textDataFilter = dbOp.generationExportTMXFilter("TEXTDATA", this.filterBean);
            mNoteFilter = dbOp.generationExportTMXFilter("MNOTE", this.filterBean);
        }
        try {
            dbOp.start();
            output = new FileOutputStream(db.getExportFilePath());
            if (encoding.equalsIgnoreCase("UTF-16LE")) {
                output.write(0xFF);
                output.write(0xFE);
            } else if (encoding.equalsIgnoreCase("UTF-16BE")) {
                output.write(0xFE);
                output.write(0xFF);
            }
            List<Integer> filterTu = dbOp.getAfterFilterTuPk(mTuFilter, mNoteFilter, textDataFilter);
            writeHeader(srcLang);
            writeString("<body>\n");
            IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
            subMonitor.beginTask(Messages.getString("document.ExportTmxImpl.task1") + db.getDbBean().getDatabaseName(), filterTu.size());
            subMonitor.setTaskName(Messages.getString("document.ExportTmxImpl.task1") + db.getDbBean().getDatabaseName());
            for (int i = 0; i < filterTu.size(); i++) {
                // long l = System.currentTimeMillis();
                if (monitor.isCanceled()) {
                    clearResource();
                    break;
                }
                int tuPk = filterTu.get(i);
                String tuNodeContent = dbOp.retrieveTu(tuPk, db.getHasSelectedLangs(), isToplevelTmx, isTagLevelTmx);
                if (tuNodeContent != null && !tuNodeContent.equals("")) {
                    writeString(tuNodeContent);
                }
                subMonitor.worked(1);
            }
            //$NON-NLS-1$
            writeString("</body>\n");
            //$NON-NLS-1$	
            writeString("</tmx>\n");
            subMonitor.done();
        } catch (SQLException e) {
            logger.error(DBOP_ERROR, e);
            e.printStackTrace();
            clearResource();
            return DBOP_ERROR + db.getDbBean().getDbType() + " " + db.getDbBean().getDatabaseName();
        } catch (ClassNotFoundException e) {
            logger.error(JDBC_ERROR, e);
            e.printStackTrace();
            clearResource();
            return JDBC_ERROR + db.getDbBean().getDbType();
        } catch (IOException e) {
            logger.error(FILE_ERROR, e);
            e.printStackTrace();
            clearResource();
            return FILE_ERROR + db.getDbBean().getDbType() + " " + db.getDbBean().getDatabaseName();
        } finally {
            try {
                output.close();
                if (dbOp != null) {
                    dbOp.end();
                }
            } catch (SQLException e) {
                logger.error(RELEASE_DB_ERROR, e);
            } catch (IOException e) {
                logger.error(RELEASE_FILE_ERROR, e);
            }
        }
    }
    if (monitor.isCanceled()) {
        return USER_CANCEL;
    }
    monitor.done();
    return SUCCESS;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SQLException(java.sql.SQLException) DBOperator(net.heartsome.cat.database.DBOperator) IOException(java.io.IOException) ExportDatabaseBean(net.heartsome.cat.database.bean.ExportDatabaseBean) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileOutputStream(java.io.FileOutputStream)

Example 7 with DBOperator

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

the class DatabaseService method importTbxWithFile.

/**
	 * 将TBX文件导入到指定的数据库中
	 * @param fileName
	 *            TBX文件完整路径
	 * @param monitor
	 *            需要使用的进度条,如果不需要使用进度条,则为null
	 * @param metaData
	 *            数据库元数据,封装了一系列连接数据库的参数,用于连接数据,参考{@link MetaData}
	 * @param strategy
	 *            TBX导入策略,参考{@link Constants}中的定义
	 * @return 导入结果,为int型数据,参考{@link ImportAbstract}中的定义;;
	 * @throws ImportException
	 */
public static int importTbxWithFile(String fileName, IProgressMonitor monitor, MetaData metaData, int strategy) throws ImportException {
    File file = new File(fileName);
    if (file.exists() || !file.isDirectory()) {
        if (!fileName.toLowerCase().endsWith(".tbx")) {
            monitor.beginTask("", 100);
            File convet2tbx = null;
            try {
                convet2tbx = ConverterUtil.convert2Tbx(fileName, new SubProgressMonitor(monitor, 30));
            } catch (OperationCanceledException e) {
                return CANCEL;
            } catch (ImportException e) {
                LOGGER.error("", e);
                throw new ImportException(e.getMessage().replace("\n", " "));
            }
            if (convet2tbx != null) {
                file = convet2tbx;
            } else {
                file = new File(fileName);
            }
        } else {
            monitor.beginTask("", 70);
        }
        DBOperator dbOp = getDBOperator(metaData);
        try {
            dbOp.start();
            ImportAbstract impOp = new ImportTbx(dbOp, strategy);
            int result = impOp.doImport(file.getAbsolutePath(), new SubProgressMonitor(monitor, 70));
            return result;
        } catch (SQLException e) {
            LOGGER.error(Messages.getString("service.DatabaseService.logger10") + dbOp.getMetaData().getDatabaseName(), e);
            return ImportAbstract.FAILURE_3;
        } catch (ClassNotFoundException e) {
            LOGGER.error(Messages.getString("service.DatabaseService.logger11"), e);
            return ImportAbstract.FAILURE_3;
        } finally {
            if (dbOp != null) {
                try {
                    dbOp.end();
                } catch (SQLException e) {
                    LOGGER.error(Messages.getString("service.DatabaseService.logger12"), e);
                }
            }
            monitor.done();
        }
    }
    return ImportAbstract.FAILURE;
}
Also used : ImportException(net.heartsome.cat.common.core.exception.ImportException) ImportTbx(net.heartsome.cat.document.ImportTbx) ImportAbstract(net.heartsome.cat.document.ImportAbstract) SQLException(java.sql.SQLException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SystemDBOperator(net.heartsome.cat.database.SystemDBOperator) DBOperator(net.heartsome.cat.database.DBOperator) File(java.io.File) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 8 with DBOperator

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

the class TermViewerBodyMenu method editTerm.

void editTerm() {
    IProject curentProject = getCurentProject();
    DBOperator dbOperator = getDbOperator(curentProject);
    try {
        if (dbOperator.getConnection() == null) {
            dbOperator.start();
        }
        GridItem selectItem = getSelectItem();
        String srcTerm = selectItem.getText(1);
        String tgtTerm = selectItem.getText(2);
        String properValue = selectItem.getText(3);
        AddTermToTBDialog dialog = AddTermToTBDialog.getInstance(this.termView.getSite().getShell(), srcTerm.toString().trim(), tgtTerm.toString().trim(), AddTermToTBDialog.EDIT_TYPE);
        dialog.setProject(curentProject);
        dialog.setSrcLang(getSrcLang());
        dialog.setTgtLang(getTgtLang());
        dialog.setPropertyValue(properValue);
        dialog.setDbOperator(dbOperator);
        Object id = selectItem.getData("DBID");
        int open = dialog.open();
        if (Dialog.OK == open) {
            if (id instanceof String) {
                String termId = (String) id;
                dbOperator.beginTransaction();
                dbOperator.deleteTermEntry(termId, getSrcLang(), getTgtLang());
            }
            dbOperator.commit();
        }
    } catch (Exception e) {
        MessageDialog.openInformation(this.termView.getSite().getShell(), Messages.getString("view.TerminologyViewPart.action.msg"), Messages.getString("view.TerminologyViewPart.action.editFailed"));
        try {
            dbOperator.rollBack();
        } catch (SQLException e1) {
            logger.error("RollBack error", e);
        }
        logger.error("", e);
    } finally {
        try {
            dbOperator.end();
        } catch (SQLException e) {
            logger.error("", e);
        }
    }
}
Also used : GridItem(org.eclipse.nebula.widgets.grid.GridItem) SQLException(java.sql.SQLException) DBOperator(net.heartsome.cat.database.DBOperator) AddTermToTBDialog(net.heartsome.cat.ts.ui.term.dialog.AddTermToTBDialog) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) SQLException(java.sql.SQLException)

Example 9 with DBOperator

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

the class NewProjectTmPage method checkDbHashMatch.

/**
	 * 需要调用Database模块 检查当前项目在库中是否有语言对的匹配
	 * @param dbModel
	 *            数据库信息;
	 */
private void checkDbHashMatch(DatabaseModelBean dbModel, String type) {
    DBOperator dbOp = DatabaseService.getDBOperator(dbModel.toDbMetaData());
    try {
        dbOp.start();
        dbModel.setHasMatch(dbOp.checkHasMatchs(super.projSourceLang.getCode(), type));
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
        dbModel.setHasMatch(false);
    } finally {
        try {
            if (dbOp != null) {
                dbOp.end();
            }
        } catch (SQLException e) {
            LOGGER.error("", e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) DBOperator(net.heartsome.cat.database.DBOperator) SQLException(java.sql.SQLException)

Example 10 with DBOperator

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

the class ListPropCol method setValue.

/**
	 * 处理添加/删除标记以及保存修改后的值(只保存在表格中)
	 * @see de.jaret.util.ui.table.model.PropCol#setValue(de.jaret.util.ui.table.model.IRow, java.lang.Object)
	 */
@SuppressWarnings("rawtypes")
public void setValue(IRow row, Object value) {
    Object oldValue = getValue(row);
    if (isRealModification(oldValue, value)) {
        try {
            Object base = row;
            for (int i = 0; i < _propPath.length - 1; i++) {
                String propName = _propPath[i];
                Method getter = base.getClass().getMethod("get" + propName, new Class[] {});
                base = getter.invoke(base, new Object[] {});
            }
            if (_accessor == null) {
                Class<?> clazz;
                if (value == null) {
                    clazz = getContentClass(row);
                } else {
                    if (value instanceof Enum) {
                        clazz = ((Enum) value).getDeclaringClass();
                    } else {
                        clazz = value.getClass();
                    }
                    if (clazz.equals(Boolean.class)) {
                        clazz = Boolean.TYPE;
                    } else if (clazz.equals(Integer.class)) {
                        clazz = Integer.TYPE;
                    } else if (clazz.equals(Double.class)) {
                        clazz = Double.TYPE;
                    }
                }
                if (index >= 0 && base instanceof XPropRow) {
                    XPropRow propRow = (XPropRow) base;
                    List<String> list = propRow.getLstTarget();
                    list.set(index, (String) value);
                    Method setter = base.getClass().getMethod("set" + _propPath[_propPath.length - 1], new Class[] { list.getClass() });
                    setter.invoke(base, new Object[] { list });
                } else {
                    //						添加/删除标记
                    if (index < 0 && base instanceof XPropRow) {
                        XPropRow propRow = (XPropRow) base;
                        HashMap<String, String> map = (HashMap<String, String>) propRow.getDataMap();
                        boolean blnIsAddTag = (Boolean) value;
                        MetaData metaData = (MetaData) propRow.getData("metaData");
                        DBOperator dbop = DatabaseService.getDBOperator(metaData);
                        try {
                            dbop.start();
                            dbop.addOrRemoveFlag(blnIsAddTag, map.get("id"));
                            dbop.commit();
                        } catch (SQLException e1) {
                            try {
                                dbop.rollBack();
                            } catch (SQLException e) {
                                String text = blnIsAddTag ? Messages.getString("dialog.ListPropCol.logger1") : Messages.getString("dialog.ListPropCol.logger2");
                                LOGGER.error(text + Messages.getString("dialog.ListPropCol.logger3"), e);
                            }
                        } catch (ClassNotFoundException e1) {
                            try {
                                dbop.rollBack();
                            } catch (SQLException e) {
                                LOGGER.error(Messages.getString("dialog.ListPropCol.logger4"), e);
                            }
                        } finally {
                            try {
                                if (dbop != null) {
                                    dbop.end();
                                }
                            } catch (SQLException e) {
                                LOGGER.error("", e);
                            }
                        }
                    }
                    Method setter = base.getClass().getMethod("set" + _propPath[_propPath.length - 1], new Class[] { clazz });
                    setter.invoke(base, new Object[] { value });
                }
            } else {
                _accessor.setValue(base, value);
            }
            fireValueChanged(row, this, oldValue, value);
        } catch (Exception e) {
            LOGGER.error("", e);
            e.printStackTrace();
            throw new RuntimeException("Could not set value " + e.getLocalizedMessage());
        }
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) DBOperator(net.heartsome.cat.database.DBOperator) Method(java.lang.reflect.Method) SQLException(java.sql.SQLException) MetaData(net.heartsome.cat.common.bean.MetaData)

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