Rewrote the csv output to use a library, instead of doing it manually.
Refs: #8187
Showing
1 changed file
with
15 additions
and
31 deletions
... | @@ -81,6 +81,7 @@ import org.ofbiz.service.ServiceContainer; | ... | @@ -81,6 +81,7 @@ import org.ofbiz.service.ServiceContainer; |
81 | 81 | ||
82 | import org.apache.commons.csv.CSVFormat; | 82 | import org.apache.commons.csv.CSVFormat; |
83 | import org.apache.commons.csv.CSVRecord; | 83 | import org.apache.commons.csv.CSVRecord; |
84 | import org.apache.commons.csv.CSVPrinter; | ||
84 | import org.apache.commons.fileupload.MultipartStream; | 85 | import org.apache.commons.fileupload.MultipartStream; |
85 | import org.apache.commons.fileupload.ParameterParser; | 86 | import org.apache.commons.fileupload.ParameterParser; |
86 | import org.apache.http.HeaderElement; | 87 | import org.apache.http.HeaderElement; |
... | @@ -520,45 +521,28 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -520,45 +521,28 @@ public class DirectControlServlet extends HttpServlet { |
520 | response.setContentType("text/csv"); | 521 | response.setContentType("text/csv"); |
521 | 522 | ||
522 | // Find the first list | 523 | // Find the first list |
523 | List<Map> resList = null; | 524 | List<Map<String, Object>> resList = null; |
524 | for (Object o : result.values()) { | 525 | for (Object o : result.values()) { |
525 | if (o instanceof List) { | 526 | if (o instanceof List) { |
526 | resList = (List<Map>) o; | 527 | resList = UtilGenerics.cast(o); |
527 | break; | 528 | break; |
528 | } | 529 | } |
529 | } | 530 | } |
530 | 531 | ||
531 | if (resList != null) { | 532 | if (UtilValidate.isNotEmpty(resList)) { |
532 | PrintWriter writer = response.getWriter(); | 533 | PrintWriter writer = response.getWriter(); |
533 | List<Map> data = null; | 534 | Set<String> headerNameSet = resList.get(0).keySet(); |
534 | if (resList.get(1) instanceof List) { | 535 | CSVFormat csvFormat = CSVFormat.EXCEL.withHeader(headerNameSet.toArray(new String[headerNameSet.size()])); |
535 | data = (List<Map>)(resList.get(1)); | 536 | CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat); |
536 | } else { | 537 | for (Map<String, Object> csvLine: resList) { |
537 | data = resList; | 538 | List<Object> csvData = new ArrayList<Object>(headerNameSet.size()); |
538 | } | 539 | for (String headerName: headerNameSet) { |
539 | 540 | csvData.add(csvLine.get(headerName)); | |
540 | if (data.size() > 0) { | ||
541 | Set keys = ((Map) data.get(0)).keySet(); | ||
542 | Iterator hi = keys.iterator(); | ||
543 | StringBuffer csvBuf = new StringBuffer(); | ||
544 | while(hi.hasNext()) { | ||
545 | csvBuf.append(hi.next()); | ||
546 | if (hi.hasNext()) csvBuf.append(","); | ||
547 | } | ||
548 | writer.println(csvBuf); | ||
549 | |||
550 | for (Map row : data) { | ||
551 | Iterator i = keys.iterator(); | ||
552 | csvBuf = new StringBuffer(); | ||
553 | while(i.hasNext()) { | ||
554 | Object val = row.get(i.next()); | ||
555 | if (!"null".equals("" + val)) csvBuf.append(val); | ||
556 | if (i.hasNext()) csvBuf.append(","); | ||
557 | } | ||
558 | writer.println(csvBuf); | ||
559 | } | 541 | } |
542 | csvPrinter.printRecord(csvData.toArray(new Object[csvData.size()])); | ||
560 | } | 543 | } |
561 | 544 | csvPrinter.flush(); | |
545 | csvPrinter.close(); | ||
562 | writer.flush(); | 546 | writer.flush(); |
563 | writer.close(); | 547 | writer.close(); |
564 | } | 548 | } | ... | ... |
-
Please register or sign in to post a comment