Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DMP-4736: Improve automated task - ApplyRetention #2644

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ and c.retentionPolicyType.fixedPolicyKey in ('PERM', 'MANUAL')


@Query("""
SELECT c
SELECT c.id
FROM CaseRetentionEntity c
WHERE c.currentState='PENDING'
AND c.courtCase.caseClosedTimestamp <= :pendingCutoff
ORDER BY c.createdDateTime DESC
"""
)
List<CaseRetentionEntity> findPendingRetention(OffsetDateTime pendingCutoff, Limit limit);
List<Integer> findPendingRetention(OffsetDateTime pendingCutoff, Limit limit);

Optional<CaseRetentionEntity> findTopByCourtCaseOrderByRetainUntilAppliedOnDesc(CourtCaseEntity courtCase);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service
Expand All @@ -32,18 +33,26 @@ public class ApplyRetentionProcessorImpl implements ApplyRetentionProcessor {

@Override
public void processApplyRetention(Integer batchSize) {
List<CaseRetentionEntity> caseRetentionEntities =
List<Integer> caseRetentionEntitiesIds =
caseRetentionRepository.findPendingRetention(currentTimeHelper.currentOffsetDateTime().minus(pendingRetentionDuration),
Limit.of(batchSize));
processList(caseRetentionEntities);
processList(caseRetentionEntitiesIds);

}

protected void processList(List<CaseRetentionEntity> caseRetentionEntities) {

@SuppressWarnings("java:S135")//Required to ensure we continue processing the list even if one of the elements fails
protected void processList(List<Integer> caseRetentionEntitiesIds) {
Set<Integer> processedCases = new HashSet<>();

//List is ordered in createdDateTime desc order
for (CaseRetentionEntity caseRetentionEntity : caseRetentionEntities) {
for (Integer caseRetentionEntitiesId : caseRetentionEntitiesIds) {
Optional<CaseRetentionEntity> caseRetentionEntityOpt = caseRetentionRepository.findById(caseRetentionEntitiesId);
if (caseRetentionEntityOpt.isEmpty()) {
log.error("CaseRetentionEntity with id {} not found", caseRetentionEntitiesId);
continue;
}
CaseRetentionEntity caseRetentionEntity = caseRetentionEntityOpt.get();
CourtCaseEntity courtCaseEntity = caseRetentionEntity.getCourtCase();
if (processedCases.contains(courtCaseEntity.getId())) {
caseRetentionEntity.setCurrentState(CaseRetentionStatus.IGNORED.name());
Expand Down