From b6ed69eccfdddc39fba4e49ada4335a8a3a43836 Mon Sep 17 00:00:00 2001 From: Mahesh Maney R <mahesh-maney@users.noreply.github.com> Date: Thu, 14 Sep 2023 14:11:42 +0530 Subject: [PATCH] adding pagination feature to user details <ManeyMR>. --- .../repository/UserAttributeRepository.java | 5 +++-- .../userManagement/services/UserService.java | 16 ++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/tarento/upsmf/userManagement/repository/UserAttributeRepository.java b/src/main/java/com/tarento/upsmf/userManagement/repository/UserAttributeRepository.java index 288df5f..4bddafd 100644 --- a/src/main/java/com/tarento/upsmf/userManagement/repository/UserAttributeRepository.java +++ b/src/main/java/com/tarento/upsmf/userManagement/repository/UserAttributeRepository.java @@ -10,6 +10,7 @@ import java.util.List; @Repository public interface UserAttributeRepository extends JpaRepository<UserAttributeModel,String> { - @Query(value = "SELECT * FROM user_attribute WHERE name=:fieldName and value=:fieldValue",nativeQuery = true) - List<UserAttributeModel> findUserByAttribute(@Param("fieldName") String fieldName, @Param("fieldValue") String fieldValue); + @Query(value = "SELECT * FROM user_attribute WHERE name=:fieldName and value=:fieldValue OFFSET :offset LIMIT :limit",nativeQuery = true) + List<UserAttributeModel> findUserByAttribute(@Param("fieldName") String fieldName, @Param("fieldValue") String fieldValue, + @Param("offset") int offset, @Param("limit") int limit); } diff --git a/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java b/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java index 8f2b709..60f89ef 100644 --- a/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java +++ b/src/main/java/com/tarento/upsmf/userManagement/services/UserService.java @@ -243,13 +243,13 @@ public class UserService { return new ResponseEntity<>(transaction, HttpStatus.OK); } - public List<UserAttributeModel> getUserByAttribute(String fieldName, String fieldValue) { - return userAttributeRepository.findUserByAttribute(fieldName, fieldValue); + public List<UserAttributeModel> getUserByAttribute(String fieldName, String fieldValue, int offset, int limit) { + return userAttributeRepository.findUserByAttribute(fieldName, fieldValue,offset,limit); } public List getUserListByAttribute(String fieldName, String fieldValue, int offset, int limit) throws SQLException { - List<UserAttributeModel> userByAttribute = getUserByAttribute(fieldName, fieldValue); + List<UserAttributeModel> userByAttribute = getUserByAttribute(fieldName, fieldValue,offset,limit); if(userByAttribute == null || userByAttribute.isEmpty()){ logger.info("No records found."); return Collections.EMPTY_LIST; @@ -257,7 +257,7 @@ public class UserService { logger.info("Records found {}",userByAttribute); List<String> collect = userByAttribute.stream().map(UserAttributeModel::getUserId).collect(Collectors.toList()); - Map<String, UserRepresentation> userRepresentationMap = getStringUserRepresentationMap(collect, offset, limit); + Map<String, UserRepresentation> userRepresentationMap = getStringUserRepresentationMap(collect); if(userRepresentationMap.isEmpty()){ logger.info("No UserRepresentation records found for {}",collect); return Collections.EMPTY_LIST; @@ -265,9 +265,9 @@ public class UserService { return new ArrayList<>(userRepresentationMap.values()); } - private Map<String, UserRepresentation> getStringUserRepresentationMap(List<String> collect, int offset, int limit) throws SQLException { + private Map<String, UserRepresentation> getStringUserRepresentationMap(List<String> collect) throws SQLException { Connection connection = Objects.requireNonNull(jdbcTemplate.getDataSource()).getConnection(); - String formattedString = getFormattedStringFromCollection(collect, offset, limit); + String formattedString = getFormattedStringFromCollection(collect); Map<String, UserRepresentation> userRepresentationMap = new HashMap<>(); ResultSet resultSet = null; PreparedStatement preparedStatement = null; @@ -311,7 +311,7 @@ public class UserService { return userRepresentationMap; } - private String getFormattedStringFromCollection(List<String> collect, int offset, int limit) { + private String getFormattedStringFromCollection(List<String> collect) { StringBuffer sbf = new StringBuffer(); sbf.append("select ue.*,ua.name,ua.value from user_entity ue join user_attribute ua on ua.user_id = ue.id WHERE ue.id IN ("); collect.stream().forEach(item -> { @@ -319,7 +319,7 @@ public class UserService { sbf.append(","); }); String substring = sbf.substring(0, sbf.lastIndexOf(",")); - substring = substring + (") OFFSET " + offset + " LIMIT " + limit); + substring = substring + (" )"); logger.info("Query to be Executed {}",substring); return substring; } -- GitLab