Search in sources :

Example 11 with RecordFilter

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

the class XMLDataExportProcess method loadAllSummaries.

private List<CollectRecordSummary> loadAllSummaries() {
    RecordFilter filter = new RecordFilter(survey);
    filter.setRootEntityId(survey.getSchema().getRootEntityDefinition(rootEntityName).getId());
    filter.setModifiedSince(modifiedSince);
    List<CollectRecordSummary> summaries = recordManager.loadSummaries(filter);
    return summaries;
}
Also used : CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 12 with RecordFilter

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

the class DataRestoreSummaryTask method findAlreadyExistingFullRecordSummary.

private CollectRecordSummary findAlreadyExistingFullRecordSummary(int entryId, Step step, CollectRecord parsedRecord) {
    List<String> keyValues = parsedRecord.getRootEntityKeyValues();
    Entity rootEntity = parsedRecord.getRootEntity();
    int rootEntityDefId = rootEntity.getDefinition().getId();
    RecordFilter filter = new RecordFilter(survey);
    filter.setRootEntityId(rootEntityDefId);
    filter.setKeyValues(keyValues);
    List<CollectRecordSummary> oldRecordSummaries = recordManager.loadSummaries(filter);
    if (oldRecordSummaries == null || oldRecordSummaries.isEmpty()) {
        return null;
    } else if (oldRecordSummaries.size() == 1) {
        CollectRecordSummary summary = oldRecordSummaries.get(0);
        CollectRecord record = recordManager.load(survey, summary.getId(), summary.getStep(), fullSummary);
        CollectRecordSummary recordSummary = CollectRecordSummary.fromRecord(record);
        recordSummary.setFiles(recordFileManager.getAllFiles(record));
        return recordSummary;
    } else {
        String errorMessage = String.format("Data file: %s - multiple records found in survey %s with key(s) %s", getEntryName(entryId, step), survey.getName(), keyValues);
        throw new IllegalStateException(errorMessage);
    }
}
Also used : Entity(org.openforis.idm.model.Entity) CollectRecord(org.openforis.collect.model.CollectRecord) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 13 with RecordFilter

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

the class DataRestoreSummaryTask method findAlreadyExistingRecordSummary.

private CollectRecordSummary findAlreadyExistingRecordSummary(int entryId, Step step, int rootEntityDefId, List<String> rootEntityKeys) {
    RecordFilter filter = new RecordFilter(survey);
    filter.setRootEntityId(rootEntityDefId);
    filter.setKeyValues(rootEntityKeys);
    List<CollectRecordSummary> summaries = recordManager.loadSummaries(filter);
    switch(summaries.size()) {
        case 0:
            return null;
        case 1:
            return summaries.get(0);
        default:
            String errorMessage = String.format("Data file: %s - multiple records found in survey %s with key(s) %s", getEntryName(entryId, step), survey.getName(), rootEntityKeys);
            throw new IllegalStateException(errorMessage);
    }
}
Also used : CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 14 with RecordFilter

use of org.openforis.collect.model.RecordFilter 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 15 with RecordFilter

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

the class RecordController method exportRecord.

@RequestMapping(value = "survey/{survey_id}/data/records/{record_id}/steps/{step}/csv_content.zip", method = GET, produces = MediaTypes.ZIP_CONTENT_TYPE)
public void exportRecord(@PathVariable(value = "survey_id") int surveyId, @PathVariable(value = "record_id") int recordId, @PathVariable(value = "step") int stepNumber, HttpServletResponse response) throws RecordPersistenceException, IOException {
    CollectSurvey survey = surveyManager.getById(surveyId);
    CollectRecord record = recordManager.load(survey, recordId);
    RecordAccessControlManager accessControlManager = new RecordAccessControlManager();
    if (accessControlManager.canEdit(sessionManager.getLoggedUser(), record)) {
        CSVDataExportJob job = jobManager.createJob(CSVDataExportJob.class);
        CSVDataExportParameters parameters = new CSVDataExportParameters();
        RecordFilter recordFilter = createRecordFilter(survey, sessionManager.getLoggedUser(), userGroupManager, null, false);
        recordFilter.setRecordId(recordId);
        recordFilter.setStepGreaterOrEqual(Step.valueOf(stepNumber));
        parameters.setRecordFilter(recordFilter);
        parameters.setAlwaysGenerateZipFile(true);
        job.setParameters(parameters);
        File outputFile = File.createTempFile("record_export", ".zip");
        job.setOutputFile(outputFile);
        jobManager.startSurveyJob(job);
        if (job.isCompleted()) {
            String fileName = String.format("record_data_%s.zip", Dates.formatDate(new Date()));
            Controllers.writeFileToResponse(response, outputFile, fileName, MediaTypes.ZIP_CONTENT_TYPE);
        }
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) CSVDataExportJob(org.openforis.collect.io.data.CSVDataExportJob) CSVDataExportParameters(org.openforis.collect.io.data.csv.CSVDataExportParameters) CollectSurvey(org.openforis.collect.model.CollectSurvey) RecordAccessControlManager(org.openforis.collect.manager.RecordAccessControlManager) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RecordFilter(org.openforis.collect.model.RecordFilter) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RecordFilter (org.openforis.collect.model.RecordFilter)66 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)33 CollectSurvey (org.openforis.collect.model.CollectSurvey)27 CollectRecord (org.openforis.collect.model.CollectRecord)14 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Date (java.util.Date)10 User (org.openforis.collect.model.User)10 SessionState (org.openforis.collect.web.session.SessionState)7 Transactional (org.springframework.transaction.annotation.Transactional)7 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 File (java.io.File)5 HashMap (java.util.HashMap)5 CSVDataExportParameters (org.openforis.collect.io.data.csv.CSVDataExportParameters)5 Step (org.openforis.collect.model.CollectRecord.Step)5 IOException (java.io.IOException)4 SurveyBackupJob (org.openforis.collect.io.SurveyBackupJob)4 CSVDataExportJob (org.openforis.collect.io.data.CSVDataExportJob)4 Schema (org.openforis.idm.metamodel.Schema)4 Test (org.junit.Test)3