Flattened Person table into Observation

This commit is contained in:
neviyn 2020-01-10 12:04:37 +00:00
parent ad01ff3cba
commit e647e853d5
7 changed files with 22 additions and 88 deletions

View File

@ -1,7 +1,6 @@
package uk.co.neviyn.observationdatabase
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import org.joda.time.LocalDate
import javax.persistence.CascadeType
import javax.persistence.Column
@ -88,8 +87,7 @@ data class Observation(
val scenarios: List<Scenario>,
@ManyToMany(mappedBy = "observations")
val tutors: Set<Tutor>,
@ManyToOne
val person: Person
val person: String
) {
fun toCsvFormat(): String {
val dataPortion = "${date.toString("dd/MM/yyyy")},\"${scenarios.joinToString { it.title }}\"," +
@ -171,12 +169,3 @@ data class Scenario(
ratingValid(knowledgeRating)
}
}
@Entity
data class Person(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,
@JsonProperty
val name: String
)

View File

@ -17,11 +17,5 @@ interface ObservationRepository : CrudRepository<Observation, Long> {
fun findByTutorsAndDateBetweenOrderByDateAsc(tutor: Tutor, startDate: LocalDate, endDate: LocalDate): List<Observation>
fun findBySiteAndPersonAndDateBetweenOrderByDateAsc(site: Site, person: Person, startDate: LocalDate, endDate: LocalDate): List<Observation>
fun findBySiteAndPersonLikeAndDateBetweenOrderByDateAsc(site: Site, person: String, startDate: LocalDate, endDate: LocalDate): List<Observation>
}
@Repository
interface PersonRepository : CrudRepository<Person, Long> {
fun findFirstByNameLike(name: String): Person?
}

View File

@ -24,8 +24,6 @@ import uk.co.neviyn.observationdatabase.GroupObservationInit
import uk.co.neviyn.observationdatabase.GroupSessionManager
import uk.co.neviyn.observationdatabase.Observation
import uk.co.neviyn.observationdatabase.ObservationRepository
import uk.co.neviyn.observationdatabase.Person
import uk.co.neviyn.observationdatabase.PersonRepository
import uk.co.neviyn.observationdatabase.SiteRepository
import uk.co.neviyn.observationdatabase.TutorRepository
import java.net.Inet4Address
@ -48,8 +46,6 @@ class GroupSessionController {
@Autowired
lateinit var observationRepository: ObservationRepository
@Autowired
lateinit var personRepository: PersonRepository
@Autowired
lateinit var websocketMessenger: SimpMessagingTemplate
@Autowired
lateinit var mailer: Email
@ -171,8 +167,7 @@ class GroupSessionController {
knowledge = x.scenarios.map { it.knowledgeRating }.average(),
scenarios = x.scenarios,
tutors = tutors,
person = personRepository.findFirstByNameLike(x.person.toUpperCase())
?: personRepository.save(Person(name = x.person.toUpperCase()))
person = x.person.toUpperCase()
)
saveObservation(observation)
observations.add(observation)
@ -220,4 +215,4 @@ class GroupSessionController {
mapOf("error" to "Could not determine port")
}
}
}
}

View File

@ -27,8 +27,6 @@ import uk.co.neviyn.observationdatabase.NewTutor
import uk.co.neviyn.observationdatabase.Observation
import uk.co.neviyn.observationdatabase.ObservationRepository
import uk.co.neviyn.observationdatabase.ObservationsRequest
import uk.co.neviyn.observationdatabase.Person
import uk.co.neviyn.observationdatabase.PersonRepository
import uk.co.neviyn.observationdatabase.Site
import uk.co.neviyn.observationdatabase.SiteRepository
import uk.co.neviyn.observationdatabase.Tutor
@ -50,8 +48,6 @@ class ObservationsController {
@Autowired
lateinit var observationRepository: ObservationRepository
@Autowired
lateinit var personRepository: PersonRepository
@Autowired
lateinit var mailer: Email
/**
@ -151,7 +147,7 @@ class ObservationsController {
knowledge = newObservation.scenarios.map { it.knowledgeRating }.average(),
scenarios = newObservation.scenarios,
tutors = tutors,
person = personRepository.findFirstByNameLike(newObservation.person.toUpperCase()) ?: personRepository.save(Person(name = newObservation.person.toUpperCase()))
person = newObservation.person.toUpperCase()
)
logger.debug("Saving new Observation to database")
observation = saveObservation(observation)
@ -174,9 +170,8 @@ class ObservationsController {
}.orElse(listOf())
} else if (observationsRequest.person != null && observationsRequest.person.isNotEmpty() && observationsRequest.site != null) {
logger.debug("Observation contains a person")
val person = personRepository.findFirstByNameLike(observationsRequest.person.toUpperCase()) ?: return listOf()
val site = siteRepository.findById(observationsRequest.site).get()
return observationRepository.findBySiteAndPersonAndDateBetweenOrderByDateAsc(site, person, observationsRequest.startDate, observationsRequest.endDate)
return observationRepository.findBySiteAndPersonLikeAndDateBetweenOrderByDateAsc(site, observationsRequest.person.toUpperCase(), observationsRequest.startDate, observationsRequest.endDate)
} else if (observationsRequest.site != null) {
logger.debug("Observation contains a site")
return siteRepository.findById(observationsRequest.site).map {
@ -293,4 +288,4 @@ class ObservationsController {
throw ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "Mail is currently unavailable due to a server configuration error.")
}
}
}
}

View File

@ -3,8 +3,6 @@ package uk.co.neviyn.observationdatabase
import org.joda.time.LocalDate
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
@ -23,9 +21,6 @@ class RepositoryTest {
@Autowired
lateinit var observationRepository: ObservationRepository
@Autowired
lateinit var personRepository: PersonRepository
@Test
fun testFindBySiteAndDateBetween_EmptyRepository() {
val result = observationRepository.findBySiteAndDateBetweenOrderByDateAsc(Site(
@ -48,7 +43,7 @@ class RepositoryTest {
@Test
fun testFindBySiteAndDateBetween() {
val correctSite = entityManager.persist(Site(name = "Correct"))
val person = entityManager.persist(Person(name = "Foo Bar"))
val person = "Foo Bar"
val incorrectSite = entityManager.persist(Site(name = "Incorrect"))
val tutor = entityManager.persist(Tutor(name = "X", site = correctSite))
val tutor2 = entityManager.persist(Tutor(name = "N", site = incorrectSite))
@ -136,7 +131,7 @@ class RepositoryTest {
@Test
fun testFindByTutorsAndDateBetween() {
val site = entityManager.persist(Site(name = "Correct"))
val person = entityManager.persist(Person(name = "Foo Bar"))
val person = "Foo Bar"
val correctTutor = entityManager.persist(Tutor(name = "X", site = site))
val incorrectTutor = entityManager.persist(Tutor(name = "N", site = site))
val tooEarly = Observation(
@ -245,9 +240,9 @@ class RepositoryTest {
}
@Test
fun testFindBySiteAndPersonsAndDateBetween() {
val person = entityManager.persist(Person(name = "Foo Bar"))
val otherPerson = entityManager.persist(Person(name = "Wrong"))
fun testFindBySiteAndPersonLikeAndDateBetween() {
val person = "Foo Bar"
val otherPerson = "Wrong"
val site = entityManager.persist(Site(name = "Incorrect"))
val tutor = entityManager.persist(Tutor(name = "X", site = site))
val tooEarly = Observation(
@ -322,7 +317,7 @@ class RepositoryTest {
entityManager.persist(tooLate)
entityManager.persist(wrongPerson)
entityManager.persist(justRight)
val result = observationRepository.findBySiteAndPersonAndDateBetweenOrderByDateAsc(
val result = observationRepository.findBySiteAndPersonLikeAndDateBetweenOrderByDateAsc(
site = site,
person = person,
startDate = LocalDate.now().minusDays(1),
@ -331,27 +326,4 @@ class RepositoryTest {
assertEquals(1, result.size)
assertEquals(justRight, result.first())
}
@Test
fun testFindPersonByName() {
val person = entityManager.persist(Person(name = "Foo Bar"))
entityManager.persist(person)
val result = personRepository.findFirstByNameLike("Foo Bar")
assertEquals(person, result)
}
@Test
fun testFindPersonByName_IncorrectName() {
val person = entityManager.persist(Person(name = "Foo Bar"))
entityManager.persist(person)
val result = personRepository.findFirstByNameLike("Foo")
assertNotSame(person, result)
assertNull(result)
}
@Test
fun testFindPersonByName_NoPersons() {
val result = personRepository.findFirstByNameLike("Foo")
assertNull(result)
}
}
}

View File

@ -22,8 +22,6 @@ import uk.co.neviyn.observationdatabase.GroupObservationInit
import uk.co.neviyn.observationdatabase.GroupSessionManager
import uk.co.neviyn.observationdatabase.Observation
import uk.co.neviyn.observationdatabase.ObservationRepository
import uk.co.neviyn.observationdatabase.Person
import uk.co.neviyn.observationdatabase.PersonRepository
import uk.co.neviyn.observationdatabase.Scenario
import uk.co.neviyn.observationdatabase.Site
import uk.co.neviyn.observationdatabase.SiteRepository
@ -47,8 +45,6 @@ class GroupSessionControllerTest {
@Mock
lateinit var observationRepository: ObservationRepository
@Mock
lateinit var personRepository: PersonRepository
@Mock
lateinit var mailer: Email
@After
@ -97,8 +93,7 @@ class GroupSessionControllerTest {
val tutor = Tutor(1, "Mr X", site)
Mockito.doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
Mockito.doReturn(listOf(tutor)).`when`(tutorRepository).findAllById(listOf(1))
val person = Person(1, "A Student")
Mockito.doReturn(person).`when`(personRepository).save(any())
val person = "A Student"
val scenario = Scenario(0, "Sample title", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "")
Mockito.doReturn(Observation(1, site, LocalDate.now(), TrainingType.INITIAL, "Sample title", 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, listOf(scenario), setOf(tutor), person)).`when`(observationRepository).save(any())
controller.startGroupObservation(GroupObservationInit(1, TrainingType.INITIAL, listOf(1), listOf("Sample title")))
@ -169,4 +164,4 @@ class GroupSessionControllerTest {
val output = controller.getParticipantData("A Student")
assertEquals(testData, output)
}
}
}

View File

@ -23,8 +23,6 @@ import uk.co.neviyn.observationdatabase.NewTutor
import uk.co.neviyn.observationdatabase.Observation
import uk.co.neviyn.observationdatabase.ObservationRepository
import uk.co.neviyn.observationdatabase.ObservationsRequest
import uk.co.neviyn.observationdatabase.Person
import uk.co.neviyn.observationdatabase.PersonRepository
import uk.co.neviyn.observationdatabase.Scenario
import uk.co.neviyn.observationdatabase.Site
import uk.co.neviyn.observationdatabase.SiteRepository
@ -46,8 +44,6 @@ class ObservationsControllerTest {
@Mock
lateinit var observationRepository: ObservationRepository
@Mock
lateinit var personRepository: PersonRepository
@Mock
lateinit var mailer: Email
@Test
@ -119,10 +115,9 @@ class ObservationsControllerTest {
fun testAddObservation() {
val site = Site(1, "X")
val tutor = Tutor(1, "Foo Bar", site)
val person = Person(name = "Mr X")
val person = "Mr X"
doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
doReturn(listOf(tutor)).`when`(tutorRepository).findAllById(listOf(1))
doReturn(person).`when`(personRepository).findFirstByNameLike("MR X")
val newData = NewObservation(1, TrainingType.INITIAL, "An Observation", listOf(Scenario(1, "Something", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "")), listOf(1), "Mr X")
val observation = Observation(1, site, LocalDate.now(), TrainingType.INITIAL, "An Observation", 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, newData.scenarios, setOf(tutor), person)
doReturn(observation).`when`(observationRepository).save(ArgumentMatchers.any())
@ -144,7 +139,7 @@ class ObservationsControllerTest {
val observation = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", monitoring = 5.0, controlProcedural = 3.0,
control = 4.0, conservatism = 3.0, teamworkCommunications = 2.0, teamworkLeadership = 3.0,
teamworkWorkload = 1.0, knowledge = 1.0, scenarios = listOf(), tutors = setOf(), person = Person(name = "noone"))
teamworkWorkload = 1.0, knowledge = 1.0, scenarios = listOf(), tutors = setOf(), person = "noone")
doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
doReturn(listOf(observation))
.`when`(observationRepository).findBySiteAndDateBetweenOrderByDateAsc(site, LocalDate.now(), LocalDate.now())
@ -161,7 +156,7 @@ class ObservationsControllerTest {
val observation = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", monitoring = 5.0, controlProcedural = 3.0,
control = 4.0, conservatism = 3.0, teamworkCommunications = 2.0, teamworkLeadership = 3.0,
teamworkWorkload = 1.0, knowledge = 1.0, scenarios = listOf(), tutors = setOf(tutor), person = Person(name = "noone"))
teamworkWorkload = 1.0, knowledge = 1.0, scenarios = listOf(), tutors = setOf(tutor), person = "noone")
doReturn(Optional.of(tutor)).`when`(tutorRepository).findById(1)
doReturn(listOf(observation))
.`when`(observationRepository).findByTutorsAndDateBetweenOrderByDateAsc(tutor, LocalDate.now(), LocalDate.now())
@ -175,16 +170,15 @@ class ObservationsControllerTest {
val request = ObservationsRequest(1, null, null, "Foo Bar", LocalDate.now(), LocalDate.now())
val site = Site(1, "Area 51")
val tutor = Tutor(1, "Mr Unknown", site)
val person = Person(1, "Foo Bar")
val person = "Foo Bar"
val observation = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", monitoring = 5.0, controlProcedural = 3.0,
control = 4.0, conservatism = 3.0, teamworkCommunications = 2.0, teamworkLeadership = 3.0,
teamworkWorkload = 1.0, knowledge = 1.0, scenarios = listOf(), tutors = setOf(tutor), person = person)
doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
doReturn(person).`when`(personRepository).findFirstByNameLike("FOO BAR")
doReturn(listOf(observation)).`when`(observationRepository).findBySiteAndPersonAndDateBetweenOrderByDateAsc(site, person, LocalDate.now(), LocalDate.now())
doReturn(listOf(observation)).`when`(observationRepository).findBySiteAndPersonLikeAndDateBetweenOrderByDateAsc(site, person.toUpperCase(), LocalDate.now(), LocalDate.now())
val result = controller.getObservations(request)
assertEquals(1, result.size)
assertEquals(observation, result[0])
}
}
}