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();
}
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;
}
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);
}
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);
}
}
}
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());
}
}
Aggregations