Search in sources :

Example 76 with CollectRecord

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

the class CSVDataImportProcess method saveLastModifiedRecord.

private void saveLastModifiedRecord() throws RecordPersistenceException {
    Step originalStep = lastModifiedRecordSummary.getStep();
    updateRecord(lastModifiedRecord, originalStep, step);
    if (step.compareTo(originalStep) < 0) {
        // reset record step to the original one
        CollectRecord record = recordManager.load(survey, lastModifiedRecordSummary.getId(), originalStep, settings.isRecordValidationEnabled());
        record.setStep(originalStep);
        updateRecord(record, originalStep, originalStep);
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Step(org.openforis.collect.model.CollectRecord.Step)

Example 77 with CollectRecord

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

the class XMLDataImportProcess method importEntries.

private void importEntries(int recordId) throws IOException, DataImportExeption, RecordPersistenceException {
    CollectRecord lastProcessedRecord = null;
    Step originalRecordStep = null;
    Step[] steps = Step.values();
    for (Step step : steps) {
        RecordEntry recordEntry = new RecordEntry(step, recordId);
        String entryName = recordEntry.getName();
        InputStream inputStream = backupFileExtractor.findEntryInputStream(entryName);
        if (inputStream != null) {
            InputStreamReader reader = OpenForisIOUtils.toReader(inputStream);
            ParseRecordResult parseRecordResult = parseRecord(reader, validateRecords);
            CollectRecord parsedRecord = parseRecordResult.getRecord();
            if (parsedRecord == null) {
                String message = parseRecordResult.getMessage();
                state.addError(entryName, message);
            } else {
                parsedRecord.setStep(step);
                List<CollectStoreQuery> queries;
                if (lastProcessedRecord == null) {
                    CollectRecordSummary oldRecordSummary = findAlreadyExistingRecordSummary(parsedRecord);
                    if (oldRecordSummary != null) {
                        // overwrite existing record
                        originalRecordStep = oldRecordSummary.getStep();
                        parsedRecord.setId(oldRecordSummary.getId());
                        if (includesRecordFiles) {
                            recordFileManager.deleteAllFiles(parsedRecord);
                        }
                        int newWorkflowSequenceNumber = oldRecordSummary.getWorkflowSequenceNumber() + (step.getStepNumber() - originalRecordStep.getStepNumber());
                        parsedRecord.setDataWorkflowSequenceNumber(newWorkflowSequenceNumber);
                        parsedRecord.setWorkflowSequenceNumber(newWorkflowSequenceNumber);
                        if (step.after(originalRecordStep)) {
                            queries = Arrays.asList(recordManager.createDataInsertQuery(parsedRecord, oldRecordSummary.getId(), step, newWorkflowSequenceNumber));
                        } else {
                            queries = Arrays.asList(recordManager.createDataUpdateQuery(parsedRecord, oldRecordSummary.getId(), step, newWorkflowSequenceNumber));
                        }
                    } else {
                        parsedRecord.setId(nextRecordId++);
                        queries = recordManager.createNewRecordInsertQueries(parsedRecord);
                    }
                    lastProcessedRecord = parsedRecord;
                } else {
                    replaceData(parsedRecord, lastProcessedRecord);
                    int sequenceNumber = parsedRecord.getWorkflowSequenceNumber() + 1;
                    parsedRecord.setDataWorkflowSequenceNumber(sequenceNumber);
                    if (step.after(originalRecordStep)) {
                        queries = Arrays.asList(recordManager.createDataInsertQuery(lastProcessedRecord, lastProcessedRecord.getId(), step, sequenceNumber));
                    } else {
                        queries = Arrays.asList(recordManager.createDataUpdateQuery(lastProcessedRecord, lastProcessedRecord.getId(), step, sequenceNumber));
                    }
                }
                appendQueries(queries);
            // if ( parseRecordResult.hasWarnings() ) {
            // state.addWarnings(entryName, parseRecordResult.getWarnings());
            // }
            }
        }
    }
    if (lastProcessedRecord != null && originalRecordStep != null && originalRecordStep.compareTo(lastProcessedRecord.getStep()) > 0) {
        // reset the step to the original one and revalidate the record
        CollectSurvey survey = (CollectSurvey) lastProcessedRecord.getSurvey();
        CollectRecord originalRecord = recordManager.load(survey, lastProcessedRecord.getId(), originalRecordStep, validateRecords);
        originalRecord.setStep(originalRecordStep);
        afterRecordUpdate(originalRecord);
        appendQuery(recordManager.createSummaryUpdateQuery(originalRecord));
    }
    if (includesRecordFiles) {
        importRecordFiles(lastProcessedRecord);
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MainStep(org.openforis.collect.io.data.DataImportState.MainStep) SubStep(org.openforis.collect.io.data.DataImportState.SubStep) Step(org.openforis.collect.model.CollectRecord.Step) CollectStoreQuery(org.openforis.collect.persistence.jooq.JooqDaoSupport.CollectStoreQuery) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) CollectSurvey(org.openforis.collect.model.CollectSurvey) ParseRecordResult(org.openforis.collect.persistence.xml.DataUnmarshaller.ParseRecordResult)

Example 78 with CollectRecord

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

the class XMLDataImportProcess method createSummaryForEntry.

private void createSummaryForEntry(String entryName, Map<String, List<NodeUnmarshallingError>> packagedSkippedFileErrors, Map<Integer, CollectRecord> packagedRecords, Map<Integer, List<Step>> packagedStepsPerRecord, Map<Step, Integer> totalPerStep, Map<Integer, CollectRecordSummary> conflictingPackagedRecords, Map<Integer, Map<Step, List<NodeUnmarshallingError>>> warnings) throws IOException, DataParsingExeption {
    RecordEntry recordEntry = RecordEntry.parse(entryName);
    Step step = recordEntry.getStep();
    InputStream is = backupFileExtractor.findEntryInputStream(entryName);
    InputStreamReader reader = OpenForisIOUtils.toReader(is);
    ParseRecordResult parseRecordResult = parseRecord(reader, false);
    CollectRecord parsedRecord = parseRecordResult.getRecord();
    if (!parseRecordResult.isSuccess()) {
        List<NodeUnmarshallingError> failures = parseRecordResult.getFailures();
        packagedSkippedFileErrors.put(entryName, failures);
    } else if (includeRecordPredicate == null || includeRecordPredicate.evaluate(parsedRecord)) {
        int entryId = recordEntry.getRecordId();
        CollectRecord recordSummary = createRecordSummary(parsedRecord);
        packagedRecords.put(entryId, recordSummary);
        List<Step> stepsPerRecord = packagedStepsPerRecord.get(entryId);
        if (stepsPerRecord == null) {
            stepsPerRecord = new ArrayList<CollectRecord.Step>();
            packagedStepsPerRecord.put(entryId, stepsPerRecord);
        }
        stepsPerRecord.add(step);
        Integer totalPerStep1 = totalPerStep.get(step);
        totalPerStep.put(step, totalPerStep1 + 1);
        CollectRecordSummary oldRecord = findAlreadyExistingRecordSummary(parsedRecord);
        if (oldRecord != null) {
            conflictingPackagedRecords.put(entryId, oldRecord);
        }
        if (parseRecordResult.hasWarnings()) {
            Map<Step, List<NodeUnmarshallingError>> warningsPerEntry = warnings.get(entryId);
            if (warningsPerEntry == null) {
                warningsPerEntry = new HashMap<CollectRecord.Step, List<NodeUnmarshallingError>>();
                warnings.put(entryId, warningsPerEntry);
            }
            warningsPerEntry.put(step, parseRecordResult.getWarnings());
        }
    }
    state.incrementCount();
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) NodeUnmarshallingError(org.openforis.collect.persistence.xml.NodeUnmarshallingError) ArrayList(java.util.ArrayList) MainStep(org.openforis.collect.io.data.DataImportState.MainStep) SubStep(org.openforis.collect.io.data.DataImportState.SubStep) Step(org.openforis.collect.model.CollectRecord.Step) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ParseRecordResult(org.openforis.collect.persistence.xml.DataUnmarshaller.ParseRecordResult)

Example 79 with CollectRecord

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

the class XMLDataImportProcess method createRecordSummary.

protected CollectRecord createRecordSummary(CollectRecord record) {
    CollectSurvey survey = (CollectSurvey) record.getSurvey();
    ModelVersion version = record.getVersion();
    String versionName = version != null ? version.getName() : null;
    CollectRecord result = new CollectRecord(survey, versionName, record.getRootEntity().getName());
    result.setCreatedBy(record.getCreatedBy());
    result.setCreationDate(record.getCreationDate());
    result.setEntityCounts(record.getEntityCounts());
    result.setErrors(record.getErrors());
    result.setId(record.getId());
    result.setMissing(record.getMissing());
    result.setModifiedBy(record.getModifiedBy());
    result.setModifiedDate(record.getModifiedDate());
    result.setRootEntityKeyValues(record.getRootEntityKeyValues());
    result.setSkipped(record.getSkipped());
    result.setState(record.getState());
    result.setStep(record.getStep());
    return result;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) ModelVersion(org.openforis.idm.metamodel.ModelVersion) CollectSurvey(org.openforis.collect.model.CollectSurvey)

Example 80 with CollectRecord

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

the class RecordFileBackupTask method backup.

private void backup(CollectRecordSummary summary) throws RecordFileException, IOException {
    Integer id = summary.getId();
    CollectRecord record = recordManager.load(survey, id, summary.getStep(), false);
    List<FileAttribute> fileAttributes = record.getFileAttributes();
    for (FileAttribute fileAttribute : fileAttributes) {
        if (StringUtils.isNotBlank(fileAttribute.getFilename())) {
            File file = recordFileManager.getRepositoryFile(fileAttribute);
            if (file != null && file.exists()) {
                String entryName = calculateRecordFileEntryName(fileAttribute);
                writeFile(file, entryName);
            } else {
                addSkippedFileError(summary, fileAttribute.getPath(), recordFileManager.getRepositoryFileAbsolutePath(fileAttribute));
                log.error(String.format("Record file not found for record %s (%d) attribute %s (%d)", StringUtils.join(record.getRootEntityKeyValues(), ','), record.getId(), fileAttribute.getPath(), fileAttribute.getInternalId()));
            // throw new RecordFileException(message);
            }
        }
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) File(java.io.File) FileAttribute(org.openforis.idm.model.FileAttribute)

Aggregations

CollectRecord (org.openforis.collect.model.CollectRecord)141 Entity (org.openforis.idm.model.Entity)36 Test (org.junit.Test)30 CollectSurvey (org.openforis.collect.model.CollectSurvey)29 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)29 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)27 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)24 Step (org.openforis.collect.model.CollectRecord.Step)23 User (org.openforis.collect.model.User)19 SessionState (org.openforis.collect.web.session.SessionState)15 RecordFilter (org.openforis.collect.model.RecordFilter)14 Transactional (org.springframework.transaction.annotation.Transactional)14 ParsingError (org.openforis.collect.io.metadata.parsing.ParsingError)12 Secured (org.springframework.security.access.annotation.Secured)12 ArrayList (java.util.ArrayList)11 Date (org.openforis.idm.model.Date)10 GregorianCalendar (java.util.GregorianCalendar)9 Code (org.openforis.idm.model.Code)9 RealAttribute (org.openforis.idm.model.RealAttribute)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9