diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MapViewStore.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MapViewStore.java index 037cbb7aab9d..5e1f45e3b2b5 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MapViewStore.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MapViewStore.java @@ -30,10 +30,13 @@ import java.util.List; import org.hisp.dhis.common.AnalyticalObjectStore; import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet; +import org.hisp.dhis.program.Program; /** * @author Morten Olav Hansen */ public interface MapViewStore extends AnalyticalObjectStore { List getByOrganisationUnitGroupSet(OrganisationUnitGroupSet groupSet); + + List findByProgram(Program program); } diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingService.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingService.java index 426add4add6e..62104a5743c3 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingService.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingService.java @@ -30,6 +30,7 @@ import java.util.List; import org.hisp.dhis.common.AnalyticalObjectService; import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet; +import org.hisp.dhis.program.Program; /** * @author Jan Henrik Overland @@ -91,6 +92,8 @@ public interface MappingService extends AnalyticalObjectService { int countMapViewMaps(MapView mapView); + List findByProgram(Program program); + // ------------------------------------------------------------------------- // ExternalMapLayer // ------------------------------------------------------------------------- diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java index 7db899cc3207..63ce13bb6ae1 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java @@ -39,6 +39,7 @@ import org.hisp.dhis.period.Period; import org.hisp.dhis.period.PeriodService; import org.hisp.dhis.period.RelativePeriods; +import org.hisp.dhis.program.Program; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -185,6 +186,11 @@ public int countMapViewMaps(MapView mapView) { return mapStore.countMapViewMaps(mapView); } + @Override + public List findByProgram(Program program) { + return mapViewStore.findByProgram(program); + } + // ------------------------------------------------------------------------- // ExternalMapLayer // ------------------------------------------------------------------------- diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/MapViewDeletionHandler.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/MapViewDeletionHandler.java index 7033801ff531..a57338d0eb12 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/MapViewDeletionHandler.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/MapViewDeletionHandler.java @@ -32,6 +32,7 @@ import java.util.List; import org.hisp.dhis.common.GenericAnalyticalObjectDeletionHandler; +import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.dataset.DataSet; import org.hisp.dhis.expressiondimensionitem.ExpressionDimensionItem; @@ -41,6 +42,7 @@ import org.hisp.dhis.organisationunit.OrganisationUnitGroup; import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet; import org.hisp.dhis.period.Period; +import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramIndicator; import org.hisp.dhis.system.deletion.DeletionVeto; import org.springframework.stereotype.Component; @@ -72,6 +74,7 @@ protected void registerHandler() { whenDeleting(OrganisationUnitGroupSet.class, this::deleteOrganisationUnitGroupSetSpecial); whenDeleting(ExpressionDimensionItem.class, this::deleteExpressionDimensionItem); whenVetoing(MapView.class, this::allowDeleteMapView); + whenDeleting(Program.class, this::deleteProgram); } private void deleteLegendSet(LegendSet legendSet) { @@ -83,6 +86,21 @@ private void deleteLegendSet(LegendSet legendSet) { } } + private void deleteProgram(Program program) { + List mapViews = service.findByProgram(program); + for (MapView mapView : mapViews) { + mapView.setProgram(null); + if (mapView.getProgramStage() != null + && program.getProgramStages().stream() + .map(IdentifiableObject::getUid) + .toList() + .contains(mapView.getProgramStage().getUid())) { + mapView.setProgramStage(null); + service.update(mapView); + } + } + } + public void deleteOrganisationUnitGroupSetSpecial(OrganisationUnitGroupSet groupSet) { List mapViews = service.getMapViewsByOrganisationUnitGroupSet(groupSet); diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/hibernate/HibernateMapViewStore.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/hibernate/HibernateMapViewStore.java index 686969428a61..0ea3e36cec08 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/hibernate/HibernateMapViewStore.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/hibernate/HibernateMapViewStore.java @@ -34,6 +34,7 @@ import org.hisp.dhis.mapping.MapView; import org.hisp.dhis.mapping.MapViewStore; import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet; +import org.hisp.dhis.program.Program; import org.hisp.dhis.security.acl.AclService; import org.springframework.context.ApplicationEventPublisher; import org.springframework.jdbc.core.JdbcTemplate; @@ -45,6 +46,7 @@ @Repository("org.hisp.dhis.mapping.MapViewStore") public class HibernateMapViewStore extends HibernateAnalyticalObjectStore implements MapViewStore { + public HibernateMapViewStore( EntityManager entityManager, JdbcTemplate jdbcTemplate, @@ -62,4 +64,12 @@ public List getByOrganisationUnitGroupSet(OrganisationUnitGroupSet grou newJpaParameters() .addPredicate(root -> builder.equal(root.get("organisationUnitGroupSet"), groupSet))); } + + @Override + public List findByProgram(Program program) { + CriteriaBuilder builder = getCriteriaBuilder(); + return getList( + builder, + newJpaParameters().addPredicate(root -> builder.equal(root.get("program"), program))); + } } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/program/ProgramServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/program/ProgramServiceTest.java index 235e222d0220..c11d1fa5c11d 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/program/ProgramServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/program/ProgramServiceTest.java @@ -27,6 +27,7 @@ */ package org.hisp.dhis.program; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -34,6 +35,7 @@ import java.util.HashSet; import java.util.List; +import org.hisp.dhis.mapping.MapView; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.organisationunit.OrganisationUnitService; import org.hisp.dhis.test.integration.PostgresIntegrationTestBase; @@ -148,4 +150,20 @@ void testProgramHasOrgUnit() { OrganisationUnit ou = organisationUnitService.getOrganisationUnit(organisationUnitA.getUid()); assertTrue(programService.hasOrgUnit(p, ou)); } + + @Test + void testDeleteProgramWithMapView() { + entityManager.persist(programA); + ProgramStage programStageA = createProgramStage('A', programA); + entityManager.persist(programStageA); + programA.getProgramStages().add(programStageA); + MapView mapView = createMapView("Test"); + mapView.setProgram(programA); + mapView.setProgramStage(programStageA); + entityManager.persist(mapView); + + assertDoesNotThrow(() -> programService.deleteProgram(programA)); + assertNull(mapView.getProgram()); + assertNull(mapView.getProgramStage()); + } }