Skip to content

Commit

Permalink
Implement more search critera for search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyHMCTS committed Feb 12, 2024
1 parent 166666b commit caae708
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 4 deletions.
36 changes: 36 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/repository/jpa/AddressSpecs.java
Original file line number Diff line number Diff line change
@@ -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<E extends AddressEntity> extends EntitySpecs<E> {

@SuppressWarnings("unchecked")
public List<Optional<Specification<E>>> 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<E> equalsName(String name) {
return (root, query, builder) -> builder.equal(root.get(AddressEntity_.name), name);
}

public Specification<E> equalsAddressLine(String addressLine) {
return (root, query, builder) -> builder.equal(root.get(AddressEntity_.addressLine1), addressLine);
}

public Specification<E> equalsPostcode(String postcode) {
return (root, query, builder) -> builder.equal(root.get(AddressEntity_.postcode), postcode);
}

}
36 changes: 36 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/repository/jpa/BaseCourtSpecs.java
Original file line number Diff line number Diff line change
@@ -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<E extends EnforcerCourtBaseEntity> extends AddressSpecs<E> {

@SuppressWarnings("unchecked")
public List<Optional<Specification<E>>> 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<E> equalsBusinessUnitId(String businessUnitId) {
return (root, query, builder) -> builder.equal(root.get(EnforcerCourtBaseEntity_.businessUnitId),
businessUnitId);
}

public Specification<E> equalsNameCy(String nameCy) {
return (root, query, builder) -> builder.equal(root.get(EnforcerCourtBaseEntity_.nameCy), nameCy);
}

public Specification<E> equalsAddressLineCy(String addressLineCy) {
return (root, query, builder) -> builder.equal(root.get(EnforcerCourtBaseEntity_.addressLine1Cy),
addressLineCy);
}

}
27 changes: 25 additions & 2 deletions src/main/java/uk/gov/hmcts/opal/repository/jpa/CourtSpecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,39 @@
import uk.gov.hmcts.opal.entity.CourtEntity;
import uk.gov.hmcts.opal.entity.CourtEntity_;

public class CourtSpecs extends EntitySpecs<CourtEntity> {
public class CourtSpecs extends BaseCourtSpecs<CourtEntity> {

public Specification<CourtEntity> 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)

));
}

public static Specification<CourtEntity> equalsCourtId(String courtId) {
return (root, query, builder) -> builder.equal(root.get(CourtEntity_.courtId), courtId);
}

public static Specification<CourtEntity> equalsCourtCode(String courtCode) {
return (root, query, builder) -> builder.equal(root.get(CourtEntity_.courtCode), courtCode);
}

public static Specification<CourtEntity> equalsParentCourtId(String parentCourtId) {
return (root, query, builder) -> builder.equal(root.get(CourtEntity_.parentCourtId), parentCourtId);
}

public static Specification<CourtEntity> equalsLocalJusticeAreaId(String localJusticeAreaId) {
return (root, query, builder) -> builder.equal(root.get(CourtEntity_.localJusticeAreaId), localJusticeAreaId);
}

public static Specification<CourtEntity> equalsNationalCourtCode(String nationalCourtCode) {
return (root, query, builder) -> builder.equal(root.get(CourtEntity_.nationalCourtCode), nationalCourtCode);
}


}
21 changes: 19 additions & 2 deletions src/main/java/uk/gov/hmcts/opal/repository/jpa/EntitySpecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,31 @@
public abstract class EntitySpecs<E> {

@SafeVarargs
public final List<Specification<E>> specificationList(
Optional<Specification<E>>... optionalSpecs) {
public final List<Specification<E>> specificationList(Optional<Specification<E>>... optionalSpecs) {
return Arrays.stream(optionalSpecs)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}

@SafeVarargs
public final List<Specification<E>> specificationList(List<Optional<Specification<E>>> specsList,
Optional<Specification<E>>... optionalSpecs) {
return combine(specsList, optionalSpecs)
.stream().filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}

@SafeVarargs
public final List<Optional<Specification<E>>> combine(List<Optional<Specification<E>>> specsList,
Optional<Specification<E>>... optionalSpecs) {
for (Optional<Specification<E>> spec: optionalSpecs) {
specsList.add(spec);
}
return specsList;
}

public Optional<String> notBlank(String candidate) {
return Optional.ofNullable(candidate).filter(s -> !s.isBlank());
}
Expand Down

0 comments on commit caae708

Please sign in to comment.