use of org.openforis.collect.model.CollectRecordSummary in project collect by openforis.
the class CollectRDBPublisher method insertData.
private void insertData(final CollectSurvey survey, String rootEntityName, final Step step, Connection targetConn, RelationalSchema relationalSchema, ProgressListener progressListener) throws IOException {
// Insert data
RecordFilter recordFilter = new RecordFilter(survey);
recordFilter.setRootEntityId(survey.getSchema().getRootEntityDefinition(rootEntityName).getId());
recordFilter.setStepGreaterOrEqual(step);
final int total = recordManager.countRecords(recordFilter);
if (LOG.isInfoEnabled()) {
LOG.info("Total records: " + total);
}
final DatabaseExporter databaseExporter = createDatabaseExporter(relationalSchema, targetConn);
ProcessProgressListener totalProgressListener = new ProcessProgressListener(2);
databaseExporter.insertReferenceData(new ProcessStepProgressListener(totalProgressListener, progressListener));
final ProcessStepProgressListener insertRecordsProgressListener = new ProcessStepProgressListener(totalProgressListener, progressListener);
final AtomicInteger count = new AtomicInteger();
recordManager.visitSummaries(recordFilter, null, new Visitor<CollectRecordSummary>() {
public void visit(CollectRecordSummary summary) {
try {
CollectRecord record = recordManager.load(survey, summary.getId(), step, false);
databaseExporter.insertRecordData(record, ProgressListener.NULL_PROGRESS_LISTENER);
} catch (CollectRdbException e) {
LOG.error(e.getMessage(), e);
}
insertRecordsProgressListener.progressMade(new Progress(count.addAndGet(1), total));
}
});
databaseExporter.close();
if (LOG.isInfoEnabled()) {
LOG.info("All records exported");
LOG.info("Adding constraints and indexes...");
}
}
use of org.openforis.collect.model.CollectRecordSummary in project collect by openforis.
the class RecordManager method loadUniqueRecordSummary.
public CollectRecordSummary loadUniqueRecordSummary(CollectSurvey survey, int recordId) {
RecordFilter filter = new RecordFilter(survey);
filter.setRecordId(recordId);
List<CollectRecordSummary> summaries = loadSummaries(filter);
return summaries.isEmpty() ? null : summaries.get(0);
}
use of org.openforis.collect.model.CollectRecordSummary in project collect by openforis.
the class RecordManager method loadUniqueRecordSummaryByKeys.
public CollectRecordSummary loadUniqueRecordSummaryByKeys(CollectSurvey survey, String rootEntityName, String... keyValues) {
RecordFilter filter = new RecordFilter(survey);
filter.setRootEntityId(survey.getSchema().getRootEntityDefinition(rootEntityName).getId());
filter.setKeyValues(keyValues);
List<CollectRecordSummary> oldRecords = loadSummaries(filter);
if (oldRecords == null || oldRecords.isEmpty()) {
return null;
} else if (oldRecords.size() == 1) {
return oldRecords.get(0);
} else {
throw new IllegalStateException(String.format("Multiple records found in survey %s with key(s): %s", survey.getName(), keyValues));
}
}
use of org.openforis.collect.model.CollectRecordSummary 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);
}
}
use of org.openforis.collect.model.CollectRecordSummary in project collect by openforis.
the class DataQueryExectutorTask method execute.
@Override
@Transactional
protected void execute() throws Throwable {
CollectSurvey survey = input.query.getSurvey();
DataQueryEvaluator queryEvaluator = createQueryEvaluator(input.query);
RecordFilter filter = createRecordsFilter(true);
List<CollectRecordSummary> recordSummaries = recordManager.loadSummaries(filter);
Iterator<CollectRecordSummary> it = recordSummaries.iterator();
while (it.hasNext() && isRunning()) {
CollectRecordSummary recordSummary = (CollectRecordSummary) it.next();
CollectRecord record = recordManager.load(survey, recordSummary.getId(), input.step, false);
List<Node<?>> nodes = queryEvaluator.evaluate(record);
for (Node<?> node : nodes) {
processNode(node);
}
incrementProcessedItems();
}
}
Aggregations