use of org.openforis.collect.manager.codelistimport.CodeListCSVReader in project collect by openforis.
the class CodeListImportTask method parseCSVLines.
private void parseCSVLines() {
long currentRowNumber = 0;
try {
CollectSurvey survey = (CollectSurvey) codeList.getSurvey();
List<String> languages = survey.getLanguages();
String defaultLanguage = survey.getDefaultLanguage();
File file = OpenForisIOUtils.copyToTempFile(inputStream);
reader = new CodeListCSVReader(file, csvFileOptions, languages, defaultLanguage);
reader.init();
levels = reader.getLevels();
addProcessedRow(1);
currentRowNumber = 2;
while (isRunning()) {
try {
CodeListLine line = reader.readNextLine();
if (line != null) {
CodeListItem currentParentItem = null;
List<String> levelCodes = line.getLevelCodes();
for (int levelIdx = 0; levelIdx < levelCodes.size(); levelIdx++) {
boolean lastLevel = levelIdx == levelCodes.size() - 1;
CodeListItem item = processLevel(currentParentItem, line, levelIdx, lastLevel);
currentParentItem = item;
}
addProcessedRow(currentRowNumber);
}
if (!reader.isReady()) {
break;
}
} catch (ParsingException e) {
addParsingError(currentRowNumber, e.getError());
} finally {
currentRowNumber++;
}
}
} catch (ParsingException e) {
changeStatus(Status.FAILED);
addParsingError(1, e.getError());
} catch (Exception e) {
changeStatus(Status.FAILED);
addParsingError(currentRowNumber, new ParsingError(ErrorType.IOERROR, e.toString()));
LOG.error("Error importing code list CSV file", e);
} finally {
IOUtils.closeQuietly(reader);
}
}
Aggregations