Search in sources :

Example 1 with CollectStoreQuery

use of org.openforis.collect.persistence.jooq.JooqDaoSupport.CollectStoreQuery 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 2 with CollectStoreQuery

use of org.openforis.collect.persistence.jooq.JooqDaoSupport.CollectStoreQuery in project collect by openforis.

the class RecordManager method executeRecordOperations.

@Transactional(readOnly = false, propagation = REQUIRED)
public void executeRecordOperations(List<RecordOperations> operationsForRecords, Consumer<RecordStepOperation> consumer) {
    Integer nextId = null;
    List<CollectStoreQuery> queries = new ArrayList<CollectStoreQuery>();
    for (RecordOperations recordOperations : operationsForRecords) {
        CollectRecordSummary existingRecordSummary = recordOperations.getRecordId() == null ? null : loadUniqueRecordSummary(recordOperations.getSurvey(), recordOperations.getRecordId());
        RecordStepOperation lastOperation = null;
        List<RecordStepOperation> operations = recordOperations.getOperations();
        for (RecordStepOperation operation : operations) {
            CollectRecord record = operation.getRecord();
            if (operation.isNewRecord()) {
                if (nextId == null) {
                    nextId = nextId();
                }
                recordOperations.initializeRecordId(nextId++);
                queries.addAll(createNewRecordInsertQueries(record));
            } else if (operation.isNewStep()) {
                queries.add(createDataInsertQuery(record, recordOperations.getRecordId(), operation.getStep(), operation.getDataStepSequenceNumber()));
            } else {
                queries.add(createDataUpdateQuery(record, recordOperations.getRecordId(), operation.getStep(), operation.getDataStepSequenceNumber()));
            }
            if (consumer != null) {
                consumer.consume(operation);
            }
            lastOperation = operation;
        }
        if (existingRecordSummary == null || existingRecordSummary.getStep().beforeEqual(lastOperation.getStep())) {
            CollectRecord lastRecord = lastOperation.getRecord();
            lastRecord.setId(recordOperations.getRecordId());
            lastRecord.setDataWorkflowSequenceNumber(lastOperation.getDataStepSequenceNumber());
            lastRecord.setStep(lastOperation.getStep());
            queries.add(createSummaryUpdateQuery(lastRecord));
        }
    }
    execute(queries);
    if (nextId != null) {
        restartIdSequence(nextId);
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) ArrayList(java.util.ArrayList) CollectStoreQuery(org.openforis.collect.persistence.jooq.JooqDaoSupport.CollectStoreQuery) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CollectRecord (org.openforis.collect.model.CollectRecord)2 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)2 CollectStoreQuery (org.openforis.collect.persistence.jooq.JooqDaoSupport.CollectStoreQuery)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 MainStep (org.openforis.collect.io.data.DataImportState.MainStep)1 SubStep (org.openforis.collect.io.data.DataImportState.SubStep)1 Step (org.openforis.collect.model.CollectRecord.Step)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 ParseRecordResult (org.openforis.collect.persistence.xml.DataUnmarshaller.ParseRecordResult)1 Transactional (org.springframework.transaction.annotation.Transactional)1