4be4d1f9 by Ean Schuessler

Merge branch 'BF-8187' into 'master'

#8187: Rewrote the csv output to use a library, instead of doing it manually.

See merge request !2
2 parents 133fa70c 0c5c7fe4
......@@ -81,6 +81,7 @@ import org.ofbiz.service.ServiceContainer;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.fileupload.MultipartStream;
import org.apache.commons.fileupload.ParameterParser;
import org.apache.http.HeaderElement;
......@@ -520,45 +521,28 @@ public class DirectControlServlet extends HttpServlet {
response.setContentType("text/csv");
// Find the first list
List<Map> resList = null;
List<Map<String, Object>> resList = null;
for (Object o : result.values()) {
if (o instanceof List) {
resList = (List<Map>) o;
resList = UtilGenerics.cast(o);
break;
}
}
if (resList != null) {
if (UtilValidate.isNotEmpty(resList)) {
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) {
Set keys = ((Map) data.get(0)).keySet();
Iterator hi = keys.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 = keys.iterator();
csvBuf = new StringBuffer();
while(i.hasNext()) {
Object val = row.get(i.next());
if (!"null".equals("" + val)) csvBuf.append(val);
if (i.hasNext()) csvBuf.append(",");
}
writer.println(csvBuf);
}
}
Set<String> headerNameSet = resList.get(0).keySet();
CSVFormat csvFormat = CSVFormat.EXCEL.withHeader(headerNameSet.toArray(new String[headerNameSet.size()]));
CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat);
for (Map<String, Object> csvLine: resList) {
List<Object> csvData = new ArrayList<Object>(headerNameSet.size());
for (String headerName: headerNameSet) {
csvData.add(csvLine.get(headerName));
}
csvPrinter.printRecord(csvData.toArray(new Object[csvData.size()]));
}
csvPrinter.flush();
csvPrinter.close();
writer.flush();
writer.close();
}
......