Search in sources :

Example 11 with CollectRecordSummary

use of org.openforis.collect.model.CollectRecordSummary in project collect by openforis.

the class DataRestoreSummaryTask method findAlreadyExistingFullRecordSummary.

private CollectRecordSummary findAlreadyExistingFullRecordSummary(int entryId, Step step, CollectRecord parsedRecord) {
    List<String> keyValues = parsedRecord.getRootEntityKeyValues();
    Entity rootEntity = parsedRecord.getRootEntity();
    int rootEntityDefId = rootEntity.getDefinition().getId();
    RecordFilter filter = new RecordFilter(survey);
    filter.setRootEntityId(rootEntityDefId);
    filter.setKeyValues(keyValues);
    List<CollectRecordSummary> oldRecordSummaries = recordManager.loadSummaries(filter);
    if (oldRecordSummaries == null || oldRecordSummaries.isEmpty()) {
        return null;
    } else if (oldRecordSummaries.size() == 1) {
        CollectRecordSummary summary = oldRecordSummaries.get(0);
        CollectRecord record = recordManager.load(survey, summary.getId(), summary.getStep(), fullSummary);
        CollectRecordSummary recordSummary = CollectRecordSummary.fromRecord(record);
        recordSummary.setFiles(recordFileManager.getAllFiles(record));
        return recordSummary;
    } else {
        String errorMessage = String.format("Data file: %s - multiple records found in survey %s with key(s) %s", getEntryName(entryId, step), survey.getName(), keyValues);
        throw new IllegalStateException(errorMessage);
    }
}
Also used : Entity(org.openforis.idm.model.Entity) CollectRecord(org.openforis.collect.model.CollectRecord) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 12 with CollectRecordSummary

use of org.openforis.collect.model.CollectRecordSummary in project collect by openforis.

the class DataRestoreSummaryTask method createRecordParsedCorrectlySummary.

private void createRecordParsedCorrectlySummary(int entryId, Step step, ParseRecordResult parseRecordResult) {
    CollectRecord parsedRecord = parseRecordResult.getRecord();
    CollectRecordSummary recordSummary = CollectRecordSummary.fromRecord(parsedRecord);
    recordSummaryByEntryId.put(entryId, recordSummary);
    addStepPerEntry(entryId, step);
    CollectRecordSummary oldRecord = findAlreadyExistingFullRecordSummary(entryId, step, parsedRecord);
    if (oldRecord != null) {
        conflictingRecordByEntryId.put(entryId, oldRecord);
    }
    if (parseRecordResult.hasWarnings()) {
        addWarningsPerStep(entryId, step, parseRecordResult.getWarnings());
    }
    incrementTotalPerStep(step);
    incrementProcessedItems();
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary)

Example 13 with CollectRecordSummary

use of org.openforis.collect.model.CollectRecordSummary 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 14 with CollectRecordSummary

use of org.openforis.collect.model.CollectRecordSummary in project collect by openforis.

the class DataRestoreSummaryTask method findAlreadyExistingRecordSummary.

private CollectRecordSummary findAlreadyExistingRecordSummary(int entryId, Step step, int rootEntityDefId, List<String> rootEntityKeys) {
    RecordFilter filter = new RecordFilter(survey);
    filter.setRootEntityId(rootEntityDefId);
    filter.setKeyValues(rootEntityKeys);
    List<CollectRecordSummary> summaries = recordManager.loadSummaries(filter);
    switch(summaries.size()) {
        case 0:
            return null;
        case 1:
            return summaries.get(0);
        default:
            String errorMessage = String.format("Data file: %s - multiple records found in survey %s with key(s) %s", getEntryName(entryId, step), survey.getName(), rootEntityKeys);
            throw new IllegalStateException(errorMessage);
    }
}
Also used : CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 15 with CollectRecordSummary

use of org.openforis.collect.model.CollectRecordSummary in project collect by openforis.

the class RecordFileRestoreTask method importRecordFiles.

private void importRecordFiles(int entryId) throws IOException, DataImportExeption, RecordPersistenceException, RecordParsingException {
    CollectRecord lastStepBackupRecord = getLastStepBackupRecord(entryId);
    if (lastStepBackupRecord == null) {
        throw new IllegalStateException("Error parsing record for entry: " + entryId);
    }
    CollectRecordSummary storedRecordSummary = findStoredRecordSummary(lastStepBackupRecord);
    if (overwriteStrategy == OVERWRITE_OLDER && isNewer(lastStepBackupRecord, storedRecordSummary) || overwriteStrategy == ONLY_SPECIFIED || overwriteStrategy == OVERWRITE_ALL) {
        CollectRecord storedRecord = recordManager.load(survey, storedRecordSummary.getId(), storedRecordSummary.getStep(), false);
        importRecordFiles(storedRecord);
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary)

Aggregations

CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)55 RecordFilter (org.openforis.collect.model.RecordFilter)33 CollectRecord (org.openforis.collect.model.CollectRecord)25 Step (org.openforis.collect.model.CollectRecord.Step)21 CollectSurvey (org.openforis.collect.model.CollectSurvey)15 IOException (java.io.IOException)8 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 User (org.openforis.collect.model.User)5 Transactional (org.springframework.transaction.annotation.Transactional)5 MainStep (org.openforis.collect.io.data.DataImportState.MainStep)4 SubStep (org.openforis.collect.io.data.DataImportState.SubStep)4 StepSummary (org.openforis.collect.model.CollectRecordSummary.StepSummary)4 Schema (org.openforis.idm.metamodel.Schema)4 List (java.util.List)3 Test (org.junit.Test)3 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2