Skip to content

Commit

Permalink
fixed xml data export using filter expression (#185)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Sep 9, 2024
1 parent 9a42b9f commit e1116fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.0.96] - 2024-09-09
### Fixed
- XML data export: fixed filtering using expression;

## [4.0.95] - 2024-09-08
### Added
- XML data export (Collect format): show records count before generation; added filter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.openforis.collect.io.SurveyBackupJob;
import org.openforis.collect.io.data.BackupDataExtractor.BackupRecordEntry;
import org.openforis.collect.manager.RecordManager;
Expand All @@ -29,6 +30,7 @@
import org.openforis.commons.io.OpenForisIOUtils;
import org.openforis.commons.io.csv.CsvWriter;
import org.openforis.concurrency.Task;
import org.openforis.idm.model.expression.ExpressionEvaluator;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -87,7 +89,7 @@ private void initializeDataSummaryCSVWriter()
}
summaryCSVWriter.writeHeaders(headers);
}

@Override
protected long countTotalItems() {
int count = 0;
Expand All @@ -103,7 +105,7 @@ protected long countTotalItems() {
}
return count;
}

@Override
protected void execute() throws Throwable {
List<CollectRecordSummary> recordSummaries = recordManager.loadSummaries(recordFilter);
Expand All @@ -113,7 +115,6 @@ protected void execute() throws Throwable {
if ( isRunning() ) {
if ( step.getStepNumber() <= summary.getStep().getStepNumber() ) {
backup(summary, step);
incrementProcessedItems();
}
} else {
break;
Expand All @@ -132,23 +133,36 @@ protected void onEnd() {
ZipFiles.writeFile(zipOutputStream, summaryTempFile, SurveyBackupJob.DATA_SUMMARY_ENTRY_NAME);
super.onEnd();
}

private boolean isFilterExpressionVerified(CollectRecord record) {
if (StringUtils.isBlank(recordFilter.getFilterExpression())) return true;
ExpressionEvaluator expressionEvaluator = record.getSurvey().getContext().getExpressionEvaluator();
try {
return expressionEvaluator.evaluateBoolean(record.getRootEntity(), null, recordFilter.getFilterExpression());
} catch (Exception e) {
return false;
}
}

private void backup(CollectRecordSummary summary, Step step) {
Integer id = summary.getId();
try {
CollectRecord record = recordManager.load(survey, id, step, false);
BackupRecordEntry recordEntry = new BackupRecordEntry(step, id);
ZipEntry entry = new ZipEntry(recordEntry.getName());
zipOutputStream.putNextEntry(entry);
OutputStreamWriter writer = new OutputStreamWriter(zipOutputStream);
dataMarshaller.write(record, writer);
zipOutputStream.closeEntry();
if (isFilterExpressionVerified(record)) {
BackupRecordEntry recordEntry = new BackupRecordEntry(step, id);
ZipEntry entry = new ZipEntry(recordEntry.getName());
zipOutputStream.putNextEntry(entry);
OutputStreamWriter writer = new OutputStreamWriter(zipOutputStream);
dataMarshaller.write(record, writer);
zipOutputStream.closeEntry();
}
} catch (Exception e) {
DataBackupError error = new DataBackupError(summary.getId(), summary.getRootEntityKeyValues(),
DataBackupError error = new DataBackupError(id, summary.getRootEntityKeyValues(),
summary.getStep(), e.getMessage());
errors.add(error);
logError(error.toString(), e);
}
incrementProcessedItems();
}

private void writeSummaryEntry(CollectRecordSummary summary) {
Expand Down

0 comments on commit e1116fb

Please sign in to comment.