Skip to content

Commit

Permalink
Merge pull request #13166 from SORMAS-Foundation/change-#13159-automa…
Browse files Browse the repository at this point in the history
…tic-soft-delete-negative-covid-tests-and-samples

Change #13159 automatic soft delete negative covid tests and samples
  • Loading branch information
leventegal-she authored Oct 21, 2024
2 parents 13b5a4b + f22a66b commit 6dda78d
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,6 @@ public interface ConfigFacade {
CaseClassificationCalculationMode getCaseClassificationCalculationMode(Disease disease);

boolean isAnyCaseClassificationCalculationEnabled();

Integer getNegaiveCovidSamplesMaxAgeDays();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public SampleReferenceDto getSample() {
public void setSample(SampleReferenceDto sample) {
this.sample = sample;
}

public PathogenTestCriteria sample(SampleReferenceDto sample) {
this.sample = sample;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public class ConfigFacadeEjb implements ConfigFacade {
public static final int DEFAULT_DOCUMENT_UPLOAD_SIZE_LIMIT_MB = 20;
public static final String IMPORT_FILE_SIZE_LIMIT_MB = "importFileSizeLimitMb";
public static final int DEFAULT_IMPOR_FILE_SIZE_LIMIT_MB = 20;
public static final String NEGAIVE_COVID_SAMPLES_MAX_AGE_DAYS = "negaiveCovidSamplesMaxAgeDays";

private final Logger logger = LoggerFactory.getLogger(getClass());

Expand Down Expand Up @@ -838,6 +839,11 @@ public void resetRequestContext() {
RequestContextHolder.reset();
}

@Override
public Integer getNegaiveCovidSamplesMaxAgeDays() {
return parseProperty(NEGAIVE_COVID_SAMPLES_MAX_AGE_DAYS, null, Integer::parseInt);
}

@LocalBean
@Stateless
public static class ConfigFacadeEjbLocal extends ConfigFacadeEjb {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import de.symeda.sormas.backend.immunization.ImmunizationFacadeEjb;
import de.symeda.sormas.backend.infrastructure.central.CentralInfraSyncFacade;
import de.symeda.sormas.backend.report.WeeklyReportFacadeEjb.WeeklyReportFacadeEjbLocal;
import de.symeda.sormas.backend.sample.SampleService;
import de.symeda.sormas.backend.specialcaseaccess.SpecialCaseAccessFacadeEjb.SpecialCaseAccessFacadeEjbLocal;
import de.symeda.sormas.backend.systemevent.SystemEventFacadeEjb.SystemEventFacadeEjbLocal;
import de.symeda.sormas.backend.task.TaskFacadeEjb.TaskFacadeEjbLocal;
Expand Down Expand Up @@ -96,6 +97,8 @@ public class CronService {
private SpecialCaseAccessFacadeEjbLocal specialCaseAccessFacade;
@EJB
private UserFacadeEjbLocal userFacade;
@EJB
private SampleService sampleService;

@Schedule(hour = "*", minute = "*/" + TASK_UPDATE_INTERVAL, second = "0", persistent = false)
public void sendNewAndDueTaskMessages() {
Expand Down Expand Up @@ -297,4 +300,9 @@ public void syncUsersFromAuthenticationProvider() {
userFacade.syncUsersFromAuthenticationProvider();
}
}

@Schedule(hour = "2", minute = "40", persistent = false)
public void sofDeleteOldNegativeSamples() {
sampleService.cleanupOldCovidSamples();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ public void setTestTypeText(String testTypeText) {
}

@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
public Date getTestDateTime() {
return testDateTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,23 @@

import org.apache.commons.collections4.CollectionUtils;

import de.symeda.sormas.api.Disease;
import de.symeda.sormas.api.EntityRelevanceStatus;
import de.symeda.sormas.api.RequestContextHolder;
import de.symeda.sormas.api.caze.IsCase;
import de.symeda.sormas.api.common.DeletableEntityType;
import de.symeda.sormas.api.common.DeletionDetails;
import de.symeda.sormas.api.common.DeletionReason;
import de.symeda.sormas.api.common.progress.ProcessedEntity;
import de.symeda.sormas.api.common.progress.ProcessedEntityStatus;
import de.symeda.sormas.api.contact.ContactReferenceDto;
import de.symeda.sormas.api.disease.DiseaseVariant;
import de.symeda.sormas.api.event.EventParticipantReferenceDto;
import de.symeda.sormas.api.feature.FeatureType;
import de.symeda.sormas.api.i18n.I18nProperties;
import de.symeda.sormas.api.i18n.Strings;
import de.symeda.sormas.api.sample.IsSample;
import de.symeda.sormas.api.sample.PathogenTestCriteria;
import de.symeda.sormas.api.sample.PathogenTestResultType;
import de.symeda.sormas.api.sample.PathogenTestType;
import de.symeda.sormas.api.sample.SampleAssociationType;
Expand All @@ -87,6 +92,7 @@
import de.symeda.sormas.backend.caze.CaseService;
import de.symeda.sormas.backend.common.AbstractDeletableAdoService;
import de.symeda.sormas.backend.common.AbstractDomainObject;
import de.symeda.sormas.backend.common.ConfigFacadeEjb.ConfigFacadeEjbLocal;
import de.symeda.sormas.backend.common.CriteriaBuilderHelper;
import de.symeda.sormas.backend.common.DeletableAdo;
import de.symeda.sormas.backend.common.JurisdictionFlagsService;
Expand Down Expand Up @@ -145,6 +151,8 @@ public class SampleService extends AbstractDeletableAdoService<Sample>
protected FeatureConfigurationFacadeEjbLocal featureConfigurationFacade;
@EJB
private SpecialCaseAccessService specialCaseAccessService;
@EJB
private ConfigFacadeEjbLocal configFacade;

public SampleService() {
super(Sample.class, DeletableEntityType.SAMPLE);
Expand Down Expand Up @@ -1276,4 +1284,37 @@ public List<DiseaseVariant> getAssociatedDiseaseVariants(String sampleUuid) {
cq.select(pathogenTestJoin.get(PathogenTest.TESTED_DISEASE_VARIANT));
return em.createQuery(cq).getResultList();
}

public void cleanupOldCovidSamples() {
final Integer maxAgeDays = configFacade.getNegaiveCovidSamplesMaxAgeDays();
if (maxAgeDays == null) {
return;
}

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<PathogenTest> cq = cb.createQuery(PathogenTest.class);
Root<PathogenTest> from = cq.from(PathogenTest.class);

cq.where(
cb.equal(from.get(PathogenTest.TESTED_DISEASE), Disease.CORONAVIRUS),
cb.equal(from.get(PathogenTest.TEST_RESULT), PathogenTestResultType.NEGATIVE),
cb.notEqual(from.get(PathogenTest.DELETED), true),
cb.lessThan(
CriteriaBuilderHelper.coalesce(
cb,
Date.class,
from.get(PathogenTest.TEST_DATE_TIME),
from.get(PathogenTest.REPORT_DATE),
from.get(PathogenTest.CREATION_DATE)),
DateHelper.subtractDays(new Date(), maxAgeDays)));
em.createQuery(cq).getResultList().stream().collect(Collectors.groupingBy(PathogenTest::getSample)).forEach((sample, tests) -> {
if (pathogenTestService.count(new PathogenTestCriteria().sample(sample.toReference())) == tests.size()) {
delete(sample, new DeletionDetails(DeletionReason.OTHER_REASON, I18nProperties.getString(Strings.entityAutomaticSoftDeletion)));
} else {
tests.forEach(
p -> pathogenTestService
.delete(p, new DeletionDetails(DeletionReason.OTHER_REASON, I18nProperties.getString(Strings.entityAutomaticSoftDeletion))));
}
});
}
}
Loading

0 comments on commit 6dda78d

Please sign in to comment.