Search in sources :

Example 16 with Table

use of io.github.ihongs.db.Table in project HongsCORE by ihongs.

the class Data method del.

/**
 * 删除记录
 *
 * 注意:
 * 删除时产生新节点,
 * 重复调用不会多增.
 *
 * @param id
 * @param rd
 * @param ctime
 * @return 有更新为 1, 无更新为 0
 * @throws HongsException
 */
public int del(String id, Map rd, long ctime) throws HongsException {
    delDoc(id);
    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 od = table.fetchCase().filter(where, param).select("ctime,state,data,name").getOne();
    if (od.isEmpty() || Synt.declare(od.get("state"), 0) == 0) {
        // 删除是幂等的可重复调用
        return 0;
    }
    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 ud = new HashMap();
    ud.put("etime", ctime);
    Map nd = new HashMap();
    nd.put("ctime", ctime);
    nd.put("etime", 0);
    nd.put("state", 0);
    nd.put("id", id);
    nd.put("form_id", fid);
    nd.put("user_id", uid);
    // 拷贝快照和日志标题
    nd.put("data", od.get("data"));
    nd.put("name", od.get("name"));
    // 操作备注和终端代码
    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);
    return 1;
}
Also used : Table(io.github.ihongs.db.Table) HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 17 with Table

use of io.github.ihongs.db.Table 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 18 with Table

use of io.github.ihongs.db.Table 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 19 with Table

use of io.github.ihongs.db.Table in project HongsCORE by ihongs.

the class Data method add.

/**
 * 添加记录
 * @param id
 * @param rd
 * @param ctime
 * @return
 * @throws HongsException
 */
public int add(String id, Map rd, long ctime) throws HongsException {
    Map dd = new HashMap();
    padInf(dd, rd);
    // 保存到文档库
    dd.put(Cnst.ID_KEY, id);
    Document dc = padDoc(dd);
    addDoc(id, dc);
    Table table = getTable();
    if (table == null) {
        return 1;
    }
    String uid = getUserId();
    String fid = getFormId();
    Map nd = new HashMap();
    nd.put("ctime", ctime);
    nd.put("etime", 0);
    nd.put("state", 1);
    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"));
    }
    table.insert(nd);
    return 1;
}
Also used : Table(io.github.ihongs.db.Table) 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 20 with Table

use of io.github.ihongs.db.Table in project HongsCORE by ihongs.

the class Form method deleteAuthRole.

protected void deleteAuthRole(String id) throws HongsException {
    ActionHelper helper = Core.getInstance(ActionHelper.class);
    String uid = (String) helper.getSessibute(Cnst.UID_SES);
    String tan;
    // 删除权限
    tan = (String) table.getParams().get("role.table");
    if (tan != null) {
        Table tab = db.getTable(tan);
        tab.remove("`role` IN (?)", Synt.setOf(centra + "/" + id + "/search", centra + "/" + id + "/create", centra + "/" + id + "/update", centra + "/" + id + "/delete", centra + "/" + id + "/revert"));
    }
    // 更新缓存(通过改变权限更新时间)
    tan = (String) table.getParams().get("user.table");
    if (tan != null) {
        Table tab = db.getTable(tan);
        tab.update(Synt.mapOf("rtime", System.currentTimeMillis() / 1000), "`id` = ?", uid);
    }
}
Also used : Table(io.github.ihongs.db.Table) ActionHelper(io.github.ihongs.action.ActionHelper)

Aggregations

Table (io.github.ihongs.db.Table)29 Map (java.util.Map)26 HashMap (java.util.HashMap)22 HongsException (io.github.ihongs.HongsException)12 HashSet (java.util.HashSet)11 Set (java.util.Set)11 DB (io.github.ihongs.db.DB)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)5 TreeSet (java.util.TreeSet)5 ActionHelper (io.github.ihongs.action.ActionHelper)4 ArrayList (java.util.ArrayList)4 Document (org.apache.lucene.document.Document)4 FetchCase (io.github.ihongs.db.util.FetchCase)3 Iterator (java.util.Iterator)3 Action (io.github.ihongs.action.anno.Action)2 CommitSuccess (io.github.ihongs.action.anno.CommitSuccess)2 Verify (io.github.ihongs.action.anno.Verify)2 Loop (io.github.ihongs.db.link.Loop)2 Dept (io.github.ihongs.serv.master.Dept)2