Search in sources :

Example 1 with TmxContexts

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

the class ImportTmx method flush.

private void flush() throws SQLException {
    if (importStrategy == Constants.IMPORT_MODEL_ALWAYSADD) {
        // 始终增加
        // transaction control
        // long l = System.currentTimeMillis();
        db.beginTransaction();
        try {
            for (TmxTU tu : tmxTuCache) {
                addTu(tu);
                monitor.worked(1);
                if (monitor != null && monitor.isCanceled()) {
                    break;
                }
            }
        } catch (SQLException e) {
            db.rollBack();
            throw e;
        }
        db.commit();
        tmxTuCache.clear();
    // System.out.println((System.currentTimeMillis() - l));
    } else if (importStrategy == Constants.IMPORT_MODEL_OVERWRITE) {
        Map<TmxTU, List<TmxTU>> duplicateTuCache = new HashMap<TmxTU, List<TmxTU>>();
        // Map<TmxTU, List<TmxSegement>> duplicateTuvPkCache = new HashMap<TmxTU, List<TmxSegement>>();
        // long l2 = System.currentTimeMillis();
        List<TmxTU> filterSrcSameTu = filterSrcSameTu(tmxTuCache, Constants.IMPORT_MODEL_OVERWRITE);
        for (TmxTU tu : filterSrcSameTu) {
            if (monitor != null && monitor.isCanceled()) {
                return;
            }
            // long l1 = System.currentTimeMillis();
            List<TmxTU> dbTus = checkDuplicate(tu);
            if (dbTus == null) {
                // error TU
                continue;
            }
            // cached the DB exist TU
            duplicateTuCache.put(tu, dbTus);
            if (dbTus.size() != 0) {
                // long l = System.currentTimeMillis();
                for (TmxTU dbTu : dbTus) {
                    List<TmxSegement> tuvs = tu.getSegments();
                    List<TmxSegement> dbTuvs = new ArrayList<TmxSegement>();
                    for (TmxSegement tuv : tuvs) {
                        dbTuvs.addAll(db.getTextDataIdByGroupIdLang(dbTu.getTmId(), "M", tuv.getLangCode()));
                    }
                    dbTu.setSegments(dbTuvs);
                    List<TmxProp> dbProps = db.getTuMprops(dbTu.getTmId(), "TU");
                    dbTu.setProps(dbProps);
                    List<TmxNote> dbNotes = db.getTuMNote(dbTu.getTmId(), "TU");
                    dbTu.setNotes(dbNotes);
                // duplicateTuvPkCache.put(dbTu, dbTuvs);
                }
            // System.out.println("getTextDataidByGroup:"+ (System.currentTimeMillis() - l));
            }
        // System.out.println("tu check with db:"+ (System.currentTimeMillis() - l1));
        }
        // System.out.println("200 tu check with db:"+ (System.currentTimeMillis() - l2));
        tmxTuCache.clear();
        filterSrcSameTu.clear();
        // Transaction control
        db.beginTransaction();
        try {
            Iterator<TmxTU> tuIt = duplicateTuCache.keySet().iterator();
            while (tuIt.hasNext()) {
                if (monitor != null && monitor.isCanceled()) {
                    break;
                }
                TmxTU tu = tuIt.next();
                List<TmxTU> dbTus = duplicateTuCache.get(tu);
                if (dbTus.size() == 0) {
                    addTu(tu);
                } else {
                    for (TmxTU dbTu : dbTus) {
                        String changeDate = tu.getChangeDate() == null ? "" : tu.getChangeDate();
                        String changeId = tu.getChangeUser() == null ? "" : tu.getChangeUser();
                        String dbChangeDate = dbTu.getChangeDate() == null ? "" : dbTu.getChangeDate();
                        String dbChangeId = dbTu.getChangeUser() == null ? "" : dbTu.getChangeUser();
                        // Update TU Attribute (XmlElement Attribute)
                        if (!changeDate.equals(dbChangeDate) || !changeId.equals(dbChangeId)) {
                            db.updateTuChangeInfo(dbTu.getTmId(), tu.getTuId(), changeId, changeDate);
                        }
                        // Update Prop
                        List<TmxProp> props = tu.getProps();
                        List<TmxProp> dbProps = dbTu.getProps();
                        if (dbProps.size() == 0 && props != null) {
                            for (TmxProp _prop : props) {
                                db.insertTMXProp(dbTu.getTmId(), "TU", _prop.getName(), null, null, _prop.getValue());
                            }
                        } else if (props != null) {
                            for (TmxProp prop : props) {
                                boolean exist = false;
                                for (TmxProp dbProp : dbProps) {
                                    if (prop.equals(dbProp)) {
                                        exist = true;
                                        break;
                                    }
                                }
                                if (!exist) {
                                    db.deleteMprop("TU", dbTu.getTmId() + "");
                                    for (TmxProp _prop : props) {
                                        db.insertTMXProp(dbTu.getTmId(), "TU", _prop.getName(), null, null, _prop.getValue());
                                    }
                                    break;
                                }
                            }
                        }
                        // Update Note
                        List<TmxNote> notes = tu.getNotes();
                        List<TmxNote> dbNotes = dbTu.getNotes();
                        if (dbNotes.size() == 0 && notes != null) {
                            for (TmxNote note : notes) {
                                db.insertTMXNote(dbTu.getTmId(), "TU", note.getContent(), null, null, null, null, note.getEncoding(), note.getXmlLang());
                            }
                        } else if (notes != null) {
                            for (TmxNote note : notes) {
                                boolean exist = false;
                                for (TmxNote dbNote : dbNotes) {
                                    if (note.equals(dbNote)) {
                                        exist = true;
                                        break;
                                    }
                                }
                                if (!exist) {
                                    db.deleteMNote("TU", dbTu.getTmId() + "");
                                    for (TmxNote _note : notes) {
                                        db.insertTMXNote(dbTu.getTmId(), "TU", _note.getContent(), null, null, null, null, _note.getEncoding(), _note.getXmlLang());
                                    }
                                    break;
                                }
                            }
                        }
                        // Update TUVS except source TUV
                        List<TmxSegement> tuvs = tu.getSegments();
                        if (tuvs != null) {
                            for (TmxSegement tuv : tuvs) {
                                List<TmxSegement> dbTuvs = dbTu.getSegments();
                                String lang = tuv.getLangCode();
                                String content = tuv.getFullText();
                                if (content == null) {
                                    continue;
                                }
                                if (dbTuvs.size() != 0) {
                                    boolean flg = false;
                                    boolean isDuplicate = false;
                                    for (TmxSegement dbTuv : dbTuvs) {
                                        if (dbTuv.getLangCode().equalsIgnoreCase(lang)) {
                                            String dbContent = dbTuv.getFullText();
                                            if (!dbContent.equals(content)) {
                                                db.deleteAllTuvRelations(Arrays.asList(new Integer[] { dbTuv.getDbPk() }), lang);
                                                flg = true;
                                            }
                                            isDuplicate = true;
                                        }
                                    }
                                    if (flg == true || !isDuplicate) {
                                        addTuv(dbTu.getTmId(), tuv, null, null);
                                    }
                                } else {
                                    addTuv(dbTu.getTmId(), tuv, null, null);
                                }
                            }
                        }
                        // Update context
                        TmxContexts dbContexts = dbTu.getContexts();
                        if (tu.getContexts() != null) {
                            String preContext = tu.getContexts().getPreContext();
                            String nextContext = tu.getContexts().getNextContext();
                            if (dbContexts == null) {
                                db.updateTuvContext(dbTu.getTmId(), tu.getSource().getLangCode(), preContext, nextContext);
                            } else {
                                String dbPreContext = dbContexts.getPreContext();
                                String dbNextContext = dbContexts.getNextContext();
                                if (!dbPreContext.equals(preContext) || !dbNextContext.equals(nextContext)) {
                                    db.updateTuvContext(dbTu.getTmId(), tu.getSource().getLangCode(), preContext, nextContext);
                                }
                            }
                        }
                    }
                }
                monitor.worked(1);
            }
        } catch (SQLException e) {
            db.rollBack();
            throw e;
        }
        db.commit();
        duplicateTuCache.clear();
    } else if (importStrategy == Constants.IMPORT_MODEL_IGNORE) {
        List<TmxTU> needAddTus = new ArrayList<TmxTU>();
        List<TmxTU> filterSrcSameTu = filterSrcSameTu(tmxTuCache, Constants.IMPORT_MODEL_IGNORE);
        for (TmxTU tu : filterSrcSameTu) {
            if (monitor != null && monitor.isCanceled()) {
                return;
            }
            List<TmxTU> dbTus = checkDuplicate(tu);
            if (dbTus == null) {
                // error TU
                continue;
            }
            // cached the DB exist TU
            if (dbTus.size() == 0) {
                needAddTus.add(tu);
            }
        }
        tmxTuCache.clear();
        filterSrcSameTu.clear();
        // Transaction control
        db.beginTransaction();
        try {
            for (TmxTU tu : needAddTus) {
                if (monitor != null && monitor.isCanceled()) {
                    break;
                }
                addTu(tu);
                monitor.worked(1);
            }
        } catch (SQLException e) {
            db.rollBack();
            throw e;
        }
        db.commit();
        needAddTus.clear();
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) TmxSegement(net.heartsome.cat.common.bean.TmxSegement) TmxProp(net.heartsome.cat.common.bean.TmxProp) TmxContexts(net.heartsome.cat.common.bean.TmxContexts) TmxTU(net.heartsome.cat.common.bean.TmxTU) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) TmxNote(net.heartsome.cat.common.bean.TmxNote) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with TmxContexts

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

the class TmMatcher method updateFuzzyResult.

public void updateFuzzyResult(FuzzySearchResult fr) throws Exception {
    Object obj = fr.getDbOp();
    if (obj == null) {
        throw new Exception(Messages.getString("match.TmMatcher.updateFuzzyResult.msg1"));
    }
    DBOperator dbOp = (DBOperator) obj;
    boolean needEnd = false;
    PreparedStatement stmt = null;
    try {
        if (dbOp.isClosed()) {
            needEnd = true;
            dbOp.start();
        }
        int tuDbPk = fr.getTu().getTmId();
        int srcDbPk = fr.getTu().getSource().getDbPk();
        int tgtDbPk = fr.getTu().getTarget().getDbPk();
        Connection conn = dbOp.getConnection();
        if (conn == null || conn.isClosed()) {
            throw new Exception(Messages.getString("match.TmMatcher.updateFuzzyResult.msg2"));
        }
        dbOp.beginTransaction();
        dbOp.deleteAllTuvRelations(Arrays.asList(srcDbPk), fr.getTu().getSource().getLangCode());
        dbOp.deleteAllTuvRelations(Arrays.asList(tgtDbPk), fr.getTu().getTarget().getLangCode());
        TmxContexts contexts = fr.getTu().getContexts();
        saveTuv(dbOp, tuDbPk, fr.getTu().getSource(), contexts == null ? null : contexts.getPreContext(), contexts == null ? null : contexts.getNextContext());
        saveTuv(dbOp, tuDbPk, fr.getTu().getTarget(), null, null);
        String sql = "UPDATE MTU SET CHANGEID = ?, CHANGEDATE = ? WHERE MTUPKID = ? ";
        stmt = conn.prepareStatement(sql);
        int i = 1;
        stmt.setString(i++, fr.getTu().getChangeUser());
        stmt.setTimestamp(i++, DateUtils.getTimestampFromUTC(fr.getTu().getChangeDate()));
        stmt.setInt(i++, tuDbPk);
        stmt.executeUpdate();
        dbOp.commit();
    } catch (Exception e) {
        logger.error("delete TM in db error", e);
        try {
            dbOp.rollBack();
        } catch (SQLException e1) {
            logger.error("delete tm DB rollback error", e);
        }
        throw e;
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                logger.error("delete TM stmt close error", e);
            }
        }
        if (needEnd) {
            try {
                if (dbOp != null) {
                    dbOp.end();
                }
            } catch (SQLException e) {
                logger.error("delete TB  DB close conneciton error", e);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DBOperator(net.heartsome.cat.database.DBOperator) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) TmxContexts(net.heartsome.cat.common.bean.TmxContexts)

Aggregations

SQLException (java.sql.SQLException)2 TmxContexts (net.heartsome.cat.common.bean.TmxContexts)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 TmxNote (net.heartsome.cat.common.bean.TmxNote)1 TmxProp (net.heartsome.cat.common.bean.TmxProp)1 TmxSegement (net.heartsome.cat.common.bean.TmxSegement)1 TmxTU (net.heartsome.cat.common.bean.TmxTU)1 DBOperator (net.heartsome.cat.database.DBOperator)1