From d841b459d373ca91d7361ff0706c667435f8ec7b Mon Sep 17 00:00:00 2001 From: raja Date: Wed, 1 Jul 2026 13:05:58 +0100 Subject: [PATCH] SPRDT-1116 tune sql --- .../repository/PersistenceTestsIT.java | 187 ++++++++++++++++++ .../repository/HearingRepository.java | 6 +- 2 files changed, 190 insertions(+), 3 deletions(-) diff --git a/listing-integration-test-persistence/src/test/java/uk/gov/moj/cpp/listing/persistence/repository/PersistenceTestsIT.java b/listing-integration-test-persistence/src/test/java/uk/gov/moj/cpp/listing/persistence/repository/PersistenceTestsIT.java index 4158aa7f5..e5265d650 100644 --- a/listing-integration-test-persistence/src/test/java/uk/gov/moj/cpp/listing/persistence/repository/PersistenceTestsIT.java +++ b/listing-integration-test-persistence/src/test/java/uk/gov/moj/cpp/listing/persistence/repository/PersistenceTestsIT.java @@ -926,6 +926,129 @@ public void shouldSaveAndFindAvailableHearingByLinkedCase() { assertThat(actualHearings.get(0).getProperties().toString(), hasJsonPath("$.listedCases[0].linkedCases[0].caseUrn", equalTo(LINKED_CASE_URN))); } + @Test + public void shouldFindAllAvailableHearingsByCaseUrnRegardlessOfAllocation() { + //given - one allocated, one unallocated hearing, both with the same case reference + givenAvailableHearingsWithMixedAllocation(); + + final Set caseUrnSet = new HashSet<>(); + caseUrnSet.add(CASE_REFERENCE); + final Set masterDefendantIdSet = new HashSet<>(); + masterDefendantIdSet.add(EMPTY_STRING); + final Set jurisdictionTypeSet = new HashSet<>(); + jurisdictionTypeSet.add(JurisdictionType.CROWN.name()); + final Set linkedCaseUrn = new HashSet<>(); + linkedCaseUrn.add(EMPTY_STRING); + + //when + final List actualHearings = hearingRepository.findHearings( + jurisdictionTypeSet, + null, + caseUrnSet, + masterDefendantIdSet, + linkedCaseUrn, + null, + now()); + + //then - both allocated and unallocated hearings are returned + assertThat(actualHearings.size(), is(2)); + assertThat(extractFields(actualHearings, "$.id"), containsInAnyOrder(HEARING_ID.toString(), OTHER_HEARING_ID.toString())); + assertThat(extractFields(actualHearings, "$.allocated"), containsInAnyOrder(TRUE.toString(), FALSE.toString())); + assertThat(actualHearings.get(0).getProperties().toString(), hasJsonPath("$.listedCases[0].caseIdentifier.caseReference", equalTo(CASE_REFERENCE))); + } + + @Test + public void shouldFindAllAvailableHearingsByMasterDefendantIdRegardlessOfAllocation() { + //given - one allocated, one unallocated hearing, both with the same master defendant + givenAvailableHearingsWithMixedAllocation(); + + final Set caseUrnSet = new HashSet<>(); + caseUrnSet.add(EMPTY_STRING); + final Set masterDefendantIdSet = new HashSet<>(); + masterDefendantIdSet.add(MASTER_DEFENDANT_ID); + final Set jurisdictionTypeSet = new HashSet<>(); + jurisdictionTypeSet.add(JurisdictionType.CROWN.name()); + final Set linkedCaseUrn = new HashSet<>(); + linkedCaseUrn.add(EMPTY_STRING); + + //when + final List actualHearings = hearingRepository.findHearings( + jurisdictionTypeSet, + null, + caseUrnSet, + masterDefendantIdSet, + linkedCaseUrn, + null, + now()); + + //then - both allocated and unallocated hearings are returned + assertThat(actualHearings.size(), is(2)); + assertThat(extractFields(actualHearings, "$.id"), containsInAnyOrder(HEARING_ID.toString(), OTHER_HEARING_ID.toString())); + assertThat(extractFields(actualHearings, "$.allocated"), containsInAnyOrder(TRUE.toString(), FALSE.toString())); + assertThat(actualHearings.get(0).getProperties().toString(), hasJsonPath("$.listedCases[0].defendants[0].masterDefendantId", equalTo(MASTER_DEFENDANT_ID))); + } + + @Test + public void shouldFindAllAvailableHearingsByCaseUrnForLinkedCasesRegardlessOfAllocation() { + //given - one allocated, one unallocated hearing, both with a linked case pointing back to the same case reference + givenAvailableHearingsWithMixedAllocation(); + + final Set caseUrnSet = new HashSet<>(); + caseUrnSet.add(EMPTY_STRING); + final Set masterDefendantIdSet = new HashSet<>(); + masterDefendantIdSet.add(EMPTY_STRING); + final Set jurisdictionTypeSet = new HashSet<>(); + jurisdictionTypeSet.add(CROWN.name()); + final Set linkedCaseUrn = new HashSet<>(); + linkedCaseUrn.add(EMPTY_STRING); + final String caseUrnForLinkedCases = CASE_REFERENCE; + + //when + final List actualHearings = hearingRepository.findHearings( + jurisdictionTypeSet, + null, + caseUrnSet, + masterDefendantIdSet, + linkedCaseUrn, + caseUrnForLinkedCases, + now()); + + //then - both allocated and unallocated hearings are returned + assertThat(actualHearings.size(), is(2)); + assertThat(extractFields(actualHearings, "$.id"), containsInAnyOrder(HEARING_ID.toString(), OTHER_HEARING_ID.toString())); + assertThat(extractFields(actualHearings, "$.allocated"), containsInAnyOrder(TRUE.toString(), FALSE.toString())); + } + + @Test + public void shouldFindAllAvailableHearingsByLinkedCaseUrnRegardlessOfAllocation() { + //given - one allocated, one unallocated hearing, both with the same linked case URN + givenAvailableHearingsWithMixedAllocation(); + + final Set caseUrnSet = new HashSet<>(); + caseUrnSet.add(EMPTY_STRING); + final Set masterDefendantIdSet = new HashSet<>(); + masterDefendantIdSet.add(EMPTY_STRING); + final Set jurisdictionTypeSet = new HashSet<>(); + jurisdictionTypeSet.add(JurisdictionType.CROWN.name()); + final Set linkedCaseUrn = new HashSet<>(); + linkedCaseUrn.add(LINKED_CASE_URN); + + //when + final List actualHearings = hearingRepository.findHearings( + jurisdictionTypeSet, + null, + caseUrnSet, + masterDefendantIdSet, + linkedCaseUrn, + null, + now()); + + //then - both allocated and unallocated hearings are returned + assertThat(actualHearings.size(), is(2)); + assertThat(extractFields(actualHearings, "$.id"), containsInAnyOrder(HEARING_ID.toString(), OTHER_HEARING_ID.toString())); + assertThat(extractFields(actualHearings, "$.allocated"), containsInAnyOrder(TRUE.toString(), FALSE.toString())); + assertThat(actualHearings.get(0).getProperties().toString(), hasJsonPath("$.listedCases[0].linkedCases[0].caseUrn", equalTo(LINKED_CASE_URN))); + } @Test public void shouldFindUnscheduledHearingsWithoutParameters() { @@ -1804,6 +1927,70 @@ private List givenAvailableHearings() { return hearingsToBeCreated; } + private void givenAvailableHearingsWithMixedAllocation() { + hearingRepository.save(getHearingJson(hearingRepositoryContext() + .withHearingId(HEARING_ID) + .withCourtCentreId(COURT_CENTRE_ID) + .withCourtRoomId(COURT_ROOM_ID) + .withAllocated(TRUE) + .withVacated(NOT_VACATED) + .withAuthorityId(AUTHORITY_ID) + .withHearingType(HEARING_TYPE) + .withJurisdictionType(JURISDICTION_TYPE) + .withJudicialId(JUDICIAL_ID) + .withStartDate(START_DATE) + .withEndDate(END_DATE) + .withStartTime(START_TIME) + .withEndTime(END_TIME) + .withHearingDate(HEARING_DATE) + .withHearingDateDay1(DAY_1_HEARING_DATE) + .withStartTimeDay1(DAY_1_START_TIME) + .withEndTimeDay1(DAY_1_END_TIME) + .withCancelledDay1(false) + .withHearingDateDay2(DAY_2_HEARING_DATE) + .withStartTimeDay2(DAY_2_START_TIME) + .withEndTimeDay2(DAY_2_END_TIME) + .withCancelledDay2(false) + .withHearingDateDay3(DAY_3_HEARING_DATE) + .withStartTimeDay3(DAY_3_START_TIME) + .withEndTimeDay3(DAY_3_END_TIME) + .withCancelledDay3(false) + .withFileLocation(TEST_DATA_SAMPLE_MULTIDAY_HEARING_JSON) + .withMultidayHearing(true) + .build())); + + hearingRepository.save(getHearingJson(hearingRepositoryContext() + .withHearingId(OTHER_HEARING_ID) + .withCourtCentreId(COURT_CENTRE_ID) + .withCourtRoomId(COURT_ROOM_ID) + .withAllocated(FALSE) + .withVacated(NOT_VACATED) + .withAuthorityId(AUTHORITY_ID) + .withHearingType(HEARING_TYPE) + .withJurisdictionType(JURISDICTION_TYPE) + .withJudicialId(JUDICIAL_ID) + .withStartDate(START_DATE) + .withEndDate(END_DATE) + .withStartTime(START_TIME) + .withEndTime(END_TIME) + .withHearingDate(HEARING_DATE) + .withHearingDateDay1(DAY_1_HEARING_DATE) + .withStartTimeDay1(DAY_1_START_TIME) + .withEndTimeDay1(DAY_1_END_TIME) + .withCancelledDay1(false) + .withHearingDateDay2(DAY_2_HEARING_DATE) + .withStartTimeDay2(DAY_2_START_TIME) + .withEndTimeDay2(DAY_2_END_TIME) + .withCancelledDay2(false) + .withHearingDateDay3(DAY_3_HEARING_DATE) + .withStartTimeDay3(DAY_3_START_TIME) + .withEndTimeDay3(DAY_3_END_TIME) + .withCancelledDay3(false) + .withFileLocation(TEST_DATA_SAMPLE_MULTIDAY_HEARING_JSON) + .withMultidayHearing(true) + .build())); + } + private List givenAvailableHearingsForCrownAndMags() { final List hearingsToBeCreated = new ArrayList<>(); hearingsToBeCreated.add(getHearingJson(hearingRepositoryContext() diff --git a/listing-viewstore/listing-viewstore-persistence/src/main/java/uk/gov/moj/cpp/listing/persistence/repository/HearingRepository.java b/listing-viewstore/listing-viewstore-persistence/src/main/java/uk/gov/moj/cpp/listing/persistence/repository/HearingRepository.java index 5cb8f9402..af7084aee 100644 --- a/listing-viewstore/listing-viewstore-persistence/src/main/java/uk/gov/moj/cpp/listing/persistence/repository/HearingRepository.java +++ b/listing-viewstore/listing-viewstore-persistence/src/main/java/uk/gov/moj/cpp/listing/persistence/repository/HearingRepository.java @@ -828,9 +828,9 @@ public abstract List findHearings(@QueryParam("allocated") final boolea "and (h.end_date is null OR h.end_date >= :currentDate) " + "and (:hearingId is null or h.id != cast(cast(:hearingId as varchar) as uuid)) " + "AND " + - "(h.id in (select distinct h.id from hearing h " + - " inner join listed_cases lc on lc.hearing_id = h.id where UPPER(lc.case_reference) in (:caseUrnSet) " + - " and (:hearingId is null or h.id != cast(cast(:hearingId as varchar) as uuid))) " + + "(h.id in (select lc.hearing_id from listed_cases lc " + + " where UPPER(lc.case_reference) in (:caseUrnSet) " + + " and (:hearingId is null or lc.hearing_id != cast(cast(:hearingId as varchar) as uuid))) " + "OR " + "(h.id in (SELECT distinct(hrng.id) FROM hearing hrng inner join listed_cases lc3 on lc3.hearing_id = hrng.id " + " WHERE lc3.case_reference IN (select lnkCase.case_urn as linkedCaseUrn from hearing h2 " +