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 package uk.co.neviyn.observationdatabase
import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import org.joda.time.LocalDate import org.joda.time.LocalDate
import javax.persistence.CascadeType import javax.persistence.CascadeType
import javax.persistence.Column import javax.persistence.Column
@ -88,8 +87,7 @@ data class Observation(
val scenarios: List<Scenario>, val scenarios: List<Scenario>,
@ManyToMany(mappedBy = "observations") @ManyToMany(mappedBy = "observations")
val tutors: Set<Tutor>, val tutors: Set<Tutor>,
@ManyToOne val person: String
val person: Person
) { ) {
fun toCsvFormat(): String { fun toCsvFormat(): String {
val dataPortion = "${date.toString("dd/MM/yyyy")},\"${scenarios.joinToString { it.title }}\"," + val dataPortion = "${date.toString("dd/MM/yyyy")},\"${scenarios.joinToString { it.title }}\"," +
@ -171,12 +169,3 @@ data class Scenario(
ratingValid(knowledgeRating) 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 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.GroupSessionManager
import uk.co.neviyn.observationdatabase.Observation import uk.co.neviyn.observationdatabase.Observation
import uk.co.neviyn.observationdatabase.ObservationRepository 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.SiteRepository
import uk.co.neviyn.observationdatabase.TutorRepository import uk.co.neviyn.observationdatabase.TutorRepository
import java.net.Inet4Address import java.net.Inet4Address
@ -48,8 +46,6 @@ class GroupSessionController {
@Autowired @Autowired
lateinit var observationRepository: ObservationRepository lateinit var observationRepository: ObservationRepository
@Autowired @Autowired
lateinit var personRepository: PersonRepository
@Autowired
lateinit var websocketMessenger: SimpMessagingTemplate lateinit var websocketMessenger: SimpMessagingTemplate
@Autowired @Autowired
lateinit var mailer: Email lateinit var mailer: Email
@ -171,8 +167,7 @@ class GroupSessionController {
knowledge = x.scenarios.map { it.knowledgeRating }.average(), knowledge = x.scenarios.map { it.knowledgeRating }.average(),
scenarios = x.scenarios, scenarios = x.scenarios,
tutors = tutors, tutors = tutors,
person = personRepository.findFirstByNameLike(x.person.toUpperCase()) person = x.person.toUpperCase()
?: personRepository.save(Person(name = x.person.toUpperCase()))
) )
saveObservation(observation) saveObservation(observation)
observations.add(observation) observations.add(observation)

View File

@ -27,8 +27,6 @@ import uk.co.neviyn.observationdatabase.NewTutor
import uk.co.neviyn.observationdatabase.Observation import uk.co.neviyn.observationdatabase.Observation
import uk.co.neviyn.observationdatabase.ObservationRepository import uk.co.neviyn.observationdatabase.ObservationRepository
import uk.co.neviyn.observationdatabase.ObservationsRequest 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.Site
import uk.co.neviyn.observationdatabase.SiteRepository import uk.co.neviyn.observationdatabase.SiteRepository
import uk.co.neviyn.observationdatabase.Tutor import uk.co.neviyn.observationdatabase.Tutor
@ -50,8 +48,6 @@ class ObservationsController {
@Autowired @Autowired
lateinit var observationRepository: ObservationRepository lateinit var observationRepository: ObservationRepository
@Autowired @Autowired
lateinit var personRepository: PersonRepository
@Autowired
lateinit var mailer: Email lateinit var mailer: Email
/** /**
@ -151,7 +147,7 @@ class ObservationsController {
knowledge = newObservation.scenarios.map { it.knowledgeRating }.average(), knowledge = newObservation.scenarios.map { it.knowledgeRating }.average(),
scenarios = newObservation.scenarios, scenarios = newObservation.scenarios,
tutors = tutors, 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") logger.debug("Saving new Observation to database")
observation = saveObservation(observation) observation = saveObservation(observation)
@ -174,9 +170,8 @@ class ObservationsController {
}.orElse(listOf()) }.orElse(listOf())
} else if (observationsRequest.person != null && observationsRequest.person.isNotEmpty() && observationsRequest.site != null) { } else if (observationsRequest.person != null && observationsRequest.person.isNotEmpty() && observationsRequest.site != null) {
logger.debug("Observation contains a person") logger.debug("Observation contains a person")
val person = personRepository.findFirstByNameLike(observationsRequest.person.toUpperCase()) ?: return listOf()
val site = siteRepository.findById(observationsRequest.site).get() 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) { } else if (observationsRequest.site != null) {
logger.debug("Observation contains a site") logger.debug("Observation contains a site")
return siteRepository.findById(observationsRequest.site).map { return siteRepository.findById(observationsRequest.site).map {

View File

@ -3,8 +3,6 @@ package uk.co.neviyn.observationdatabase
import org.joda.time.LocalDate import org.joda.time.LocalDate
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
@ -23,9 +21,6 @@ class RepositoryTest {
@Autowired @Autowired
lateinit var observationRepository: ObservationRepository lateinit var observationRepository: ObservationRepository
@Autowired
lateinit var personRepository: PersonRepository
@Test @Test
fun testFindBySiteAndDateBetween_EmptyRepository() { fun testFindBySiteAndDateBetween_EmptyRepository() {
val result = observationRepository.findBySiteAndDateBetweenOrderByDateAsc(Site( val result = observationRepository.findBySiteAndDateBetweenOrderByDateAsc(Site(
@ -48,7 +43,7 @@ class RepositoryTest {
@Test @Test
fun testFindBySiteAndDateBetween() { fun testFindBySiteAndDateBetween() {
val correctSite = entityManager.persist(Site(name = "Correct")) 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 incorrectSite = entityManager.persist(Site(name = "Incorrect"))
val tutor = entityManager.persist(Tutor(name = "X", site = correctSite)) val tutor = entityManager.persist(Tutor(name = "X", site = correctSite))
val tutor2 = entityManager.persist(Tutor(name = "N", site = incorrectSite)) val tutor2 = entityManager.persist(Tutor(name = "N", site = incorrectSite))
@ -136,7 +131,7 @@ class RepositoryTest {
@Test @Test
fun testFindByTutorsAndDateBetween() { fun testFindByTutorsAndDateBetween() {
val site = entityManager.persist(Site(name = "Correct")) 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 correctTutor = entityManager.persist(Tutor(name = "X", site = site))
val incorrectTutor = entityManager.persist(Tutor(name = "N", site = site)) val incorrectTutor = entityManager.persist(Tutor(name = "N", site = site))
val tooEarly = Observation( val tooEarly = Observation(
@ -245,9 +240,9 @@ class RepositoryTest {
} }
@Test @Test
fun testFindBySiteAndPersonsAndDateBetween() { fun testFindBySiteAndPersonLikeAndDateBetween() {
val person = entityManager.persist(Person(name = "Foo Bar")) val person = "Foo Bar"
val otherPerson = entityManager.persist(Person(name = "Wrong")) val otherPerson = "Wrong"
val site = entityManager.persist(Site(name = "Incorrect")) val site = entityManager.persist(Site(name = "Incorrect"))
val tutor = entityManager.persist(Tutor(name = "X", site = site)) val tutor = entityManager.persist(Tutor(name = "X", site = site))
val tooEarly = Observation( val tooEarly = Observation(
@ -322,7 +317,7 @@ class RepositoryTest {
entityManager.persist(tooLate) entityManager.persist(tooLate)
entityManager.persist(wrongPerson) entityManager.persist(wrongPerson)
entityManager.persist(justRight) entityManager.persist(justRight)
val result = observationRepository.findBySiteAndPersonAndDateBetweenOrderByDateAsc( val result = observationRepository.findBySiteAndPersonLikeAndDateBetweenOrderByDateAsc(
site = site, site = site,
person = person, person = person,
startDate = LocalDate.now().minusDays(1), startDate = LocalDate.now().minusDays(1),
@ -331,27 +326,4 @@ class RepositoryTest {
assertEquals(1, result.size) assertEquals(1, result.size)
assertEquals(justRight, result.first()) 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.GroupSessionManager
import uk.co.neviyn.observationdatabase.Observation import uk.co.neviyn.observationdatabase.Observation
import uk.co.neviyn.observationdatabase.ObservationRepository 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.Scenario
import uk.co.neviyn.observationdatabase.Site import uk.co.neviyn.observationdatabase.Site
import uk.co.neviyn.observationdatabase.SiteRepository import uk.co.neviyn.observationdatabase.SiteRepository
@ -47,8 +45,6 @@ class GroupSessionControllerTest {
@Mock @Mock
lateinit var observationRepository: ObservationRepository lateinit var observationRepository: ObservationRepository
@Mock @Mock
lateinit var personRepository: PersonRepository
@Mock
lateinit var mailer: Email lateinit var mailer: Email
@After @After
@ -97,8 +93,7 @@ class GroupSessionControllerTest {
val tutor = Tutor(1, "Mr X", site) val tutor = Tutor(1, "Mr X", site)
Mockito.doReturn(Optional.of(site)).`when`(siteRepository).findById(1) Mockito.doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
Mockito.doReturn(listOf(tutor)).`when`(tutorRepository).findAllById(listOf(1)) Mockito.doReturn(listOf(tutor)).`when`(tutorRepository).findAllById(listOf(1))
val person = Person(1, "A Student") val person = "A Student"
Mockito.doReturn(person).`when`(personRepository).save(any())
val scenario = Scenario(0, "Sample title", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "", 5, "", "") 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()) 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"))) controller.startGroupObservation(GroupObservationInit(1, TrainingType.INITIAL, listOf(1), listOf("Sample title")))

View File

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