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);
}
}
}
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);
}
}
}
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;
}
Aggregations