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