Updated code
This commit is contained in:
@@ -2,15 +2,18 @@ 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 net.gepafin.tendermanagement.entities.RoleActionContextEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserActionEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.*;
|
||||
import net.gepafin.tendermanagement.enums.ApplicationStatusTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.TimePeriodEnum;
|
||||
import net.gepafin.tendermanagement.enums.UserActionContextEnum;
|
||||
import net.gepafin.tendermanagement.model.response.ActionContextLabelResponse;
|
||||
import net.gepafin.tendermanagement.model.response.SummaryPageResponseBean;
|
||||
import net.gepafin.tendermanagement.model.response.UserActionResponseBean;
|
||||
import net.gepafin.tendermanagement.enums.UserActionLogsEnum;
|
||||
import net.gepafin.tendermanagement.model.request.ApplicationPageableRequestBean;
|
||||
import net.gepafin.tendermanagement.model.request.UserActionPaginationRequest;
|
||||
import net.gepafin.tendermanagement.model.response.*;
|
||||
import net.gepafin.tendermanagement.model.util.SortBy;
|
||||
import net.gepafin.tendermanagement.repositories.AssignedApplicationsRepository;
|
||||
import net.gepafin.tendermanagement.repositories.RoleActionContextRepository;
|
||||
import net.gepafin.tendermanagement.repositories.UserActionsRepository;
|
||||
@@ -23,9 +26,12 @@ import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.isEmpty;
|
||||
|
||||
@Component
|
||||
public class UserActionDao {
|
||||
|
||||
@@ -47,10 +53,11 @@ public class UserActionDao {
|
||||
|
||||
List<UserActionEntity> userActions = getFilterUserActions(userEntity.getId(),timeFilter,actionContext);
|
||||
|
||||
return createSummaryPageResponse(userEntity,numberOfLoginAttempts,applicationsProcessed,userActions);
|
||||
return null;
|
||||
// createSummaryPageResponse(userEntity,numberOfLoginAttempts,applicationsProcessed,userActions);
|
||||
}
|
||||
|
||||
public SummaryPageResponseBean createSummaryPageResponse(UserEntity user, Long numberOfLoginAttempts, Long applicationsProcessed, List<UserActionEntity> userActions){
|
||||
public SummaryPageResponseBean createSummaryPageResponse(UserEntity user, Long numberOfLoginAttempts, Long applicationsProcessed,PageableResponseBean<List<UserActionResponseBean>> userActions){
|
||||
SummaryPageResponseBean response = new SummaryPageResponseBean();
|
||||
response.setRole(user.getRoleEntity().getRoleName());
|
||||
response.setLastLogin(user.getLastLogin());
|
||||
@@ -59,9 +66,7 @@ public class UserActionDao {
|
||||
response.setEmail(user.getEmail());
|
||||
response.setNumberOfLoginAttempts(numberOfLoginAttempts);
|
||||
response.setApplicationsProcessed(applicationsProcessed);
|
||||
|
||||
List<UserActionResponseBean> userAction = convertEntityToResponse(userActions);
|
||||
response.setUserActions(userAction);
|
||||
response.setUserActions(userActions);
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -157,4 +162,138 @@ public class UserActionDao {
|
||||
return responseBean;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
public SummaryPageResponseBean getUserActionByPagination(UserEntity user, UserActionPaginationRequest userActionPaginationRequest) {
|
||||
Integer pageNo = null;
|
||||
Integer pageLimit = null;
|
||||
if (userActionPaginationRequest.getGlobalFilters() != null) {
|
||||
pageNo = userActionPaginationRequest.getGlobalFilters().getPage();
|
||||
pageLimit = userActionPaginationRequest.getGlobalFilters().getLimit();
|
||||
}
|
||||
if (pageLimit == null || pageLimit <= 0) {
|
||||
pageLimit = GepafinConstant.DEFAULT_PAGE_LIMIT;
|
||||
}
|
||||
if (pageNo == null || pageNo <= 0) {
|
||||
pageNo = GepafinConstant.DEFAULT_PAGE;
|
||||
}
|
||||
Specification<UserActionEntity> spec = search(userActionPaginationRequest,user);
|
||||
Page<UserActionEntity> entityPage = userActionsRepository.findAll(spec, PageRequest.of(pageNo - 1, pageLimit));
|
||||
// Prepare the response
|
||||
Long numberOfLoginAttempts = userActionsRepository.countUserLoginAttempts(user.getId());
|
||||
Long applicationsProcessed = assignedApplicationsRepository.countAssignedApplicationsByUserId(user.getId());
|
||||
|
||||
|
||||
List<UserActionResponseBean> userActionResponseBeans = convertEntityToResponse(entityPage.stream().toList());
|
||||
|
||||
|
||||
PageableResponseBean<List<UserActionResponseBean>> pageableResponseBean = new PageableResponseBean<>();
|
||||
pageableResponseBean.setBody(userActionResponseBeans);
|
||||
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 createSummaryPageResponse(user,numberOfLoginAttempts,applicationsProcessed,pageableResponseBean);
|
||||
}
|
||||
public Specification<UserActionEntity> search(UserActionPaginationRequest userActionPaginationRequest,UserEntity userEntity) {
|
||||
return (root, query, criteriaBuilder) -> {
|
||||
|
||||
List<Predicate> predicates = getPredicates(userActionPaginationRequest, criteriaBuilder, root,userEntity);
|
||||
SortBy sortBy = new SortBy(GepafinConstant.CREATED_DATE, true);
|
||||
|
||||
if (userActionPaginationRequest .getGlobalFilters() != null
|
||||
&& userActionPaginationRequest.getGlobalFilters().getSortBy() != null &&
|
||||
userActionPaginationRequest.getGlobalFilters().getSortBy().getColumnName() != null && Boolean.FALSE.equals(
|
||||
isEmpty(userActionPaginationRequest.getGlobalFilters().getSortBy().getColumnName()))) {
|
||||
sortBy.setColumnName(userActionPaginationRequest.getGlobalFilters().getSortBy().getColumnName());
|
||||
sortBy.setSortDesc(true);
|
||||
if (userActionPaginationRequest.getGlobalFilters().getSortBy().getSortDesc() != null) {
|
||||
sortBy.setSortDesc(userActionPaginationRequest.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(UserActionPaginationRequest userActionPaginationRequest,
|
||||
CriteriaBuilder criteriaBuilder, Root<UserActionEntity> root,UserEntity userEntity) {
|
||||
|
||||
Integer year = null;
|
||||
String search = null;
|
||||
if (userActionPaginationRequest.getGlobalFilters() != null) {
|
||||
year = userActionPaginationRequest.getGlobalFilters().getYear();
|
||||
search = userActionPaginationRequest.getGlobalFilters().getSearch();
|
||||
}
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
if (year != null && year > 0) {
|
||||
int filterYear = userActionPaginationRequest.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 actionType = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get(GepafinConstant.ACTION_TYPE)),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(actionType));
|
||||
Predicate actionContext = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get(GepafinConstant.ACTION_CONTEXT)),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(actionContext));
|
||||
Predicate methodType = criteriaBuilder.like(
|
||||
criteriaBuilder.upper(root.get(GepafinConstant.METHOD_TYPE)),
|
||||
"%" + search.toUpperCase() + "%"
|
||||
);
|
||||
predicates.add(criteriaBuilder.or(methodType));
|
||||
}
|
||||
|
||||
// Filter by `status` (if status list is provided)
|
||||
if (userActionPaginationRequest.getActionContext() != null && !userActionPaginationRequest.getActionContext().isEmpty()) {
|
||||
List<String> statusValues = userActionPaginationRequest.getActionContext().stream()
|
||||
.map(UserActionContextEnum::name) // Convert enum to string
|
||||
.toList();
|
||||
predicates.add(root.get("actionContext").in(statusValues));
|
||||
}
|
||||
if (userActionPaginationRequest.getActionType() != null && !userActionPaginationRequest.getActionType().isEmpty()) {
|
||||
List<String> statusValues = userActionPaginationRequest.getActionType().stream()
|
||||
.map(UserActionLogsEnum::name) // Convert enum to string
|
||||
.toList();
|
||||
predicates.add(root.get("actionType").in(statusValues));
|
||||
}
|
||||
|
||||
TimePeriodEnum timeRange= userActionPaginationRequest.getTimeFilter();
|
||||
if (timeRange != null) {
|
||||
LocalDateTime startDate = null;
|
||||
LocalDateTime endDate = LocalDateTime.now();
|
||||
|
||||
switch (timeRange) {
|
||||
case LAST_WEEK -> startDate = endDate.minusWeeks(1);
|
||||
case LAST_QUARTER -> startDate = endDate.minusMonths(3);
|
||||
case LAST_SEMESTER -> startDate = endDate.minusMonths(6);
|
||||
case LAST_YEAR -> startDate = endDate.minusYears(1);
|
||||
}
|
||||
|
||||
if (startDate != null) {
|
||||
predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), startDate, endDate));
|
||||
}
|
||||
}
|
||||
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_ID), userEntity.getId()));
|
||||
|
||||
return predicates;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user