Skip to content

Commit

Permalink
Refactor entity classes and update its dependencies.
Browse files Browse the repository at this point in the history
A lot of the changes are no longer working. But they are going to be removed anyway.
  • Loading branch information
BilledTrain380 committed Aug 1, 2018
1 parent 8957ce1 commit d01af5a
Show file tree
Hide file tree
Showing 36 changed files with 836 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ class DefaultClassManager(
*/
override fun getAllClasses(): List<Clazz> {
return classRepository.findAll()
.filter { it != null }
.map {
Clazz(
it?.name!!,
it.teacher.name,
it.pendingParticipation())
}
.mapNotNull { it?.map() }
}

/**
Expand All @@ -81,7 +75,7 @@ class DefaultClassManager(

val clazz = classRepository.findByName(name)

return clazz.map { Clazz(it.name, it.teacher.name, it.pendingParticipation()) }
return clazz.map { it.map() }
}

/**
Expand All @@ -95,5 +89,12 @@ class DefaultClassManager(
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

private fun ClazzEntity.pendingParticipation() = competitorRepository.findByClazzId(this.id!!).any { it.sport == null }
private fun ClazzEntity.pendingParticipation() = competitorRepository.findByClazzName(this.name).any { it.sport == null }

private fun ClazzEntity.map(): Clazz {
return Clazz(
name,
coach.name,
pendingParticipation())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@

package ch.schulealtendorf.sporttagpsa.business.competitors

import ch.schulealtendorf.sporttagpsa.entity.ClazzEntity
import ch.schulealtendorf.sporttagpsa.entity.TeacherEntity
import ch.schulealtendorf.sporttagpsa.business.parsing.FlatCompetitor
import ch.schulealtendorf.sporttagpsa.repository.ClazzRepository
import ch.schulealtendorf.sporttagpsa.repository.TeacherRepository
Expand All @@ -59,20 +57,20 @@ class EntrySafeCompetitorClazzConsumer(

/**
* Saves a {@link ClazzEntity} based on the passed in argument.
* The {@link FlatCompetitor#teacher} attribute has to be exist as a TeacherEntity already.
* The {@link FlatCompetitor#teacher} attribute has to be exist as a CoachEntity already.
* @param competitor the input argument
* @throws EntityNotFoundException if the {@link FlatCompetitor#teacher} attribute does not exist as a TeacherEntity already
* @throws EntityNotFoundException if the {@link FlatCompetitor#teacher} attribute does not exist as a CoachEntity already
*/
override fun accept(competitor: FlatCompetitor) {

if (!clazzRepository.findByName(competitor.clazz).isPresent) {
val teacherEntity: TeacherEntity = teacherRepository.findByName(competitor.teacher) ?:
throw EntityNotFoundException("Clazz ${competitor.clazz} expecting an existing TeacherEntity: No TeacherEntity found")

val clazzEntity = ClazzEntity(null, competitor.clazz, teacherEntity)

clazzRepository.save(clazzEntity)
}
// if (!clazzRepository.findByName(competitor.clazz).isPresent) {
// val teacherEntity: CoachEntity = teacherRepository.findByName(competitor.teacher) ?:
// throw EntityNotFoundException("Clazz ${competitor.clazz} expecting an existing CoachEntity: No CoachEntity found")
//
// val clazzEntity = ClazzEntity(null, competitor.clazz, teacherEntity)
//
// clazzRepository.save(clazzEntity)
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
package ch.schulealtendorf.sporttagpsa.business.competitors

import ch.schulealtendorf.sporttagpsa.business.parsing.FlatCompetitor
import ch.schulealtendorf.sporttagpsa.entity.TeacherEntity
import ch.schulealtendorf.sporttagpsa.repository.TeacherRepository
import org.springframework.stereotype.Component

Expand All @@ -54,16 +53,16 @@ class EntrySafeCompetitorTeacherConsumer(
): CompetitorTeacherConsumer {

/**
* Saves a {@link TeacherEntity} based on the passed in argument.
* Saves a {@link CoachEntity} based on the passed in argument.
*
* @param competitor the input argument
*/
override fun accept(competitor: FlatCompetitor) {

if (teacherRepository.findByName(competitor.teacher) == null) {
val teacherEntity: TeacherEntity = TeacherEntity(null, competitor.teacher)

teacherRepository.save(teacherEntity)
}
// if (teacherRepository.findByName(competitor.teacher) == null) {
// val teacherEntity: CoachEntity = CoachEntity(null, competitor.teacher)
//
// teacherRepository.save(teacherEntity)
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class PRAParticipantListReporter(
surname = it.surname
isGender = it.gender
clazz = it.clazz.name
teacher = it.clazz.teacher.name
teacher = it.clazz.coach.name
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (c) 2018 by Nicolas Märchy
*
* This file is part of Sporttag PSA.
*
* Sporttag PSA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sporttag PSA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sporttag PSA. If not, see <http://www.gnu.org/licenses/>.
*
* Diese Datei ist Teil von Sporttag PSA.
*
* Sporttag PSA ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
* veröffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Sporttag PSA wird in der Hoffnung, dass es nützlich sein wird, aber
* OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*
*
*/

package ch.schulealtendorf.sporttagpsa.business.participant

import ch.schulealtendorf.sporttagpsa.business.clazz.ClassManager
import ch.schulealtendorf.sporttagpsa.business.participation.AbsentManager
import ch.schulealtendorf.sporttagpsa.entity.CompetitorEntity
import ch.schulealtendorf.sporttagpsa.model.Birthday
import ch.schulealtendorf.sporttagpsa.model.Gender
import ch.schulealtendorf.sporttagpsa.model.Participant
import ch.schulealtendorf.sporttagpsa.model.Town
import ch.schulealtendorf.sporttagpsa.repository.CompetitorRepository
import org.springframework.stereotype.Component
import java.util.*
import kotlin.NoSuchElementException

/**
* Default implementation of a {@link ParticipantManager} which uses the repository classes.
*
* @author nmaerchy <billedtrain380@gmail.com>
* @since 2.0.0
*/
@Component
class DefaultParticipantManager(
private val competitorRepository: CompetitorRepository,
private val classManager: ClassManager,
private val absentManager: AbsentManager
): ParticipantManager {

/**
* @return all participants
*/
override fun getAllParticipants(): List<Participant> {
return competitorRepository.findAll()
.mapNotNull { it?.map() }
}

/**
* Returns an Optional containing the participant matching
* the given {@code participantId} or an empty Optional if
* no participant could be found.
*
* @param participantId the id of the participant
*
* @return the resulting participant
*/
override fun getParticipant(participantId: Int): Optional<Participant> {

val participant = competitorRepository.findById(participantId)

return participant.map { it?.map() }
}

/**
* Updates the given {@code participant}.
*
* @param participant the participant to update
*
* @throws NoSuchElementException if the given {@code participant} or any of its relations could not be found
*/
override fun updateParticipant(participant: Participant) {

val competitor = competitorRepository.findById(participant.id).get()

competitor.apply {
id = participant.id
surname = participant.surname
prename = participant.prename
gender = participant.gender.value
address = participant.address
birthday = participant.birthday.milliseconds
}

competitorRepository.save(competitor)
}

private fun CompetitorEntity.map(): Participant {

val clazz = classManager.getClass(clazz.name)
val sport: String? = if (sport == null) null else sport?.name

return Participant(
id!!,
surname,
prename,
Gender(gender),
Birthday(birthday),
absentManager.isAbsent(this),
address,
Town(town.id!!, town.zip, town.name),
clazz.get(),
Optional.ofNullable(sport)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2018 by Nicolas Märchy
*
* This file is part of Sporttag PSA.
*
* Sporttag PSA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sporttag PSA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sporttag PSA. If not, see <http://www.gnu.org/licenses/>.
*
* Diese Datei ist Teil von Sporttag PSA.
*
* Sporttag PSA ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
* veröffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Sporttag PSA wird in der Hoffnung, dass es nützlich sein wird, aber
* OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*
*
*/

package ch.schulealtendorf.sporttagpsa.business.participant

import ch.schulealtendorf.sporttagpsa.model.Participant
import java.util.*

/**
* A manager for the participants of PSA.
*
* @author nmaerchy <billedtrain380@gmail.com>
* @since 2.0.0
*/
interface ParticipantManager {

/**
* @return all participants
*/
fun getAllParticipants(): List<Participant>

/**
* Returns an Optional containing the participant matching
* the given {@code participantId} or an empty Optional if
* no participant could be found.
*
* @param participantId the id of the participant
*
* @return the resulting participant
*/
fun getParticipant(participantId: Int): Optional<Participant>

/**
* Updates the given {@code participant}.
*
* @param participant the participant to update
*
* @throws NoSuchElementException if the given {@code participant} or any of its relations could not be found
*/
fun updateParticipant(participant: Participant)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2018 by Nicolas Märchy
*
* This file is part of Sporttag PSA.
*
* Sporttag PSA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sporttag PSA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sporttag PSA. If not, see <http://www.gnu.org/licenses/>.
*
* Diese Datei ist Teil von Sporttag PSA.
*
* Sporttag PSA ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
* veröffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Sporttag PSA wird in der Hoffnung, dass es nützlich sein wird, aber
* OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*
*
*/

package ch.schulealtendorf.sporttagpsa.business.participation

import ch.schulealtendorf.sporttagpsa.entity.CompetitorEntity

/**
* Manager for the absent status of a competitor.
*
* @author nmaerchy <billedtrain380@gmail.com>
* @since 2.0.0
*/
interface AbsentManager {

/**
* @return true if the given {@code competitor} is absent, otherwise false
*/
fun isAbsent(competitor: CompetitorEntity): Boolean
}
Loading

0 comments on commit d01af5a

Please sign in to comment.