use of org.openforis.collect.model.CollectSurvey in project collect by openforis.
the class RecordController method startCsvDataImportJob.
@RequestMapping(value = "survey/{surveyId}/data/csvimport/records", method = POST, consumes = MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public JobView startCsvDataImportJob(@PathVariable("surveyId") int surveyId, @RequestParam("file") MultipartFile multipartFile, @RequestParam String rootEntityName, @RequestParam String importType, @RequestParam String steps, @RequestParam(required = false) Integer entityDefinitionId, @RequestParam(required = false) boolean validateRecords, @RequestParam(required = false) boolean deleteEntitiesBeforeImport, @RequestParam(required = false) String newRecordVersionName) throws IOException {
File file = Files.writeToTempFile(multipartFile.getInputStream(), multipartFile.getOriginalFilename(), "ofc_csv_data_import");
CollectSurvey survey = surveyManager.getById(surveyId);
CSVDataImportJob job = jobManager.createJob(TransactionalCSVDataImportJob.class);
CSVDataImportSettings settings = new CSVDataImportSettings();
settings.setDeleteExistingEntities(deleteEntitiesBeforeImport);
settings.setRecordValidationEnabled(validateRecords);
settings.setInsertNewRecords("newRecords".equals(importType));
settings.setNewRecordVersionName(newRecordVersionName);
CSVDataImportInput input = new CSVDataImportInput(file, survey, fromStepNames(steps), entityDefinitionId, settings);
job.setInput(input);
jobManager.start(job);
this.csvDataImportJob = job;
return new JobView(job);
}
use of org.openforis.collect.model.CollectSurvey 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;
}
use of org.openforis.collect.model.CollectSurvey in project collect by openforis.
the class RecordController method startCsvDataExportJob.
@RequestMapping(value = "survey/{surveyId}/data/records/startcsvexport", method = POST)
@ResponseBody
public JobView startCsvDataExportJob(@PathVariable("surveyId") int surveyId, @RequestBody CSVExportParametersForm parameters) throws IOException {
User user = sessionManager.getLoggedUser();
CollectSurvey survey = surveyManager.getById(surveyId);
csvDataExportJob = jobManager.createJob(CSVDataExportJob.class);
csvDataExportJob.setSurvey(survey);
csvDataExportJob.setOutputFile(File.createTempFile("collect-csv-data-export", ".zip"));
CSVDataExportParameters exportParameters = parameters.toExportParameters(survey, user, userGroupManager);
csvDataExportJob.setParameters(exportParameters);
jobManager.start(csvDataExportJob);
return new JobView(csvDataExportJob);
}
use of org.openforis.collect.model.CollectSurvey 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);
}
}
}
use of org.openforis.collect.model.CollectSurvey in project collect by openforis.
the class RecordController method downloadBackupExportResult.
@RequestMapping(value = "survey/{surveyId}/data/records/exportresult.collect-data", method = GET)
public void downloadBackupExportResult(HttpServletResponse response) throws FileNotFoundException, IOException {
File file = fullBackupJob.getOutputFile();
CollectSurvey survey = fullBackupJob.getSurvey();
String surveyName = survey.getName();
Controllers.writeFileToResponse(response, file, String.format("%s-%s.collect-data", surveyName, Dates.formatLocalDateTime(new Date())), Controllers.ZIP_CONTENT_TYPE);
}
Aggregations