From 6673540515bfc88d081b93358c377420bcc984aa Mon Sep 17 00:00:00 2001 From: Shilpa M Date: Wed, 9 Mar 2022 10:59:14 +0530 Subject: [PATCH] Mosip 19954 Issue when we create a location with the same name with different parents in the same hierarchy level --- .../masterdata/repository/LocationRepository.java | 6 ++++++ .../masterdata/service/impl/LocationServiceImpl.java | 10 ++++++---- .../integration/LocationControllerIntegrationTest.java | 1 + 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java index 27ebbc5e165..dc942d14954 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java @@ -154,4 +154,10 @@ List findLocationByHierarchyLevelStartsWith(Short hierarchyLevel, Stri @Query(value = "FROM Location l where l.code=?1 and l.langCode=?2 and (l.isDeleted is null or l.isDeleted=false) and isActive=true") Location findLocationByCodeAndLanguageCodeAndIsActiveTrue(String locCode, String languagecode); + @Query("FROM Location l WHERE l.name=?1 AND l.parentLocCode=?5 AND l.hierarchyLevel=?2 AND l.langCode=?3 AND NOT code=?4") + List findByNameParentlocCodeAndLevelLangCodeNotCode(String name,Short hierarchyLevel, String langCode, String code,String parentLocCode); + + @Query("FROM Location l WHERE l.name=?1 AND l.parentLocCode=?2 AND l.hierarchyLevel=?3 AND l.langCode=?4 AND (l.isDeleted is null or l.isDeleted=false)" ) + List findByNameParentCodeAndLevelLangCode(String name, String parentCode,Short hierarchyLevel, String langCode); + } diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java index 7895b63f26e..9f1f91db0ca 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java @@ -236,8 +236,9 @@ public LocationPostResponseDto createLocation(LocationCreateDto dto) { throw new RequestException(LocationErrorCode.INVALID_HIERARCY_LEVEL.getErrorCode(), LocationErrorCode.INVALID_HIERARCY_LEVEL.getErrorMessage()); } - List list = locationRepository.findByNameAndLevelLangCode(dto.getName(), - dto.getHierarchyLevel(), dto.getLangCode()); + List list =(null!=dto.getParentLocCode() && !dto.getParentLocCode().isEmpty())?locationRepository.findByNameParentCodeAndLevelLangCode(dto.getName(),dto.getParentLocCode(), + dto.getHierarchyLevel(), dto.getLangCode()):locationRepository.findByNameAndLevelLangCode(dto.getName(), + dto.getHierarchyLevel(), dto.getLangCode()); if (list != null && !list.isEmpty()) { auditUtil.auditRequest( String.format(MasterDataConstant.FAILURE_CREATE, LocationDto.class.getSimpleName()), @@ -311,8 +312,9 @@ public LocationPutResponseDto updateLocationDetails(LocationDto locationDto) { throw new RequestException(LocationErrorCode.INVALID_HIERARCY_LEVEL.getErrorCode(), LocationErrorCode.INVALID_HIERARCY_LEVEL.getErrorMessage()); } - List list = locationRepository.findByNameAndLevelLangCodeNotCode(locationDto.getName(), - locationDto.getHierarchyLevel(), locationDto.getLangCode(), locationDto.getCode()); + List list = (null==locationDto.getParentLocCode() || locationDto.getParentLocCode().isEmpty())? locationRepository.findByNameAndLevelLangCodeNotCode(locationDto.getName(), + locationDto.getHierarchyLevel(), locationDto.getLangCode(), locationDto.getCode()):locationRepository.findByNameParentlocCodeAndLevelLangCodeNotCode(locationDto.getName(), + locationDto.getHierarchyLevel(), locationDto.getLangCode(), locationDto.getCode(),locationDto.getParentLocCode()); if (list != null && !list.isEmpty()) { auditUtil.auditRequest( String.format(MasterDataConstant.FAILURE_CREATE, LocationDto.class.getSimpleName()), diff --git a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java index ec0a59c4581..654c01dc9ed 100644 --- a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java +++ b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java @@ -245,6 +245,7 @@ public void updateLocationAlreadyExistsUnderHeirarchyExceptionTest() throws Exce String requestJson = mapper.writeValueAsString(request); when(repo.findLocationHierarchyByCodeAndLanguageCode(Mockito.any(), Mockito.any())) .thenReturn(Arrays.asList(location1)); + when(repo.findByNameParentlocCodeAndLevelLangCodeNotCode(Mockito.any(),Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any())).thenReturn(Arrays.asList(location1)); when(repo.findByNameAndLevelLangCodeNotCode(Mockito.any(),Mockito.any(),Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(location1)); mockMvc.perform(put("/locations").contentType(MediaType.APPLICATION_JSON).content(requestJson)) .andExpect(status().isOk());