Commit 3433c4a3 authored by Andrey Lugovskiy's avatar Andrey Lugovskiy Committed by Matija Obreza
Browse files

- added number format support

parent 87cbbf84
......@@ -81,12 +81,13 @@ public class ValidatorController {
@RequestParam(name = "escapeChar", required = false, defaultValue = "0") final Character escapeChar,
@RequestParam(name = "toCurrentTaxa", required = false) final Boolean toCurrentTaxa,
@RequestParam(name = "validateType", required = false, defaultValue = "") final String validateType,
@RequestParam(name = "decimalMark", defaultValue = ".") final Character decimalMark,
final ModelMap model) throws IOException, ParseException, TaxonomyException {
LOG.info("Processing uploaded file");
try (Reader reader = new InputStreamReader(file.getInputStream())) {
List<String[]> rows = processService.process(reader, separator, quoteChar, escapeChar, validateType, toCurrentTaxa);
List<String[]> rows = processService.process(reader, separator, quoteChar, escapeChar, validateType, toCurrentTaxa, decimalMark);
model.addAttribute("result", rows);
model.addAttribute("tableHeaders", rows.get(0));
model.addAttribute("newLineSymbol", "\n");
......@@ -117,11 +118,13 @@ public class ValidatorController {
@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 ModelMap model) throws IOException, ParseException, TaxonomyException {
LOG.info("Processing uploaded CSV string");
List<String[]> rows = processService.process(new StringReader(StringUtils.defaultString(csvText, "")), separator, quoteChar, escapeChar, validateType, toCurrentTaxa);
List<String[]> rows = processService.process(new StringReader(StringUtils.defaultString(csvText, "")), separator, quoteChar, escapeChar, validateType,
toCurrentTaxa, decimalMark);
model.addAttribute("result", rows);
model.addAttribute("tableHeaders", rows.get(0));
model.addAttribute("newLineSymbol", "\n");
......
......@@ -19,6 +19,8 @@ import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.genesys.taxonomy.checker.TaxonomyException;
......@@ -42,6 +44,27 @@ public interface ProcessService {
* @throws IOException Signals that an I/O exception has occurred.
* @throws TaxonomyException the taxonomy exception
*/
List<String[]> process(Reader reader, Character separator, Character quoteChar, Character escapeChar, String validateType, Boolean toCurrentTaxa) throws ParseException, IOException, TaxonomyException;
List<String[]> process(Reader reader, Character separator, Character quoteChar, Character escapeChar, String validateType, Boolean toCurrentTaxa, Character decimalMark)
throws ParseException, IOException, TaxonomyException;
/**
* Method for parsing numeric values(for example DECLATITUDE and DECLONGITUDE values)
* @param number string number for parsing
* @param decimalMark decimal mark
* @return parsed value
* @throws RuntimeException if parsing is wrong
*/
default String parseNumber(String number, Character decimalMark){
StringBuffer sBuffer = new StringBuffer();
Pattern p = Pattern.compile("\\d*[" + decimalMark + "]\\d*");
Matcher m = p.matcher(number);
while (m.find()) {
sBuffer.append(m.group());
}
if (sBuffer.toString().length() == 0) {
throw new RuntimeException("You set wrong 'Decimal mark': '" + decimalMark + "' or wrong numeric value in table: " + number);
}
return sBuffer.toString();
}
}
......@@ -10,6 +10,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
......@@ -40,12 +41,12 @@ public class CountryProcessServiceImpl implements ProcessService {
private CountryOfOriginService countryOfOriginService;
@Override
public List<String[]> process(Reader reader, Character separator, Character quoteChar, Character escapeChar, String validateType, Boolean toCurrentTaxa)
public List<String[]> process(Reader reader, Character separator, Character quoteChar, Character escapeChar, String validateType, Boolean toCurrentTaxa, Character decimalMark)
throws ParseException, IOException, TaxonomyException {
return execute(reader, separator, quoteChar, escapeChar);
return execute(reader, separator, quoteChar, escapeChar, decimalMark);
}
private List<String[]> execute(Reader readerr, char separatorChar, char quoteChar, char escapeChar) throws IOException {
private List<String[]> execute(Reader readerr, char separatorChar, char quoteChar, char escapeChar, Character decimalMark) throws IOException {
final List<String[]> lines = new LinkedList<>();
......@@ -119,7 +120,7 @@ public class CountryProcessServiceImpl implements ProcessService {
if (inputs.size() == 0)
break;
threadPool.submit(new Runnable() {
Future<?> future = threadPool.submit(new Runnable() {
@Override
public void run() {
for (String[] nextLine : inputs) {
......@@ -130,8 +131,8 @@ public class CountryProcessServiceImpl implements ProcessService {
// if ("101346".equals(nextLine[0])) {
// LOG.info("!!! Got what we're looking for");
// }
String declongitude = nextLine[sourceMapping.get(ApplicationUtils.HEADER_LONGITUDE)].trim();
String declatitude = nextLine[sourceMapping.get(ApplicationUtils.HEADER_LATITUDE)].trim();
String declongitude = parseNumber(nextLine[sourceMapping.get(ApplicationUtils.HEADER_LONGITUDE)], decimalMark);
String declatitude = parseNumber(nextLine[sourceMapping.get(ApplicationUtils.HEADER_LATITUDE)], decimalMark);
String origCty = nextLine[sourceMapping.get(ApplicationUtils.HEADER_ORIGCTY)].trim();
if (StringUtils.isNotBlank(declatitude) && StringUtils.isNotBlank(declongitude)) {
......@@ -163,6 +164,12 @@ public class CountryProcessServiceImpl implements ProcessService {
}
});
try {
future.get();
} catch (Exception e) {
throw new IOException(e);
}
} while (true);
LOG.info("Shutting down");
......
......@@ -10,6 +10,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
......@@ -41,15 +42,15 @@ public class LandOrSeaProcessServiceImpl implements ProcessService {
@Override
public List<String[]> process(Reader reader, Character separator, Character quoteChar, Character escapeChar, String validateType, Boolean toCurrentTaxa)
public List<String[]> process(Reader reader, Character separator, Character quoteChar, Character escapeChar, String validateType, Boolean toCurrentTaxa, Character decimalMark)
throws ParseException, IOException, TaxonomyException {
return execute(reader, separator, quoteChar, escapeChar);
return execute(reader, separator, quoteChar, escapeChar, decimalMark);
}
/* (non-Javadoc)
* @see org.genesys.geotools.cli.GeoTool#execute(java.io.InputStream, java.io.OutputStream, char, char, char)
*/
public List<String[]> execute(Reader readerr, char separatorChar, char quoteChar, char escapeChar) throws IOException {
public List<String[]> execute(Reader readerr, char separatorChar, char quoteChar, char escapeChar, char decimalMark) throws IOException {
final List<String[]> lines = new LinkedList<>();
......@@ -120,15 +121,15 @@ public class LandOrSeaProcessServiceImpl implements ProcessService {
if (inputs.size() == 0)
break;
threadPool.submit(new Runnable() {
Future<?> future = threadPool.submit(new Runnable() {
@Override
public void run() {
for (String[] nextLine : inputs) {
String[] outputLine = HeaderUtils.toOutputLine(nextLine, outputHeaders, outputMapping);
String declongitude = nextLine[sourceMapping.get(ApplicationUtils.HEADER_LONGITUDE)].trim();
String declatitude = nextLine[sourceMapping.get(ApplicationUtils.HEADER_LATITUDE)].trim();
String declongitude = parseNumber(nextLine[sourceMapping.get(ApplicationUtils.HEADER_LONGITUDE)], decimalMark);
String declatitude = parseNumber(nextLine[sourceMapping.get(ApplicationUtils.HEADER_LATITUDE)], decimalMark);
String result = null;
......@@ -153,6 +154,12 @@ public class LandOrSeaProcessServiceImpl implements ProcessService {
}
});
try {
future.get();
} catch (Exception e) {
throw new IOException(e);
}
} while (true);
LOG.info("Shutting down");
......@@ -168,7 +175,7 @@ public class LandOrSeaProcessServiceImpl implements ProcessService {
LOG.info("Shut down.");
stopWatch.stop();
long processingTimeSeconds = Math.max(1, stopWatch.getTime() / 1000);
long processingTimeSeconds = Math.max(1, stopWatch.getTime() / 1000);
LOG.info("FYI, " + lineCount + " entries have been processed in " + processingTimeSeconds + " = " + (lineCount / processingTimeSeconds) + " lines/second");
}
......
......@@ -35,9 +35,9 @@ public class ProcessServiceImpl implements ProcessService, InitializingBean {
private Map<String, ProcessService> serviceMap;
@Override
public List<String[]> process(Reader reader, Character separator, Character quoteChar, Character escapeChar, String validateType, Boolean toCurrentTaxa)
public List<String[]> process(Reader reader, Character separator, Character quoteChar, Character escapeChar, String validateType, Boolean toCurrentTaxa, Character decimalMark)
throws ParseException, IOException, TaxonomyException {
return serviceMap.get(validateType).process(reader, separator, quoteChar, escapeChar, validateType, toCurrentTaxa);
return serviceMap.get(validateType).process(reader, separator, quoteChar, escapeChar, validateType, toCurrentTaxa, decimalMark);
}
@Override
......
......@@ -118,13 +118,13 @@ public class TaxonomyProcessServiceImpl implements ProcessService {
* @see org.genesys.taxonomy.checker.web.service.ProcessService#process(Reader, Character, Character, Character, Boolean)
*/
@Override
public List<String[]> process(final Reader reader, final Character separator, final Character quoteChar, final Character escapeChar, String validateType, final Boolean toCurrentTaxa)
throws ParseException, IOException, TaxonomyException {
return run(reader, separator, quoteChar, escapeChar, toCurrentTaxa);
public List<String[]> process(final Reader reader, final Character separator, final Character quoteChar, final Character escapeChar,
String validateType, final Boolean toCurrentTaxa, Character decimalMark) throws ParseException, IOException, TaxonomyException {
return run(reader, separator, quoteChar, escapeChar, toCurrentTaxa, decimalMark);
}
private List<String[]> run(final Reader reader, final Character separator, final Character quoteChar, final Character escapeChar, final Boolean toCurrentTaxa)
throws UnsupportedEncodingException, FileNotFoundException, IOException {
private List<String[]> run(final Reader reader, final Character separator, final Character quoteChar, final Character escapeChar, final Boolean toCurrentTaxa,
final Character decimalMark) throws UnsupportedEncodingException, FileNotFoundException, IOException {
final List<String[]> lines = new LinkedList<>();
......
......@@ -23,6 +23,7 @@ label.non.current.taxa=Flag non-current taxa
label.yes=Yes
label.no=No
label.decimal.mark=Decimal mark
http-error.401=Unauthorized
http-error.200= It's ok
......
......@@ -90,6 +90,12 @@
<input type="radio" name="toCurrentTaxa" value="false" checked><spring:message code="label.no"/>
</div>
<div>
<label><spring:message code="label.decimal.mark"/>:</label>
<input type="radio" name="decimalMark" value=",">","
<input type="radio" name="decimalMark" value="." checked>"."</br>
</div>
<div>
<button class="btn" type="submit" value="" name="validateType">
<spring:message code="label.run-check"/>
......
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