use of org.openforis.collect.io.data.CSVDataExportJob 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.io.data.CSVDataExportJob 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.io.data.CSVDataExportJob in project collect by openforis.
the class SurveyEditVM method exportCeCsvDataImportTemplate.
@Command
public void exportCeCsvDataImportTemplate() throws IOException {
CSVDataExportJob job = jobManager.createJob(CSVDataExportJob.class);
job.setOutputFile(File.createTempFile("ce-data-import-template-" + survey.getName(), ".csv"));
RecordFilter recordFilter = new RecordFilter(survey);
EntityDefinition rootEntityDef = survey.getSchema().getFirstRootEntityDefinition();
recordFilter.setRootEntityId(rootEntityDef.getId());
CSVDataExportParameters parameters = new CSVDataExportParameters();
parameters.setRecordFilter(recordFilter);
parameters.setEntityId(rootEntityDef.getId());
parameters.setAlwaysGenerateZipFile(false);
parameters.setIncludeEnumeratedEntities(true);
job.setParameters(parameters);
jobManager.start(job, false);
if (job.isCompleted()) {
File outputFile = job.getOutputFile();
String dateStr = Dates.formatLocalDateTime(new Date());
String fileName = String.format(DATA_IMPORT_TEMPLATE_FILE_NAME_PATTERN, survey.getName(), dateStr, "csv");
String contentType = URLConnection.guessContentTypeFromName(fileName);
FileInputStream is = new FileInputStream(outputFile);
Filedownload.save(is, contentType, fileName);
} else {
throw new RuntimeException("Error generating the CSV data export template: " + job.getErrorMessage(), job.getLastException());
}
}
use of org.openforis.collect.io.data.CSVDataExportJob in project collect by openforis.
the class SurveyEditVM method exportCsvDataImportTemplate.
@Command
public void exportCsvDataImportTemplate() throws IOException {
CSVDataExportJob job = jobManager.createJob(CSVDataExportJob.class);
job.setOutputFile(File.createTempFile("data-import-template", ".zip"));
CSVDataExportParameters parameters = new CSVDataExportParameters();
EntityDefinition rootEntityDef = survey.getSchema().getFirstRootEntityDefinition();
RecordFilter recordFilter = new RecordFilter(survey, rootEntityDef.getId());
parameters.setRecordFilter(recordFilter);
parameters.setAlwaysGenerateZipFile(true);
parameters.setIncludeEnumeratedEntities(false);
job.setParameters(parameters);
jobManager.start(job, false);
if (job.isCompleted()) {
File outputFile = job.getOutputFile();
String dateStr = Dates.formatLocalDateTime(new Date());
String fileName = String.format(DATA_IMPORT_TEMPLATE_FILE_NAME_PATTERN, survey.getName(), dateStr, "zip");
String contentType = URLConnection.guessContentTypeFromName(fileName);
FileInputStream is = new FileInputStream(outputFile);
Filedownload.save(is, contentType, fileName);
} else {
throw new RuntimeException("Error generating the CSV data export template: " + job.getErrorMessage(), job.getLastException());
}
}
Aggregations