Search in sources :

Example 71 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Data method set.

/**
 * 保存记录
 *
 * 注意:
 * 更新不产生新节点,
 * 仅供内部持续补充.
 *
 * @param id
 * @param rd
 * @param ctime
 * @return 有更新为 1, 无更新为 0
 * @throws HongsException
 */
public int set(String id, Map rd, long ctime) throws HongsException {
    Map dd = get(id);
    int t = dd.isEmpty() ? 1 : 2;
    int i = padInf(dd, rd);
    // 无更新不存储
    if (i == 0) {
        return 0;
    }
    // 保存到文档库
    dd.put(Cnst.ID_KEY, id);
    Document dc = padDoc(dd);
    setDoc(id, dc);
    Table table = getTable();
    if (table == null) {
        return 1;
    }
    String uid = getUserId();
    String fid = getFormId();
    Object[] param = new String[] { id, fid, "0" };
    String where = "`id`=? AND `form_id`=? AND `etime`=?";
    Map nd = table.fetchCase().filter(where, param).select("ctime,state").getOne();
    if (!nd.isEmpty()) {
        if (Synt.declare(nd.get("state"), 0) == 0) {
            throw new HongsException(404, "Data item '" + id + "' is removed in " + getDbName()).setLocalizedContent("matrix.item.is.removed").setLocalizedContext("matrix");
        }
    /* 没有新增, 不必限时
            if (Synt.declare(nd.get("ctime"), 0L ) >= ctime) {
                throw new HongsException(400, "Wait 1 second to put '"+id+"' in "+getDbName())
                    .setLocalizedContent("matrix.wait.one.second")
                    .setLocalizedContext("matrix");
            } */
    } else {
        nd.put("ctime", ctime);
        nd.put("etime", 0);
        nd.put("state", t);
        nd.put("id", id);
        nd.put("form_id", fid);
        nd.put("user_id", uid);
    }
    // 数据快照和日志标题
    nd.put("__data__", dd);
    nd.put("data", Dawn.toString(dd, true));
    nd.put("name", getText(dd, "name"));
    // 操作备注和终端代码
    if (rd.containsKey("memo")) {
        nd.put("memo", getText(rd, "memo"));
    }
    if (rd.containsKey("meno")) {
        nd.put("meno", getText(rd, "meno"));
    }
    if (nd.containsKey("etime") == false) {
        table.update(nd, where, param);
    } else {
        table.insert(nd);
    }
    return 1;
}
Also used : Table(io.github.ihongs.db.Table) HongsException(io.github.ihongs.HongsException) Document(org.apache.lucene.document.Document) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 72 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Data method rev.

/**
 * 恢复记录
 * @param id
 * @param rd
 * @param ctime
 * @return 有更新为 1, 无更新为 0
 * @throws HongsException
 */
public int rev(String id, Map rd, long ctime) throws HongsException {
    Table table = getTable();
    if (table == null) {
        throw new HongsException(405, "Data table for '" + getDbName() + "' is not exists").setLocalizedContent("matrix.rev.unsupported").setLocalizedContext("matrix");
    }
    String uid = getUserId();
    String fid = getFormId();
    long rtime = Synt.declare(rd.get("rtime"), 0L);
    Object[] param = new String[] { id, fid, "0" };
    String where = "`id`=? AND `form_id`=? AND `etime`=?";
    Object[] para2 = new Object[] { id, fid, rtime };
    String wher2 = "`id`=? AND `form_id`=? AND `ctime`=?";
    // 获取当前数据
    Map od = table.fetchCase().filter(where, param).select("ctime").getOne();
    if (od.isEmpty()) {
    // throw new HongsException(404, "Can not find current '"+id+"' in "+getDbName())
    // .setLocalizedContent("matrix.node.not.exists")
    // .setLocalizedContext("matrix");
    } else if (Synt.declare(od.get("ctime"), 0L) >= ctime) {
        throw new HongsException(400, "Wait 1 second to del '" + id + "' in " + getDbName()).setLocalizedContent("matrix.wait.one.second").setLocalizedContext("matrix");
    }
    // 获取快照数据
    Map nd = table.fetchCase().filter(wher2, para2).getOne();
    if (nd.isEmpty()) {
        throw new HongsException(404, "Empty '" + id + "' at '" + ctime + "' in " + getDbName()).setLocalizedContent("matrix.node.not.exists").setLocalizedContext("matrix");
    }
    // 删除时保留的是删除前的快照, 即使为最终记录仍然可以恢复
    if (Synt.declare(nd.get("state"), 0) != 0) {
        if (Synt.declare(nd.get("etime"), 0L) == 0L) {
            throw new HongsException(400, "Alive '" + id + "' at '" + ctime + "' in " + getDbName()).setLocalizedContent("matrix.node.is.current").setLocalizedContext("matrix");
        }
    }
    Map ud = new HashMap();
    ud.put("etime", ctime);
    nd.put("ctime", ctime);
    nd.put("rtime", rtime);
    nd.put("etime", 0);
    nd.put("state", 3);
    nd.put("form_id", fid);
    nd.put("user_id", uid);
    // 操作备注和终端代码
    if (rd.containsKey("memo")) {
        nd.put("memo", getText(rd, "memo"));
    }
    if (rd.containsKey("meno")) {
        nd.put("meno", getText(rd, "meno"));
    }
    table.update(ud, where, param);
    table.insert(nd);
    // 保存到索引库
    Map dd = (Map) Dawn.toObject((String) nd.get("data"));
    dd.put(Cnst.ID_KEY, id);
    Document dc = padDoc(dd);
    setDoc(id, dc);
    return 1;
}
Also used : Table(io.github.ihongs.db.Table) HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Document(org.apache.lucene.document.Document) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 73 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Data method padInf.

/**
 * 填充准备保存的信息
 * @param dd 旧数据
 * @param rd 新数据
 * @return 0 无更新
 */
protected int padInf(Map dd, Map rd) {
    // 填充关联冗余
    try {
        includes(dd, rd);
    } catch (HongsException ex) {
        throw ex.toExemption();
    }
    int i = 0;
    Map<String, Map> fs = getFields();
    for (String fn : fs.keySet()) {
        if (!rd.containsKey(fn)) {
            continue;
        }
        Object fr = rd.get(fn);
        Object fo = dd.get(fn);
        dd.put(fn, fr);
        // 跳过环境字段, 比如修改时间
        if (!canSkip(fn, fr, fo)) {
            i++;
        }
    }
    // 填充自述字段, 如名称关键词
    if (i > 0) {
        Map<String, Map> fields = getFields();
        if (fields.containsKey("name")) {
            Map m = fields.get("name");
            if (Synt.declare(m.get("disabled"), false)) {
                dd.put("name", getName(dd));
            }
        }
        if (fields.containsKey("word")) {
            Map m = fields.get("word");
            if (Synt.declare(m.get("disabled"), false)) {
                dd.put("word", getWord(dd));
            }
        }
    }
    return i;
}
Also used : HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 74 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Form method readDocument.

private Document readDocument(File file) throws HongsException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        try {
            return builder.parse(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            return builder.newDocument();
        }
    } catch (ParserConfigurationException e) {
        throw new HongsException(e);
    } catch (SAXException e) {
        throw new HongsException(e);
    } catch (IOException e) {
        throw new HongsException(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HongsException(io.github.ihongs.HongsException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException)

Example 75 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class CommitRunner method run.

public static void run(Run run, Core core) throws HongsException, HongsExemption {
    // 外部已指定则不再处理
    if (core.isset(Cnst.REFLUX_MODE)) {
        try {
            // 执行
            run.run();
        } catch (Throwable ex) {
            if (ex instanceof HongsException) {
                throw (HongsException) ex;
            } else if (ex instanceof HongsExemption) {
                throw (HongsExemption) ex;
            }
            throw new HongsException(ex);
        }
        return;
    }
    try {
        // 开启
        core.set(Cnst.REFLUX_MODE, true);
        Hub crux = new Hub(core);
        Core.THREAD_CORE.set(crux);
        for (Object o : crux.values()) {
            if (o instanceof IReflux) {
                ((IReflux) o).begin();
            }
        }
        try {
            // 执行
            run.run();
            // 提交
            for (Object o : crux.values().toArray()) {
                if (o instanceof IReflux) {
                    ((IReflux) o).commit();
                }
            }
        } catch (Throwable ex) {
            // 回滚
            for (Object o : crux.values().toArray()) {
                if (o instanceof IReflux) {
                    ((IReflux) o).revert();
                }
            }
            if (ex instanceof HongsException) {
                throw (HongsException) ex;
            } else if (ex instanceof HongsExemption) {
                throw (HongsExemption) ex;
            }
            // 其他异常需包裹
            throw new HongsException(1109, ex);
        }
    } finally {
        // 重置
        core.unset(Cnst.REFLUX_MODE);
        Core.THREAD_CORE.set(core);
    }
}
Also used : HongsException(io.github.ihongs.HongsException) IReflux(io.github.ihongs.dh.IReflux) HongsExemption(io.github.ihongs.HongsExemption)

Aggregations

HongsException (io.github.ihongs.HongsException)138 Map (java.util.Map)77 HashMap (java.util.HashMap)61 LinkedHashMap (java.util.LinkedHashMap)31 IOException (java.io.IOException)29 Set (java.util.Set)26 HashSet (java.util.HashSet)25 ArrayList (java.util.ArrayList)24 List (java.util.List)20 HongsExemption (io.github.ihongs.HongsExemption)14 Action (io.github.ihongs.action.anno.Action)14 LinkedHashSet (java.util.LinkedHashSet)14 SQLException (java.sql.SQLException)13 FormSet (io.github.ihongs.action.FormSet)12 Table (io.github.ihongs.db.Table)12 FileNotFoundException (java.io.FileNotFoundException)11 CoreConfig (io.github.ihongs.CoreConfig)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 Iterator (java.util.Iterator)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9