Search in sources :

Example 16 with CollectRecordSummary

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

the class RecordOperationGenerator method generate.

public RecordOperations generate() throws IOException, MissingStepsException, RecordParsingException {
    RecordOperations operations = new RecordOperations();
    boolean firstStepToBeProcessed = true;
    CollectRecordSummary existingRecordSummary = null;
    int workflowSequenceNumber = -1;
    boolean newRecord = true;
    for (Step step : Step.values()) {
        CollectRecord parsedRecord = recordProvider.provideRecord(entryId, step);
        if (parsedRecord == null || !isToBeProcessed(parsedRecord)) {
            continue;
        }
        setDefaultValues(parsedRecord);
        if (firstStepToBeProcessed) {
            existingRecordSummary = findAlreadyExistingRecordSummary(parsedRecord);
            newRecord = existingRecordSummary == null;
            if (newRecord) {
                insertRecordDataUntilStep(operations, parsedRecord, step);
                workflowSequenceNumber = calculateStepDataSequenceNumber(existingRecordSummary, step);
            } else {
                Step existingRecordStep = existingRecordSummary.getStep();
                operations.initializeRecordId(existingRecordSummary.getId());
                operations.setOriginalStep(existingRecordStep);
                if (overwriteStrategy == OVERWRITE_OLDER && isNewer(parsedRecord, existingRecordSummary) || overwriteStrategy == ONLY_SPECIFIED || overwriteStrategy == OVERWRITE_ALL) {
                    // overwrite existing record data
                    parsedRecord.setId(existingRecordSummary.getId());
                    boolean insertNewDataStep = step.after(existingRecordStep);
                    workflowSequenceNumber = calculateStepDataSequenceNumber(existingRecordSummary, step);
                    operations.addUpdate(parsedRecord, step, insertNewDataStep, workflowSequenceNumber);
                }
            }
            firstStepToBeProcessed = false;
        } else {
            boolean insertNewDataStep = newRecord ? true : step.after(operations.getOriginalStep());
            workflowSequenceNumber = calculateStepDataSequenceNumber(existingRecordSummary, step);
            operations.addUpdate(parsedRecord, step, insertNewDataStep, workflowSequenceNumber);
        }
    }
    return operations;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) Step(org.openforis.collect.model.CollectRecord.Step) RecordOperations(org.openforis.collect.manager.RecordManager.RecordOperations)

Example 17 with CollectRecordSummary

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

the class RecordController method loadRecordSummaries.

@RequestMapping(value = "survey/{surveyId}/data/records/summary", method = GET)
@ResponseBody
public Map<String, Object> loadRecordSummaries(@PathVariable("surveyId") int surveyId, @Valid RecordSummarySearchParameters params) {
    CollectSurvey survey = surveyManager.getOrLoadSurveyById(surveyId);
    User user = loadUser(params.getUserId(), params.getUsername());
    Map<String, Object> result = new HashMap<String, Object>();
    Schema schema = survey.getSchema();
    EntityDefinition rootEntityDefinition = params.getRootEntityName() == null ? schema.getFirstRootEntityDefinition() : schema.getRootEntityDefinition(params.getRootEntityName());
    RecordFilter filter = createRecordFilter(survey, user, userGroupManager, rootEntityDefinition.getId(), false);
    filter.setKeyValues(params.getKeyValues());
    filter.setCaseSensitiveKeyValues(params.isCaseSensitiveKeyValues());
    if (CollectionUtils.isEmpty(filter.getQualifiers())) {
        // filter by qualifiers only if not already done by user group qualifiers
        filter.setQualifiers(params.getQualifierValues());
    }
    filter.setSummaryValues(params.getSummaryValues());
    if (filter.getOwnerIds() == null && params.getOwnerIds() != null && params.getOwnerIds().length > 0) {
        filter.setOwnerIds(Arrays.asList(params.getOwnerIds()));
    }
    filter.setOffset(params.getOffset());
    filter.setMaxNumberOfRecords(params.getMaxNumberOfRows());
    // load summaries
    List<CollectRecordSummary> summaries = params.isFullSummary() ? recordManager.loadFullSummaries(filter, params.getSortFields()) : recordManager.loadSummaries(filter, params.getSortFields());
    result.put("records", toProxies(summaries));
    // count total records
    int count = recordManager.countRecords(filter);
    result.put("count", count);
    if (params.isIncludeOwners()) {
        Set<User> owners = recordManager.loadDistinctOwners(createRecordFilter(survey, user, userGroupManager, rootEntityDefinition.getId(), false));
        Set<BasicUserProxy> ownerProxies = Proxies.fromSet(owners, BasicUserProxy.class);
        result.put("owners", ownerProxies);
    }
    return result;
}
Also used : User(org.openforis.collect.model.User) HashMap(java.util.HashMap) Schema(org.openforis.idm.metamodel.Schema) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) BasicUserProxy(org.openforis.collect.model.proxy.BasicUserProxy) CollectSurvey(org.openforis.collect.model.CollectSurvey) RecordFilter(org.openforis.collect.model.RecordFilter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 18 with CollectRecordSummary

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

the class RecordStatsGenerator method generate.

public RecordsStats generate(int surveyId, Date[] period) {
    final RecordsStats stats = new RecordsStats(period);
    CollectSurvey survey = surveyManager.getById(surveyId);
    recordManager.visitSummaries(new RecordFilter(survey), null, new Visitor<CollectRecordSummary>() {

        public void visit(CollectRecordSummary s) {
            for (Entry<Step, StepSummary> entry : s.getStepSummaries().entrySet()) {
                Step step = entry.getKey();
                StepSummary stepSummary = entry.getValue();
                if (stepSummary.getCreationDate() != null) {
                    PointStats pointStats = stats.getOrCreateDailyStats(stepSummary.getCreationDate());
                    switch(step) {
                        case ENTRY:
                            pointStats.incrementCreated();
                            break;
                        case CLEANSING:
                            pointStats.incrementEntered();
                            break;
                        case ANALYSIS:
                            pointStats.incrementCleansed();
                            break;
                    }
                }
            }
            if (s.getModifiedDate() != null) {
                PointStats pointStats = stats.getOrCreateDailyStats(s.getModifiedDate());
                pointStats.incrementModified();
            }
        }
    }, true);
    stats.finalize();
    return stats;
}
Also used : Entry(java.util.Map.Entry) StepSummary(org.openforis.collect.model.CollectRecordSummary.StepSummary) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) Step(org.openforis.collect.model.CollectRecord.Step) CollectSurvey(org.openforis.collect.model.CollectSurvey) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 19 with CollectRecordSummary

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

the class ValidationController method validateAllRecords.

@RequestMapping(value = "/validateAllRecords.htm", method = RequestMethod.GET)
public void validateAllRecords(HttpServletRequest request, HttpServletResponse response, @RequestParam String s, @RequestParam String r) throws IOException {
    final ServletOutputStream outputStream = response.getOutputStream();
    try {
        if (s == null || r == null) {
            outputStream.println("Wrong parameters: please specify 's' (survey) and 'r' (root entity name).");
            return;
        }
        SessionState sessionState = getSessionState(request);
        final User user = sessionState.getUser();
        final String sessionId = sessionState.getSessionId();
        print(outputStream, "Starting validation of all records: ");
        final CollectSurvey survey = surveyManager.get(s);
        if (survey == null) {
            print(outputStream, "Survey not found");
            return;
        }
        RecordFilter filter = new RecordFilter(survey);
        filter.setRootEntityId(survey.getSchema().getRootEntityDefinition(r).getId());
        final ValidationMessageBuilder validationMessageHelper = ValidationMessageBuilder.createInstance(messageContextHolder);
        recordManager.visitSummaries(filter, null, new Visitor<CollectRecordSummary>() {

            public void visit(CollectRecordSummary summary) {
                try {
                    String recordKey = validationMessageHelper.getRecordKey(summary);
                    long start = System.currentTimeMillis();
                    print(outputStream, "Start validating record: " + recordKey);
                    Integer id = summary.getId();
                    Step step = summary.getStep();
                    recordManager.validateAndSave(survey, user, sessionId, id, step);
                    long elapsedMillis = System.currentTimeMillis() - start;
                    print(outputStream, "Validation of record " + recordKey + " completed in " + elapsedMillis + " millis");
                } catch (Exception e) {
                    try {
                        String message = "ERROR validating record " + summary.getId();
                        outputStream.println(message);
                        LOG.error(message);
                    } catch (IOException e1) {
                    }
                }
            }
        });
        print(outputStream, "End of validation of all records.");
    } catch (Exception e) {
        outputStream.println("ERROR - Validation of records not completed: " + e.getMessage());
        LOG.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
Also used : ValidationMessageBuilder(org.openforis.collect.model.validation.ValidationMessageBuilder) SessionState(org.openforis.collect.web.session.SessionState) User(org.openforis.collect.model.User) ServletOutputStream(javax.servlet.ServletOutputStream) Step(org.openforis.collect.model.CollectRecord.Step) IOException(java.io.IOException) IOException(java.io.IOException) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) CollectSurvey(org.openforis.collect.model.CollectSurvey) RecordFilter(org.openforis.collect.model.RecordFilter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with CollectRecordSummary

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

the class GeoDataController method processNodes.

private void processNodes(CollectSurvey survey, Integer recordOffset, Integer maxNumberOfRecords, int attributeId, NodeProcessor nodeProcessor) throws Exception {
    NodeDefinition nodeDef = survey.getSchema().getDefinitionById(attributeId);
    RecordFilter filter = new RecordFilter(survey);
    filter.setOffset(recordOffset);
    filter.setMaxNumberOfRecords(maxNumberOfRecords);
    List<CollectRecordSummary> summaries = recordManager.loadSummaries(filter);
    for (CollectRecordSummary summary : summaries) {
        CollectRecord record = recordManager.load(survey, summary.getId(), summary.getStep(), false);
        List<Node<?>> nodes = record.findNodesByPath(nodeDef.getPath());
        for (Node<?> node : nodes) {
            nodeProcessor.process(node);
        }
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) Node(org.openforis.idm.model.Node) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) RecordFilter(org.openforis.collect.model.RecordFilter)

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