Skip to content

Commit

Permalink
Filter Courts reference data by business unit
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyHMCTS committed Jun 21, 2024
1 parent eee1f7e commit 209f172
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ void testPostCourtsSearch_WhenCourtDoesNotExist() throws Exception {

@Test
void testGetCourtRefData() throws Exception {
CourtReferenceData refData = new CourtReferenceData(1L, (short)11,"Main Court", null, "MN1234");
CourtReferenceData refData = new CourtReferenceData(1L, (short)007, (short)11,
"Main Court", null, "MN1234");

when(courtService.getReferenceData(any())).thenReturn(singletonList(refData));
when(courtService.getReferenceData(any(), any())).thenReturn(singletonList(refData));

mockMvc.perform(get("/api/court/ref-data")
.header("authorization", "Bearer some_value"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.hmcts.opal.dto.reference.CourtReferenceDataResults;
import uk.gov.hmcts.opal.dto.search.CourtSearchDto;
Expand Down Expand Up @@ -76,10 +77,10 @@ public ResponseEntity<List<CourtEntity>> postCourtsSearch(@RequestBody CourtSear
@GetMapping(value = {"/ref-data", "/ref-data/", "/ref-data/{filter}"})
@Operation(summary = "Returns courts as reference data with an optional filter applied")
public ResponseEntity<CourtReferenceDataResults> getCourtRefData(
@PathVariable Optional<String> filter) {
log.info(":GET:getCourtRefData: query: \n{}", filter);
@PathVariable Optional<String> filter, @RequestParam Optional<Short> businessUnit) {
log.info(":GET:getCourtRefData: business unit: {}, filter string: {}", businessUnit, filter);

List<CourtReferenceData> refData = opalCourtService.getReferenceData(filter);
List<CourtReferenceData> refData = opalCourtService.getReferenceData(filter, businessUnit);

log.info(":GET:getCourtRefData: court reference data count: {}", refData.size());
return ResponseEntity.ok(CourtReferenceDataResults.builder().refData(refData).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public abstract class BaseCourtSearch extends AddressSearch {
private String businessUnitId;
public abstract class AddressCySearch extends AddressSearch {
private String nameCy;
private String addressLineCy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
@Builder
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class CourtSearchDto extends BaseCourtSearch implements ToJsonString {
public class CourtSearchDto extends AddressCySearch implements ToJsonString {

private String courtId;
private String businessUnitId;
private String courtCode;
private String parentCourtId;
private String localJusticeAreaId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
@Builder
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class EnforcerSearchDto extends BaseCourtSearch implements ToJsonString {
public class EnforcerSearchDto extends AddressCySearch implements ToJsonString {

private String enforcerId;
private String businessUnitId;
private String enforcerCode;
private String warrantReferenceSequence;
private String warrantRegisterSequence;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.Column;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -18,11 +16,7 @@
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class EnforcerCourtBaseEntity extends AddressEntity {

@ManyToOne
@JoinColumn(name = "business_unit_id", updatable = false)
private BusinessUnitEntity businessUnit;
public class AddressCyEntity extends AddressEntity {

@Column(name = "name_cy", length = 35)
private String nameCy;
Expand Down
36 changes: 35 additions & 1 deletion src/main/java/uk/gov/hmcts/opal/entity/CourtEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
@AllArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "courtId")
public class CourtEntity extends EnforcerCourtBaseEntity {
public class CourtEntity extends AddressCyEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "court_id")
private Long courtId;

@ManyToOne
@JoinColumn(name = "business_unit_id", updatable = false)
private BusinessUnitEntity businessUnit;

@Column(name = "court_code", nullable = false)
private Short courtCode;

Expand All @@ -48,4 +52,34 @@ public class CourtEntity extends EnforcerCourtBaseEntity {
@Column(name = "national_court_code", length = 7)
private String nationalCourtCode;

@Column(name = "gob_enforcing_court_code")
private Short gobEnforcingCourtCode;

@Column(name = "lja")
private Short lja;

@Column(name = "court_type", length = 2)
private String courtType;

@Column(name = "division", length = 2)
private String division;

@Column(name = "session", length = 2)
private String session;

@Column(name = "start_time", length = 8)
private String startTime;

@Column(name = "max_load")
private Long maxLoad;

@Column(name = "record_session_times", length = 1)
private String recordSessionTimes;

@Column(name = "max_court_duration")
private Long maxCourtDuration;

@Column(name = "group_code", length = 24)
private String groupCode;

}
8 changes: 7 additions & 1 deletion src/main/java/uk/gov/hmcts/opal/entity/EnforcerEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -24,13 +26,17 @@
@AllArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "enforcerId")
public class EnforcerEntity extends EnforcerCourtBaseEntity {
public class EnforcerEntity extends AddressCyEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "enforcer_id", nullable = false)
private Long enforcerId;

@ManyToOne
@JoinColumn(name = "business_unit_id", updatable = false)
private BusinessUnitEntity businessUnit;

@Column(name = "enforcer_code", nullable = false)
private Short enforcerCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.hmcts.opal.entity.projection;

public record CourtReferenceData(Long courtId, Short courtCode, String name, String nameCy, String nationalCourtCode) {
public record CourtReferenceData(Long courtId, Short businessUnitId, Short courtCode, String name,
String nameCy, String nationalCourtCode) {

}
48 changes: 48 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/repository/jpa/AddressCySpecs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package uk.gov.hmcts.opal.repository.jpa;

import org.springframework.data.jpa.domain.Specification;
import uk.gov.hmcts.opal.dto.search.AddressCySearch;
import uk.gov.hmcts.opal.entity.AddressCyEntity;
import uk.gov.hmcts.opal.entity.AddressCyEntity_;

import java.util.List;
import java.util.Optional;


public abstract class AddressCySpecs<E extends AddressCyEntity> extends AddressSpecs<E> {

@SuppressWarnings("unchecked")
public List<Optional<Specification<E>>> findByBaseCourtCriteria(AddressCySearch criteria) {
return combine(findByAddressCriteria(criteria),
notBlank(criteria.getNameCy()).map(this::likeNameCy),
notBlank(criteria.getAddressLineCy()).map(this::likeAnyAddressLineCy));
}

public Specification<E> likeNameCy(String nameCy) {
return (root, query, builder) ->
likeWildcardPredicate(root.get(AddressCyEntity_.nameCy), builder, nameCy);
}

public Specification<E> likeAnyAddressLineCy(String addressLine) {
String addressLinePattern = "%" + addressLine.toLowerCase() + "%";
return Specification.anyOf(
likeAddressLine1Cy(addressLinePattern),
likeAddressLine2Cy(addressLinePattern),
likeAddressLine3Cy(addressLinePattern));
}

private Specification<E> likeAddressLine1Cy(String addressLinePattern) {
return (root, query, builder) ->
likeLowerCasePredicate(root.get(AddressCyEntity_.addressLine1Cy), builder, addressLinePattern);
}

private Specification<E> likeAddressLine2Cy(String addressLinePattern) {
return (root, query, builder) ->
likeLowerCasePredicate(root.get(AddressCyEntity_.addressLine2Cy), builder, addressLinePattern);
}

private Specification<E> likeAddressLine3Cy(String addressLinePattern) {
return (root, query, builder) ->
likeLowerCasePredicate(root.get(AddressCyEntity_.addressLine3Cy), builder, addressLinePattern);
}
}
65 changes: 0 additions & 65 deletions src/main/java/uk/gov/hmcts/opal/repository/jpa/BaseCourtSpecs.java

This file was deleted.

19 changes: 16 additions & 3 deletions src/main/java/uk/gov/hmcts/opal/repository/jpa/CourtSpecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import jakarta.persistence.criteria.Predicate;
import org.springframework.data.jpa.domain.Specification;
import uk.gov.hmcts.opal.dto.search.CourtSearchDto;
import uk.gov.hmcts.opal.entity.BusinessUnitEntity;
import uk.gov.hmcts.opal.entity.CourtEntity;
import uk.gov.hmcts.opal.entity.CourtEntity_;
import uk.gov.hmcts.opal.entity.LocalJusticeAreaEntity;

import java.util.Optional;

import static uk.gov.hmcts.opal.repository.jpa.BusinessUnitSpecs.equalsBusinessUnitIdPredicate;
import static uk.gov.hmcts.opal.repository.jpa.LocalJusticeAreaSpecs.equalsLocalJusticeAreaIdPredicate;

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

public Specification<CourtEntity> findBySearchCriteria(CourtSearchDto criteria) {
return Specification.allOf(specificationList(
Expand All @@ -27,9 +29,11 @@ public Specification<CourtEntity> findBySearchCriteria(CourtSearchDto criteria)
));
}

public Specification<CourtEntity> referenceDataFilter(Optional<String> filter) {
public Specification<CourtEntity> referenceDataFilter(Optional<String> filter,
Optional<Short> businessUnitId) {
return Specification.allOf(specificationList(
filter.filter(s -> !s.isBlank()).map(this::likeAnyCourt)
filter.filter(s -> !s.isBlank()).map(this::likeAnyCourt),
businessUnitId.map(CourtSpecs::equalsBusinessUnitId)
));
}

Expand All @@ -41,6 +45,11 @@ public static Predicate equalsCourtIdPredicate(From<?, CourtEntity> from, Criter
return builder.equal(from.get(CourtEntity_.courtId), courtId);
}

public static Specification<CourtEntity> equalsBusinessUnitId(Short businessUnitId) {
return (root, query, builder) ->
equalsBusinessUnitIdPredicate(joinBusinessUnit(root), builder, businessUnitId);
}

public static Specification<CourtEntity> equalsCourtCode(String courtCode) {
return (root, query, builder) -> builder.equal(root.get(CourtEntity_.courtCode), courtCode);
}
Expand All @@ -67,6 +76,10 @@ public Specification<CourtEntity> likeAnyCourt(String filter) {
);
}

public static Join<CourtEntity, BusinessUnitEntity> joinBusinessUnit(From<?, CourtEntity> from) {
return from.join(CourtEntity_.businessUnit);
}

public static Join<CourtEntity, CourtEntity> joinParentCourt(From<?, CourtEntity> from) {
return from.join(CourtEntity_.parentCourt);
}
Expand Down
Loading

0 comments on commit 209f172

Please sign in to comment.