package ciai.service; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import ciai.exception.EvaluationNotEditedException; import ciai.exception.EvaluationNotFoundException; import ciai.exception.EvaluationNotRemovedFromEdition; import ciai.model.Edition; import ciai.model.Evaluation; import ciai.repository.EditionRepository; import ciai.repository.EvaluationRepository; import ciai.repository.StudentEvaluationRepository; @Service public class EvaluationService { @Autowired EvaluationRepository evalRep; @Autowired EditionRepository editionRep; @Autowired StudentEvaluationRepository studentevalRep; Evaluation getEvaluation(Long id) throws EvaluationNotFoundException { Evaluation eval = evalRep.findOne(id); if (eval == null) throw new EvaluationNotFoundException(); return eval; } public void editEvaluation(Edition edition, Evaluation eval, String name, String date, String time, float weight, int type) throws EvaluationNotFoundException, EvaluationNotEditedException { if (!edition.getEvaluations().contains(eval)) throw new EvaluationNotFoundException(); eval.setName(name); eval.setDate(date); eval.setTime(time); eval.setWeight(weight); eval.setType(type); if (evalRep.save(eval) == null) throw new EvaluationNotEditedException(); } public void removeEvaluationFromEdition(Edition edition, Evaluation eval) throws EvaluationNotRemovedFromEdition { Set evals = edition.getEvaluations(); if (!evals.contains(eval)) throw new EvaluationNotRemovedFromEdition(); // apaga a avaliação das avaliações de cada estudante // (student_evaluation) studentevalRep.delete(studentevalRep.getStudentsByEvaluation(eval.getId())); // apaga a avaliação da tabela da edição evals.remove(eval); editionRep.save(edition); // apaga a avaliação da tabela de avaliaçõs evalRep.delete(eval); } public Set getEditionEvaluations(Long id) { return evalRep.editionEvaluationsByType(id); } }