diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/feedback/ErrorCode.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/feedback/ErrorCode.java index 23a0c14c9f10..1c1cf44d0afe 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/feedback/ErrorCode.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/feedback/ErrorCode.java @@ -67,10 +67,7 @@ public enum ErrorCode { E1120("Update cannot be applied as it would make existing data values inaccessible"), E1121("Data element `{0}` value type cannot be changed as it has associated data values"), - E1122("Category option combo {0} already exists for category combo {1}"), - E1123("Category option combo {0} must be associated with a category combo"), - E1124("Category option combo {0} cannot be associated with the default category combo"), - + E1122("Category option combo {0} cannot be associated with the default category combo"), E1125("Category option combo {0} contains options not associated with category combo {1}"), /* Org unit merge */ E1500("At least two source orgs unit must be specified"), diff --git a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/objectbundle/hooks/CategoryOptionComboObjectBundleHook.java b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/objectbundle/hooks/CategoryOptionComboObjectBundleHook.java index ed95588fc917..8347f96b4e88 100644 --- a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/objectbundle/hooks/CategoryOptionComboObjectBundleHook.java +++ b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/objectbundle/hooks/CategoryOptionComboObjectBundleHook.java @@ -27,13 +27,9 @@ */ package org.hisp.dhis.dxf2.metadata.objectbundle.hooks; -import java.util.List; -import java.util.Set; import java.util.function.Consumer; -import java.util.stream.Collectors; import lombok.AllArgsConstructor; import org.hisp.dhis.category.CategoryCombo; -import org.hisp.dhis.category.CategoryOption; import org.hisp.dhis.category.CategoryOptionCombo; import org.hisp.dhis.category.CategoryService; import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle; @@ -47,70 +43,6 @@ public class CategoryOptionComboObjectBundleHook extends AbstractObjectBundleHook { private final CategoryService categoryService; - static boolean haveEqualCatComboCatOptionReferenceIds( - CategoryOptionCombo one, CategoryOptionCombo other) { - if (one == null || other == null) { - return false; - } - - if (one.getCategoryCombo() == null || other.getCategoryCombo() == null) { - return false; - } - - if (one.getCategoryOptions() == null || other.getCategoryOptions() == null) { - return false; - } - - if (!one.getCategoryCombo().getUid().equals(other.getCategoryCombo().getUid())) { - return false; - } - - Set oneCategoryOptionUids = - one.getCategoryOptions().stream().map(CategoryOption::getUid).collect(Collectors.toSet()); - - Set otherCategoryOptionUids = - other.getCategoryOptions().stream().map(CategoryOption::getUid).collect(Collectors.toSet()); - - return oneCategoryOptionUids.equals(otherCategoryOptionUids); - } - - private void checkDuplicateCategoryOptionCombos( - CategoryOptionCombo categoryOptionCombo, - ObjectBundle bundle, - Consumer addReports) { - - if (bundle.isPersisted(categoryOptionCombo)) { - return; // Only check for duplicates if the object is not persisted - } - - List categoryOptionCombos = - categoryService.getAllCategoryOptionCombos().stream() - .filter( - coc -> - coc.getCategoryCombo() - .getUid() - .equals(categoryOptionCombo.getCategoryCombo().getUid())) - .toList(); - - // This could be an update or re-import of the same object. - if (categoryOptionCombos.stream() - .anyMatch(coc -> coc.getUid().equals(categoryOptionCombo.getUid()))) { - return; - } - // Check to see if the COC already exists in the list of COCs by comparing reference ids - for (CategoryOptionCombo existingCategoryOptionCombo : categoryOptionCombos) { - if (haveEqualCatComboCatOptionReferenceIds(categoryOptionCombo, existingCategoryOptionCombo) - && !categoryOptionCombo.getUid().equals(existingCategoryOptionCombo.getUid())) { - addReports.accept( - new ErrorReport( - CategoryOptionCombo.class, - ErrorCode.E1122, - categoryOptionCombo.getName(), - existingCategoryOptionCombo.getName())); - } - } - } - private void checkNonStandardDefaultCatOptionCombo( CategoryOptionCombo categoryOptionCombo, Consumer addReports) { @@ -125,7 +57,7 @@ private void checkNonStandardDefaultCatOptionCombo( if (!categoryOptionCombo.getUid().equals(defaultCatOptionCombo.getUid())) { addReports.accept( new ErrorReport( - CategoryOptionCombo.class, ErrorCode.E1124, categoryOptionCombo.getName())); + CategoryOptionCombo.class, ErrorCode.E1122, categoryOptionCombo.getName())); } } @@ -136,6 +68,5 @@ public void validate( Consumer addReports) { checkNonStandardDefaultCatOptionCombo(categoryOptionCombo, addReports); - checkDuplicateCategoryOptionCombos(categoryOptionCombo, bundle, addReports); } } diff --git a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/CategoryOptionComboControllerTest.java b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/CategoryOptionComboControllerTest.java index 32c6fd53a882..0268fe8f443e 100644 --- a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/CategoryOptionComboControllerTest.java +++ b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/CategoryOptionComboControllerTest.java @@ -121,35 +121,6 @@ void catOptionCombosExcludingDefaultTest() { catOptionComboNames.contains("default"), "default catOptionCombo is not in payload"); } - @Test - @DisplayName("Duplicate CategoryOptionCombos should not be allowed") - void catOptionCombosDuplicatedTest() { - - JsonObject response = - GET("/categoryOptionCombos?filter=id:eq:CocUid0001&fields=id,categoryCombo[id],categoryOptions[id]") - .content(); - JsonList catOptionCombos = - response.getList("categoryOptionCombos", JsonCategoryOptionCombo.class); - String catOptionComboAOptions = catOptionCombos.get(0).getCategoryOptions().get(0).getId(); - String catOptionComboACatComboId = catOptionCombos.get(0).getCategoryCombo().getId(); - - JsonErrorReport error = - POST( - "/categoryOptionCombos/", - """ - { "name": "A_1", - "categoryOptions" : [{"id" : "%s"}], - "categoryCombo" : {"id" : "%s"} } - """ - .formatted(catOptionComboAOptions, catOptionComboACatComboId)) - .content(HttpStatus.CONFLICT) - .find(JsonErrorReport.class, report -> report.getErrorCode() == ErrorCode.E1122); - assertNotNull(error); - assertEquals( - "Category option combo A_1 already exists for category combo CatOptCombo A", - error.getMessage()); - } - @Test @DisplayName("Duplicate default category option combos should not be allowed") void catOptionCombosDuplicatedDefaultTest() { @@ -176,7 +147,7 @@ void catOptionCombosDuplicatedDefaultTest() { response.find(JsonErrorReport.class, report -> report.getErrorCode() == ErrorCode.E1122); assertNotNull(error); assertEquals( - "Category option combo Not default already exists for category combo default", + "Category option combo Not default cannot be associated with the default category combo", error.getMessage()); } }