Skip to content

Commit

Permalink
Merge pull request #2728 from ministryofjustice/bug/fix-multiple-beds…
Browse files Browse the repository at this point in the history
…-in-room-issue-with-Excel-seed-job

APS-1744 bug - fix multiple beds in room issue with excel seed job
  • Loading branch information
danhumphreys-moj authored Dec 20, 2024
2 parents c6f30ce + 5dc6481 commit 9d85443
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import org.slf4j.LoggerFactory
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.ServiceName
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.BedEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.BedRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.PremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.RoomEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.RoomRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.ExcelSeedJob
Expand All @@ -26,11 +26,15 @@ class ApprovedPremisesRoomsSeedFromXLSXJob(
private val approvedPremisesRepository: ApprovedPremisesRepository,
private val roomRepository: RoomRepository,
private val bedRepository: BedRepository,
private val siteSurvey: SiteSurvey,
private val characteristicRepository: CharacteristicRepository,
) : ExcelSeedJob {
private val log = LoggerFactory.getLogger(this::class.java)
private val questionCriteriaMapping = QuestionCriteriaMapping(characteristicRepository)
private lateinit var qCode: String

override fun processDataFrame(roomsWorksheet: DataFrame<*>, premisesId: UUID) {
findExistingPremisesOrThrow(premisesId)
var premises = findExistingPremisesOrThrow(premisesId)
qCode = premises.qCode

var rooms = buildRooms(roomsWorksheet, premisesId)
val characteristics = buildCharacteristics(roomsWorksheet)
Expand All @@ -50,27 +54,27 @@ class ApprovedPremisesRoomsSeedFromXLSXJob(

for (i in 1..<dataFrame.columnsCount()) {
val roomAnswers = dataFrame.getColumn(i)
val room = buildRoom(premisesId, roomCode = roomAnswers.name, roomName = roomAnswers[0].toString())
val room = buildRoom(premisesId, roomCode = "$qCode - ${roomAnswers[0]}", roomName = roomAnswers[0].toString())

rooms.add(room)
if (rooms.none { it.code == room.code }) rooms.add(room)
}
return rooms
}

private fun buildCharacteristics(dataFrame: DataFrame<*>): MutableMap<String, MutableList<CharacteristicEntity>> {
var premisesCharacteristics = mutableMapOf<String, MutableList<CharacteristicEntity>>()
private fun buildCharacteristics(dataFrame: DataFrame<*>): MutableMap<String, MutableSet<CharacteristicEntity>> {
var premisesCharacteristics = mutableMapOf<String, MutableSet<CharacteristicEntity>>()

siteSurvey.questionToCharacterEntityMapping.forEach { (question, characteristic) ->
questionCriteriaMapping.questionToCharacterEntityMapping.forEach { (question, characteristic) ->
val rowId = dataFrame.getColumn(0).values().indexOf(question)

if (rowId == -1) throw SiteSurveyImportException("Characteristic question '$question' not found on sheet Sheet3.")

for (colId in 1..<dataFrame.columnsCount()) {
val roomCode = dataFrame.getColumn(colId).name
val roomCode = "$qCode - ${dataFrame.getColumn(colId)[0]}"
val answer = dataFrame[rowId][colId].toString().trim()

if (answer.equals("yes", ignoreCase = true)) {
premisesCharacteristics.computeIfAbsent(roomCode) { mutableListOf() }.add(characteristic!!)
premisesCharacteristics.computeIfAbsent(roomCode) { mutableSetOf() }.add(characteristic!!)
} else if (!answer.equals("no", ignoreCase = true) && !answer.equals("N/A", ignoreCase = true)) {
throw SiteSurveyImportException("Expecting 'yes' or 'no' for question '$question' but is '$answer' on sheet Sheet3 (row = ${rowId + 1}, col = $colId).")
}
Expand All @@ -83,13 +87,13 @@ class ApprovedPremisesRoomsSeedFromXLSXJob(
val beds = mutableListOf<BedEntity>()
for (i in 1..<dataFrame.columnsCount()) {
val roomAnswers = dataFrame.getColumn(i)
val bedNumber = roomAnswers[1].toString()
val roomCode = "$qCode - ${roomAnswers[0]}"
beds.add(
buildBed(
bedName = "${roomAnswers.name}",
bedCode = "${roomAnswers.name} - $bedNumber",
room = rooms.firstOrNull { it.code == roomAnswers.name }
?: throw IllegalArgumentException("Room not found with id ${roomAnswers.name} and code ${roomAnswers.name}."),
bedName = roomAnswers[1].toString(),
bedCode = roomAnswers.name,
room = rooms.firstOrNull { it.code == roomCode }
?: throw IllegalArgumentException("Room not found with code $roomCode"),
),
)
}
Expand Down Expand Up @@ -146,15 +150,14 @@ class ApprovedPremisesRoomsSeedFromXLSXJob(
createdAt = null,
)

private fun findExistingPremisesOrThrow(premisesId: UUID): PremisesEntity {
private fun findExistingPremisesOrThrow(premisesId: UUID): ApprovedPremisesEntity {
return approvedPremisesRepository.findByIdOrNull(premisesId) ?: throw SiteSurveyImportException(
"No premises with id '$premisesId' found.",
)
}
}

@Component
class SiteSurvey(characteristicRepository: CharacteristicRepository) {
class QuestionCriteriaMapping(characteristicRepository: CharacteristicRepository) {
private val questionToPropertyNameMapping = mapOf(
"Is this bed in a single room?" to "isSingle",
"Is this room located on the ground floor?" to "isGroundFloor",
Expand Down
Loading

0 comments on commit 9d85443

Please sign in to comment.