Skip to content

Commit bdeea6a

Browse files
1 parent f15a846 commit bdeea6a

File tree

6 files changed

+136
-13
lines changed

6 files changed

+136
-13
lines changed

projects/core-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.springframework.context.ApplicationListener
88
import org.springframework.stereotype.Component
99
import org.springframework.transaction.annotation.Transactional
1010
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
11+
import uk.gov.justice.digital.hmpps.data.generator.ProbationAreaGenerator
1112
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator
1213
import uk.gov.justice.digital.hmpps.user.AuditUserRepository
1314

@@ -26,21 +27,30 @@ class DataLoader(
2627
@Transactional
2728
override fun onApplicationEvent(are: ApplicationReadyEvent) {
2829
saveAll(
30+
ProbationAreaGenerator.DO_NOT_FIND_PA,
31+
ProbationAreaGenerator.DEFAULT,
2932
PersonGenerator.TITLE,
3033
PersonGenerator.GENDER,
3134
PersonGenerator.ETHNICITY,
3235
PersonGenerator.NATIONALITY,
3336
PersonGenerator.MAIN_ADDRESS,
3437
PersonGenerator.PREVIOUS_ADDRESS,
3538
PersonGenerator.MIN_PERSON,
39+
ProbationAreaGenerator.generatePersonManager(PersonGenerator.MIN_PERSON),
3640
PersonGenerator.FULL_PERSON,
41+
ProbationAreaGenerator.generatePersonManager(PersonGenerator.FULL_PERSON),
3742
*PersonGenerator.FULL_PERSON_ALIASES.toTypedArray(),
3843
*PersonGenerator.FULL_PERSON_ADDRESSES.toTypedArray(),
3944
*PersonGenerator.FULL_PERSON_EXCLUSIONS.map { it.user }.toTypedArray(),
4045
*PersonGenerator.FULL_PERSON_EXCLUSIONS.toTypedArray(),
4146
*PersonGenerator.FULL_PERSON_RESTRICTIONS.map { it.user }.toTypedArray(),
4247
*PersonGenerator.FULL_PERSON_RESTRICTIONS.toTypedArray(),
43-
*PersonGenerator.SENTENCES.toTypedArray()
48+
*PersonGenerator.SENTENCES.toTypedArray(),
49+
PersonGenerator.DO_NOT_FIND_PERSON,
50+
ProbationAreaGenerator.generatePersonManager(
51+
PersonGenerator.DO_NOT_FIND_PERSON,
52+
ProbationAreaGenerator.DO_NOT_FIND_PA
53+
),
4454
)
4555
}
4656

projects/core-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ object PersonGenerator {
1212
val PREVIOUS_ADDRESS = generateReferenceData("P", "Previous Address")
1313
val MAIN_ADDRESS = generateReferenceData("M", "Main Address")
1414

15+
val DO_NOT_FIND_PERSON = generatePerson(
16+
crn = "X999999",
17+
nomsId = "A9999BC",
18+
firstname = "John",
19+
surname = "Doe",
20+
dob = LocalDate.of(1975, 7, 15),
21+
telephoneNumber = "0191 255 9874",
22+
mobileNumber = "07515481285",
23+
emailAddress = "[email protected]",
24+
title = TITLE,
25+
gender = GENDER,
26+
nationality = NATIONALITY,
27+
ethnicity = ETHNICITY,
28+
)
29+
1530
val MIN_PERSON =
1631
generatePerson("M123456", firstname = "Isabelle", surname = "Necessary", dob = LocalDate.of(1990, 3, 5))
1732
val FULL_PERSON = generatePerson(
@@ -144,6 +159,7 @@ object PersonGenerator {
144159
restrictionMessage = restrictionMessage,
145160
softDeleted = softDeleted,
146161
id = id,
162+
personManager = null
147163
)
148164

149165
fun generateAlias(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package uk.gov.justice.digital.hmpps.data.generator
2+
3+
import uk.gov.justice.digital.hmpps.integration.delius.entity.Person
4+
import uk.gov.justice.digital.hmpps.integration.delius.entity.PersonManager
5+
import uk.gov.justice.digital.hmpps.integration.delius.entity.ProbationArea
6+
7+
object ProbationAreaGenerator {
8+
val DO_NOT_FIND_PA = generateProbationArea("XXX")
9+
val DEFAULT = generateProbationArea("N01")
10+
11+
fun generateProbationArea(code: String, id: Long = IdGenerator.getAndIncrement()) = ProbationArea(code, id)
12+
fun generatePersonManager(
13+
person: Person,
14+
probationArea: ProbationArea = DEFAULT,
15+
active: Boolean = true,
16+
softDeleted: Boolean = false,
17+
id: Long = IdGenerator.getAndIncrement()
18+
) = PersonManager(person, probationArea, active, softDeleted, id)
19+
}

projects/core-person-record-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CorePersonIntegrationTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ internal class CorePersonIntegrationTest {
4545
)
4646
}
4747

48+
@Test
49+
fun `returns 404 not found for test cases by crn`() {
50+
mockMvc
51+
.perform(get("/probation-cases/${PersonGenerator.DO_NOT_FIND_PERSON.crn}").withToken())
52+
.andExpect(status().isNotFound)
53+
}
54+
4855
@Test
4956
fun `correctly returns detail by id`() {
5057
mockMvc

projects/core-person-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integration/delius/entity/Person.kt

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,11 @@ import org.springframework.data.domain.Page
88
import org.springframework.data.domain.Pageable
99
import org.springframework.data.jpa.repository.EntityGraph
1010
import org.springframework.data.jpa.repository.JpaRepository
11+
import org.springframework.data.jpa.repository.Query
1112
import uk.gov.justice.digital.hmpps.exception.NotFoundException
1213
import java.time.LocalDate
1314
import java.util.*
1415

15-
@NamedEntityGraph(
16-
name = "person-with-ref-data",
17-
attributeNodes = [
18-
NamedAttributeNode("title"),
19-
NamedAttributeNode("gender"),
20-
NamedAttributeNode("nationality"),
21-
NamedAttributeNode("ethnicity")
22-
]
23-
)
2416
@Entity
2517
@Immutable
2618
@Table(name = "offender")
@@ -58,6 +50,9 @@ data class Person(
5850
@Column(name = "e_mail_address")
5951
val emailAddress: String?,
6052

53+
@OneToOne(mappedBy = "person", optional = false)
54+
val personManager: PersonManager?,
55+
6156
@ManyToOne
6257
@JoinColumn(name = "title_id")
6358
val title: ReferenceData?,
@@ -88,13 +83,43 @@ data class Person(
8883
)
8984

9085
interface PersonRepository : JpaRepository<Person, Long> {
91-
@EntityGraph(value = "person-with-ref-data")
86+
@Query(
87+
"""
88+
select p from Person p
89+
join fetch p.personManager pm
90+
join fetch pm.probationArea pa
91+
left join fetch p.gender
92+
left join fetch p.ethnicity
93+
left join fetch p.nationality
94+
where p.crn = :crn and pa.code <> 'XXX' and pm.softDeleted = false and pm.active = true
95+
"""
96+
)
9297
fun findByCrn(crn: String): Person?
9398

94-
@EntityGraph(value = "person-with-ref-data")
99+
@Query(
100+
"""
101+
select p from Person p
102+
join fetch p.personManager pm
103+
join fetch pm.probationArea pa
104+
left join fetch p.gender
105+
left join fetch p.ethnicity
106+
left join fetch p.nationality
107+
where p.id = :id and pa.code <> 'XXX' and pm.softDeleted = false and pm.active = true
108+
"""
109+
)
95110
override fun findById(id: Long): Optional<Person>
96111

97-
@EntityGraph(value = "person-with-ref-data")
112+
@Query(
113+
"""
114+
select p from Person p
115+
join fetch p.personManager pm
116+
join fetch pm.probationArea pa
117+
left join fetch p.gender
118+
left join fetch p.ethnicity
119+
left join fetch p.nationality
120+
where pa.code <> 'XXX' and pm.softDeleted = false and pm.active = true
121+
"""
122+
)
98123
override fun findAll(pageable: Pageable): Page<Person>
99124
}
100125

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package uk.gov.justice.digital.hmpps.integration.delius.entity
2+
3+
import jakarta.persistence.*
4+
import org.hibernate.annotations.Immutable
5+
import org.hibernate.annotations.SQLRestriction
6+
import org.hibernate.type.NumericBooleanConverter
7+
8+
@Immutable
9+
@Entity
10+
@Table(name = "offender_manager")
11+
@SQLRestriction("soft_deleted = 0 and active_flag = 1")
12+
class PersonManager(
13+
14+
@OneToOne
15+
@JoinColumn(name = "offender_id")
16+
val person: Person,
17+
18+
@ManyToOne
19+
@JoinColumn(name = "probation_area_id")
20+
val probationArea: ProbationArea,
21+
22+
@Column(name = "active_flag", columnDefinition = "number")
23+
@Convert(converter = NumericBooleanConverter::class)
24+
val active: Boolean,
25+
26+
@Column(columnDefinition = "number")
27+
@Convert(converter = NumericBooleanConverter::class)
28+
val softDeleted: Boolean,
29+
30+
@Id @Column(name = "offender_manager_id")
31+
val id: Long
32+
)
33+
34+
@Entity
35+
@Immutable
36+
@Table(name = "probation_area")
37+
@SQLRestriction("code <> 'XXX'")
38+
class ProbationArea(
39+
40+
@Column(columnDefinition = "char(3)")
41+
val code: String,
42+
43+
@Id
44+
@Column(name = "probation_area_id")
45+
val id: Long
46+
)

0 commit comments

Comments
 (0)