Search in sources :

Example 1 with CodeListLine

use of org.openforis.collect.manager.codelistimport.CodeListLine 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);
    }
}
Also used : CodeListCSVReader(org.openforis.collect.manager.codelistimport.CodeListCSVReader) ParsingException(org.openforis.collect.io.exception.ParsingException) ParsingError(org.openforis.collect.io.metadata.parsing.ParsingError) ParsingException(org.openforis.collect.io.exception.ParsingException) CollectSurvey(org.openforis.collect.model.CollectSurvey) CodeListLine(org.openforis.collect.manager.codelistimport.CodeListLine) CodeListItem(org.openforis.idm.metamodel.CodeListItem) File(java.io.File)

Aggregations

File (java.io.File)1 ParsingException (org.openforis.collect.io.exception.ParsingException)1 ParsingError (org.openforis.collect.io.metadata.parsing.ParsingError)1 CodeListCSVReader (org.openforis.collect.manager.codelistimport.CodeListCSVReader)1 CodeListLine (org.openforis.collect.manager.codelistimport.CodeListLine)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 CodeListItem (org.openforis.idm.metamodel.CodeListItem)1