Search in sources :

Example 1 with CustomReplies

use of app.hongs.action.anno.CustomReplies 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)

Aggregations

ActionRunner (app.hongs.action.ActionRunner)1 Action (app.hongs.action.anno.Action)1 CustomReplies (app.hongs.action.anno.CustomReplies)1 Preset (app.hongs.action.anno.Preset)1 Loop (app.hongs.dh.lucene.LuceneRecord.Loop)1 SearchAction (app.hongs.dh.search.SearchAction)1 Data (app.hongs.serv.matrix.Data)1 Writer (java.io.Writer)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1