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);
}
}
}
}
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;
}
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;
}
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;
}
Aggregations