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