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