Done ticket GEPAFINBE-195

This commit is contained in:
piyushkag
2025-04-03 14:51:16 +05:30
parent a4bd8d9329
commit 5afdf042bc
19 changed files with 429 additions and 253 deletions

View File

@@ -1467,185 +1467,6 @@ public class ApplicationDao {
}
}
public PageableResponseBean<List<ApplicationResponse>> getAllApplicationByPagination(UserEntity userEntity, Long callId, Long companyId, ApplicationPageableRequestBean applicationPageableRequestBean) {
Integer pageNo = null;
Integer pageLimit = null;
UserWithCompanyEntity userWithCompany= userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userEntity.getId(), companyId).orElse(null);
Long userWithCompanyId = userWithCompany!=null?userWithCompany.getId():null;
if (applicationPageableRequestBean.getGlobalFilters() != null) {
pageNo = applicationPageableRequestBean.getGlobalFilters().getPage();
pageLimit = applicationPageableRequestBean.getGlobalFilters().getLimit();
}
if (pageLimit == null || pageLimit <= 0) {
pageLimit = GepafinConstant.DEFAULT_PAGE_LIMIT;
}
if (pageNo == null || pageNo <= 0) {
pageNo = GepafinConstant.DEFAULT_PAGE;
}
Specification<ApplicationEntity> spec = search(callId,companyId, userWithCompanyId, applicationPageableRequestBean, userEntity);
Page<ApplicationEntity> entityPage = applicationRepository.findAll(spec, PageRequest.of(pageNo - 1, pageLimit));
// Prepare the response
List<ApplicationResponse> applicationResponses = entityPage.getContent().stream()
.map(application -> {
ApplicationResponse response = getApplicationResponse(application);
return response;
})
.collect(Collectors.toList());
PageableResponseBean<List<ApplicationResponse>> 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<ApplicationEntity> search(Long callId, Long companyId, Long userWithCompanyId, ApplicationPageableRequestBean applicationPageableRequestBean, UserEntity userEntity) {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = getPredicates(applicationPageableRequestBean, criteriaBuilder, root, callId,companyId, userWithCompanyId, userEntity);
SortBy sortBy = new SortBy(GepafinConstant.CREATED_DATE, true);
if (applicationPageableRequestBean.getGlobalFilters() != null
&& applicationPageableRequestBean.getGlobalFilters().getSortBy() != null &&
applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName() != null && Boolean.FALSE.equals(
isEmpty(applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName()))) {
sortBy.setColumnName(applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName());
sortBy.setSortDesc(true);
if (applicationPageableRequestBean.getGlobalFilters().getSortBy().getSortDesc() != null) {
sortBy.setSortDesc(applicationPageableRequestBean.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(ApplicationPageableRequestBean applicationPageableRequestBean,
CriteriaBuilder criteriaBuilder, Root<ApplicationEntity> root, Long callId,Long companyId, Long userWithCompanyId, UserEntity userEntity) {
Integer year = null;
String search = null;
Integer daysRange = null;
Map<String, FilterCriteria> filters = new HashMap<>();
if (applicationPageableRequestBean.getGlobalFilters() != null) {
year = applicationPageableRequestBean.getGlobalFilters().getYear();
search = applicationPageableRequestBean.getGlobalFilters().getSearch();
daysRange = applicationPageableRequestBean.getDaysRange();
}
if (applicationPageableRequestBean.getFilters() != null) {
filters = applicationPageableRequestBean.getFilters();
}
List<Predicate> predicates = new ArrayList<>();
// Boolean isBeneficiary = validator.checkIsBeneficiary();
if (Boolean.TRUE.equals(validator.checkIsBeneficiary()) || Boolean.TRUE.equals(validator.checkIsConfidi())) {
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_ID), userEntity.getId()));
}
if (year != null && year > 0) {
int filterYear = applicationPageableRequestBean.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 titlePredicate = criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.COMMENTS)),
"%" + search.toUpperCase() + "%"
);
// Predicate protocolPredicate = criteriaBuilder.like(
// criteriaBuilder.function(
// "TO_CHAR",
// String.class,
// criteriaBuilder.function("CAST", String.class, root.get(GepafinConstant.PROTOCOL).get(GepafinConstant.PROTOCOL_NUMBER))
// ),
// "%" + search + "%"
// );
Predicate callNamePredicate =criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.CALL).get(GepafinConstant.NAME)), // Adjust field name
"%" + search.toUpperCase() + "%"
);
// predicates.add(criteriaBuilder.or(protocolPredicate));
predicates.add(criteriaBuilder.or(callNamePredicate));
predicates.add(criteriaBuilder.or(titlePredicate));
}
// Filter by `status` (if status list is provided)
if (applicationPageableRequestBean.getStatus() != null && !applicationPageableRequestBean.getStatus().isEmpty()) {
List<String> statusValues = applicationPageableRequestBean.getStatus().stream()
.map(ApplicationStatusTypeEnum::name) // Convert enum to string
.toList();
predicates.add(root.get(GepafinConstant.STATUS).in(statusValues));
}
if (callId != null) {
CallEntity call = callService.validateCall(callId);
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.CALL).get(GepafinConstant.ID), callId));
}
// Optional companyId filter
if (userWithCompanyId != null) {
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_WITH_COMPANY).get(GepafinConstant.ID), userWithCompanyId));
}
if (companyId != null) {
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.COMPANY_ID), companyId));
}
if (daysRange != null && daysRange >= 0) {
LocalDateTime today = LocalDateTime.now();
LocalDateTime pastDate = today.minusDays(daysRange);
predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), pastDate, today));
}
applyFilters(root, criteriaBuilder, predicates, filters);
predicates.add(criteriaBuilder.isFalse(root.get(GepafinConstant.IS_DELETED)));
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.HUB_ID), userEntity.getHub().getId()));
return predicates;
}
private void applyFilters(Root<?> root, CriteriaBuilder criteriaBuilder, List<Predicate> predicates, Map<String, FilterCriteria> filters) {
if (Boolean.FALSE.equals(filters.isEmpty())) {
for (Map.Entry<String, FilterCriteria> entry : filters.entrySet()) {
String fieldName = entry.getKey();
FilterCriteria filterCriteria = entry.getValue();
Object value = filterCriteria.getValue();
MatchModeEnum matchMode = filterCriteria.getMatchMode();
if (value != null && matchMode != null) {
Path<?> fieldPath = root.get(fieldName);
if (fieldPath != null) {
Utils.applyStringFilter(fieldPath, criteriaBuilder, predicates, value, matchMode);
Utils.applyNumberFilter(fieldPath, criteriaBuilder, predicates, value, matchMode);
Utils.applyDateFilter(fieldPath, criteriaBuilder, predicates, value, matchMode,root);
}
}
}
}
}
// private Path<?> getFieldPath(Root<?> root, String fieldName) {
// try {
@@ -1918,27 +1739,29 @@ public class ApplicationDao {
}
// Search in `title` and `message` (if search term is provided)
if (search != null && !search.isEmpty()) {
String searchPattern = "%" + search.toUpperCase() + "%";
Predicate titlePredicate = criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.COMMENTS)),
"%" + search.toUpperCase() + "%"
);
// Predicate protocolPredicate = criteriaBuilder.like(
// criteriaBuilder.function(
// "TO_CHAR",
// String.class,
// criteriaBuilder.function("CAST", String.class, root.get(GepafinConstant.PROTOCOL).get(GepafinConstant.PROTOCOL_NUMBER))
// ),
// "%" + search + "%"
// );
Predicate callNamePredicate =criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.CALL_TITLE)), // Adjust field name
"%" + search.toUpperCase() + "%"
searchPattern
);
// predicates.add(criteriaBuilder.or(protocolPredicate));
predicates.add(criteriaBuilder.or(callNamePredicate));
predicates.add(criteriaBuilder.or(titlePredicate));
Predicate callTitlePredicate = criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.CALL_TITLE)),
searchPattern
);
Predicate callNamePredicate = criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.CALL_NAME)),
searchPattern
);
Predicate companyName = criteriaBuilder.like(
criteriaBuilder.upper(root.get(GepafinConstant.COMPANY_NAME)),
searchPattern
);
// Combine them using a single `or()`
Predicate finalPredicate = criteriaBuilder.or(titlePredicate, callTitlePredicate,callNamePredicate,companyName);
predicates.add(finalPredicate);
}
// Filter by `status` (if status list is provided)
@@ -1967,7 +1790,7 @@ public class ApplicationDao {
LocalDateTime pastDate = today.minusDays(daysRange);
predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), pastDate, today));
}
applyFilters(root, criteriaBuilder, predicates, filters);
Utils.applyFiltersByPagination(root, criteriaBuilder, predicates, filters);
predicates.add(criteriaBuilder.isFalse(root.get(GepafinConstant.IS_DELETED)));
@@ -1981,7 +1804,7 @@ public class ApplicationDao {
ApplicationResponse responseBean = new ApplicationResponse();
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByCallId(applicationView.getCallId());
Long totalFormSteps = flowFormDao.calculateTotalSteps(flowEdgesList);
Long completedSteps= Long.valueOf(flowFormDao.getCompletedStepsByView(applicationView.getId(), false));
Long completedSteps = Long.valueOf(flowFormDao.getCompletedStepsByView(applicationView.getId(), false));
Integer progress = calculateProgress(totalFormSteps, completedSteps);
responseBean.setId(applicationView.getId());
responseBean.setProgress(progress);
@@ -1995,19 +1818,10 @@ public class ApplicationDao {
responseBean.setEvaluationVersion(EvaluationVersionEnum.valueOf(applicationView.getEvaluationVersion()));
responseBean.setComments(applicationView.getComments());
responseBean.setCompanyId(applicationView.getCompanyId());
Optional<AssignedApplicationsEntity> assignedApplicationsOptional =
assignedApplicationsRepository.findByApplicationIdAndIsDeletedFalse(applicationView.getId());
if(assignedApplicationsOptional.isPresent()){
responseBean.setAssignedUserId(assignedApplicationsOptional.get().getUserId());
UserEntity user = userService.validateUser(assignedApplicationsOptional.get().getUserId());
String firstName = user.getFirstName() != null ? user.getFirstName() : "";
String lastName = user.getLastName() != null ? user.getLastName() : "";
String userName = String.join(" ", firstName, lastName).trim();
responseBean.setAssignedUserName(userName);
}
CompanyEntity company=companyService.validateCompany(applicationView.getCompanyId());
responseBean.setCompanyName(company.getCompanyName());
responseBean.setProtocolNumber(applicationView.getProtocolNumber());
responseBean.setAssignedUserId(applicationView.getAssignedUserId());
responseBean.setAssignedUserName(applicationView.getAssignedUserName());
responseBean.setCompanyName(applicationView.getCompanyName());
responseBean.setProtocolNumber(applicationView.getProtocolNumber());
responseBean.setAmountAccepted(applicationView.getAmountAccepted());
responseBean.setAmountRequested(applicationView.getAmountRequested());
responseBean.setDateAccepted(applicationView.getDateAccepted());