Done ticket GEPAFINBE-128

This commit is contained in:
rajesh
2025-01-13 16:28:01 +05:30
parent 577055d169
commit f9e9673d93
18 changed files with 661 additions and 5 deletions

View File

@@ -0,0 +1,138 @@
package net.gepafin.tendermanagement.dao;
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.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, String actionContext){
Long numberOfLoginAttempts = userActionsRepository.countUserLoginAttempts(userEntity.getId());
Long applicationsProcessed = assignedApplicationsRepository.countAssignedApplicationsByUserId(userEntity.getId());
List<RoleActionContextEntity> actionContextLabel = roleActionContextRepository.findByRoleIdAndIsDeletedFalse(userEntity.getRoleEntity().getId());
List<UserActionEntity> userActions = getFilterUserActions(userEntity.getId(),timeFilter,actionContext);
return createSummaryPageResponse(userEntity,numberOfLoginAttempts,applicationsProcessed,actionContextLabel,userActions);
}
public SummaryPageResponseBean createSummaryPageResponse(UserEntity user, Long numberOfLoginAttempts, Long applicationsProcessed, List<RoleActionContextEntity> actionContextLabel, List<UserActionEntity> 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<String> actionContextNames = actionContextLabel.stream()
.map(RoleActionContextEntity::getActionContext)
.collect(Collectors.toList());
response.setActionContextLabels(actionContextNames);
List<UserActionResponseBean> userAction = convertEntityToResponse(userActions);
response.setUserActions(userAction);
return response;
}
public List<UserActionEntity> getFilterUserActions(Long userId ,TimePeriodEnum timeFilter, String actionContext) {
LocalDateTime endDate = LocalDateTime.now();
LocalDateTime startDate = (timeFilter != null) ? getStartTimeFromFilter(timeFilter) : null;
Pageable pageable = PageRequest.of(0, 25);
Specification<UserActionEntity> spec = getUserActionsSpecification(userId, startDate, endDate, actionContext);
Page<UserActionEntity> 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<UserActionEntity> getUserActionsSpecification(Long userId, LocalDateTime startDate, LocalDateTime endDate, String actionContext) {
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 (actionContext != null) {
predicate = builder.and(predicate, builder.equal(root.get("actionContext"), actionContext));
}
query.orderBy(builder.desc(root.get("createdDate")));
return predicate;
};
}
private List<UserActionResponseBean> convertEntityToResponse(List<UserActionEntity> 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());
}
}