Search in sources :

Example 1 with CsvLine

use of org.openforis.commons.io.csv.CsvLine in project collect by openforis.

the class DataRestoreSummaryTask method execute.

@Override
protected void execute() throws Throwable {
    if (fullSummary || dataSummaryFile == null) {
        List<Integer> idsToImport = extractEntryIdsToImport();
        for (Integer entryId : idsToImport) {
            if (!isRunning()) {
                break;
            }
            for (Step step : Step.values()) {
                if (!isRunning()) {
                    break;
                }
                createSummaryForEntry(entryId, step);
            }
        }
    } else {
        CsvReader dataSummaryCSVReader = null;
        try {
            dataSummaryCSVReader = new CsvReader(dataSummaryFile);
            dataSummaryCSVReader.readHeaders();
            CsvLine line = dataSummaryCSVReader.readNextLine();
            while (line != null) {
                Integer entryId = line.getValue("entry_id", Integer.class);
                String stepVal = line.getValue("step", String.class);
                Step step = Step.valueOf(stepVal);
                CollectRecordSummary recordSummary = new CollectRecordSummary();
                recordSummary.setStep(step);
                Integer rootEntityDefId = line.getValue("root_entity_id", Integer.class);
                recordSummary.setRootEntityDefinitionId(rootEntityDefId);
                Date creationDate = Dates.parseDateTime(line.getValue("created_on", String.class));
                Date modifiedDate = Dates.parseDateTime(line.getValue("last_modified", String.class));
                recordSummary.setCreationDate(creationDate);
                recordSummary.setModifiedDate(modifiedDate);
                StepSummary stepSummary = new StepSummary(step);
                List<String> rootEntityKeyValues = Arrays.asList(line.getValue("key1", String.class), line.getValue("key2", String.class), line.getValue("key3", String.class));
                stepSummary.setRootEntityKeyValues(rootEntityKeyValues);
                stepSummary.setCreationDate(creationDate);
                stepSummary.setModifiedDate(modifiedDate);
                recordSummary.addStepSummary(stepSummary);
                recordSummaryByEntryId.put(entryId, recordSummary);
                for (Step s : Step.values()) {
                    if (s.beforeEqual(step)) {
                        addStepPerEntry(entryId, s);
                        incrementTotalPerStep(step);
                        incrementProcessedItems();
                    }
                }
                CollectRecordSummary oldRecord = findAlreadyExistingRecordSummary(entryId, step, rootEntityDefId, rootEntityKeyValues);
                if (oldRecord != null) {
                    conflictingRecordByEntryId.put(entryId, oldRecord);
                }
                line = dataSummaryCSVReader.readNextLine();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(dataSummaryCSVReader);
        }
    }
}
Also used : CsvReader(org.openforis.commons.io.csv.CsvReader) StepSummary(org.openforis.collect.model.CollectRecordSummary.StepSummary) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) Step(org.openforis.collect.model.CollectRecord.Step) CsvLine(org.openforis.commons.io.csv.CsvLine) Date(java.util.Date) IOException(java.io.IOException)

Example 2 with CsvLine

use of org.openforis.commons.io.csv.CsvLine in project collect by openforis.

the class DataRestoreSummaryTask method extractEntryIdsToImport.

private List<Integer> extractEntryIdsToImport() {
    if (dataSummaryFile == null) {
        return recordProvider.findEntryIds();
    } else {
        CsvReader dataSummaryCSVReader = null;
        try {
            dataSummaryCSVReader = new CsvReader(dataSummaryFile);
            dataSummaryCSVReader.readHeaders();
            List<Integer> entryIds = new ArrayList<Integer>(dataSummaryCSVReader.size());
            CsvLine line = dataSummaryCSVReader.readNextLine();
            while (line != null) {
                Integer entryId = line.getValue("entry_id", Integer.class);
                entryIds.add(entryId);
                line = dataSummaryCSVReader.readNextLine();
            }
            return entryIds;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(dataSummaryCSVReader);
        }
    }
}
Also used : CsvReader(org.openforis.commons.io.csv.CsvReader) ArrayList(java.util.ArrayList) CsvLine(org.openforis.commons.io.csv.CsvLine) IOException(java.io.IOException)

Example 3 with CsvLine

use of org.openforis.commons.io.csv.CsvLine in project collect by openforis.

the class CollectEarthGridTemplateGenerator method validateCsvRows.

private List<CSVRowValidationResult> validateCsvRows(CsvReader csvReader, CollectSurvey survey, boolean firstLineIsHeaders, boolean validateOnlyFirstLines) throws IOException {
    List<CSVRowValidationResult> results = new ArrayList<CSVRowValidationResult>();
    List<CSVRowValidationResult> validateCsvRow = null;
    // Get the list of attribute types expected per row!
    List<AttributeDefinition> attributesPerRow = getAttributesPerRow(survey);
    int rowNumber = 1;
    if (!firstLineIsHeaders) {
        validateCsvRow = validateCsvRow(survey, attributesPerRow, (String[]) csvReader.getColumnNames().toArray(new String[csvReader.getColumnNames().size()]), rowNumber++);
        if (!validateCsvRow.isEmpty()) {
            results.addAll(validateCsvRow);
        }
    }
    CsvLine csvLine;
    while ((csvLine = csvReader.readNextLine()) != null) {
        if (validateOnlyFirstLines && rowNumber < 50) {
            validateCsvRow = validateCsvRow(survey, attributesPerRow, csvLine.getLine(), rowNumber++);
            if (validateCsvRow != null) {
                results.addAll(validateCsvRow);
            }
        }
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) DateAttributeDefinition(org.openforis.idm.metamodel.DateAttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) CsvLine(org.openforis.commons.io.csv.CsvLine)

Aggregations

CsvLine (org.openforis.commons.io.csv.CsvLine)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CsvReader (org.openforis.commons.io.csv.CsvReader)2 Date (java.util.Date)1 Step (org.openforis.collect.model.CollectRecord.Step)1 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)1 StepSummary (org.openforis.collect.model.CollectRecordSummary.StepSummary)1 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)1 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)1 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)1 DateAttributeDefinition (org.openforis.idm.metamodel.DateAttributeDefinition)1 NumberAttributeDefinition (org.openforis.idm.metamodel.NumberAttributeDefinition)1 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)1