Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOSIP-29094]Updated sql queries with case insensitive search #920

Merged
merged 5 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
balaji-alluru marked this conversation as resolved.
Show resolved Hide resolved

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