Search in sources :

Example 46 with CollectRecordSummary

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...");
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Progress(org.openforis.concurrency.Progress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JooqDatabaseExporter(org.openforis.collect.relational.jooq.JooqDatabaseExporter) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) ProcessStepProgressListener(org.openforis.concurrency.ProcessStepProgressListener) ProcessProgressListener(org.openforis.concurrency.ProcessProgressListener) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 47 with CollectRecordSummary

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);
}
Also used : CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 48 with CollectRecordSummary

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));
    }
}
Also used : CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 49 with CollectRecordSummary

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

Example 50 with CollectRecordSummary

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();
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) Node(org.openforis.idm.model.Node) CollectSurvey(org.openforis.collect.model.CollectSurvey) XPathDataQueryEvaluator(org.openforis.collect.datacleansing.xpath.XPathDataQueryEvaluator) RecordFilter(org.openforis.collect.model.RecordFilter) Transactional(org.springframework.transaction.annotation.Transactional)

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