Commit 8657fe96 authored by Matija Obreza's avatar Matija Obreza
Browse files

Return validation results as text/csv on request :-)

parent 5710ca31
...@@ -16,10 +16,13 @@ ...@@ -16,10 +16,13 @@
package org.genesys.taxonomy.checker.web.controller; package org.genesys.taxonomy.checker.web.controller;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader; import java.io.StringReader;
import java.text.ParseException; import java.text.ParseException;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.genesys.taxonomy.checker.TaxonomyException; import org.genesys.taxonomy.checker.TaxonomyException;
import org.genesys.taxonomy.checker.web.service.ProcessService; import org.genesys.taxonomy.checker.web.service.ProcessService;
...@@ -33,6 +36,8 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -33,6 +36,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import com.opencsv.CSVWriter;
/** /**
* The controller * The controller
* *
...@@ -97,6 +102,51 @@ public class ValidatorController { ...@@ -97,6 +102,51 @@ public class ValidatorController {
return "result"; return "result";
} }
/**
* Process string.
*
* @param separator the separator
* @param quoteChar the quote char
* @param escapeChar the escape char
* @param csvText the csv text
* @param toCurrentTaxa the to current taxa
* @param model the model
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
* @throws ParseException the parse exception
* @throws TaxonomyException the taxonomy exception
*/
@RequestMapping(value = "process", method = RequestMethod.POST, params = "csvText", produces = "text/csv")
public void processStringToCSV(@RequestParam(name = "separator", required = false, defaultValue = ",") final Character separator,
@RequestParam(name = "separatorOther", required = false, defaultValue = "") final Character separatorOther,
@RequestParam(name = "quoteChar", required = false, defaultValue = "\"") final Character quoteChar,
@RequestParam(name = "escapeChar", required = false, defaultValue = "\0") final Character escapeChar,
@RequestParam(name = "csvText", required = true) final String csvText,
@RequestParam(name = "toCurrentTaxa", required = false) final Boolean toCurrentTaxa,
@RequestParam(name = "validateType", defaultValue = "") final String validateType,
@RequestParam(name = "decimalMark", defaultValue = ".") final Character decimalMark,
final HttpServletResponse response) throws IOException, ParseException, TaxonomyException {
LOG.trace("Processing uploaded CSV string:\n{}", csvText);
List<String[]> rows = processService.process(new StringReader(StringUtils.defaultString(csvText, "")), (separator == 'O' ? separatorOther : separator), quoteChar, escapeChar, validateType,
toCurrentTaxa, decimalMark);
response.setContentType("text/csv;charset=UTF-16LE");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"validator-results.csv\""));
response.flushBuffer();
// Write CSV to the stream.
final OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream(), "UTF-16LE");
try (CSVWriter csvWriter=new CSVWriter(osw, (separator == 'O' ? separatorOther : separator), quoteChar, escapeChar, "\n")) {
csvWriter.writeAll(rows);
csvWriter.flush();
} finally {
response.flushBuffer();
}
}
private String[] getStyles(String[] strings) { private String[] getStyles(String[] strings) {
String[] styles = new String[strings.length]; String[] styles = new String[strings.length];
for (int i = 0; i < strings.length; i++) { for (int i = 0; i < strings.length; i++) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment