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