Skip to content

Commit

Permalink
fix: FK constraint error when delete Program with MapView (#19686)
Browse files Browse the repository at this point in the history
  • Loading branch information
vietnguyen authored Jan 16, 2025
1 parent 3a5ec37 commit f4c8d17
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
public interface MapViewStore extends AnalyticalObjectStore<MapView> {
List<MapView> getByOrganisationUnitGroupSet(OrganisationUnitGroupSet groupSet);

List<MapView> findByProgram(Program program);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +92,8 @@ public interface MappingService extends AnalyticalObjectService<MapView> {

int countMapViewMaps(MapView mapView);

List<MapView> findByProgram(Program program);

// -------------------------------------------------------------------------
// ExternalMapLayer
// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -185,6 +186,11 @@ public int countMapViewMaps(MapView mapView) {
return mapStore.countMapViewMaps(mapView);
}

@Override
public List<MapView> findByProgram(Program program) {
return mapViewStore.findByProgram(program);
}

// -------------------------------------------------------------------------
// ExternalMapLayer
// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -83,6 +86,21 @@ private void deleteLegendSet(LegendSet legendSet) {
}
}

private void deleteProgram(Program program) {
List<MapView> 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<MapView> mapViews = service.getMapViewsByOrganisationUnitGroupSet(groupSet);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,6 +46,7 @@
@Repository("org.hisp.dhis.mapping.MapViewStore")
public class HibernateMapViewStore extends HibernateAnalyticalObjectStore<MapView>
implements MapViewStore {

public HibernateMapViewStore(
EntityManager entityManager,
JdbcTemplate jdbcTemplate,
Expand All @@ -62,4 +64,12 @@ public List<MapView> getByOrganisationUnitGroupSet(OrganisationUnitGroupSet grou
newJpaParameters()
.addPredicate(root -> builder.equal(root.get("organisationUnitGroupSet"), groupSet)));
}

@Override
public List<MapView> findByProgram(Program program) {
CriteriaBuilder builder = getCriteriaBuilder();
return getList(
builder,
newJpaParameters().addPredicate(root -> builder.equal(root.get("program"), program)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
*/
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;
import static org.junit.jupiter.api.Assertions.assertTrue;

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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit f4c8d17

Please sign in to comment.