Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Genesys PGR
validator.genesys-pgr.org
Commits
8657fe96
Commit
8657fe96
authored
Apr 17, 2018
by
Matija Obreza
Browse files
Return validation results as text/csv on request :-)
parent
5710ca31
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/genesys/taxonomy/checker/web/controller/ValidatorController.java
View file @
8657fe96
...
...
@@ -16,10 +16,13 @@
package
org.genesys.taxonomy.checker.web.controller
;
import
java.io.IOException
;
import
java.io.OutputStreamWriter
;
import
java.io.StringReader
;
import
java.text.ParseException
;
import
java.util.List
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.commons.lang3.StringUtils
;
import
org.genesys.taxonomy.checker.TaxonomyException
;
import
org.genesys.taxonomy.checker.web.service.ProcessService
;
...
...
@@ -33,6 +36,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
com.opencsv.CSVWriter
;
/**
* The controller
*
...
...
@@ -97,6 +102,51 @@ public class ValidatorController {
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
)
{
String
[]
styles
=
new
String
[
strings
.
length
];
for
(
int
i
=
0
;
i
<
strings
.
length
;
i
++)
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment