Skip to content

Commit

Permalink
#9666 - contact and visit delete permanent - test and query update
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiupacurariu committed Oct 13, 2022
1 parent 458c4ed commit 223cedf
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void syncInfraWithCentral() {
centralInfraSyncFacade.syncAll();
}

@Schedule(hour = "1", minute = "55", persistent = false)
@Schedule(hour = "*", minute = "*/2", persistent = false)
public void deleteExpiredEntities() {
coreEntityDeletionService.executeAutomaticDeletion();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1506,8 +1506,15 @@ public void deletePermanent(Contact contact) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaDelete<Visit> cd = cb.createCriteriaDelete(Visit.class);
Root<Visit> visitRoot = cd.from(Visit.class);
Subquery<Long> visitContactSubquery = getVisitsContactSubqueryForDelete(cb, cd, visitRoot);
cd.where(cb.and(cb.isNull(visitRoot.get(Visit.CAZE).get(Case.ID)), cb.lessThanOrEqualTo(visitContactSubquery, 1L)));
Subquery<Long> visitContactSubquery = countVisitsContactSubqueryForDelete(cb, cd, visitRoot, contact);

Subquery<Visit> getVisitContactSubquery = getVisitsContactSubqueryForDelete(cb, cd, contact);

cd.where(
cb.and(
// cb.isNull(visitRoot.get(Visit.CAZE).get(Case.ID)),
// cb.lessThanOrEqualTo(visitContactSubquery, 1L),
visitRoot.in(getVisitContactSubquery)));
em.createQuery(cd).executeUpdate();

// Remove the contact that will be deleted from contact_visits
Expand All @@ -1533,17 +1540,28 @@ public void deletePermanent(Contact contact) {
super.deletePermanent(contact);
}

private Subquery<Long> getVisitsContactSubqueryForDelete(CriteriaBuilder cb, CriteriaDelete<Visit> cd, Root<Visit> visitRoot) {
private Subquery<Long> countVisitsContactSubqueryForDelete(CriteriaBuilder cb, CriteriaDelete<Visit> cd, Root<Visit> visitRoot, Contact contact) {
Subquery<Long> contactVisitsSubquery = cd.subquery(Long.class);
Root<Visit> subqueryRoot = contactVisitsSubquery.from(Visit.class);
Join<Visit, Contact> visitContactJoin = subqueryRoot.join(Visit.CONTACTS, JoinType.INNER);
contactVisitsSubquery.where(cb.equal(subqueryRoot.get(Visit.ID), visitRoot.get(Visit.ID)));
contactVisitsSubquery.where(
cb.and(cb.equal(subqueryRoot.get(Visit.ID), visitRoot.get(Visit.ID)), cb.equal(visitContactJoin.get(Contact.ID), contact.getId())));

contactVisitsSubquery.select(cb.count(visitContactJoin.get(Contact.ID)));

return contactVisitsSubquery;
}

private Subquery<Visit> getVisitsContactSubqueryForDelete(CriteriaBuilder cb, CriteriaDelete<Visit> cd, Contact contact) {
Subquery<Visit> contactVisitsSubquery = cd.subquery(Visit.class);
Root<Visit> subqueryRoot = contactVisitsSubquery.from(Visit.class);
contactVisitsSubquery.where(cb.equal(subqueryRoot.join(Visit.CONTACTS).get(Contact.ID), contact.getId()));

contactVisitsSubquery.select(subqueryRoot);

return contactVisitsSubquery;
}

private void deleteContactLinks(Contact contact) {
// Remove the contact from any sample that is also connected to other entities
contact.getSamples().stream().filter(s -> s.getAssociatedCase() != null || s.getAssociatedEventParticipant() != null).forEach(s -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.validation.ConstraintViolationException;

import de.symeda.sormas.api.followup.FollowUpLogic;
import de.symeda.sormas.backend.visit.Visit;
import org.apache.commons.lang3.time.DateUtils;
import org.hibernate.internal.SessionImpl;
import org.hibernate.query.spi.NativeQueryImplementor;
import org.hibernate.query.spi.QueryImplementor;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -515,37 +519,96 @@ public void testContactPermanentDeletion() throws IOException {

ContactDto contactDto = creator.createContact(user.toReference(), person.toReference(), Disease.CORONAVIRUS);

TaskDto taskDto = creator
.createTask(TaskContext.CONTACT, TaskType.CONTACT_FOLLOW_UP, TaskStatus.PENDING, null, contactDto.toReference(), null, new Date(), null);
final Date beyondRelevanceDate = DateUtils.addDays(new Date(), (-90));
contactDto.setReportDateTime(beyondRelevanceDate);
getContactFacade().save(contactDto);

SampleDto sample = creator.createSample(
contactDto.toReference(),
user.toReference(),
rdcf.facility,
sampleDto -> sampleDto.setAssociatedContact(contactDto.toReference()));
// TaskDto taskDto = creator
// .createTask(TaskContext.CONTACT, TaskType.CONTACT_FOLLOW_UP, TaskStatus.PENDING, null, contactDto.toReference(), null, new Date(), null);

// SampleDto sample = creator.createSample(
// contactDto.toReference(),
// user.toReference(),
// rdcf.facility,
// sampleDto -> sampleDto.setAssociatedContact(contactDto.toReference()));

VisitDto visitDto = creator.createVisit(contactDto.getDisease(), contactDto.getPerson(), contactDto.getReportDateTime());

assertEquals(1, getContactService().count());
assertEquals(1, getTaskFacade().getAllByContact(contactDto.toReference()).size());
assertEquals(1, getSampleService().count());
assertEquals(1, getVisitService().count());
ContactDto contactDto2 = creator.createContact(user.toReference(), person.toReference(), Disease.CORONAVIRUS);

// TaskDto taskDto2 = creator
// .createTask(TaskContext.CONTACT, TaskType.CONTACT_FOLLOW_UP, TaskStatus.PENDING, null, contactDto2.toReference(), null, new Date(), null);
//
// SampleDto sample2 = creator.createSample(
// contactDto2.toReference(),
// user.toReference(),
// rdcf.facility,
// sampleDto -> sampleDto.setAssociatedContact(contactDto2.toReference()));

VisitDto visitDto2 = creator.createVisit(contactDto2.getDisease(), contactDto2.getPerson(), contactDto2.getReportDateTime());

assertEquals(2, getContactService().count());
// assertEquals(1, getTaskFacade().getAllByContact(contactDto.toReference()).size());
// assertEquals(1, getTaskFacade().getAllByContact(contactDto2.toReference()).size());
// assertEquals(2, getSampleService().count());
assertEquals(2, getVisitService().count());

final Date tenYearsPlusAgo = DateUtils.addDays(new Date(), (-1) * coreEntityTypeConfig.deletionPeriod - 1);
SessionImpl em = (SessionImpl) getEntityManager();
NativeQueryImplementor select_from_contacts_visits = em.createNativeQuery("select from contacts_visits");
List contacVisist1 = select_from_contacts_visits.getResultList();


QueryImplementor query3 = em.createQuery("select i from contact i where i.uuid=:uuid");
query3.setParameter("uuid", contactDto2.getUuid());
Contact singleResult3 = (Contact) query3.getSingleResult();


final Date tenYearsPlusAgo = DateUtils.addDays(new Date(), (-1) * coreEntityTypeConfig.deletionPeriod - 1);
QueryImplementor query = em.createQuery("select i from contact i where i.uuid=:uuid");
query.setParameter("uuid", contactDto.getUuid());
Contact singleResult = (Contact) query.getSingleResult();
singleResult.setCreationDate(new Timestamp(tenYearsPlusAgo.getTime()));
singleResult.setChangeDate(new Timestamp(tenYearsPlusAgo.getTime()));
em.save(singleResult);

NativeQueryImplementor selectForDelete = em.createNativeQuery("select uuid\n" +
"from Visit\n" +
"where (caze_id is null)\n" +
" and (id in (select visit1_.id\n" +
" from Visit visit1_\n" +
" inner join contacts_visits contacts2_ on visit1_.id = contacts2_.visit_id\n" +
" inner join contact contact3_ on contacts2_.contact_id = contact3_.id\n" +
" where contact3_.id=:uuid))");
selectForDelete.setParameter("uuid", singleResult.getId());
List<String> selectForDeleteResultList = selectForDelete.getResultList();

useSystemUser();
getCoreEntityDeletionService().executeAutomaticDeletion();
loginWith(user);

NativeQueryImplementor select_from_contacts_visits2 = em.createNativeQuery("select from contacts_visits");
List contacVisist2 = select_from_contacts_visits2.getResultList();

assertEquals(1, getContactService().count());
// assertEquals(1, getTaskFacade().getAllByContact(contactDto2.toReference()).size());
// assertEquals(1, getSampleService().count());
assertEquals(1, getVisitService().count());

final Date tenYearsPlusAgoSecondContact = DateUtils.addDays(new Date(), (-1) * coreEntityTypeConfig.deletionPeriod - 1);
SessionImpl emSecondContact = (SessionImpl) getEntityManager();
QueryImplementor query2 = emSecondContact.createQuery("select i from contact i where i.uuid=:uuid");
query2.setParameter("uuid", contactDto2.getUuid());
Contact singleResult2 = (Contact) query2.getSingleResult();
singleResult2.setCreationDate(new Timestamp(tenYearsPlusAgoSecondContact.getTime()));
singleResult2.setChangeDate(new Timestamp(tenYearsPlusAgoSecondContact.getTime()));
emSecondContact.save(singleResult2);

useSystemUser();
getCoreEntityDeletionService().executeAutomaticDeletion();
loginWith(user);

assertEquals(0, getContactService().count());
assertEquals(0, getTaskFacade().getAllByContact(contactDto.toReference()).size());
assertEquals(0, getTaskFacade().getAllByContact(contactDto2.toReference()).size());
assertEquals(0, getSampleService().count());
assertEquals(0, getVisitService().count());
}
Expand Down

0 comments on commit 223cedf

Please sign in to comment.