Done ticket GEPAFINBE-213
This commit is contained in:
@@ -26,6 +26,7 @@ import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundExceptio
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -1032,7 +1033,7 @@ public class ApplicationAmendmentRequestDao {
|
||||
public ApplicationAmendmentRequestResponse closeAmendmentRequest(Long id, CloseAmendmentRequest closeAmendmentRequest) {
|
||||
|
||||
log.info("Closing application amendement with ID: {}", id);
|
||||
ApplicationAmendmentRequestEntity existingApplicationAmendment = validateApplicationAmendmentRequest(id);
|
||||
ApplicationAmendmentRequestEntity existingApplicationAmendment = validatApplicationAmendmentRequestByStatus(id,ApplicationAmendmentRequestEnum.AWAITING.getValue());
|
||||
//cloned entity for old data and versioning
|
||||
ApplicationAmendmentRequestEntity oldApplicationAmendmentEntity = Utils.getClonedEntityForData(existingApplicationAmendment);
|
||||
List<ApplicationAmendmentRequestEntity> amendmentRequestList = applicationAmendmentRequestRepository.findAllByApplicationEvaluationIdAndIsDeletedFalse(
|
||||
@@ -1042,17 +1043,18 @@ public class ApplicationAmendmentRequestDao {
|
||||
// Check if this is the last amendment being closed
|
||||
boolean isLastRemaining = amendmentRequestList.stream()
|
||||
.filter(amendment -> !amendment.getId().equals(id)) // Exclude the current amendment
|
||||
.allMatch(amendment -> amendment.getStatus().equals(ApplicationAmendmentRequestEnum.CLOSE.getValue()));
|
||||
.allMatch(amendment -> amendment.getStatus().equals(ApplicationAmendmentRequestEnum.CLOSE.getValue()) || amendment.getStatus().equals(ApplicationAmendmentRequestEnum.EXPIRED.getValue()));
|
||||
|
||||
|
||||
if (isLastRemaining) {
|
||||
log.info("The current amendment is the last remaining one to be closed.");
|
||||
applicationAmendmentRequestDao.calculateEndDateAndSuspensionDays(existingApplicationAmendment.getApplicationEvaluationEntity());
|
||||
}
|
||||
setIfUpdated(existingApplicationAmendment::getInternalNote, existingApplicationAmendment::setInternalNote, closeAmendmentRequest.getInternalNote());
|
||||
setIfUpdated(existingApplicationAmendment::getStatus, existingApplicationAmendment::setStatus, ApplicationAmendmentRequestEnum.CLOSE.getValue());
|
||||
existingApplicationAmendment.setClosingDate(LocalDateTime.now());
|
||||
ApplicationAmendmentRequestEntity updatedApplicationAmendment = saveApplicationAmendmentRequestEntity(existingApplicationAmendment, oldApplicationAmendmentEntity,
|
||||
VersionActionTypeEnum.UPDATE);
|
||||
if (isLastRemaining) {
|
||||
log.info("The current amendment is the last remaining one to be closed.");
|
||||
applicationAmendmentRequestDao.calculateEndDateAndSuspensionDaysOnExpirationOrClosing(existingApplicationAmendment.getApplicationEvaluationEntity());
|
||||
}
|
||||
ApplicationAmendmentRequestResponse response = convertEntityToResponse(updatedApplicationAmendment,false);
|
||||
|
||||
List<ApplicationAmendmentRequestEntity> amendmentRequests = applicationAmendmentRequestRepository.findAllByApplicationEvaluationIdAndIsDeletedFalse(
|
||||
@@ -1104,15 +1106,17 @@ public class ApplicationAmendmentRequestDao {
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
public ApplicationAmendmentRequestResponse extendResponseDays(Long id, Long newResponseDays) {
|
||||
ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity = validateApplicationAmendmentRequest(id);
|
||||
ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity = validatApplicationAmendmentRequestByStatus(id,ApplicationAmendmentRequestEnum.EXPIRED.getValue());
|
||||
|
||||
if (newResponseDays != null && newResponseDays > 0) {
|
||||
ApplicationAmendmentRequestEntity oldApplicationAmendmentEntity = Utils.getClonedEntityForData(applicationAmendmentRequestEntity);
|
||||
ApplicationAmendmentRequestEntity oldApplicationAmendmentEntity = Utils.getClonedEntityForData(applicationAmendmentRequestEntity);
|
||||
Long currentResponseDays = applicationAmendmentRequestEntity.getResponseDays() != null ? applicationAmendmentRequestEntity.getResponseDays() : 0L;
|
||||
applicationAmendmentRequestEntity.setResponseDays(currentResponseDays + newResponseDays);
|
||||
applicationAmendmentRequestEntity.setEndDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now().plusDays(applicationAmendmentRequestEntity.getResponseDays())));
|
||||
applicationAmendmentRequestEntity.setEndDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now().plusDays(newResponseDays)));
|
||||
applicationAmendmentRequestEntity.setStatus(ApplicationAmendmentRequestEnum.AWAITING.getValue());
|
||||
applicationAmendmentRequestEntity.getApplicationEvaluationEntity().setStatus(ApplicationEvaluationStatusTypeEnum.SOCCORSO.getValue());
|
||||
applicationAmendmentRequestEntity.getApplicationEvaluationEntity().getAssignedApplicationsEntity().getApplication().setStatus(ApplicationStatusTypeEnum.SOCCORSO.getValue());
|
||||
applicationAmendmentRequestRepository.save(applicationAmendmentRequestEntity);
|
||||
|
||||
/** This code is responsible for adding a version history log for the "Update Application Amendment" operation. **/
|
||||
@@ -1219,26 +1223,26 @@ public class ApplicationAmendmentRequestDao {
|
||||
return Utils.replacePlaceholders(template.getHtmlContent(), bodyPlaceholders);
|
||||
}
|
||||
|
||||
public ApplicationEvaluationEntity calculateEndDateAndSuspensionDays(ApplicationEvaluationEntity applicationEvaluationEntity){
|
||||
LocalDateTime currentDate=DateTimeUtil.DateServerToUTC(LocalDateTime.now());
|
||||
LocalDateTime endDate=currentDate.plusDays(applicationEvaluationEntity.getRemainingDays());
|
||||
Long suspendedDays = ChronoUnit.DAYS.between(applicationEvaluationEntity.getStopDateTime(), currentDate);
|
||||
|
||||
ApplicationEvaluationEntity oldApplicationEvaluationEntity = Utils.getClonedEntityForData(applicationEvaluationEntity);
|
||||
applicationEvaluationEntity.setEndDate(endDate);
|
||||
if(applicationEvaluationEntity.getSuspendedDays() == null) {
|
||||
applicationEvaluationEntity.setSuspendedDays(0L);
|
||||
}
|
||||
applicationEvaluationEntity.setSuspendedDays(applicationEvaluationEntity.getSuspendedDays()+suspendedDays);
|
||||
ApplicationEvaluationEntity applicationEvaluation = applicationEvaluationRepository.save(applicationEvaluationEntity);
|
||||
|
||||
/** This code is responsible for adding a version history log for the "Update Application Amendment" operation. **/
|
||||
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationEvaluationEntity).newData(applicationEvaluation).build());
|
||||
|
||||
return applicationEvaluation;
|
||||
|
||||
|
||||
}
|
||||
// public ApplicationEvaluationEntity calculateEndDateAndSuspensionDays(ApplicationEvaluationEntity applicationEvaluationEntity){
|
||||
// LocalDateTime currentDate=DateTimeUtil.DateServerToUTC(LocalDateTime.now());
|
||||
// LocalDateTime endDate=currentDate.plusDays(applicationEvaluationEntity.getRemainingDays());
|
||||
// Long suspendedDays = ChronoUnit.DAYS.between(applicationEvaluationEntity.getStopDateTime(), currentDate);
|
||||
//
|
||||
// ApplicationEvaluationEntity oldApplicationEvaluationEntity = Utils.getClonedEntityForData(applicationEvaluationEntity);
|
||||
// applicationEvaluationEntity.setEndDate(endDate);
|
||||
// if(applicationEvaluationEntity.getSuspendedDays() == null) {
|
||||
// applicationEvaluationEntity.setSuspendedDays(0L);
|
||||
// }
|
||||
// applicationEvaluationEntity.setSuspendedDays(applicationEvaluationEntity.getSuspendedDays()+suspendedDays);
|
||||
// ApplicationEvaluationEntity applicationEvaluation = applicationEvaluationRepository.save(applicationEvaluationEntity);
|
||||
//
|
||||
// /** This code is responsible for adding a version history log for the "Update Application Amendment" operation. **/
|
||||
// loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationEvaluationEntity).newData(applicationEvaluation).build());
|
||||
//
|
||||
// return applicationEvaluation;
|
||||
//
|
||||
//
|
||||
// }
|
||||
|
||||
private GetAllAmendmentResponseBean initializeGetAllBasicResponse(ApplicationAmendmentRequestEntity entity) {
|
||||
GetAllAmendmentResponseBean response = new GetAllAmendmentResponseBean();
|
||||
@@ -1542,5 +1546,93 @@ public class ApplicationAmendmentRequestDao {
|
||||
return applicationAmendmentRequestViewResponse;
|
||||
}
|
||||
|
||||
public ApplicationEvaluationEntity calculateEndDateAndSuspensionDaysOnExpirationOrClosing
|
||||
(ApplicationEvaluationEntity applicationEvaluationEntity) {
|
||||
|
||||
|
||||
// Fetch all linked amendments
|
||||
List<ApplicationAmendmentRequestEntity> amendments =
|
||||
applicationAmendmentRequestRepository.findAllByApplicationEvaluationIdAndIsDeletedFalse(applicationEvaluationEntity.getId());
|
||||
|
||||
// Recalculate suspension
|
||||
long suspendedDays = calculateSuspendedDays(amendments);
|
||||
|
||||
// Update end date and save
|
||||
|
||||
ApplicationEvaluationEntity oldEntity = Utils.getClonedEntityForData(applicationEvaluationEntity);
|
||||
HubEntity hub = hubService.valdateHub(applicationEvaluationEntity.getAssignedApplicationsEntity().getApplication().getHubId());
|
||||
Long initialDays = (hub != null) ? hub.getEvaluationExpirationDays() : 30L;
|
||||
LocalDateTime endDate = applicationEvaluationEntity.getStartDate().plusDays(suspendedDays+initialDays);
|
||||
applicationEvaluationEntity.setEndDate(endDate);
|
||||
applicationEvaluationEntity.setSuspendedDays(suspendedDays);
|
||||
ApplicationEvaluationEntity updated = applicationEvaluationRepository.save(applicationEvaluationEntity);
|
||||
|
||||
loggingUtil.addVersionHistory(VersionHistoryRequest.builder()
|
||||
.request(request)
|
||||
.actionType(VersionActionTypeEnum.UPDATE)
|
||||
.oldData(oldEntity)
|
||||
.newData(updated)
|
||||
.build());
|
||||
|
||||
return updated;
|
||||
}
|
||||
|
||||
public long calculateSuspendedDays(List<ApplicationAmendmentRequestEntity> amendments) {
|
||||
List<Pair<LocalDateTime, LocalDateTime>> periods = amendments.stream()
|
||||
.filter(amendmentRequest -> amendmentRequest.getStartDate() != null)
|
||||
.map(amendmentRequest -> {
|
||||
LocalDateTime start = amendmentRequest.getStartDate();
|
||||
LocalDateTime end;
|
||||
|
||||
String status = amendmentRequest.getStatus();
|
||||
if (Boolean.TRUE.equals(ApplicationAmendmentRequestEnum.CLOSE.getValue().equals(status)) && amendmentRequest.getClosingDate() != null) {
|
||||
end = amendmentRequest.getClosingDate();
|
||||
} else if (Boolean.TRUE.equals(ApplicationAmendmentRequestEnum.EXPIRED.getValue().equals(status)) && amendmentRequest.getEndDate() != null) {
|
||||
end = amendmentRequest.getStartDate().plusDays(amendmentRequest.getResponseDays());
|
||||
}else {
|
||||
end= amendmentRequest.getEndDate();
|
||||
}
|
||||
|
||||
return Pair.of(start, end);
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.sorted(Comparator.comparing(Pair::getLeft))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
long totalDays = 0;
|
||||
LocalDateTime currentStart = null;
|
||||
LocalDateTime currentEnd = null;
|
||||
|
||||
for (Pair<LocalDateTime, LocalDateTime> period : periods) {
|
||||
if (currentStart == null) {
|
||||
currentStart = period.getLeft();
|
||||
currentEnd = period.getRight();
|
||||
} else if (!period.getLeft().isAfter(currentEnd)) {
|
||||
// Merge overlapping/touching periods
|
||||
currentEnd = currentEnd.isAfter(period.getRight()) ? currentEnd : period.getRight();
|
||||
} else {
|
||||
// Non-overlapping: count previous period
|
||||
totalDays += ChronoUnit.DAYS.between(currentStart.toLocalDate(), currentEnd.toLocalDate());
|
||||
currentStart = period.getLeft();
|
||||
currentEnd = period.getRight();
|
||||
}
|
||||
}
|
||||
|
||||
if (currentStart != null && currentEnd != null) {
|
||||
totalDays += ChronoUnit.DAYS.between(currentStart.toLocalDate(), currentEnd.toLocalDate());
|
||||
}
|
||||
|
||||
return totalDays;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ApplicationAmendmentRequestEntity validatApplicationAmendmentRequestByStatus(Long id,String status) {
|
||||
ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity= applicationAmendmentRequestRepository.findByIdAndIsDeletedFalseAndStatus(id,status);
|
||||
if(applicationAmendmentRequestEntity==null){
|
||||
throw new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.APPLICATION_AMENDMENT_NOT_FOUND_MSG));
|
||||
}
|
||||
return applicationAmendmentRequestEntity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,4 +150,6 @@ public interface ApplicationAmendmentRequestRepository extends JpaRepository<App
|
||||
|
||||
@Query("SELECT COUNT(a) FROM ApplicationAmendmentRequestEntity a WHERE a.applicationId IN :applicationIds AND a.status IN :statuses AND a.isDeleted = false")
|
||||
Long countAmendmentsByApplicationIds(@Param("applicationIds") List<Long> applicationIds, @Param("statuses") List<String> statuses);
|
||||
|
||||
ApplicationAmendmentRequestEntity findByIdAndIsDeletedFalseAndStatus(Long id,String status);
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ public class ApplicationAmendmentScheduler {
|
||||
.findEvaluationsWithoutActiveAmendmentsByIds(applicationEvaluationIds);
|
||||
evaluationsWithoutActiveAmendmentList.forEach(evaluation -> {
|
||||
try {
|
||||
applicationAmendmentRequestDao.calculateEndDateAndSuspensionDays(evaluation);
|
||||
applicationAmendmentRequestDao.calculateEndDateAndSuspensionDaysOnExpirationOrClosing(evaluation);
|
||||
|
||||
updateEvaluationStatus(evaluation);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user