package net.gepafin.tendermanagement.dao; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.Predicate; 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.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.repositories.AssignedApplicationsRepository; import net.gepafin.tendermanagement.repositories.RoleActionContextRepository; import net.gepafin.tendermanagement.repositories.UserActionsRepository; import net.gepafin.tendermanagement.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.List; import java.util.stream.Collectors; @Component public class UserActionDao { @Autowired private UserService userService; @Autowired private UserActionsRepository userActionsRepository; @Autowired private AssignedApplicationsRepository assignedApplicationsRepository; @Autowired private RoleActionContextRepository roleActionContextRepository; public SummaryPageResponseBean getUserAction(HttpServletRequest request, UserEntity userEntity, TimePeriodEnum timeFilter, List actionContext){ Long numberOfLoginAttempts = userActionsRepository.countUserLoginAttempts(userEntity.getId()); Long applicationsProcessed = assignedApplicationsRepository.countAssignedApplicationsByUserId(userEntity.getId()); List userActions = getFilterUserActions(userEntity.getId(),timeFilter,actionContext); return createSummaryPageResponse(userEntity,numberOfLoginAttempts,applicationsProcessed,userActions); } public SummaryPageResponseBean createSummaryPageResponse(UserEntity user, Long numberOfLoginAttempts, Long applicationsProcessed, List userActions){ SummaryPageResponseBean response = new SummaryPageResponseBean(); response.setRole(user.getRoleEntity().getRoleName()); response.setLastLogin(user.getLastLogin()); response.setRegistrationDate(user.getCreatedDate()); response.setUsername(user.getFirstName()); response.setEmail(user.getEmail()); response.setNumberOfLoginAttempts(numberOfLoginAttempts); response.setApplicationsProcessed(applicationsProcessed); List userAction = convertEntityToResponse(userActions); response.setUserActions(userAction); return response; } public List getFilterUserActions(Long userId ,TimePeriodEnum timeFilter, List actionContextList) { LocalDateTime endDate = LocalDateTime.now(); LocalDateTime startDate = (timeFilter != null) ? getStartTimeFromFilter(timeFilter) : null; Pageable pageable = PageRequest.of(0, 25); // String actionContextLabel = (actionContext != null) ? actionContext.toString() : null; List actionContextLabels = (actionContextList != null && !actionContextList.isEmpty()) ? actionContextList.stream().map(Enum::toString).collect(Collectors.toList()) : null; Specification spec = getUserActionsSpecification(userId, startDate, endDate, actionContextLabels); Page pageResult = userActionsRepository.findAll(spec, pageable); return pageResult.getContent(); } private LocalDateTime getStartTimeFromFilter(TimePeriodEnum timeFilter) { LocalDateTime now = LocalDateTime.now(); if (timeFilter.equals(TimePeriodEnum.LAST_WEEK)) { return now.minusDays(7); } else if (timeFilter.equals(TimePeriodEnum.LAST_QUARTER)) { return now.minusMonths(4); } else if (timeFilter.equals(TimePeriodEnum.LAST_SEMESTER)) { return now.minusMonths(6); } else if (timeFilter.equals(TimePeriodEnum.LAST_YEAR)) { return now.minusYears(1); } else { return null; } } public Specification getUserActionsSpecification(Long userId, LocalDateTime startDate, LocalDateTime endDate, List actionContextList) { return (root, query, builder) -> { Predicate predicate = builder.isFalse(root.get("isDeleted")); predicate = builder.and(predicate, builder.equal(root.get("userId"), userId)); if (startDate != null && endDate != null) { predicate = builder.and(predicate, builder.between(root.get("createdDate"), startDate, endDate)); } if (actionContextList != null && !actionContextList.isEmpty()) { CriteriaBuilder.In inClause = builder.in(root.get("actionContext")); for (String actionContext : actionContextList) { inClause.value(actionContext); } predicate = builder.and(predicate, inClause); } query.orderBy(builder.desc(root.get("createdDate"))); return predicate; }; } private List convertEntityToResponse(List userActions) { return userActions.stream().map(action -> { UserActionResponseBean responseBean = new UserActionResponseBean(); responseBean.setId(action.getId()); responseBean.setUserId(action.getUserId()); responseBean.setActionType(action.getActionType()); responseBean.setRequestBody(action.getRequestBody()); responseBean.setLoginAttemptId(action.getLoginAttemptId()); responseBean.setIpAddress(action.getIpAddress()); responseBean.setActionContext(action.getActionContext()); responseBean.setHubId(action.getHubId()); responseBean.setUrl(action.getUrl()); responseBean.setResponse(action.getResponse()); responseBean.setCreatedDate(action.getCreatedDate()); responseBean.setUpdatedDate(action.getUpdatedDate()); return responseBean; }).collect(Collectors.toList()); } public List getActionContextLabels(HttpServletRequest request, UserEntity userEntity){ List actionContextLabel = roleActionContextRepository.findActionContextLabel(userEntity.getRoleEntity().getId()); return convertRoleContextEntityToResponse(actionContextLabel); } private List convertRoleContextEntityToResponse(List actionContextEntities){ return actionContextEntities.stream().map(actionContext -> { ActionContextLabelResponse responseBean = new ActionContextLabelResponse(); responseBean.setId(actionContext.getId()); responseBean.setActionContext(UserActionContextEnum.valueOf(actionContext.getActionContext())); responseBean.setRoleId(actionContext.getRoleId()); responseBean.setDescription(actionContext.getDescription()); responseBean.setIsViewed(actionContext.getIsViewed()); return responseBean; }).collect(Collectors.toList()); } }