Skip to content

Commit

Permalink
fix: remove programStage fk when delete mapview
Browse files Browse the repository at this point in the history
  • Loading branch information
vietnguyen committed Jan 23, 2025
1 parent a8ed9ff commit 18db51c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.hisp.dhis.common.AnalyticalObjectStore;
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;

/**
* @author Morten Olav Hansen <[email protected]>
Expand All @@ -39,4 +40,6 @@ public interface MapViewStore extends AnalyticalObjectStore<MapView> {
List<MapView> getByOrganisationUnitGroupSet(OrganisationUnitGroupSet groupSet);

List<MapView> findByProgram(Program program);

List<MapView> findByProgramStage(ProgramStage programStage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.hisp.dhis.common.AnalyticalObjectService;
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;

/**
* @author Jan Henrik Overland
Expand Down Expand Up @@ -94,6 +95,8 @@ public interface MappingService extends AnalyticalObjectService<MapView> {

List<MapView> findByProgram(Program program);

List<MapView> findByProgramStage(ProgramStage programStage);

// -------------------------------------------------------------------------
// ExternalMapLayer
// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.hisp.dhis.period.PeriodService;
import org.hisp.dhis.period.RelativePeriods;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -192,6 +193,12 @@ public List<MapView> findByProgram(Program program) {
return mapViewStore.findByProgram(program);
}

@Override
@Transactional(readOnly = true)
public List<MapView> findByProgramStage(ProgramStage programStage) {
return mapViewStore.findByProgramStage(programStage);
}

// -------------------------------------------------------------------------
// ExternalMapLayer
// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

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 @@ -44,6 +43,7 @@
import org.hisp.dhis.period.Period;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramIndicator;
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.system.deletion.DeletionVeto;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -75,6 +75,7 @@ protected void registerHandler() {
whenDeleting(ExpressionDimensionItem.class, this::deleteExpressionDimensionItem);
whenVetoing(MapView.class, this::allowDeleteMapView);
whenDeleting(Program.class, this::deleteProgram);
whenDeleting(ProgramStage.class, this::deleteProgramStage);
}

private void deleteLegendSet(LegendSet legendSet) {
Expand All @@ -86,16 +87,18 @@ private void deleteLegendSet(LegendSet legendSet) {
}
}

private void deleteProgramStage(ProgramStage programStage) {
List<MapView> mapViews = service.findByProgramStage(programStage);

for (MapView mapView : mapViews) {
mapView.setProgramStage(null);
service.update(mapView);
}
}

private void deleteProgram(Program program) {
List<MapView> mapViews = service.findByProgram(program);
for (MapView mapView : mapViews) {
if (mapView.getProgramStage() != null
&& program.getProgramStages().stream()
.map(IdentifiableObject::getUid)
.toList()
.contains(mapView.getProgramStage().getUid())) {
mapView.setProgramStage(null);
}
mapView.setProgram(null);
service.update(mapView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.hisp.dhis.mapping.MapViewStore;
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.security.acl.AclService;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.jdbc.core.JdbcTemplate;
Expand Down Expand Up @@ -72,4 +73,13 @@ public List<MapView> findByProgram(Program program) {
builder,
newJpaParameters().addPredicate(root -> builder.equal(root.get("program"), program)));
}

@Override
public List<MapView> findByProgramStage(ProgramStage programStage) {
CriteriaBuilder builder = getCriteriaBuilder();
return getList(
builder,
newJpaParameters()
.addPredicate(root -> builder.equal(root.get("programStage"), programStage)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
import org.hisp.dhis.user.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -368,8 +367,7 @@ void testCopyProgramWithNoPublicSharing() {
assertStatus(HttpStatus.NOT_FOUND, POST("/programs/%s/copy".formatted(PROGRAM_UID)));
}

@Disabled(
"Throws error only on jenkins: Referential integrity constraint violation: fk_mapview_programstageid")
@Test
void testDeleteWithMapView() {
String mapViewJson =
"""
Expand Down

0 comments on commit 18db51c

Please sign in to comment.