use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class FetchCase method getSQLDeep.
/**
* 获取SQL组合
* @return SQL组合
*/
private void getSQLDeep(StringBuilder t, StringBuilder f, StringBuilder w, StringBuilder g, StringBuilder h, StringBuilder o, String pn, String qn) {
if (this.tableName == null || this.tableName.length() < 1) {
throw new Error(new HongsException(1162, "tableName can not be empty"));
}
// 有关联表
boolean hasJoins = pn != null || !joinSet.isEmpty();
// 补全语句
boolean fixField = getOption("CLEVER_MODE", false);
// 表名
String tn;
StringBuilder b = new StringBuilder();
b.append("`").append(this.tableName).append("`");
// 别名
if (this.name != null && !this.name.equals("") && !this.name.equals(this.tableName)) {
b.append(" AS `").append(this.name).append("`");
tn = this.name;
} else {
tn = this.tableName;
}
// 关联
if (pn != null) {
switch(this.joinType) {
case FetchCase.LEFT:
b.insert(0, " LEFT JOIN ");
break;
case FetchCase.RIGHT:
b.insert(0, " RIGHT JOIN ");
break;
case FetchCase.FULL:
b.insert(0, " FULL JOIN ");
break;
case FetchCase.INNER:
b.insert(0, " INNER JOIN ");
break;
case FetchCase.CROSS:
b.insert(0, " CROSS JOIN ");
break;
default:
return;
}
if (this.joinExpr != null && this.joinExpr.length() != 0) {
CharSequence s = this.joinExpr;
if (fixField) {
s = fixSQLField(s, tn, pn);
}
b.append(" ON ").append(s);
}
/**
* 别名层级
* 第一层是不许前缀的
* 第二层等同于与表名
* 二层之后携带所有上级表名(不包含第一层表名)
* 下面代码逻辑需从下往上看
*/
if (!"".equals(qn)) {
qn = qn + "." + tn;
} else {
qn = tn;
}
} else {
qn = "";
}
t.append(b);
// 字段
if (this.fields.length() != 0) {
CharSequence s = this.fields.toString().trim();
// 为关联表的查询列添加层级名
if (fixField && null != joinName) {
if (0 < joinName.length()) {
s = fixSQLAlias(s, joinName);
} else if (0 < qn.length()) {
s = fixSQLAlias(s, qn);
}
}
if (fixField) {
if (hasJoins) {
s = fixSQLField(s, tn, pn);
} else {
s = fixSQLPoint(s, "", pn);
}
}
f.append(" ").append(s);
}
// 条件
if (this.wheres.length() != 0) {
CharSequence s = this.wheres.toString().trim();
if (fixField) {
if (hasJoins) {
s = fixSQLField(s, tn, pn);
} else {
s = fixSQLPoint(s, "", pn);
}
}
w.append(" ").append(s);
}
// 分组
if (this.groups.length() != 0) {
CharSequence s = this.groups.toString().trim();
if (fixField) {
if (hasJoins) {
s = fixSQLField(s, tn, pn);
} else {
s = fixSQLPoint(s, "", pn);
}
}
g.append(" ").append(s);
}
// 筛选
if (this.havins.length() != 0) {
CharSequence s = this.havins.toString().trim();
if (fixField) {
if (hasJoins) {
s = fixSQLField(s, tn, pn);
} else {
s = fixSQLPoint(s, "", pn);
}
}
h.append(" ").append(s);
}
// 排序
if (this.orders.length() != 0) {
CharSequence s = this.orders.toString().trim();
if (fixField) {
if (hasJoins) {
s = fixSQLField(s, tn, pn);
} else {
s = fixSQLPoint(s, "", pn);
}
}
o.append(" ").append(s);
}
// 下级
for (FetchCase caze : this.joinSet) {
if (caze.joinType != 0) {
caze.getSQLDeep(t, f, w, g, h, o, tn, qn);
}
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class AssocCase method allow.
private void allow(Table table, Table assoc, Map ac, String tn, String qn, Map al) {
/**
* 三个变量: 层级名(qn), 名前缀(ax), 表前缀(tx)
* 第一层时, 无需加名前缀, 无关联表前缀也不用加
* 第二层时, 需将表名作为名前缀, 下级需带层级名
*/
String tx, ax;
tx = "`" + tn + "`.";
if (null == qn) {
qn = "";
ax = "";
} else if ("".equals(qn)) {
qn = tn;
ax = qn + ".";
} else {
qn = qn + "." + tn;
ax = qn + ".";
}
try {
Map fs = assoc.getFields();
for (Object n : fs.keySet()) {
String k = (String) n;
String f = (String) n;
k = ax + /**/
k;
f = tx + "`" + f + "`";
al.put(k, f);
}
} catch (HongsException e) {
throw e.toExemption();
}
if (ac == null || ac.isEmpty()) {
return;
}
Iterator it = ac.entrySet().iterator();
while (it.hasNext()) {
Map.Entry et = (Map.Entry) it.next();
Map tc = (Map) et.getValue();
String jn = (String) tc.get("join");
// 不是 JOIN 的不理会
if (!"INNER".equals(jn) && !"LEFT".equals(jn) && !"RIGHT".equals(jn) && !"FULL".equals(jn)) {
continue;
}
tn = (String) et.getKey();
ac = (Map) tc.get("assocs");
// 获取真实的表名, 构建关联表实例
String rn;
rn = (String) tc.get("tableName");
if (rn == null || "".equals(rn)) {
rn = (String) tc.get("name");
}
try {
assoc = table.db.getTable(rn);
} catch (HongsException e) {
throw e.toExemption();
}
if (null == assoc) {
throw new HongsExemption(1026, "Can not get table '" + rn + "' in DB '" + table.db.name + "'");
}
allow(table, assoc, ac, tn, qn, al);
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Data method depletes.
/**
* 引用检查
* 检查被哪些资源关联引用
* @param rs 将删除的
* @throws HongsException
*/
protected void depletes(Set rs) throws HongsException {
Map<String, Object> aq = getCascades();
if (aq == null || aq.isEmpty()) {
return;
}
if (rs == null || rs.isEmpty()) {
return;
}
StringBuilder nb = new StringBuilder();
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> xt : aq.entrySet()) {
String at = xt.getKey();
Object av = xt.getValue();
if (at == null || at.isEmpty()) {
continue;
}
Set aa = Synt.toSet(av);
if (!aa.contains("DEPEND")) {
continue;
}
// 解析关联描述串, 格式: conf!form?fk
int p = at.indexOf("?");
String k = at.substring(1 + p);
String c = at.substring(0, p);
p = c.indexOf("!");
String f = c.substring(1 + p);
c = c.substring(0, p);
// 可能存在多个引用字段
Map ar = new HashMap();
Set or = new HashSet();
ar.put(Cnst.OR_KEY, or);
for (String fn : k.split(";")) {
or.add(Synt.mapOf(fn, rs));
}
// 查询依赖当前表的资源
Data inst = Data.getInstance(c, f);
int hits = inst.search(ar, 0, 1).hits();
if (hits > 0) {
String l = (String) inst.getParams().get("__text__");
nb.append(l).append(" (").append(hits).append("), ");
sb.append(f).append(" (").append(hits).append("), ");
}
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 2);
nb.setLength(nb.length() - 2);
// 抛出异常告知依赖情况
throw new HongsException(1097, "Being dependent on " + sb.toString()).setLocalizedContent("core.delete.depend.error").setLocalizedOptions(nb.toString()).setLocalizedContext("matrix");
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Form method put.
@Override
public int put(String id, Map rd) throws HongsException {
Map xd = table.fetchCase().filter("id = ?", id).select("state,name").getOne();
String stax = Synt.asString(xd.get("state"));
String namx = Synt.asString(xd.get("name"));
String stat = Synt.asString(rd.get("state"));
String name = Synt.asString(rd.get("name"));
List conf = parseConf(id, rd);
// 再操作可能会冲掉自定配置
if ("8".equals(stax)) {
throw new HongsException(400, "表单冻结, 禁止操作");
}
if ("0".equals(stax)) {
throw new HongsException(404, "表单缺失, 无法操作");
}
int n = superPut(id, rd);
if (n != 0) {
// 用旧数据补全
if (stat == null && name != null) {
stat = stax;
}
if (name == null && stat != null) {
name = namx;
}
// 备份配置数据
if (conf != null) {
storeConf(id, rd.get("conf"));
}
// 更新配置文件
if (name != null || stat != null) {
updateFormMenu(id, stat, name);
}
if (conf != null || stat != null) {
updateFormConf(id, stat, conf);
}
// 更新单元菜单
if (stat != null) {
updateUnitMenu(id);
}
}
return n;
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class Form method saveDocument.
private void saveDocument(File file, Document docm) throws HongsException {
File fold = file.getParentFile();
if (!fold.exists()) {
fold.mkdirs();
}
TransformerFactory tf = TransformerFactory.newInstance();
try {
Transformer tr = tf.newTransformer();
DOMSource ds = new DOMSource(docm);
StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
tr.setOutputProperty(OutputKeys.ENCODING, "utf-8");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
tr.transform(ds, sr);
} catch (TransformerConfigurationException e) {
throw new HongsException(e);
} catch (UnsupportedEncodingException e) {
throw new HongsException(e);
} catch (IllegalArgumentException e) {
throw new HongsException(e);
} catch (FileNotFoundException e) {
throw new HongsException(e);
} catch (TransformerException e) {
throw new HongsException(e);
}
}
Aggregations