Skip to content

Commit

Permalink
PI-2443: Add entity graphs for ordering (#4191)
Browse files Browse the repository at this point in the history
* PI-2443: Changes for ordering
  • Loading branch information
pmcphee77 authored Aug 22, 2024
1 parent 9634137 commit 6e14690
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ object SentenceGenerator {
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Event(
person,
person.id,
mainOffence,
inBreach,
breachDate,
Expand Down Expand Up @@ -353,7 +353,7 @@ object SentenceGenerator {
outcome: ReferenceData? = ReferenceDataGenerator.NSI_BREACH_OUTCOME,
status: NsiStatus = ACTIVE_NSI_STATUS
) = Nsi(
disposal.event.person.id,
disposal.event.personId,
disposal.event.id,
ReferenceDataGenerator.NSI_TYPE,
status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import java.time.LocalDateTime
@Entity
@Immutable
@Table(name = "court_appearance")
@SQLRestriction("soft_deleted = 0")
class CourtAppearance(

@JoinColumn(name = "event_id")
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
val event: Event,

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "outcome_id")
val outcome: Outcome?,

Expand All @@ -45,7 +44,50 @@ class CourtAppearance(

@Id
@Column(name = "court_appearance_id")
val id: Long
val id: Long,

@Column(name = "crown_court_calendar_number")
val crownCourtCalendarNumber: String? = null,

@Column(name = "bail_conditions")
val bailConditions: String? = null,

@Column(name = "court_notes", columnDefinition = "clob")
val courtNotes: String? = null,

@Column(name = "team_id")
val teamId: Long? = null,

@Column(name = "staff_id")
val staffId: Long? = null,

@Column(name = "partition_area_id")
val partitionAreaId: Long? = null,

@Column(name = "row_version")
val rowVersion: Long? = null,

@Column(name = "plea_id")
val pleaId: Long? = null,

@Column(name = "remand_status_id")
val remandStatusId: Long? = null,

@Column(name = "created_by_user_id")
val createdByUserId: Long? = null,

@Column(name = "created_datetime")
val createdDatetime: LocalDateTime? = null,

@Column(name = "last_updated_user_id")
val lastUpdatedUserId: Long? = null,

@Column(name = "last_updated_datetime")
val lastUpdatedDatetime: LocalDateTime? = null,

@Column(name = "training_session_id")
val trainingSessionId: Long? = null

) {
fun isSentenceing(): Boolean {
return appearanceType.code == "S"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package uk.gov.justice.digital.hmpps.integrations.delius.event.entity

import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import uk.gov.justice.digital.hmpps.exception.NotFoundException
Expand All @@ -15,19 +15,18 @@ import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.ProviderEmployee
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.*
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZonedDateTime

@Immutable
@Entity
@SQLRestriction("soft_deleted = 0")
@Table(name = "event")
class Event(

@ManyToOne
@JoinColumn(name = "offender_id", nullable = false)
val person: Person,
@Column(name = "offender_id", nullable = false)
val personId: Long,

@OneToOne(mappedBy = "event")
@OneToOne(mappedBy = "event", fetch = FetchType.EAGER)
val mainOffence: MainOffence? = null,

@Column(name = "in_breach", columnDefinition = "number")
Expand Down Expand Up @@ -70,6 +69,18 @@ class Event(
@OneToMany(mappedBy = "event")
val orderManagers: List<OrderManager> = emptyList(),

@Column(name = "created_by_user_id")
val createdByUserId: Long? = null,

@Column(name = "created_datetime")
val createdDatetime: LocalDateTime? = null,

@Column(name = "last_updated_user_id")
val lastUpdatedUserId: Long? = null,

@Column(name = "last_updated_datetime")
val lastUpdatedDatetime: LocalDateTime? = null,

@ManyToOne
@JoinColumn(name = "court_id")
val court: Court?,
Expand All @@ -78,11 +89,14 @@ class Event(

interface EventRepository : JpaRepository<Event, Long> {

fun findAllByPerson(person: Person): List<Event>
@EntityGraph(attributePaths = ["mainOffence", "court", "disposal"])
fun findAllByPersonId(personId: Long): List<Event>

fun findAllByPersonAndActiveIsTrue(person: Person): List<Event>
@EntityGraph(attributePaths = ["mainOffence", "court", "disposal"])
fun findAllByPersonIdAndActiveIsTrue(personId: Long): List<Event>

fun findByPersonAndId(person: Person, id: Long): Event?
@EntityGraph(attributePaths = ["mainOffence", "court", "disposal"])
fun findByPersonIdAndId(personId: Long, id: Long): Event?

@Query(
"""
Expand All @@ -101,7 +115,7 @@ interface EventRepository : JpaRepository<Event, Long> {
fun awaitingPSR(eventId: Long): Int
}

fun EventRepository.getByPersonAndEventNumber(person: Person, eventId: Long) = findByPersonAndId(person, eventId)
fun EventRepository.getByPersonAndEventNumber(person: Person, eventId: Long) = findByPersonIdAndId(person.id, eventId)
?: throw NotFoundException("Conviction with ID $eventId for Offender with crn ${person.crn} not found")

@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,6 @@ class Release(
val softDeleted: Boolean = false
)

interface DisposalRepository : JpaRepository<Disposal, Long> {

@Query(
"""
select d from Disposal d where d.event.person.crn = :crn
and d.softDeleted = false
"""
)
fun getByCrn(crn: String): List<Disposal>
}

@Entity
@Table(name = "upw_details")
@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import jakarta.persistence.*
import org.hibernate.annotations.Fetch
import org.hibernate.annotations.FetchMode
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceData
import java.time.LocalDate
import java.time.ZonedDateTime

@Immutable
@Entity
@Table(name = "disability")
@SQLRestriction("soft_deleted = 0 and (finish_date is null or finish_date > current_date)")
class Disability(
@Id
@Column(name = "disability_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ class OffenderAlias(

@Entity
@Table(name = "offender_address")
@SQLRestriction("soft_deleted = 0")
class PersonAddress(
@Id
@Column(name = "offender_address_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class ConvictionService(
val person = personRepository.getPerson(crn)

return when (activeOnly) {
true -> eventRepository.findAllByPersonAndActiveIsTrue(person)
else -> eventRepository.findAllByPerson(person)
}.sortedWith(compareByDescending(Event::referralDate).thenByDescending(Event::id))
true -> eventRepository.findAllByPersonIdAndActiveIsTrue(person.id)
else -> eventRepository.findAllByPersonId(person.id)
}.filter { event -> !event.softDeleted }
.sortedBy(Event::referralDate).reversed()
.map { it.toConviction() }
}

Expand Down Expand Up @@ -75,9 +76,11 @@ class ConvictionService(
}

fun Event.toLatestCourtAppearanceOutcome(): KeyValue? {
courtAppearances.filter { it.outcome != null }.maxByOrNull { it.appearanceDate }
?.let { return KeyValue(it.outcome!!.code, it.outcome.description) }
?: return null
return courtAppearances
.filter { courtAppearance -> courtAppearance.outcome != null }
.maxByOrNull(CourtAppearance::appearanceDate)?.let {
KeyValue(it.outcome!!.code, it.outcome.description)
}
}

fun Event.toLatestOrSentencingCourtAppearanceOf(): CourtAppearanceBasic? {
Expand Down Expand Up @@ -109,7 +112,7 @@ class ConvictionService(
offenceCount = offenceCount,
tics = tics,
verdict = verdict,
offenderId = event.person.id,
offenderId = event.personId,
createdDatetime = created.toLocalDateTime(),
lastUpdatedDatetime = updated.toLocalDateTime()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fun DocumentEntity?.toPreviousConviction() = PreviousConviction(convictionDate =
fun Person.toProfile(previousConviction: DocumentEntity?) = OffenderProfile(
genderIdentity = genderIdentity?.description,
selfDescribedGender = genderIdentityDescription ?: genderIdentity?.description,
disabilities = disabilities.sortedByDescending { it.startDate }.map {
disabilities = disabilities.filter { d -> !d.softDeleted }.sortedBy { it.startDate }.reversed().map {
it.toDisability()
}.takeIf { disabilities.isNotEmpty() },
ethnicity = ethnicity?.description,
Expand Down

0 comments on commit 6e14690

Please sign in to comment.