Search in sources :

Example 1 with CSVDataExportParameters

use of org.openforis.collect.io.data.csv.CSVDataExportParameters in project collect by openforis.

the class DataExportService method export.

@Transactional
public Proxy export(String rootEntityName, int stepNumber, Integer entityId, boolean includeAllAncestorAttributes, boolean includeEnumeratedEntities, boolean includeCompositeAttributeMergedColumn, boolean codeAttributeExpanded, boolean onlyOwnedRecords, String[] rootEntityKeyValues, boolean includeKMLColumnForCoordinates, boolean includeCodeItemLabelColumn, String headingSource, String languageCode, boolean includeGroupingLabels) throws IOException {
    if (dataExportProcess == null || !dataExportProcess.getStatus().isRunning()) {
        resetJobs();
        SessionState sessionState = sessionManager.getSessionState();
        CollectSurvey survey = sessionState.getActiveSurvey();
        File outputFile = File.createTempFile("collect_data_export_" + survey.getName(), ".zip");
        Step step = Step.valueOf(stepNumber);
        // prepare record filter
        Schema schema = survey.getSchema();
        EntityDefinition rootEntityDefn = schema.getRootEntityDefinition(rootEntityName);
        RecordFilter recordFilter = createRecordFilter(survey, rootEntityDefn.getId(), onlyOwnedRecords, rootEntityKeyValues);
        // filter by record step
        recordFilter.setStepGreaterOrEqual(step);
        // instantiate process
        CSVDataExportProcess process = appContext.getBean(CSVDataExportProcess.class);
        process.setOutputFile(outputFile);
        process.setRecordFilter(recordFilter);
        process.setEntityId(entityId);
        process.setAlwaysGenerateZipFile(true);
        CSVDataExportParameters config = new CSVDataExportParameters();
        config.setIncludeAllAncestorAttributes(includeAllAncestorAttributes);
        config.setIncludeEnumeratedEntities(includeEnumeratedEntities);
        config.setIncludeCompositeAttributeMergedColumn(includeCompositeAttributeMergedColumn);
        config.setIncludeKMLColumnForCoordinates(includeKMLColumnForCoordinates);
        config.setCodeAttributeExpanded(codeAttributeExpanded);
        config.setIncludeCodeItemLabelColumn(includeCodeItemLabelColumn);
        config.setHeadingSource(HeadingSource.valueOf(headingSource));
        config.setLanguageCode(languageCode);
        config.setIncludeGroupingLabels(includeGroupingLabels);
        process.setConfiguration(config);
        process.init();
        // start process
        dataExportProcess = process;
        ExecutorServiceUtil.executeInCachedPool(process);
    }
    return getCurrentJob();
}
Also used : SessionState(org.openforis.collect.web.session.SessionState) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CSVDataExportProcess(org.openforis.collect.io.data.CSVDataExportProcess) Schema(org.openforis.idm.metamodel.Schema) Step(org.openforis.collect.model.CollectRecord.Step) CSVDataExportParameters(org.openforis.collect.io.data.csv.CSVDataExportParameters) CollectSurvey(org.openforis.collect.model.CollectSurvey) File(java.io.File) RecordFilter(org.openforis.collect.model.RecordFilter) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with CSVDataExportParameters

use of org.openforis.collect.io.data.csv.CSVDataExportParameters in project collect by openforis.

the class DataCSVReader method calculateIgnoredColumns.

private List<String> calculateIgnoredColumns() {
    final CSVDataExportParameters csvExportConfig = new CSVDataExportParameters();
    csvExportConfig.setIncludeCodeItemLabelColumn(true);
    csvExportConfig.setIncludeEnumeratedEntities(false);
    CollectSurvey survey = (CollectSurvey) parentEntityDefinition.getSurvey();
    ColumnProviderChain columnProvider = new CSVDataExportColumnProviderGenerator(survey, csvExportConfig).generateColumnProviderChain(parentEntityDefinition);
    List<String> result = new ArrayList<String>();
    List<String> colNames = getColumnNames();
    for (final String colName : colNames) {
        final MutableBoolean ignored = new MutableBoolean(false);
        columnProvider.traverseProviders(new Visitor<ColumnProvider>() {

            public void visit(ColumnProvider p) {
                if (!(p instanceof ColumnProviderChain) && p instanceof BasicColumnProvider) {
                    List<String> finalColumnHeadings = ((BasicColumnProvider) p).generateFinalColumnHeadings();
                    if (finalColumnHeadings.contains(colName)) {
                        if (p instanceof CodeColumnProvider && colName.endsWith(csvExportConfig.getFieldHeadingSeparator() + CodeColumnProvider.ITEM_LABEL_SUFFIX)) {
                            ignored.setTrue();
                        }
                    }
                }
            }
        });
        if (ignored.booleanValue()) {
            result.add(colName);
        }
    }
    return result;
}
Also used : ColumnProviderChain(org.openforis.collect.io.data.csv.ColumnProviderChain) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) ArrayList(java.util.ArrayList) CSVDataExportParameters(org.openforis.collect.io.data.csv.CSVDataExportParameters) CodeColumnProvider(org.openforis.collect.io.data.csv.CodeColumnProvider) BasicColumnProvider(org.openforis.collect.io.data.csv.BasicColumnProvider) CodeColumnProvider(org.openforis.collect.io.data.csv.CodeColumnProvider) ColumnProvider(org.openforis.collect.io.data.csv.ColumnProvider) BasicColumnProvider(org.openforis.collect.io.data.csv.BasicColumnProvider) ArrayList(java.util.ArrayList) List(java.util.List) CollectSurvey(org.openforis.collect.model.CollectSurvey)

Example 3 with CSVDataExportParameters

use of org.openforis.collect.io.data.csv.CSVDataExportParameters 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 4 with CSVDataExportParameters

use of org.openforis.collect.io.data.csv.CSVDataExportParameters 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 5 with CSVDataExportParameters

use of org.openforis.collect.io.data.csv.CSVDataExportParameters 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)

Aggregations

CSVDataExportParameters (org.openforis.collect.io.data.csv.CSVDataExportParameters)7 CSVDataExportJob (org.openforis.collect.io.data.CSVDataExportJob)5 CollectSurvey (org.openforis.collect.model.CollectSurvey)5 RecordFilter (org.openforis.collect.model.RecordFilter)5 File (java.io.File)4 Date (java.util.Date)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 FileInputStream (java.io.FileInputStream)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 Command (org.zkoss.bind.annotation.Command)2 GlobalCommand (org.zkoss.bind.annotation.GlobalCommand)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)1 DataQuery (org.openforis.collect.datacleansing.DataQuery)1 CSVDataExportProcess (org.openforis.collect.io.data.CSVDataExportProcess)1 DescendantNodeFilter (org.openforis.collect.io.data.DescendantNodeFilter)1 BasicColumnProvider (org.openforis.collect.io.data.csv.BasicColumnProvider)1