From caae70838a2a301feb54655890e6da9385b35759 Mon Sep 17 00:00:00 2001 From: Russell Dodd Date: Mon, 12 Feb 2024 13:42:50 +0000 Subject: [PATCH] Implement more search critera for search endpoint --- .../opal/repository/jpa/AddressSpecs.java | 36 +++++++++++++++++++ .../opal/repository/jpa/BaseCourtSpecs.java | 36 +++++++++++++++++++ .../hmcts/opal/repository/jpa/CourtSpecs.java | 27 ++++++++++++-- .../opal/repository/jpa/EntitySpecs.java | 21 +++++++++-- 4 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 src/main/java/uk/gov/hmcts/opal/repository/jpa/AddressSpecs.java create mode 100644 src/main/java/uk/gov/hmcts/opal/repository/jpa/BaseCourtSpecs.java diff --git a/src/main/java/uk/gov/hmcts/opal/repository/jpa/AddressSpecs.java b/src/main/java/uk/gov/hmcts/opal/repository/jpa/AddressSpecs.java new file mode 100644 index 000000000..037b282b8 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/repository/jpa/AddressSpecs.java @@ -0,0 +1,36 @@ +package uk.gov.hmcts.opal.repository.jpa; + +import org.springframework.data.jpa.domain.Specification; +import uk.gov.hmcts.opal.dto.search.AddressSearch; +import uk.gov.hmcts.opal.entity.AddressEntity; +import uk.gov.hmcts.opal.entity.AddressEntity_; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public abstract class AddressSpecs extends EntitySpecs { + + @SuppressWarnings("unchecked") + public List>> findByAddressCriteria(AddressSearch criteria) { + return new ArrayList(Arrays.asList( + notBlank(criteria.getName()).map(this::equalsName), + notBlank(criteria.getAddressLine()).map(this::equalsAddressLine), + notBlank(criteria.getPostcode()).map(this::equalsPostcode) + )); + } + + public Specification equalsName(String name) { + return (root, query, builder) -> builder.equal(root.get(AddressEntity_.name), name); + } + + public Specification equalsAddressLine(String addressLine) { + return (root, query, builder) -> builder.equal(root.get(AddressEntity_.addressLine1), addressLine); + } + + public Specification equalsPostcode(String postcode) { + return (root, query, builder) -> builder.equal(root.get(AddressEntity_.postcode), postcode); + } + +} diff --git a/src/main/java/uk/gov/hmcts/opal/repository/jpa/BaseCourtSpecs.java b/src/main/java/uk/gov/hmcts/opal/repository/jpa/BaseCourtSpecs.java new file mode 100644 index 000000000..86cc36f5e --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/repository/jpa/BaseCourtSpecs.java @@ -0,0 +1,36 @@ +package uk.gov.hmcts.opal.repository.jpa; + +import org.springframework.data.jpa.domain.Specification; +import uk.gov.hmcts.opal.dto.search.BaseCourtSearch; +import uk.gov.hmcts.opal.entity.EnforcerCourtBaseEntity; +import uk.gov.hmcts.opal.entity.EnforcerCourtBaseEntity_; + +import java.util.List; +import java.util.Optional; + +public abstract class BaseCourtSpecs extends AddressSpecs { + + @SuppressWarnings("unchecked") + public List>> findByBaseCourtCriteria(BaseCourtSearch criteria) { + return combine(findByAddressCriteria(criteria), + notBlank(criteria.getBusinessUnitId()).map(this::equalsBusinessUnitId), + notBlank(criteria.getNameCy()).map(this::equalsNameCy), + notBlank(criteria.getAddressLineCy()).map(this::equalsAddressLineCy)); + } + + + public Specification equalsBusinessUnitId(String businessUnitId) { + return (root, query, builder) -> builder.equal(root.get(EnforcerCourtBaseEntity_.businessUnitId), + businessUnitId); + } + + public Specification equalsNameCy(String nameCy) { + return (root, query, builder) -> builder.equal(root.get(EnforcerCourtBaseEntity_.nameCy), nameCy); + } + + public Specification equalsAddressLineCy(String addressLineCy) { + return (root, query, builder) -> builder.equal(root.get(EnforcerCourtBaseEntity_.addressLine1Cy), + addressLineCy); + } + +} diff --git a/src/main/java/uk/gov/hmcts/opal/repository/jpa/CourtSpecs.java b/src/main/java/uk/gov/hmcts/opal/repository/jpa/CourtSpecs.java index 36e22b10a..5bf03dbe4 100644 --- a/src/main/java/uk/gov/hmcts/opal/repository/jpa/CourtSpecs.java +++ b/src/main/java/uk/gov/hmcts/opal/repository/jpa/CourtSpecs.java @@ -5,11 +5,17 @@ import uk.gov.hmcts.opal.entity.CourtEntity; import uk.gov.hmcts.opal.entity.CourtEntity_; -public class CourtSpecs extends EntitySpecs { +public class CourtSpecs extends BaseCourtSpecs { public Specification findBySearchCriteria(CourtSearchDto criteria) { return Specification.allOf(specificationList( - notBlank(criteria.getCourtId()).map(CourtSpecs::equalsCourtId) + findByBaseCourtCriteria(criteria), + notBlank(criteria.getCourtId()).map(CourtSpecs::equalsCourtId), + notBlank(criteria.getCourtCode()).map(CourtSpecs::equalsCourtCode), + notBlank(criteria.getParentCourtId()).map(CourtSpecs::equalsParentCourtId), + notBlank(criteria.getLocalJusticeAreaId()).map(CourtSpecs::equalsLocalJusticeAreaId), + notBlank(criteria.getNationalCourtCode()).map(CourtSpecs::equalsNationalCourtCode) + )); } @@ -17,4 +23,21 @@ public static Specification equalsCourtId(String courtId) { return (root, query, builder) -> builder.equal(root.get(CourtEntity_.courtId), courtId); } + public static Specification equalsCourtCode(String courtCode) { + return (root, query, builder) -> builder.equal(root.get(CourtEntity_.courtCode), courtCode); + } + + public static Specification equalsParentCourtId(String parentCourtId) { + return (root, query, builder) -> builder.equal(root.get(CourtEntity_.parentCourtId), parentCourtId); + } + + public static Specification equalsLocalJusticeAreaId(String localJusticeAreaId) { + return (root, query, builder) -> builder.equal(root.get(CourtEntity_.localJusticeAreaId), localJusticeAreaId); + } + + public static Specification equalsNationalCourtCode(String nationalCourtCode) { + return (root, query, builder) -> builder.equal(root.get(CourtEntity_.nationalCourtCode), nationalCourtCode); + } + + } diff --git a/src/main/java/uk/gov/hmcts/opal/repository/jpa/EntitySpecs.java b/src/main/java/uk/gov/hmcts/opal/repository/jpa/EntitySpecs.java index f264b5d96..d3e596332 100644 --- a/src/main/java/uk/gov/hmcts/opal/repository/jpa/EntitySpecs.java +++ b/src/main/java/uk/gov/hmcts/opal/repository/jpa/EntitySpecs.java @@ -13,14 +13,31 @@ public abstract class EntitySpecs { @SafeVarargs - public final List> specificationList( - Optional>... optionalSpecs) { + public final List> specificationList(Optional>... optionalSpecs) { return Arrays.stream(optionalSpecs) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); } + @SafeVarargs + public final List> specificationList(List>> specsList, + Optional>... optionalSpecs) { + return combine(specsList, optionalSpecs) + .stream().filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toList()); + } + + @SafeVarargs + public final List>> combine(List>> specsList, + Optional>... optionalSpecs) { + for (Optional> spec: optionalSpecs) { + specsList.add(spec); + } + return specsList; + } + public Optional notBlank(String candidate) { return Optional.ofNullable(candidate).filter(s -> !s.isBlank()); }