Search in sources :

Example 26 with DBOperator

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

the class TermViewerBodyMenu method deleteSelectTerm.

void deleteSelectTerm() {
    IProject project = getCurentProject();
    if (null == project) {
        // show some msg here
        return;
    }
    DBOperator dbOperator = getDbOperator(project);
    if (null == dbOperator) {
        // show error msg here
        MessageDialog.openInformation(this.termView.getSite().getShell(), Messages.getString("handler.AddTermToTBHandler.msgTitle"), Messages.getString("handler.AddTermToTBHandler.msg"));
        return;
    }
    GridItem selectItem = getSelectItem();
    if (null == selectItem) {
        // show some msg here
        return;
    }
    try {
        dbOperator.start();
        dbOperator.beginTransaction();
        Object id = selectItem.getData("DBID");
        if (id instanceof String) {
            String termId = (String) id;
            dbOperator.deleteTermEntry(termId, getSrcLang(), getTgtLang());
        }
        dbOperator.commit();
    } catch (SQLException e) {
        MessageDialog.openInformation(this.termView.getSite().getShell(), Messages.getString("view.TerminologyViewPart.action.msg"), Messages.getString("view.TerminologyViewPart.action.deleteFailed"));
        try {
            dbOperator.rollBack();
        } catch (SQLException e1) {
            logger.error("RollBack error", e);
        }
        logger.error("execute sql error", e);
    } catch (ClassNotFoundException e) {
        logger.error("data base driver not found", e);
    } finally {
        if (null != dbOperator) {
            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) IProject(org.eclipse.core.resources.IProject)

Example 27 with DBOperator

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

the class DatabaseService method importTmxWithFile.

/**
	 * 将TMX文件导入到指定的数据库中
	 * @param metaData
	 *            数据库元数据,封装了一系列连接数据库的参数,用于连接数据,参考{@link MetaData}
	 * @param fileName
	 *            TMX文件完整路径
	 * @param monitor
	 *            需要使用的进度条,如果不需要使用进度条,则为null
	 * @param strategy
	 *            TMX导入策略,参考{@link Constants}中的定义
	 * @param checkContext
	 *            TMX导入时,是否需要检查上下文
	 * @return 导入结果,为int型数据,参考{@link ImportAbstract}中的定义;
	 * @throws ImportException
	 */
public static int importTmxWithFile(MetaData metaData, String fileName, IProgressMonitor monitor, int strategy, boolean checkContext) throws ImportException {
    // add convert to tmx method
    File convert2Tmx = null;
    try {
        convert2Tmx = ConverterUtil.convert2Tmx(fileName, monitor);
    } catch (ImportException e) {
        LOGGER.error("", e);
        throw new ImportException(e.getMessage().replace("\n", " "));
    }
    File file = null;
    if (convert2Tmx != null) {
        file = convert2Tmx;
    } else {
        file = new File(fileName);
    }
    if (file.exists() || !file.isDirectory()) {
        // try {
        // // 检查原始文件是否能通过 VTD 的解析
        // new TmxReader(file);
        // } catch (TmxReadException e1) {
        // throw new ImportException(e1.getMessage().replace("\n", " "));
        // }
        // File _file = Utils.clearTmxFile(file);
        DBOperator dbOp = getDBOperator(metaData);
        try {
            dbOp.start();
            // ImportAbstract impOp = new ImportTmx(dbOp, strategy, checkContext);
            // int result = impOp.doImport(_file.getAbsolutePath(), monitor);
            // return result;
            ImportTmx i = new ImportTmx(dbOp, strategy, monitor);
            i.importTmxFile(file);
            return 1;
        } catch (SQLException e) {
            LOGGER.error(Messages.getString("service.DatabaseService.logger1") + dbOp.getMetaData().getDatabaseName(), e);
            return ImportAbstract.FAILURE_3;
        } catch (ClassNotFoundException e) {
            LOGGER.error(Messages.getString("service.DatabaseService.logger2"), e);
            return ImportAbstract.FAILURE_3;
        } finally {
            if (dbOp != null) {
                try {
                    dbOp.end();
                } catch (SQLException e) {
                    LOGGER.error(Messages.getString("service.DatabaseService.logger3"), e);
                }
            }
            if (null != convert2Tmx) {
                convert2Tmx.delete();
            }
        // _file.delete();
        }
    }
    return ImportAbstract.FAILURE_1;
}
Also used : ImportException(net.heartsome.cat.common.core.exception.ImportException) SQLException(java.sql.SQLException) SystemDBOperator(net.heartsome.cat.database.SystemDBOperator) DBOperator(net.heartsome.cat.database.DBOperator) File(java.io.File) ImportTmx(net.heartsome.cat.document.ImportTmx)

Example 28 with DBOperator

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

the class DatabaseService method importTmxWithString.

/**
	 * 将内容为TMX标准格式的字符串数据导入到指定的数据库中
	 * @param metaData
	 *            数据库元数据,封装了一系列连接数据库的参数,用于连接数据,参考{@link MetaData}
	 * @param tmxStr
	 *            内容为TMX标准格式的字符串
	 * @param monitor
	 *            需要使用的进度条,如果不需要使用进度条,则为null
	 * @param strategy
	 *            TMX导入策略,参考{@link Constants}中的定义
	 * @param checkContext
	 *            TMX导入时,是否需要检查上下文
	 * @param sourceLanguage
	 *            该字符串内容的TMX源语言
	 * @return 导入结果,为int型数据,参考{@link ImportAbstract}中的定义;
	 * @throws ImportException
	 */
public static int importTmxWithString(MetaData metaData, String tmxStr, IProgressMonitor monitor, int strategy, boolean checkContext, String sourceLanguage) throws ImportException {
    if (tmxStr != null && tmxStr.length() > 0) {
        DBOperator dbOp = getDBOperator(metaData);
        try {
            dbOp.start();
            // ImportAbstract impOp = new ImportTmx(dbOp, strategy, checkContext);
            // int result = impOp.doImport(tmxStr, sourceLanguage, monitor);
            ImportTmx i = new ImportTmx(dbOp, strategy, monitor);
            i.importTmxContent(tmxStr);
            return 1;
        } catch (SQLException e) {
            LOGGER.error(Messages.getString("service.DatabaseService.logger4") + dbOp.getMetaData().getDatabaseName(), e);
            return ImportAbstract.FAILURE_3;
        } catch (ClassNotFoundException e) {
            LOGGER.error(Messages.getString("service.DatabaseService.logger5"), e);
            return ImportAbstract.FAILURE_3;
        } finally {
            if (dbOp != null) {
                try {
                    dbOp.end();
                } catch (SQLException e) {
                    LOGGER.error(Messages.getString("service.DatabaseService.logger6"), e);
                }
            }
        }
    }
    return ImportAbstract.FAILURE;
}
Also used : SQLException(java.sql.SQLException) SystemDBOperator(net.heartsome.cat.database.SystemDBOperator) DBOperator(net.heartsome.cat.database.DBOperator) ImportTmx(net.heartsome.cat.document.ImportTmx)

Example 29 with DBOperator

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

the class DatabaseService method getDBOperator.

/**
	 * 获取数据处理对象{@link DBOperator},该数据处理对象用于访问指定的数据库中的数据<br>
	 * 在访问数据库时,根据不同数据库类型获取相应的处理对象.这些数据库被封装在{@link MetaData}中
	 * @param metaData
	 *            当前数据库的元数据,封装了连接数据库的一系列参数
	 * @return 返回null时,表示未获取到数据处理对象;
	 */
public static DBOperator getDBOperator(MetaData metaData) {
    DBOperator dbOperator = null;
    @SuppressWarnings({ "rawtypes", "unchecked" }) ServiceTracker tracker = new ServiceTracker(Activator.context, DBServiceProvider.class.getName(), null);
    tracker.open();
    try {
        Object[] services = tracker.getServices();
        for (Object i : services) {
            DBServiceProvider serviceProvider = (DBServiceProvider) i;
            DBOperator temp = serviceProvider.getTmDatabaseInstance();
            if (temp == null) {
                continue;
            }
            if (temp.getDbConfig().getDefaultType().equals(metaData.getDbType())) {
                dbOperator = temp;
                dbOperator.setMetaData(metaData);
            }
        }
    } finally {
        tracker.close();
    }
    return dbOperator;
}
Also used : DBServiceProvider(net.heartsome.cat.database.DBServiceProvider) ServiceTracker(org.osgi.util.tracker.ServiceTracker) SystemDBOperator(net.heartsome.cat.database.SystemDBOperator) DBOperator(net.heartsome.cat.database.DBOperator)

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