Skip to content

Commit

Permalink
better csv handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mdavis95 committed Jul 21, 2017
1 parent c6cfb61 commit d181911
Showing 1 changed file with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ else if ("1".equals(sortDir) || "ASC".equalsIgnoreCase(sortDir)) {
output.write(header.getBytes());
output.flush();


int count = 0;

while (qr.getResultsList().size() > 0) {
Expand Down Expand Up @@ -397,8 +396,7 @@ else if ("1".equals(sortDir) || "ASC".equalsIgnoreCase(sortDir)) {

}

private String buildHeaderForCSV(@QueryParam(LumongoConstants.FIELDS) List<String> fields)
throws Exception {
private String buildHeaderForCSV(@QueryParam(LumongoConstants.FIELDS) List<String> fields) throws Exception {

StringBuilder headerBuilder = new StringBuilder();
fields.stream().filter(field -> !field.startsWith("-")).forEach(field -> headerBuilder.append(field).append(","));
Expand Down Expand Up @@ -617,20 +615,38 @@ private void appendDocument(List<String> fields, StringBuilder responseBuilder,
if (obj != null) {
if (obj instanceof List) {
List value = (List) obj;
String output = "\"";
for (Object o : value) {
if (o instanceof String) {
String item = (String) o;
if (item.contains(",") || value.contains("\"") || value.contains("\n")) {
output += item.replace("\"", "\"\"") + ";";
StringBuilder output = new StringBuilder();

if (!value.isEmpty()) {
responseBuilder.append("\"");

boolean first = true;
for (Object o : value) {
if (o instanceof String) {

String item = (String) o;

if (first) {
first = false;
}
else {
responseBuilder.append(";");
}

if (item.contains("\"")) {
item = item.replace("\"", "\"\"");
}

responseBuilder.append((item));

}
else {
output += item + ";";
output.append(o.toString());
}
}
responseBuilder.append("\"");
}
output += "\"";
responseBuilder.append(output);

}
else if (obj instanceof Date) {
Date value = (Date) obj;
Expand Down

0 comments on commit d181911

Please sign in to comment.