package ciai.service; import java.util.HashSet; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import ciai.exception.EditionNotFoundException; import ciai.exception.EvaluationNotAddedToEditionException; import ciai.exception.ProfessorNotAddedToEditionException; import ciai.exception.ProfessorNotRemovedFromEdition; import ciai.exception.StudentNotAddedToEditionException; import ciai.exception.StudentNotRemovedFromEditionException; import ciai.model.Edition; import ciai.model.Evaluation; import ciai.model.Professor; import ciai.model.Student; import ciai.model.StudentEvaluation; import ciai.model.StudentEvaluationPK; import ciai.repository.EditionRepository; import ciai.repository.EvaluationRepository; import ciai.repository.StudentEvaluationRepository; @Service public class EditionService { @Autowired EditionRepository editionRep; @Autowired StudentEvaluationRepository studentEvalRep; @Autowired EvaluationRepository evalRep; public Edition getEdition(Long id) throws EditionNotFoundException { Edition e = editionRep.findOne(id); if (e == null) throw new EditionNotFoundException(); return e; } public Set getStudentCurrentEditions(int number) { return editionRep.currentEditionsFromStudent(number); } public Set getProfessorCurrentEditions(Long id) { return editionRep.currentEditionsFromProfessor(id); } public void addProfessorToEdition(Edition edition, Professor p) throws ProfessorNotAddedToEditionException { Set professors = edition.getProfessors(); if (professors == null) professors = new HashSet(); professors.add(p); edition.setProfessors(professors); if (editionRep.save(edition) == null) throw new ProfessorNotAddedToEditionException(); } public Set getEditionProfessors(Long id) throws EditionNotFoundException { return getEdition(id).getProfessors(); } public void removeProfessorFromEdition(Edition edition, Professor professor) throws ProfessorNotRemovedFromEdition { Set professors = edition.getProfessors(); if (professors.contains(professor)) { if (!professors.remove(professor)) { throw new ProfessorNotRemovedFromEdition(); } } if (editionRep.save(edition) == null) throw new ProfessorNotRemovedFromEdition(); } public Set getEditionStudents(Long id) throws EditionNotFoundException { return getEdition(id).getStudents(); } public void addStudentToEdition(Edition edition, Student student) throws StudentNotAddedToEditionException { Set students = edition.getStudents(); if (students == null) students = new HashSet(); if (students.contains(student)) throw new StudentNotAddedToEditionException(); students.add(student); edition.setStudents(students); for (Evaluation eval : edition.getEvaluations()) { StudentEvaluation se = new StudentEvaluation(new StudentEvaluationPK(eval, student, 0, 0)); studentEvalRep.save(se); } if (editionRep.save(edition) == null) throw new StudentNotAddedToEditionException(); } public void removeStudentFromEdition(Edition edition, Student student) throws StudentNotRemovedFromEditionException { Set students = edition.getStudents(); if (students.contains(student)) { if (!students.remove(student)) { throw new StudentNotRemovedFromEditionException(); } } if (editionRep.save(edition) == null) throw new StudentNotRemovedFromEditionException(); } public void addEvaluationToEdition(Edition edition, String name, int type, float weight, String date, String time) throws EvaluationNotAddedToEditionException { Evaluation eval = new Evaluation(name, type, weight, date, time, edition); if (evalRep.save(eval) == null) throw new EvaluationNotAddedToEditionException(); Set evalslist = edition.getEvaluations(); if (edition.getEvaluations() == null) evalslist = new HashSet(); evalslist.add(eval); if (editionRep.save(edition) == null) throw new EvaluationNotAddedToEditionException(); for (Student s : edition.getStudents()) { StudentEvaluation se = new StudentEvaluation(new StudentEvaluationPK(eval, s, 0, 0)); studentEvalRep.save(se); } } }