Search in sources :

Example 36 with HongsException

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

the class DBFields method imports.

@Override
protected final void imports() throws HongsException {
    fields = new LinkedHashMap();
    try (Loop rs = db.query("SELECT * FROM `" + tn + "`", 0, 1)) {
        ResultSetMetaData md = rs.getMetaData();
        for (int i = 1; i <= md.getColumnCount(); i++) {
            Map field = new HashMap();
            field.put("type", md.getColumnType(i));
            field.put("size", md.getPrecision(i));
            field.put("scale", md.getScale(i));
            field.put("unsigned", md.isSigned(i) != true);
            field.put("required", md.isNullable(i) != 1);
            // 用处不大的的属性:
            /*
        field.put("autoIncrement",  md.isAutoIncrement(i));
        field.put("caseSensitive",  md.isCaseSensitive(i));
        field.put("currency",       md.isCurrency(i));
        field.put("readOnly",       md.isReadOnly(i));
        field.put("writable",       md.isWritable(i));
        field.put("searchable",     md.isSearchable(i));
        field.put("tableName",      md.getTableName(i));
        field.put("schemaName",     md.getSchemaName(i));
        field.put("catalogName",    md.getCatalogName(i));
        field.put("label",          md.getColumnLable(i));
        field.put("typeName",       md.getColumnTypeName(i));
        field.put("className",      md.getColumnClassName(i));
        field.put("displaySize",    md.getColumnDisplaySize(i));
        */
            this.fields.put(md.getColumnName(i), field);
        }
    } catch (SQLException ex) {
        throw new HongsException(1068, ex);
    }
    // 由于 ResultSetMetaData 缺少默认值, 通过 DatabaseMetaData 作补充检测
    try (ResultSet rs = db.open().getMetaData().getColumns(null, "%", tn, "%")) {
        while (rs.next()) {
            String fn = rs.getString("COLUMN_NAME");
            int dt = rs.getInt("DATA_TYPE");
            Map fd = fields.get(fn);
            if (null != fd)
                continue;
            fd.put("required", (null == rs.getString("COLUMN_DEF")) && !"YES".equals(rs.getString("IS_NULLABLE")) && !"YES".equals(rs.getString("IS_AUTOINCREMENT")) && !"YES".equals(rs.getString("IS_GENERATEDCOLUMN")) && dt != Types.TIMESTAMP && dt != Types.TIMESTAMP_WITH_TIMEZONE);
        }
    } catch (SQLException ex) {
        throw new HongsException(1068, ex);
    }
}
Also used : Loop(io.github.ihongs.db.link.Loop) ResultSetMetaData(java.sql.ResultSetMetaData) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) HongsException(io.github.ihongs.HongsException) ResultSet(java.sql.ResultSet) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 37 with HongsException

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

the class SelectHelper method injectFork.

public void injectFork(List<Map> list, byte ad) {
    if (forks.isEmpty())
        return;
    ActionHelper ah = ActionHelper.newInstance();
    MergeMore mm = new MergeMore(list);
    Map cd = new HashMap();
    Map rd = new HashMap();
    Set ab = new HashSet();
    Set rb = new HashSet();
    // 子级 rb
    Map<String, Set> sb = new HashMap();
    // 子级 ob
    Map<String, Set> ob = new HashMap();
    ah.setContextData(cd);
    ah.setRequestData(rd);
    // 传递 ab 参数
    if (TEXT == (TEXT & ad)) {
        ab.add("_text");
    }
    if (TIME == (TIME & ad)) {
        ab.add("_time");
    }
    if (LINK == (LINK & ad)) {
        ab.add("_link");
    }
    if (FORK == (FORK & ad)) {
        ab.add("_fork");
    }
    // 预先提取所有下级
    Set ts = new HashSet();
    for (Map.Entry et : forks.entrySet()) {
        Map mt = (Map) et.getValue();
        String fn = (String) et.getKey();
        // 关联外键
        String fk = (String) mt.get("data-fk");
        // 数据放入此下
        String ak = (String) mt.get("data-ak");
        if (ak == null || ak.isEmpty()) {
            if (fk == null || fk.isEmpty()) {
                if (fn.endsWith("_id")) {
                    int ln = fn.length() - 3;
                    ak = fn.substring(0, ln);
                } else {
                    ak = fn + "_fork";
                }
            } else {
                ak = fn;
            }
        }
        ts.add(ak);
    }
    // 子级 rb 参数
    if (_cols != null) {
        String t, f;
        for (Object o : _cols) {
            f = Synt.asString(o);
            int p = f.indexOf(".");
            if (p != -1) {
                t = f.substring(0, p);
                f = f.substring(1 + p);
                if (!ts.contains(t))
                    continue;
                Set nb = sb.get(t);
                if (nb == null) {
                    nb = new LinkedHashSet();
                    sb.put(t, nb);
                }
                nb.add(f);
            }
        }
    }
    // 子级 ob 参数
    if (_ords != null) {
        String t, f;
        for (Object o : _ords) {
            f = Synt.asString(o);
            int p = f.indexOf(".");
            if (p != -1) {
                t = f.substring(0, p);
                f = f.substring(1 + p);
                if (!ts.contains(t))
                    continue;
                Set nb = ob.get(t);
                if (nb == null) {
                    nb = new LinkedHashSet();
                    ob.put(t, nb);
                }
                nb.add(f);
            }
        }
    }
    for (Map.Entry et : forks.entrySet()) {
        Map mt = (Map) et.getValue();
        String fn = (String) et.getKey();
        // 关联外键
        String fk = (String) mt.get("data-fk");
        // 建立映射, 清除空值可避免不必要的查询
        Map<Object, List> ms = mm.mapped(fk != null ? fk : fn);
        ms.remove("");
        ms.remove(null);
        if (ms.isEmpty()) {
            continue;
        }
        // 关联动作路径
        String at = (String) mt.get("data-at");
        // 数据放入此下
        String ak = (String) mt.get("data-ak");
        // 关联字段
        String vk = (String) mt.get("data-vk");
        // 名称字段
        String tk = (String) mt.get("data-tk");
        if (at == null || at.isEmpty()) {
            String c = (String) mt.get("conf");
            String f = (String) mt.get("form");
            at = c + "/" + f + "/search";
        }
        if (ak == null || ak.isEmpty()) {
            if (fk == null || fk.isEmpty()) {
                if (fn.endsWith("_id")) {
                    int ln = fn.length() - 3;
                    ak = fn.substring(0, ln);
                } else {
                    ak = fn + "_fork";
                }
            } else {
                ak = fn;
            }
        }
        if (vk == null || vk.isEmpty()) {
            vk = Cnst.ID_KEY;
        }
        if (tk == null || tk.isEmpty()) {
            tk = "name";
        }
        rd.clear();
        rb.clear();
        rb.add(vk);
        rb.add(tk);
        // 附加参数
        int ps = at.indexOf('?');
        if (ps > -1) {
            String aq;
            aq = at.substring(1 + ps).trim();
            at = at.substring(0, ps).trim();
            if (aq.startsWith("{") && aq.endsWith("}")) {
                rd.putAll((Map) Dawn.toObject(aq));
            } else {
                rd.putAll(ActionHelper.parseQuery(aq));
            }
        }
        // 关联参数
        rd.put(vk, ms.keySet());
        rd.put(Cnst.RN_KEY, 0);
        Set xb;
        xb = Synt.toTerms(rd.get(Cnst.AB_KEY));
        if (xb == null) {
            xb = ab;
        }
        rd.put(Cnst.AB_KEY, xb);
        xb = Synt.toTerms(rd.get(Cnst.RB_KEY));
        if (xb == null) {
            xb = rb;
        }
        rd.put(Cnst.RB_KEY, xb);
        // 请求参数
        Map rq = Synt.toMap(mt.get("data-rd"));
        if (rq != null && !rq.isEmpty()) {
            rd.putAll(rq);
        }
        // 查询字段
        // 若内部有许可的且外部有指定的
        // 则取二者的交集作为查询的字段
        Set rp = Synt.toSet(mt.get("data-rb"));
        if (rp != null && !rp.isEmpty()) {
            Set nb = sb.get(ak);
            if (nb != null) {
                nb.retainAll(rp);
                if (!nb.isEmpty())
                    rd.put(Cnst.RB_KEY, nb);
            }
        }
        // 排序字段
        // 若内部有许可的且外部有指定的
        // 则取二者的交集作为排序的字段
        Set op = Synt.toSet(mt.get("data-ob"));
        if (op != null && !op.isEmpty()) {
            Set nb = ob.get(ak);
            if (nb != null) {
                nb.retainAll(op);
                if (!nb.isEmpty())
                    rd.put(Cnst.OB_KEY, nb);
            }
        }
        // 没有 ab 就没必要调用注解过滤
        try {
            ActionRunner ar = ActionRunner.newInstance(ah, at);
            if (xb.isEmpty()) {
                ar.doInvoke();
            } else {
                ar.doAction();
            }
        } catch (HongsException e) {
            throw e.toExemption();
        }
        // 整合数据
        Map sd = ah.getResponseData();
        List<Map> ls = (List) sd.get("list");
        if (ls == null) {
            continue;
        }
        if (Synt.declare(mt.get("__repeated__"), false)) {
            // 预置数据
            for (Map.Entry<Object, List> lr : ms.entrySet()) {
                List<Map> lst = lr.getValue();
                for (Map row : lst) {
                    row.put(ak, new ArrayList());
                }
            }
            mm.append(ls, ms, vk, ak);
        } else {
            // 预置数据
            for (Map.Entry<Object, List> lr : ms.entrySet()) {
                List<Map> lst = lr.getValue();
                for (Map row : lst) {
                    row.put(ak, new HashMap());
                }
            }
            mm.extend(ls, ms, vk, ak);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) MergeMore(io.github.ihongs.dh.MergeMore) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) HongsException(io.github.ihongs.HongsException) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 38 with HongsException

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

the class SelectHelper method inject.

/**
 * 深度补充
 * @param values 返回数据
 * @param action 填充规则, 使用常量 ENUM,TEXT 等, 可用或运算传多个值
 */
public void inject(Map values, byte action) {
    List<Map> list = new ArrayList(1);
    if (values.containsKey("info")) {
        list.add((Map) values.get("info"));
    }
    if (values.containsKey("list")) {
        list.addAll((List) values.get("list"));
    }
    Map xnum = null;
    if (ENFO == (ENFO & action)) {
        xnum = (Map) values.get("enfo");
        if (xnum == null) {
            xnum = new LinkedHashMap();
            values.put("enfo", xnum);
        }
    }
    // 数据映射整理
    Map<String, List> maps = new HashMap();
    for (String fn : forms.keySet()) {
        maps.put(fn, new LinkedList());
    }
    for (Map info : list) {
        for (String fn : forms.keySet()) {
            Object fv = info.get(fn);
            if (fv != null && !"".equals(fv)) {
                // 需规避误把空值存成了空串
                if (fv instanceof Object[]) {
                    maps.get(fn).addAll(Arrays.asList((Object[]) fv));
                } else if (fv instanceof Collection) {
                    maps.get(fn).addAll((Collection) fv);
                } else {
                    maps.get(fn).add(fv);
                }
            }
        }
    }
    // 向下递归补充
    for (Map.Entry<String, Map> et : forms.entrySet()) {
        String fn = et.getKey();
        Map fields = et.getValue();
        Map valuez = new HashMap();
        Map anum = new HashMap();
        valuez.put("enfo", anum);
        valuez.put("list", maps.get(fn));
        try {
            new SelectHelper().addItemsByForm(fields).select(valuez, action);
        } catch (HongsException e) {
            throw e.toExemption();
        }
        // 将枚举向上并
        if (xnum != null && !anum.isEmpty())
            for (Object ot2 : anum.entrySet()) {
                Map.Entry et2 = (Map.Entry) ot2;
                Object fn2 = et2.getKey();
                Object fv2 = et2.getValue();
                Dict.put(xnum, fv2, fn, fn2);
            }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) HongsException(io.github.ihongs.HongsException) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 39 with HongsException

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

the class PermitInvoker method invoke.

@Override
public void invoke(ActionHelper helper, ActionRunner chains, Annotation anno) throws HongsException {
    Permit ann = (Permit) anno;
    String conf = ann.conf();
    String[] role = ann.role();
    // 识别路径
    if (conf.length() == 0) {
        // String form;
        // form = chains.getEntity();
        conf = chains.getModule();
    // 照顾 Module Action 的配置规则. 2018/7/7 改为完全由外部预判
    // if (NaviMap.hasConfFile(conf+"/"+form)) {
    // conf = conf+"/"+form ;
    // }
    }
    NaviMap map = NaviMap.getInstance(conf);
    boolean was = map.getAuthSet() != null;
    boolean has = false;
    if (!was) {
        throw new HongsException(1101);
    }
    if (role == null || role.length < 1) {
        has = map.chkAuth(chains.getAction());
    } else
        for (String rale : role) {
            if (rale.startsWith("@")) {
                if (map.chkAuth(rale.substring(1))) {
                    has = true;
                    break;
                }
            } else {
                if (map.chkRole(rale)) {
                    has = true;
                    break;
                }
            }
        }
    if (!has) {
        throw new HongsException(1103);
    }
    chains.doAction();
}
Also used : HongsException(io.github.ihongs.HongsException) NaviMap(io.github.ihongs.action.NaviMap)

Example 40 with HongsException

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

the class ActsAction method service.

/**
 * 服务方法
 * Servlet Mapping: *.act<br/>
 * 注意: 不支持请求URI的路径中含有"."(句点), 且必须区分大小写;
 * 其目的是为了防止产生多种形式的请求路径, 影响动作过滤, 产生安全隐患.
 *
 * @param req
 * @param rsp
 * @throws javax.servlet.ServletException
 */
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException {
    String act = ActionDriver.getRecentPath(req);
    Core core = ActionDriver.getActualCore(req);
    ActionHelper helper = core.got(ActionHelper.class);
    Core.THREAD_CORE.set(core);
    if (act == null || act.length() == 0) {
        helper.fault(new HongsException(404, "Action URI can not be empty."));
        return;
    }
    // 去掉根和扩展名
    int pos = act.lastIndexOf('.');
    if (pos != -1) {
        act = act.substring(1, pos);
    } else {
        act = act.substring(1);
    }
    // 获取并执行动作
    try {
        new ActionRunner(helper, act).doAction();
    } catch (HongsException e) {
        helper.fault(e);
    } catch (HongsExemption e) {
        helper.fault(e);
    } catch (RuntimeException e) {
        helper.fault(new HongsException(500, e));
    }
}
Also used : ActionRunner(io.github.ihongs.action.ActionRunner) HongsException(io.github.ihongs.HongsException) ActionHelper(io.github.ihongs.action.ActionHelper) HongsExemption(io.github.ihongs.HongsExemption) Core(io.github.ihongs.Core)

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