115554f0 by Ean Schuessler

Add CSV writer

1 parent de2aadd0
......@@ -33,6 +33,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GroovyUtil;
......@@ -290,6 +291,52 @@ public class DirectControlServlet extends HttpServlet {
writer.close();
}
if ("CSV".equals(outputHandler)) {
response.setContentType("text/csv");
// Find the first list
List resList = null;
for (Object o : result.values()) {
if (o instanceof List) {
resList = (List) o;
break;
}
}
if (resList != null) {
PrintWriter writer = response.getWriter();
List<Map> data = null;
if (resList.get(1) instanceof List) {
data = (List<Map>)(resList.get(1));
} else {
data = resList;
}
if (data.size() > 0) {
Iterator hi = ((Map) data.get(0)).keySet().iterator();
StringBuffer csvBuf = new StringBuffer();
while(hi.hasNext()) {
csvBuf.append(hi.next());
if (hi.hasNext()) csvBuf.append(",");
}
writer.println(csvBuf);
for (Map row : data) {
Iterator i = row.values().iterator();
csvBuf = new StringBuffer();
while(i.hasNext()) {
csvBuf.append(i.next());
if (i.hasNext()) csvBuf.append(",");
}
writer.println(csvBuf);
}
}
writer.flush();
writer.close();
}
}
if ("PDF".equals(outputHandler)) {
LibreOfficeRenderer.service(request, response, result);
}
......