Search in sources :

Example 1 with CSVDataExportJob

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);
}
Also used : User(org.openforis.collect.model.User) JobView(org.openforis.collect.web.controller.CollectJobController.JobView) CSVDataExportJob(org.openforis.collect.io.data.CSVDataExportJob) CSVDataExportParameters(org.openforis.collect.io.data.csv.CSVDataExportParameters) CollectSurvey(org.openforis.collect.model.CollectSurvey) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with 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);
        }
    }
}
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)

Example 3 with CSVDataExportJob

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());
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CSVDataExportJob(org.openforis.collect.io.data.CSVDataExportJob) CSVDataExportParameters(org.openforis.collect.io.data.csv.CSVDataExportParameters) File(java.io.File) RecordFilter(org.openforis.collect.model.RecordFilter) Date(java.util.Date) FileInputStream(java.io.FileInputStream) GlobalCommand(org.zkoss.bind.annotation.GlobalCommand) Command(org.zkoss.bind.annotation.Command)

Example 4 with CSVDataExportJob

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());
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CSVDataExportJob(org.openforis.collect.io.data.CSVDataExportJob) CSVDataExportParameters(org.openforis.collect.io.data.csv.CSVDataExportParameters) File(java.io.File) RecordFilter(org.openforis.collect.model.RecordFilter) Date(java.util.Date) FileInputStream(java.io.FileInputStream) GlobalCommand(org.zkoss.bind.annotation.GlobalCommand) Command(org.zkoss.bind.annotation.Command)

Aggregations

CSVDataExportJob (org.openforis.collect.io.data.CSVDataExportJob)4 CSVDataExportParameters (org.openforis.collect.io.data.csv.CSVDataExportParameters)4 File (java.io.File)3 Date (java.util.Date)3 RecordFilter (org.openforis.collect.model.RecordFilter)3 FileInputStream (java.io.FileInputStream)2 CollectSurvey (org.openforis.collect.model.CollectSurvey)2 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Command (org.zkoss.bind.annotation.Command)2 GlobalCommand (org.zkoss.bind.annotation.GlobalCommand)2 RecordAccessControlManager (org.openforis.collect.manager.RecordAccessControlManager)1 CollectRecord (org.openforis.collect.model.CollectRecord)1 User (org.openforis.collect.model.User)1 JobView (org.openforis.collect.web.controller.CollectJobController.JobView)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1