Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Entity changes to match delius database
- Removed oracle profile from application.yml
  • Loading branch information
joseph-bcl committed Oct 23, 2024
1 parent bbd9e9f commit f98faf3
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ object PersonGenerator {
forename = UUID.randomUUID().toString().substring(0, 15),
surname = UUID.randomUUID().toString().substring(0, 15),
dateOfBirth = LocalDate.now().minusYears(Random.nextInt(16, 76).toLong()),
gender = if (Random.nextBoolean()) ReferenceDataGenerator.GENDER_MALE else ReferenceDataGenerator.GENDER_FEMALE
gender = if (Random.nextBoolean()) ReferenceDataGenerator.GENDER_MALE else ReferenceDataGenerator.GENDER_FEMALE,
surnameSoundex = "surnameSoundex",
firstNameSoundex = "firstNameSoundex",
middleNameSoundex = null
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ object DatasetGenerator {
}

object CourtGenerator {
val UNKNOWN_COURT_N07_PROVIDER = generate(code = "UNKNCT", nationalCourtCode = "A00AA00")
val UNKNOWN_COURT_N07_PROVIDER = generate(code = "UNKNCT", ouCode = "A00AA00")

fun generate(
id: Long = IdGenerator.getAndIncrement(),
code: String,
selectable: Boolean = true,
courtName: String = "Court not known",
provider: Provider = ProviderGenerator.DEFAULT,
nationalCourtCode: String? = null,
ouCode: String? = null,
) = Court(
id = id,
code = code,
selectable = selectable,
courtName = courtName,
probationArea = provider,
nationalCourtCode = nationalCourtCode
name = courtName,
provider = provider,
ouCode = ouCode
)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CREATE OR REPLACE PACKAGE offender_support_api IS
CREATE OR REPLACE PACKAGE offender_support_api AS
FUNCTION getNextCRN RETURN VARCHAR2;
END offender_support_api;

GRANT EXECUTE ON offender_support_api TO delius_app_schema;

CREATE OR REPLACE PACKAGE BODY offender_support_api IS
CREATE OR REPLACE PACKAGE BODY offender_support_api AS
FUNCTION getNextCRN RETURN VARCHAR2 IS
BEGIN
RETURN 'A111111';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonAddr
import uk.gov.justice.digital.hmpps.message.HmppsDomainEvent
import uk.gov.justice.digital.hmpps.message.Notification
import uk.gov.justice.digital.hmpps.messaging.HmppsChannelManager
import uk.gov.justice.digital.hmpps.service.AddressService
import uk.gov.justice.digital.hmpps.service.PersonService
import uk.gov.justice.digital.hmpps.telemetry.TelemetryMessagingExtensions.notificationReceived
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
Expand Down Expand Up @@ -67,9 +66,6 @@ internal class IntegrationTest {
@SpyBean
lateinit var personService: PersonService

@SpyBean
lateinit var addressService: AddressService

@BeforeEach
fun setup() {
doReturn("A111111").whenever(personService).generateCrn()
Expand Down Expand Up @@ -97,8 +93,8 @@ internal class IntegrationTest {
val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT)
channelManager.getChannel(queueName).publishAndWait(notification)
verify(personService, never()).insertPerson(any(), any())
verify(personService, never()).insertAddress(any())
verify(personRepository, never()).save(any())
verify(addressService, never()).insertAddress(any())
verify(addressRepository, never()).save(any())
verify(auditedInteractionService, Mockito.never()).createAuditedInteraction(
any(),
Expand Down Expand Up @@ -126,7 +122,7 @@ internal class IntegrationTest {
channelManager.getChannel(queueName).publishAndWait(notification)

verify(personService, never()).insertPerson(any(), any())
verify(addressService, never()).insertAddress(any())
verify(personService, never()).insertAddress(any())
verify(addressRepository, never()).save(any())
verify(personRepository, never()).save(any())
verify(auditedInteractionService, Mockito.never())
Expand All @@ -150,8 +146,8 @@ internal class IntegrationTest {
channelManager.getChannel(queueName).publishAndWait(notification)

verify(personService, never()).insertPerson(any(), any())
verify(personService, never()).insertAddress(any())
verify(personRepository, never()).save(any())
verify(addressService, never()).insertAddress(any())
verify(addressRepository, never()).save(any())
verify(auditedInteractionService, Mockito.never())
.createAuditedInteraction(any(), any(), any(), any(), anyOrNull())
Expand Down Expand Up @@ -205,7 +201,7 @@ internal class IntegrationTest {
val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT)
channelManager.getChannel(queueName).publishAndWait(notification)

verify(addressService).insertAddress(any())
verify(personService).insertAddress(any())

verify(addressRepository).save(check<PersonAddress> {
assertThat(it.start, Matchers.equalTo(LocalDate.now()))
Expand Down Expand Up @@ -242,7 +238,7 @@ internal class IntegrationTest {
val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT_BLANK_ADDRESS)
channelManager.getChannel(queueName).publishAndWait(notification)

verify(addressService, never()).insertAddress(any())
verify(personService, never()).insertAddress(any())

verify(addressRepository, never()).save(any())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedBy
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import org.springframework.data.jpa.repository.JpaRepository
import java.time.ZonedDateTime

@Entity
@Table(name = "equality")
@SequenceGenerator(name = "equality_id_seq", sequenceName = "equality_id_seq", allocationSize = 1)
@EntityListeners(AuditingEntityListener::class)
class Equality(
@Id
@Column(name = "equality_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.springframework.data.annotation.LastModifiedBy
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.time.LocalDate
import java.time.ZonedDateTime

Expand All @@ -34,6 +35,15 @@ class Person(
@Column(name = "first_name", length = 35)
val forename: String,

@Column
val surnameSoundex: String,

@Column
val firstNameSoundex: String,

@Column
val middleNameSoundex: String?,

@Column(name = "second_name", length = 35)
val secondName: String? = null,

Expand Down Expand Up @@ -75,11 +85,23 @@ class Person(
@LastModifiedDate
var lastUpdatedDatetime: ZonedDateTime = ZonedDateTime.now(),

@LastModifiedDate
var lastUpdatedDatetimeDiversit: ZonedDateTime = ZonedDateTime.now(),

@LastModifiedBy
var lastUpdatedUserIdDiversity: Long = 0,

@CreatedBy
var createdByUserId: Long = 0,

@LastModifiedBy
var lastUpdatedUserId: Long = 0,
)

interface PersonRepository : JpaRepository<Person, Long>
@Column
val partitionAreaId: Long = 0L
)

interface PersonRepository : JpaRepository<Person, Long>{
@Query("SELECT SOUNDEX(:name) FROM DUAL", nativeQuery = true)
fun getSoundex(name: String): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedBy
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import org.springframework.data.jpa.repository.JpaRepository
import java.time.LocalDate
import java.time.LocalDateTime
Expand All @@ -16,6 +17,7 @@ import java.time.ZonedDateTime
@Table(name = "offender_manager")
@SQLRestriction("soft_deleted = 0 and active_flag = 1")
@SequenceGenerator(name = "offender_manager_id_seq", sequenceName = "offender_manager_id_seq", allocationSize = 1)
@EntityListeners(AuditingEntityListener::class)
class PersonManager(
@Id
@Column(name = "offender_manager_id")
Expand Down Expand Up @@ -75,6 +77,9 @@ class PersonManager(

@LastModifiedBy
var lastUpdatedUserId: Long = 0,

@Column
val partitionAreaId: Long = 0L
)

@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,21 @@ class Court(
@Column(name = "court_id", nullable = false)
val id: Long,

@Column(name = "code", length = 6, nullable = false)
@Column(columnDefinition = "char(6)", nullable = false)
val code: String,

@Convert(converter = YesNoConverter::class)
val selectable: Boolean = true,

@Column(name = "code_description", length = 80)
val courtName: String,
@Column(name = "court_name", length = 80)
val name: String,

@ManyToOne
@JoinColumn(name = "probation_area_id")
val probationArea: Provider,
val provider: Provider,

@Column(name = "national_court_code")
val nationalCourtCode: String?
@Column(name = "court_ou_code")
val ouCode: String?
)

interface ReferenceDataRepository : JpaRepository<ReferenceData, Long> {
Expand Down Expand Up @@ -149,5 +149,7 @@ fun ReferenceDataRepository.awaitingAssessmentAddressType() =
?: throw NotFoundException("Address Type", "code", ReferenceData.StandardRefDataCode.AWAITING_ASSESSMENT.code)

interface CourtRepository : JpaRepository<Court, Long> {
fun findByNationalCourtCode(nationalCourtCode: String): Court
}
fun findByOuCode(ouCode: String): Court?
}
fun CourtRepository.getByOuCode(ouCode: String) =
findByOuCode(ouCode) ?: throw NotFoundException("Court", "ouCode", ouCode)
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import org.openfolder.kotlinasyncapi.annotation.Schema
import org.openfolder.kotlinasyncapi.annotation.channel.Channel
import org.openfolder.kotlinasyncapi.annotation.channel.Message
import org.openfolder.kotlinasyncapi.annotation.channel.Publish
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.converter.NotificationConverter
import uk.gov.justice.digital.hmpps.integrations.client.ProbationMatchRequest
import uk.gov.justice.digital.hmpps.integrations.client.ProbationSearchClient
import uk.gov.justice.digital.hmpps.integrations.delius.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonAddress
import uk.gov.justice.digital.hmpps.message.Notification
import uk.gov.justice.digital.hmpps.service.AddressService
import uk.gov.justice.digital.hmpps.service.PersonService
import uk.gov.justice.digital.hmpps.telemetry.TelemetryMessagingExtensions.notificationReceived
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
Expand All @@ -26,8 +21,6 @@ class Handler(
override val converter: NotificationConverter<CommonPlatformHearing>,
private val telemetryService: TelemetryService,
private val personService: PersonService,
private val addressService: AddressService,
private val referenceDataRepository: ReferenceDataRepository,
private val probationSearchClient: ProbationSearchClient
) : NotificationHandler<CommonPlatformHearing> {

Expand Down Expand Up @@ -58,58 +51,18 @@ class Handler(
val matchedPersonResponse = probationSearchClient.match(matchRequest)

if (matchedPersonResponse.matches.isNotEmpty()) {
log.debug("Matching offender(s) found for: {}", matchRequest)
return
}

defendants.forEach { defendant ->
// Insert each defendant as a person record
val savedPerson = personService.insertPerson(defendant.toPerson(), courtCode)

val address = defendant.personDefendant?.personDetails?.address

// Insert each defendant's address record
val savedAddress = if (address.containsInformation()) {
addressService.insertAddress(
PersonAddress(
id = null,
start = LocalDate.now(),
status = referenceDataRepository.mainAddressStatus(),
personId = savedPerson.id!!,
notes = address?.buildNotes(),
postcode = address?.postcode,
type = referenceDataRepository.awaitingAssessmentAddressType()
)
)
} else {
log.debug("No address found for defendant with pncId: {}", defendant.pncId)
null
}
}
}
val savedPerson = personService.insertPerson(defendant, courtCode)

companion object {
val log: Logger = LoggerFactory.getLogger(this::class.java)
}

fun Defendant.toPerson(): Person {
val personDetails = personDefendant?.personDetails ?: throw IllegalArgumentException("No person found")
val genderCode = personDetails.gender.toDeliusGender()

return Person(
id = null,
crn = personService.generateCrn(),
croNumber = this.croNumber,
pncNumber = this.pncId,
forename = personDetails.firstName,
secondName = personDetails.middleName,
telephoneNumber = personDetails.contact?.home,
mobileNumber = personDetails.contact?.mobile,
surname = personDetails.lastName,
dateOfBirth = personDetails.dateOfBirth,
gender = referenceDataRepository.findByCodeAndDatasetCode(genderCode, DatasetCode.GENDER)!!,
softDeleted = false
)
telemetryService.trackEvent("PersonCreated", mapOf(
"CRN" to savedPerson.crn,
"personId" to savedPerson.id.toString(),
"hearingId" to notification.message.hearing.id)
)
}
}

fun CommonPlatformHearing.toProbationMatchRequest(): ProbationMatchRequest {
Expand All @@ -124,27 +77,6 @@ class Handler(
croNumber = defendant.croNumber
)
}

fun String.toDeliusGender() = ReferenceData.GenderCode.entries.find { it.commonPlatformValue == this }?.deliusValue
?: throw IllegalStateException("Gender not found: $this")

fun Address?.containsInformation(): Boolean {
return this != null && listOf(
this.address1, this.address2, this.address3,
this.address4, this.address5, this.postcode
).any { !it.isNullOrBlank() }
}

fun Address.buildNotes(): String {
return listOf(
"Address record automatically created by common-platform-delius-service with the following information:",
"Address1: ${this.address1 ?: "N/A"}",
"Address2: ${this.address2 ?: "N/A"}",
"Address3: ${this.address3 ?: "N/A"}",
"Address4: ${this.address4 ?: "N/A"}",
"Postcode: ${this.postcode ?: "N/A"}"
).joinToString("\n")
}
}


Expand Down

This file was deleted.

Loading

0 comments on commit f98faf3

Please sign in to comment.