Search in sources :

Example 1 with ActionRunner

use of app.hongs.action.ActionRunner in project HongsCORE by ihongs.

the class IsFork method verify.

@Override
public Object verify(Object value) throws Wrongs, HongsException {
    if (value == null || "".equals(value)) {
        // 允许为空
        return null;
    }
    String vl = Synt.declare(params.get("verify-type"), "");
    if ("any".equals(vl)) {
        return value;
    } else if ("number".equals(vl)) {
        IsNumber rl;
        rl = new IsNumber();
        rl.setHelper(helper);
        rl.setParams(params);
        rl.setValues(values);
        rl.setCleans(cleans);
        return rl.verify(value);
    } else if ("string".equals(vl)) {
        IsString rl;
        rl = new IsString();
        rl.setHelper(helper);
        rl.setParams(params);
        rl.setValues(values);
        rl.setCleans(cleans);
        return rl.verify(value);
    }
    String at = Synt.declare(params.get("data-at"), "");
    String vk = Synt.declare(params.get("data-vk"), "");
    String cl = Synt.declare(params.get("conf"), "");
    String fl = Synt.declare(params.get("form"), "");
    String ck = Synt.declare(params.get("__conf__"), "");
    String fk = Synt.declare(params.get("__name__"), "");
    String ap = null;
    String aq = null;
    if ("".equals(vk)) {
        vk = Cnst.ID_KEY;
    }
    if ("".equals(cl)) {
        cl = ck;
    }
    if ("".equals(fl)) {
        fl = fk.replaceFirst("_id$", "");
    }
    if ("".equals(at)) {
        at = cl + "/" + fl + "/search";
    } else {
        // 尝试解析附加参数
        int ps;
        ps = at.indexOf('?');
        if (ps > 0) {
            aq = at.substring(1 + ps).trim();
            at = at.substring(0, ps).trim();
        }
        ps = at.indexOf('!');
        if (ps > 0) {
            ap = at.substring(1 + ps).trim();
            at = at.substring(0, ps).trim();
        }
    }
    ActionHelper ah = ActionHelper.newInstance();
    ah.setContextData(Synt.mapOf(Cnst.ORIGIN_ATTR, Core.ACTION_NAME.get()));
    // 请求数据
    Map rd = new HashMap();
    Set rb = Synt.setOf(vk);
    Set id = Synt.asSet(value);
    rb.add(vk);
    rd.put(Cnst.RN_KEY, 0);
    rd.put(Cnst.RB_KEY, rb);
    rd.put(Cnst.ID_KEY, id);
    ah.setRequestData(rd);
    // 虚拟路径
    if (ap != null && ap.length() != 0) {
        ah.setAttribute(Cnst.ACTION_ATTR, ap + Cnst.ACT_EXT);
    }
    // 附加参数
    if (aq != null && aq.length() != 0) {
        if (aq.startsWith("{") && aq.endsWith("}")) {
            rd.putAll((Map) Data.toObject(aq));
        } else {
            rd.putAll(ActionHelper.parseQuery(aq));
        }
    }
    // 获取结果
    new ActionRunner(ah, at).doInvoke();
    Map sd = ah.getResponseData();
    List<Map> ls = (List) sd.get("list");
    // 对比结果
    Set vs = Synt.asSet(value);
    Set us = new HashSet();
    if (null != ls) {
        for (Map um : ls) {
            us.add(um.get(vk));
        }
    }
    if (vs.size() != us.size() || !vs.containsAll(us)) {
        throw new Wrong("fore.form.is.not.exists", fl);
    }
    return value;
}
Also used : ActionRunner(app.hongs.action.ActionRunner) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ActionHelper(app.hongs.action.ActionHelper) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 2 with ActionRunner

use of app.hongs.action.ActionRunner in project HongsCORE by ihongs.

the class DataAction method export.

@Action("stream")
@Preset(conf = "", form = "")
@CustomReplies
public void export(ActionHelper helper) throws HongsException, IOException {
    ActionRunner runner = (ActionRunner) helper.getAttribute(ActionRunner.class.getName());
    String ent = runner.getEntity();
    String mod = runner.getModule();
    Data sr = (Data) getEntity(helper);
    Map rd = helper.getRequestData();
    rd = getReqMap(helper, sr, "export", rd);
    int rn = Synt.declare(rd.get("rn"), 0);
    int pn = Synt.declare(rd.get("pn"), 0);
    Loop lp = sr.search(rd, pn, rn);
    /**
     * 逐行输出
     */
    HttpServletResponse rs = helper.getResponse();
    String csv = ent + "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    rs.setHeader("Content-Disposition", "attachment;filename=" + csv + ".csv");
    rs.setHeader("Content-Type", "application/octet-stream;charset=UTF-8");
    rs.setHeader("Transfer-Encoding", "chunked");
    Writer ot = rs.getWriter();
    Map fs = sr.getFields();
    Map es = new HashMap();
    if (lp.hasNext()) {
        Map<String, Object> ds = lp.next();
        StringBuilder sf = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        Map ts = FormSet.getInstance("default").getEnum("__types__");
        for (Map.Entry<String, Object> et : ds.entrySet()) {
            Object fv = et.getValue();
            String fn = et.getKey();
            Map mt = (Map) fs.get(fn);
            Map ex = null;
            String fx, ft;
            fx = (String) mt.get("__text__");
            ft = (String) mt.get("__type__");
            ft = (String) ts.get(ft);
            if ("enum".equals(ft)) {
                String xonf = (String) mt.get("conf");
                String xame = (String) mt.get("enum");
                if (null == xonf || "".equals(xonf))
                    xonf = mod;
                if (null == xame || "".equals(xame))
                    xame = fn;
                ex = FormSet.getInstance(xonf).getEnum(xame);
                es.put(fn, ex);
            }
            sf.append(",\"").append(escape(fx, null)).append("\"");
            sb.append(",\"").append(escape(fv, ex)).append("\"");
        }
        ot.write(sf.append("\r\n").substring(1));
        ot.write(sb.append("\r\n").substring(1));
        ot.flush();
    }
    while (lp.hasNext()) {
        Map<String, Object> ds = lp.next();
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Object> et : ds.entrySet()) {
            Object fv = et.getValue();
            String fn = et.getKey();
            Map ex = (Map) es.get(fn);
            sb.append(",\"").append(escape(fv, ex)).append("\"");
        }
        ot.write(sb.append("\r\n").substring(1));
    }
}
Also used : ActionRunner(app.hongs.action.ActionRunner) Loop(app.hongs.dh.lucene.LuceneRecord.Loop) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) Data(app.hongs.serv.matrix.Data) Date(java.util.Date) HashMap(java.util.HashMap) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat) Writer(java.io.Writer) Action(app.hongs.action.anno.Action) SearchAction(app.hongs.dh.search.SearchAction) Preset(app.hongs.action.anno.Preset) CustomReplies(app.hongs.action.anno.CustomReplies)

Example 3 with ActionRunner

use of app.hongs.action.ActionRunner in project HongsCORE by ihongs.

the class SearchAction method counts.

@Action("counts/search")
public void counts(ActionHelper helper) throws HongsException {
    ActionRunner runner = (ActionRunner) helper.getAttribute(ActionRunner.class.getName());
    String ent = runner.getEntity();
    String mod = runner.getModule();
    LuceneRecord sr = (LuceneRecord) getEntity(helper);
    SearchHelper sh = new SearchHelper(sr);
    Map rd = helper.getRequestData();
    rd = getReqMap(helper, sr, "counts", rd);
    Map sd = sh.counts(rd);
    sd = getRspMap(helper, sr, "counts", sd);
    // sr.close (  ); // 应用容器可自行关闭
    // 增加标题
    titled(rd, sd, mod, ent);
    helper.reply(sd);
}
Also used : ActionRunner(app.hongs.action.ActionRunner) LuceneRecord(app.hongs.dh.lucene.LuceneRecord) Map(java.util.Map) Action(app.hongs.action.anno.Action) LuceneAction(app.hongs.dh.lucene.LuceneAction)

Example 4 with ActionRunner

use of app.hongs.action.ActionRunner in project HongsCORE by ihongs.

the class SearchAction method statis.

@Action("statis/search")
public void statis(ActionHelper helper) throws HongsException {
    ActionRunner runner = (ActionRunner) helper.getAttribute(ActionRunner.class.getName());
    String ent = runner.getEntity();
    String mod = runner.getModule();
    LuceneRecord sr = (LuceneRecord) getEntity(helper);
    SearchHelper sh = new SearchHelper(sr);
    Map rd = helper.getRequestData();
    rd = getReqMap(helper, sr, "statis", rd);
    Map sd = sh.statis(rd);
    sd = getRspMap(helper, sr, "statis", sd);
    // sr.close (  ); // 应用容器可自行关闭
    // 增加标题
    titled(rd, sd, mod, ent);
    helper.reply(sd);
}
Also used : ActionRunner(app.hongs.action.ActionRunner) LuceneRecord(app.hongs.dh.lucene.LuceneRecord) Map(java.util.Map) Action(app.hongs.action.anno.Action) LuceneAction(app.hongs.dh.lucene.LuceneAction)

Example 5 with ActionRunner

use of app.hongs.action.ActionRunner in project HongsCORE by ihongs.

the class SearchTitler method addForks.

/**
 * 通过调用关联动作来补全名称
 * @param ls
 * @param fs
 * @param fn
 * @throws HongsException
 */
protected void addForks(List<Object[]> ls, Map fs, String fn) throws HongsException {
    String at = (String) fs.get("data-at");
    String vk = (String) fs.get("data-vk");
    String tk = (String) fs.get("data-tk");
    if (at == null || at.length() == 0 || vk == null || vk.length() == 0 || tk == null || tk.length() == 0) {
        CoreLogger.error("data-at, data-vk or data-tk can not be empty in field " + fn);
        return;
    }
    // 映射关系
    Map<String, List> lm = new HashMap();
    for (Object[] lx : ls) {
        String lv = (String) lx[0];
        List<Object[]> lw = lm.get(lv);
        if (lw == null) {
            lw = new ArrayList();
            lm.put(lv, lw);
        }
        lw.add(lx);
    }
    ActionHelper ah = ActionHelper.newInstance();
    ah.setContextData(Synt.mapOf(Cnst.ORIGIN_ATTR, Core.ACTION_NAME.get()));
    // 查询结构
    Map rd = new HashMap();
    Set rb = new HashSet();
    int ps;
    ps = at.indexOf("?");
    if (ps > -1) {
        String aq;
        aq = at.substring(1 + ps).trim();
        at = at.substring(0, ps).trim();
        if (!"".equals(aq)) {
            if (aq.startsWith("{") && aq.endsWith("}")) {
                rd = (Map) Data.toObject(aq);
            } else {
                rd = ActionHelper.parseQuery(aq);
            }
        }
    }
    ps = at.indexOf("!");
    if (ps > -1) {
        String ap;
        ap = at.substring(1 + ps).trim();
        at = at.substring(0, ps).trim();
        if (!"".equals(ap)) {
            ap = ap + Cnst.ACT_EXT;
            ah.setAttribute(Cnst.ACTION_ATTR, ap);
        }
    }
    rb.add(vk);
    rb.add(tk);
    rd.put(Cnst.RN_KEY, 0);
    rd.put(Cnst.RB_KEY, rb);
    rd.put(Cnst.ID_KEY, lm.keySet());
    // 获取结果
    ah.setRequestData(rd);
    new ActionRunner(ah, at).doInvoke();
    Map sd = ah.getResponseData();
    List<Map> lz = (List) sd.get("list");
    if (lz == null) {
        return;
    }
    // 整合数据
    for (Map ro : lz) {
        String lv = Synt.declare(ro.get(vk), "");
        String lt = Synt.declare(ro.get(tk), "");
        List<Object[]> lw = lm.get(lv);
        if (null != lw)
            for (Object[] lx : lw) {
                lx[1] = lt;
            }
    }
}
Also used : ActionRunner(app.hongs.action.ActionRunner) Set(java.util.Set) HashSet(java.util.HashSet) FormSet(app.hongs.action.FormSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionHelper(app.hongs.action.ActionHelper) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

ActionRunner (app.hongs.action.ActionRunner)10 Map (java.util.Map)6 ActionHelper (app.hongs.action.ActionHelper)4 Action (app.hongs.action.anno.Action)4 HashMap (java.util.HashMap)4 Set (java.util.Set)3 CoreLocale (app.hongs.CoreLocale)2 FormSet (app.hongs.action.FormSet)2 LuceneAction (app.hongs.dh.lucene.LuceneAction)2 LuceneRecord (app.hongs.dh.lucene.LuceneRecord)2 SearchAction (app.hongs.dh.search.SearchAction)2 Data (app.hongs.serv.matrix.Data)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Core (app.hongs.Core)1 HongsError (app.hongs.HongsError)1 HongsException (app.hongs.HongsException)1 HongsExpedient (app.hongs.HongsExpedient)1 SelectHelper (app.hongs.action.SelectHelper)1 SpreadHelper (app.hongs.action.SpreadHelper)1