Updated code
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import net.gepafin.tendermanagement.config.SamlSuccessHandler;
|
||||
@@ -10,6 +13,7 @@ import net.gepafin.tendermanagement.enums.*;
|
||||
import net.gepafin.tendermanagement.model.request.*;
|
||||
import net.gepafin.tendermanagement.model.response.*;
|
||||
import net.gepafin.tendermanagement.model.util.JWTToken;
|
||||
import net.gepafin.tendermanagement.model.util.SortBy;
|
||||
import net.gepafin.tendermanagement.repositories.BeneficiaryRepository;
|
||||
import net.gepafin.tendermanagement.repositories.UserRepository;
|
||||
import net.gepafin.tendermanagement.service.HubService;
|
||||
@@ -28,8 +32,13 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -37,6 +46,7 @@ import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
|
||||
import static org.apache.commons.lang3.StringUtils.isEmpty;
|
||||
|
||||
@Component
|
||||
public class UserDao {
|
||||
@@ -596,6 +606,151 @@ public class UserDao {
|
||||
log.info("Total users found with role ID {}: {}", roleIds, userResponseBeans.size());
|
||||
return userResponseBeans;
|
||||
}
|
||||
public PageableResponseBean<List<UserResponseBean>> getUserByPagination(UserPaginationRequestBean userPaginationRequestBean,UserEntity userEntity) {
|
||||
Integer pageNo = null;
|
||||
Integer pageLimit = null;
|
||||
if (userPaginationRequestBean.getGlobalFilters() != null) {
|
||||
pageNo = userPaginationRequestBean.getGlobalFilters().getPage();
|
||||
pageLimit = userPaginationRequestBean.getGlobalFilters().getLimit();
|
||||
}
|
||||
if (pageLimit == null || pageLimit <= 0) {
|
||||
pageLimit = GepafinConstant.DEFAULT_PAGE_LIMIT;
|
||||
}
|
||||
if (pageNo == null || pageNo <= 0) {
|
||||
pageNo = GepafinConstant.DEFAULT_PAGE;
|
||||
}
|
||||
Specification<UserEntity> spec = search(userPaginationRequestBean,userEntity);
|
||||
Page<UserEntity> entityPage = userRepository.findAll(spec, PageRequest.of(pageNo - 1, pageLimit));
|
||||
|
||||
|
||||
List<UserResponseBean> applicationResponses = entityPage.getContent().stream()
|
||||
.map(user -> {
|
||||
UserResponseBean response = convertUserEntityToUserResponse(user);
|
||||
return response;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
|
||||
PageableResponseBean<List<UserResponseBean>> pageableResponseBean = new PageableResponseBean<>();
|
||||
pageableResponseBean.setBody(applicationResponses);
|
||||
pageableResponseBean.setCurrentPage(entityPage.getNumber() + 1); // Page numbers typically start from 0, so add 1 for user-friendly indexing
|
||||
pageableResponseBean.setTotalPages(entityPage.getTotalPages());
|
||||
pageableResponseBean.setTotalRecords(entityPage.getTotalElements());
|
||||
pageableResponseBean.setPageSize(entityPage.getSize());
|
||||
|
||||
return pageableResponseBean;
|
||||
}
|
||||
|
||||
public Specification<UserEntity> search(UserPaginationRequestBean userPaginationRequestBean,UserEntity userEntity) {
|
||||
return (root, query, criteriaBuilder) -> {
|
||||
|
||||
List<Predicate> predicates = getPredicates(userPaginationRequestBean, criteriaBuilder, root,userEntity);
|
||||
SortBy sortBy = new SortBy(GepafinConstant.CREATED_DATE, true);
|
||||
|
||||
if (userPaginationRequestBean .getGlobalFilters() != null
|
||||
&& userPaginationRequestBean.getGlobalFilters().getSortBy() != null &&
|
||||
userPaginationRequestBean.getGlobalFilters().getSortBy().getColumnName() != null && Boolean.FALSE.equals(
|
||||
isEmpty(userPaginationRequestBean.getGlobalFilters().getSortBy().getColumnName()))) {
|
||||
sortBy.setColumnName(userPaginationRequestBean.getGlobalFilters().getSortBy().getColumnName());
|
||||
sortBy.setSortDesc(true);
|
||||
if (userPaginationRequestBean.getGlobalFilters().getSortBy().getSortDesc() != null) {
|
||||
sortBy.setSortDesc(userPaginationRequestBean.getGlobalFilters().getSortBy().getSortDesc());
|
||||
}
|
||||
}
|
||||
|
||||
query.orderBy(criteriaBuilder.desc(root.get(sortBy.getColumnName())));
|
||||
if (Boolean.FALSE.equals(sortBy.getSortDesc())) {
|
||||
query.orderBy(criteriaBuilder.asc(root.get(sortBy.getColumnName())));
|
||||
}
|
||||
return query.where(criteriaBuilder.and(predicates.toArray(new Predicate[0]))).getRestriction();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
private List<Predicate> getPredicates(UserPaginationRequestBean userPaginationRequestBean,
|
||||
CriteriaBuilder criteriaBuilder, Root<UserEntity> root,UserEntity user) {
|
||||
|
||||
Integer year = null;
|
||||
String search = null;
|
||||
if (userPaginationRequestBean.getGlobalFilters() != null) {
|
||||
year = userPaginationRequestBean.getGlobalFilters().getYear();
|
||||
search = userPaginationRequestBean.getGlobalFilters().getSearch();
|
||||
}
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
if (year != null && year > 0) {
|
||||
int filterYear = userPaginationRequestBean.getGlobalFilters().getYear();
|
||||
|
||||
// Create LocalDateTime boundaries for the start and end of the year
|
||||
LocalDateTime startOfYear = LocalDateTime.of(filterYear, 1, 1, 0, 0);
|
||||
LocalDateTime endOfYear = LocalDateTime.of(filterYear, 12, 31, 23, 59, 59);
|
||||
|
||||
// Add the range comparison to filter records within the year
|
||||
predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), startOfYear, endOfYear));
|
||||
|
||||
}
|
||||
// Search in `title` and `message` (if search term is provided)
|
||||
if (search != null && !search.isEmpty()) {
|
||||
Predicate predicate = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get("firstName")),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(predicate));
|
||||
Predicate predicate1 = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get("lastName")),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(predicate1));
|
||||
|
||||
Predicate predicate2 = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get("phoneNumber")),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(predicate2));
|
||||
|
||||
Predicate predicate3 = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get("organization")),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(predicate3));
|
||||
|
||||
Predicate predicate4 = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get("address")),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(predicate4));
|
||||
|
||||
Predicate predicate5 = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get("city")),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(predicate5));
|
||||
|
||||
Predicate predicate6 = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get("country")),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(predicate6));
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Filter by `status` (if status list is provided)
|
||||
if (userPaginationRequestBean.getStatus() != null && !userPaginationRequestBean.getStatus().isEmpty()) {
|
||||
List<String> statusValues = userPaginationRequestBean.getStatus().stream()
|
||||
.map(UserStatusEnum::name) // Convert enum to string
|
||||
.toList();
|
||||
predicates.add(root.get(GepafinConstant.STATUS).in(statusValues));
|
||||
}
|
||||
if (userPaginationRequestBean.getRole() != null && !userPaginationRequestBean.getRole().isEmpty()) {
|
||||
List<String> roleValues = userPaginationRequestBean.getRole().stream()
|
||||
.map(RoleStatusEnum::name) // Convert enum to string
|
||||
.toList();
|
||||
predicates.add(root.get("roleEntity").get("roleType").in(roleValues));
|
||||
}
|
||||
predicates.add(root.get(GepafinConstant.HUB).get("id").in(user.getHub().getId()));
|
||||
|
||||
return predicates;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user