generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd47bc4
commit 66bf6e7
Showing
13 changed files
with
315 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ database: | |
tables: | ||
- audited_interaction | ||
- contact | ||
- offender_address | ||
|
||
audit: | ||
username: Cas3AndDelius | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...d-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/AddressRDGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package uk.gov.justice.digital.hmpps.data.generator | ||
|
||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceData | ||
|
||
object AddressRDGenerator { | ||
val CAS3_ADDRESS_TYPE = generate("CAS3", DatasetGenerator.ADDRESS_TYPE.id, "Approved Premises") | ||
val MAIN_ADDRESS_STATUS = generate("M", DatasetGenerator.ADDRESS_STATUS.id, "Main Address") | ||
val PREV_ADDRESS_STATUS = generate("P", DatasetGenerator.ADDRESS_STATUS.id, "Previous Address") | ||
|
||
fun generate( | ||
code: String, | ||
datasetId: Long, | ||
description: String = "Description of $code", | ||
id: Long = IdGenerator.getAndIncrement() | ||
) = ReferenceData(id, code, description, datasetId) | ||
} |
9 changes: 9 additions & 0 deletions
9
...and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/DatasetGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package uk.gov.justice.digital.hmpps.data.generator | ||
|
||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.Dataset | ||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.DatasetCode | ||
|
||
object DatasetGenerator { | ||
val ADDRESS_TYPE = Dataset(IdGenerator.getAndIncrement(), DatasetCode.ADDRESS_TYPE) | ||
val ADDRESS_STATUS = Dataset(IdGenerator.getAndIncrement(), DatasetCode.ADDRESS_STATUS) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/AddressService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package uk.gov.justice.digital.hmpps.integrations.delius | ||
|
||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.PersonArrived | ||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person | ||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonAddress | ||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonAddressRepository | ||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceDataRepository | ||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.cas3AddressType | ||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.mainAddressStatus | ||
import uk.gov.justice.digital.hmpps.integrations.delius.entity.previousAddressStatus | ||
import java.time.LocalDate | ||
|
||
@Service | ||
class AddressService( | ||
private val personAddressRepository: PersonAddressRepository, | ||
private val referenceDataRepository: ReferenceDataRepository | ||
) { | ||
fun updateMainAddress(person: Person, details: PersonArrived) { | ||
endMainAddress(person, details.arrivedAt.toLocalDate()) | ||
toPersonAddress(person, details).apply(personAddressRepository::save) | ||
} | ||
|
||
fun endMainAddress(person: Person, endDate: LocalDate) { | ||
val currentMain = personAddressRepository.findMainAddress(person.id) | ||
currentMain?.apply { | ||
val previousStatus = referenceDataRepository.previousAddressStatus() | ||
currentMain.status = previousStatus | ||
currentMain.endDate = endDate | ||
} | ||
} | ||
|
||
private fun toPersonAddress(person: Person, details: PersonArrived) = PersonAddress( | ||
0, | ||
person.id, | ||
referenceDataRepository.cas3AddressType(), | ||
referenceDataRepository.mainAddressStatus(), | ||
details.premises.addressLine1, | ||
details.premises.town, | ||
details.premises.region, | ||
details.premises.postcode, | ||
startDate = details.arrivedAt.toLocalDate() | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
.../src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/PersonAddress.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package uk.gov.justice.digital.hmpps.integrations.delius.entity | ||
|
||
import jakarta.persistence.Column | ||
import jakarta.persistence.Convert | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.EntityListeners | ||
import jakarta.persistence.GeneratedValue | ||
import jakarta.persistence.GenerationType | ||
import jakarta.persistence.Id | ||
import jakarta.persistence.JoinColumn | ||
import jakarta.persistence.ManyToOne | ||
import jakarta.persistence.SequenceGenerator | ||
import jakarta.persistence.Table | ||
import org.hibernate.type.YesNoConverter | ||
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 org.springframework.data.jpa.repository.Query | ||
import java.time.LocalDate | ||
import java.time.ZonedDateTime | ||
|
||
@Entity | ||
@Table(name = "offender_address") | ||
@EntityListeners(AuditingEntityListener::class) | ||
@SequenceGenerator(name = "offender_address_id_generator", sequenceName = "offender_address_id_seq", allocationSize = 1) | ||
class PersonAddress( | ||
@Id | ||
@Column(name = "offender_address_id") | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "offender_address_id_generator") | ||
val id: Long, | ||
@Column(name = "offender_id") | ||
val personId: Long, | ||
@ManyToOne | ||
@JoinColumn(name = "address_type_id") | ||
val type: ReferenceData, | ||
@ManyToOne | ||
@JoinColumn(name = "address_status_id") | ||
var status: ReferenceData, | ||
val streetName: String?, | ||
@Column(name = "town_city") | ||
val town: String?, | ||
val county: String?, | ||
val postcode: String?, | ||
val telephoneNumber: String? = null, | ||
val buildingName: String? = null, | ||
val district: String? = null, | ||
val addressNumber: String? = null, | ||
@Convert(converter = YesNoConverter::class) | ||
val noFixedAbode: Boolean? = false, | ||
@Convert(converter = YesNoConverter::class) | ||
val typeVerified: Boolean? = false, | ||
val startDate: LocalDate = LocalDate.now(), | ||
var endDate: LocalDate? = null, | ||
@Column(updatable = false, columnDefinition = "NUMBER") | ||
val softDeleted: Boolean = false, | ||
|
||
@CreatedDate | ||
@Column(nullable = false) | ||
var createdDatetime: ZonedDateTime = ZonedDateTime.now(), | ||
|
||
@Column(nullable = false) | ||
@CreatedBy | ||
var createdByUserId: Long = 0, | ||
|
||
@Column(nullable = false) | ||
@LastModifiedDate | ||
var lastUpdatedDatetime: ZonedDateTime = ZonedDateTime.now(), | ||
|
||
@Column(nullable = false) | ||
@LastModifiedBy | ||
var lastUpdatedUserId: Long = 0, | ||
|
||
@Column(nullable = false) | ||
val partitionAreaId: Long = 0 | ||
) | ||
|
||
interface PersonAddressRepository : JpaRepository<PersonAddress, Long> { | ||
@Query( | ||
""" | ||
select pa from PersonAddress pa | ||
join fetch pa.status | ||
join fetch pa.type | ||
where pa.personId = :personId | ||
and pa.softDeleted = false | ||
and pa.endDate is null | ||
and pa.status.code = 'M' | ||
""" | ||
) | ||
fun findMainAddress(personId: Long): PersonAddress? | ||
} |
64 changes: 64 additions & 0 deletions
64
.../src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/entity/ReferenceData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package uk.gov.justice.digital.hmpps.integrations.delius.entity | ||
|
||
import jakarta.persistence.AttributeConverter | ||
import jakarta.persistence.Column | ||
import jakarta.persistence.Convert | ||
import jakarta.persistence.Converter | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.Id | ||
import jakarta.persistence.Table | ||
import org.hibernate.annotations.Immutable | ||
|
||
@Immutable | ||
@Entity | ||
@Table(name = "r_standard_reference_list") | ||
class ReferenceData( | ||
@Id | ||
@Column(name = "standard_reference_list_id", nullable = false) | ||
val id: Long, | ||
|
||
@Column(name = "code_value", length = 100, nullable = false) | ||
val code: String, | ||
|
||
@Column(name = "code_description", length = 500, nullable = false) | ||
val description: String, | ||
|
||
@Column(name = "reference_data_master_id", nullable = false) | ||
val datasetId: Long | ||
) | ||
|
||
@Immutable | ||
@Entity | ||
@Table(name = "r_reference_data_master") | ||
class Dataset( | ||
@Id | ||
@Column(name = "reference_data_master_id") | ||
val id: Long, | ||
|
||
@Convert(converter = DatasetCodeConverter::class) | ||
@Column(name = "code_set_name", nullable = false) | ||
val code: DatasetCode | ||
) | ||
|
||
enum class DatasetCode(val value: String) { | ||
ADDRESS_STATUS("ADDRESS STATUS"), | ||
ADDRESS_TYPE("ADDRESS TYPE"); | ||
|
||
companion object { | ||
private val index = DatasetCode.entries.associateBy { it.value } | ||
fun fromString(value: String): DatasetCode = | ||
index[value] ?: throw IllegalArgumentException("Invalid DatasetCode") | ||
} | ||
} | ||
|
||
enum class AddressTypeCode(val code: String) { | ||
CAS3("CAS3") | ||
} | ||
|
||
|
||
@Converter | ||
class DatasetCodeConverter : AttributeConverter<DatasetCode, String> { | ||
override fun convertToDatabaseColumn(attribute: DatasetCode): String = attribute.value | ||
|
||
override fun convertToEntityAttribute(dbData: String): DatasetCode = DatasetCode.fromString(dbData) | ||
} |
Oops, something went wrong.