Skip to content

Commit

Permalink
[MOSIP-29094]Updated sql queries with case insensitive search (mosip#920
Browse files Browse the repository at this point in the history
)

* Updated sql queries with case insensitive search

Signed-off-by: Balaji <[email protected]>

* updated getter methods

Signed-off-by: Balaji <[email protected]>

* Updated user detail History and zone user history repositories

Signed-off-by: Balaji <[email protected]>

* Updated zone user repository

Signed-off-by: Balaji <[email protected]>

* Fixed admin build issues

Signed-off-by: Balaji <[email protected]>

---------

Signed-off-by: Balaji <[email protected]>
Signed-off-by: dhanendra06 <[email protected]>
  • Loading branch information
balaji-alluru authored and dhanendra06 committed Jun 12, 2024
1 parent 79c33f1 commit 9355644
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 22 deletions.
27 changes: 26 additions & 1 deletion admin/admin-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -129,7 +149,12 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>

<dependency>
<groupId>io.mosip.commons</groupId>
<artifactId>commons-packet-manager</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import io.mosip.commons.packet.impl.OnlinePacketCryptoServiceImpl;
import io.mosip.commons.packet.keeper.PacketKeeper;

import javax.validation.Validator;

import org.mockito.Mockito;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -36,4 +40,8 @@ public PacketKeeper packetKeeper() {
return Mockito.mock(PacketKeeper.class);
}

@Bean
public Validator validator() {
return Mockito.mock(Validator.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public ResponseWrapper<PageResponseDto<UserCenterMappingExtnDto>> serachUserCent
auditUtil.auditRequest(MasterDataConstant.SEARCH_USER_DETAILS_API_IS_CALLED + SearchDto.class.getCanonicalName(),
MasterDataConstant.AUDIT_SYSTEM,
MasterDataConstant.SEARCH_USER_DETAILS_API_IS_CALLED + SearchDto.class.getCanonicalName(),"ADM-922");
responseWrapper.setResponse(userDetailsService.serachUserCenterMappingDetails(dto.getRequest()));
responseWrapper.setResponse(userDetailsService.searchUserCenterMappingDetails(dto.getRequest()));
return responseWrapper;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@ public class UserDetailsDto {


private String langCode;


public String getId() {
return this.id.toLowerCase();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ public class ZoneUser extends BaseEntity implements Serializable {

@Column(name = "lang_code", nullable = true, length = 3)
private String langCode;

public String getUserId() {
return this.userId.toLowerCase();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public interface UserDetailsHistoryRepository extends BaseRepository<UserDetails
*/
// select a.id,a.langCode,a.name,a.uin,a.email,a.mobile,a.statusCode
// SELECT m.id,m.langCode,m.name,m.uin,m.email,m.mobile,m.statusCode,m.effDTimes
@Query(value = "select * from (SELECT * FROM master.user_detail_h m WHERE id = ?1 AND eff_dtimes<= ?2 and (is_deleted is null or is_deleted =false) ORDER BY eff_dtimes DESC) a LIMIT 1", nativeQuery = true)
@Query(value = "select * from (SELECT * FROM master.user_detail_h m WHERE LOWER(id) = LOWER(?1) AND eff_dtimes<= ?2 and (is_deleted is null or is_deleted =false) ORDER BY eff_dtimes DESC) a LIMIT 1", nativeQuery = true)
List<UserDetailsHistory> getByUserIdAndTimestamp(String userId, LocalDateTime effDTimes);
// (?2 BETWEEN effDTimes AND CURRENT_TIMESTAMP)

@Query(value = "Select * from master.user_detail_h m where m.regcntr_id = ?1 and m.id = ?2 and m.eff_dtimes <= ?3 and ( m.is_deleted = false or m.is_deleted is null) order by m.eff_dtimes desc ", nativeQuery = true)
@Query(value = "Select * from master.user_detail_h m where m.regcntr_id = ?1 and LOWER(m.id) = LOWER(?2) and m.eff_dtimes <= ?3 and ( m.is_deleted = false or m.is_deleted is null) order by m.eff_dtimes desc ", nativeQuery = true)
List<UserDetailsHistory> findByCntrIdAndUsrIdAndEffectivetimesLessThanEqualAndIsDeletedFalseOrIsDeletedIsNull(
String registrationCenterId, String userId, LocalDateTime lDateAndTime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ public interface UserDetailsRepository extends BaseRepository<UserDetails, Strin
nativeQuery = true)
public List<UserDetails> findByRegIdAndIsDeletedFalseOrIsDeletedIsNull(String centerId, Pageable pageable);

@Query("FROM UserDetails m where m.id = ?1 and (m.isDeleted is null or m.isDeleted = false)")
@Query("FROM UserDetails m where LOWER(m.id) = LOWER(?1) and (m.isDeleted is null or m.isDeleted = false)")
UserDetails findByIdAndIsDeletedFalseorIsDeletedIsNull(String id);

@Query("FROM UserDetails m where m.id = ?1")
@Query("FROM UserDetails m where LOWER(m.id) = LOWER(?1)")
UserDetails findUserDetailsById(String id);

@Query("FROM UserDetails m where (m.isDeleted is null or m.isDeleted = false)")
List<UserDetails> findAllByAndIsDeletedFalseorIsDeletedIsNull();

@Query("FROM UserDetails m where m.id = ?1 and (m.isDeleted is null or m.isDeleted = false) and isActive = true")
@Query("FROM UserDetails m where LOWER(m.id) = LOWER(?1) and (m.isDeleted is null or m.isDeleted = false) and isActive = true")
UserDetails findByIdAndIsDeletedFalseorIsDeletedIsNullAndIsActive(String id);

@Query("FROM UserDetails m where (m.isDeleted is null or m.isDeleted = false) and m.isActive = true")
Page<UserDetails> findAllByIsDeletedFalseorIsDeletedIsNull(Pageable pageable);

@Query("FROM UserDetails m where m.id = ?1 and m.langCode = ?2 and (m.isDeleted is null or m.isDeleted = false)")
@Query("FROM UserDetails m where LOWER(m.id) = LOWER(?1) and m.langCode = ?2 and (m.isDeleted is null or m.isDeleted = false)")
UserDetails findByIdAndIsDeletedFalseorIsDeletedIsNull(String id,String langCode);

@Modifying
@Query("UPDATE UserDetails m SET m.updatedBy=?3, m.isDeleted =true, m.isActive = false, m.updatedDateTime=?2 ,m.deletedDateTime = ?2 WHERE m.id =?1 and (m.isDeleted is null or m.isDeleted =false)")
@Query("UPDATE UserDetails m SET m.updatedBy=?3, m.isDeleted =true, m.isActive = false, m.updatedDateTime=?2 ,m.deletedDateTime = ?2 WHERE LOWER(m.id) = LOWER(?1) and (m.isDeleted is null or m.isDeleted =false)")
int deleteUserCenterMapping(String id, LocalDateTime deletedDateTime, String updatedBy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.mosip.kernel.masterdata.entity.id.ZoneUserHistoryId;
@Repository
public interface ZoneUserHistoryRepository extends BaseRepository<ZoneUserHistory, ZoneUserHistoryId>{
@Query(value = "SELECT * FROM master.zone_user_h m WHERE usr_id = ?1 AND eff_dtimes>= ?2 ORDER BY eff_dtimes DESC ", nativeQuery = true)
@Query(value = "SELECT * FROM master.zone_user_h m WHERE LOWER(usr_id) = LOWER(?1) AND eff_dtimes>= ?2 ORDER BY eff_dtimes DESC ", nativeQuery = true)
List<ZoneUserHistory> getByUserIdAndTimestamp(String userId, LocalDateTime localDateTime);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,40 @@
*/
public interface ZoneUserRepository extends BaseRepository<ZoneUser, String> {

@Query("FROM ZoneUser zu WHERE zu.userId=?1 and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
@Query("FROM ZoneUser zu WHERE LOWER(zu.userId)=LOWER(?1) and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
public List<ZoneUser> findByUserIdNonDeleted(String userId);

@Query("FROM ZoneUser zu WHERE zu.userId=?1 and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
@Query("FROM ZoneUser zu WHERE LOWER(zu.userId)=LOWER(?1) and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
public ZoneUser findZoneByUserIdNonDeleted(String userId);

@Query("FROM ZoneUser zu WHERE zu.userId=?1 ")
@Query("FROM ZoneUser zu WHERE LOWER(zu.userId)=LOWER(?1) ")
public ZoneUser findByUserId(String userId);

@Query("FROM ZoneUser zu WHERE zu.userId=?1 and zu.zoneCode=?2 ")
@Query("FROM ZoneUser zu WHERE LOWER(zu.userId)=LOWER(?1) and zu.zoneCode=?2 ")
public List<ZoneUser> findByUserIdAndZoneCode(String userId, String zoneCode);

@Query("FROM ZoneUser zu WHERE zu.userId=?1 and zu.zoneCode=?2 and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
@Query("FROM ZoneUser zu WHERE LOWER(zu.userId)=LOWER(?1) and zu.zoneCode=?2 and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
public ZoneUser findByIdAndIsDeletedFalseOrIsDeletedIsNull(String userId, String zoneCode);

@Query("FROM ZoneUser zu WHERE zu.zoneCode=?1 and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
public List<ZoneUser> findtoUpdateZoneUserByCode(String zoneCode);

@Query("FROM ZoneUser zu WHERE zu.userId=?1 and zu.langCode=?2 and zu.zoneCode=?3 and (zu.isDeleted IS NULL OR zu.isDeleted = false) and zu.isActive=true")
@Query("FROM ZoneUser zu WHERE LOWER(zu.userId)=LOWER(?1) and zu.langCode=?2 and zu.zoneCode=?3 and (zu.isDeleted IS NULL OR zu.isDeleted = false) and zu.isActive=true")
public ZoneUser findZoneUserByUserIdZoneCodeLangCodeIsActive(String userId, String langCode, String zoneCode);

@Query("FROM ZoneUser zu WHERE zu.userId IN :userids")
public List<ZoneUser> findByUserIds(@Param("userids") List<String> userIds);

@Query("FROM ZoneUser zu WHERE zu.userId=?1 and zu.isActive=true and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
@Query("FROM ZoneUser zu WHERE LOWER(zu.userId)=LOWER(?1) and zu.isActive=true and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
public ZoneUser findZoneByUserIdActiveAndNonDeleted(String userId);

@Query("FROM ZoneUser zu WHERE LOWER(zu.zoneCode) like (%?1%) and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
/*
* This is a query used by a deprecated end point /users/search, this should be removed in next release.
* The end point using this query was added in version 1.2.0-rc1 and marked as deprecated in version 1.2.0
* For security reason, we replaced the 'LIKE' query with equals in where clause.
*/

@Query("FROM ZoneUser zu WHERE LOWER(zu.zoneCode)=?1 and (zu.isDeleted IS NULL OR zu.isDeleted = false) ")
public List<ZoneUser> findZoneByZoneCodeActiveAndNonDeleted(String zoneCode);

@Modifying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ public UsersDto getUsers(String roleName,int pageStart, int pageFetch,

public StatusResponseDto updateUserStatus(String id, @Valid boolean isActive);

public PageResponseDto<UserCenterMappingExtnDto> serachUserCenterMappingDetails(SearchDtoWithoutLangCode request);
public PageResponseDto<UserCenterMappingExtnDto> searchUserCenterMappingDetails(SearchDtoWithoutLangCode request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ public PageResponseDto<UserDetailsExtnDto> searchUserDetails(SearchDtoWithoutLan
}

@Override
public PageResponseDto<UserCenterMappingExtnDto> serachUserCenterMappingDetails(SearchDtoWithoutLangCode searchDto) {
public PageResponseDto<UserCenterMappingExtnDto> searchUserCenterMappingDetails(SearchDtoWithoutLangCode searchDto) {
PageResponseDto<ZoneUserSearchDto> pageDto = new PageResponseDto<>();
PageResponseDto<UserCenterMappingExtnDto> userCenterPageDto = new PageResponseDto<>();
List<UserCenterMappingExtnDto> userCenterMappingExtnDtos = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public ZoneUserExtnDto createZoneUserMapping(ZoneUserDto zoneUserDto) {

// Throws exception if not found
zoneservice.getZone(zoneUserDto.getZoneCode(), languageUtils.getDefaultLanguage());

zu = zoneUserRepo.save(zu);
ZoneUserHistory zuh = new ZoneUserHistory();
MapperUtils.map(zu, zuh);
Expand Down Expand Up @@ -309,7 +308,7 @@ public StatusResponseDto updateZoneUserMapping(String userId, boolean isActive)
throw new MasterDataServiceException(ZoneUserErrorCode.USER_MAPPING_EXIST.getErrorCode(),
ZoneUserErrorCode.USER_MAPPING_EXIST.getErrorMessage());
}
masterdataCreationUtil.updateMasterDataStatus(ZoneUser.class, userId, isActive, "userId");
masterdataCreationUtil.updateMasterDataStatus(ZoneUser.class, userId.toLowerCase(), isActive, "userId");

ZoneUserHistory zoneUserHistory = new ZoneUserHistory();
MetaDataUtils.setUpdateMetaData(zoneUser, zoneUserHistory, true);
Expand Down

0 comments on commit 9355644

Please sign in to comment.