Search in sources :

Example 1 with TmxNote

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

the class ImportTmx method addTu.

private boolean addTu(TmxTU tu) throws SQLException {
    TmxSegement srcTuv = tu.getSource();
    List<TmxSegement> tuvs = tu.getSegments();
    if ((srcTuv == null && tuvs == null) || (srcTuv == null && tuvs.size() < 2)) {
        // check TU
        return false;
    }
    String tuId = tu.getTuId();
    if (tuId == null || tuId.equals("")) {
        tuId = generateTuId();
    }
    int tuPk = db.insertTU(0, tuId, tu.getCreationUser(), tu.getCreationDate(), tu.getChangeUser(), tu.getChangeDate(), tu.getCreationTool(), tu.getCreationToolVersion(), null, null, null);
    List<TmxNote> notes = tu.getNotes();
    if (notes != null && notes.size() != 0) {
        for (TmxNote note : notes) {
            db.insertTMXNote(tuPk, "TU", note.getContent(), null, null, null, null, note.getEncoding(), note.getXmlLang());
        }
    }
    List<TmxProp> props = tu.getProps();
    if (props != null && props.size() != 0) {
        for (TmxProp prop : props) {
            db.insertTMXProp(tuPk, "TU", prop.getName(), null, null, prop.getValue());
        }
    }
    String preContext = null;
    String nextContext = null;
    if (tu.getContexts() != null) {
        preContext = tu.getContexts().getPreContext();
        nextContext = tu.getContexts().getNextContext();
    }
    if (srcTuv != null) {
        addTuv(tuPk, srcTuv, preContext, nextContext);
        preContext = null;
        nextContext = null;
    }
    if (tuvs != null) {
        for (TmxSegement tuv : tuvs) {
            addTuv(tuPk, tuv, preContext, nextContext);
        }
    }
    return true;
}
Also used : TmxNote(net.heartsome.cat.common.bean.TmxNote) TmxSegement(net.heartsome.cat.common.bean.TmxSegement) TmxProp(net.heartsome.cat.common.bean.TmxProp)

Example 2 with TmxNote

use of net.heartsome.cat.common.bean.TmxNote 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 3 with TmxNote

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

the class TmxReader method readTuNoteElement.

private void readTuNoteElement(TmxTU tu) throws VTDException {
    VTDNav vn = vu.getVTDNav();
    vn.push();
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath("./note");
    while (ap.evalXPath() != -1) {
        String fragment = vu.getElementFragment();
        TmxNote note = new TmxNote();
        note.setContent(fragment);
        int inx = vn.getAttrVal("xml:lang");
        String value = inx != -1 ? vn.toString(inx) : null;
        if (value != null) {
            note.setXmlLang(value);
        }
        inx = vn.getAttrVal("o-encoding");
        value = inx != -1 ? vn.toString(inx) : null;
        if (value != null) {
            note.setXmlLang(value);
        }
        tu.appendNote(note);
    }
    vn.pop();
}
Also used : AutoPilot(com.ximpleware.AutoPilot) TmxNote(net.heartsome.cat.common.bean.TmxNote) VTDNav(com.ximpleware.VTDNav)

Example 4 with TmxNote

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

the class DBOperator method getTuMNote.

public List<TmxNote> getTuMNote(int parentId, String parentName) throws SQLException {
    List<TmxNote> result = new ArrayList<TmxNote>();
    // SELECT CONTENT FROM MNOTE WHERE PARENTID=? AND PARENTNAME=?
    String sql = dbConfig.getOperateDbSQL("get-mnote-byparentid");
    PreparedStatement psmt = null;
    ResultSet rs = null;
    try {
        psmt = conn.prepareStatement(sql);
        psmt.setInt(1, parentId);
        psmt.setString(2, parentName);
        rs = psmt.executeQuery();
        while (rs.next()) {
            TmxNote note = new TmxNote();
            note.setContent(rs.getString(1));
            result.add(note);
        }
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (psmt != null) {
            psmt.close();
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) TmxNote(net.heartsome.cat.common.bean.TmxNote) PreparedStatement(java.sql.PreparedStatement)

Aggregations

TmxNote (net.heartsome.cat.common.bean.TmxNote)4 ArrayList (java.util.ArrayList)2 TmxProp (net.heartsome.cat.common.bean.TmxProp)2 TmxSegement (net.heartsome.cat.common.bean.TmxSegement)2 AutoPilot (com.ximpleware.AutoPilot)1 VTDNav (com.ximpleware.VTDNav)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 TmxContexts (net.heartsome.cat.common.bean.TmxContexts)1 TmxTU (net.heartsome.cat.common.bean.TmxTU)1