package ciai.service; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import ciai.exception.StudentNotEditedException; import ciai.exception.StudentNotFoundException; import ciai.model.Account; import ciai.model.Course; import ciai.model.Edition; import ciai.model.Evaluation; import ciai.model.Grade; import ciai.model.Result; import ciai.model.ResultByEdition; import ciai.model.Student; import ciai.model.StudentEvaluation; import ciai.model.StudentEvaluationPK; import ciai.repository.StudentEvaluationRepository; import ciai.repository.StudentRepository; @Service public class StudentService { @Autowired StudentRepository studentRep; @Autowired StudentEvaluationRepository studentEvalRep; public Student getStudentByNumber(int number) throws StudentNotFoundException { Student s = studentRep.findByNumber(number); if (s == null) throw new StudentNotFoundException(); return s; } public void editStudent(int number, String name, String photo, String personalemail, String address, String birthday) throws StudentNotFoundException, StudentNotEditedException { Student student = getStudentByNumber(number); if (name != null && name.length() > 0) student.setName(name); if (photo != null && photo.length() > 0) student.setPhoto(photo); if (personalemail != null && personalemail.length() > 0) student.setPersonalEmail(personalemail); if (address != null && address.length() > 0) student.setAddress(address); if (birthday != null && birthday.length() > 0) student.setBirthday(birthday); if (studentRep.save(student) == null) throw new StudentNotEditedException(); } public Collection getStudentResults(int number) { Map resultsfinal = new HashMap(); for (StudentEvaluation se : studentEvalRep.findAll()) { StudentEvaluationPK sepk = se.getId(); if (sepk != null && sepk.getStatus() == 1) { Student st = sepk.getStudent(); if (st != null) { if (st.getNumber() == number) { Evaluation ev = sepk.getEvaluation(); if (ev != null) { Edition ed = ev.getEdition(); if (ed != null) { Course c = ed.getCourse(); if (c != null) { if (resultsfinal.containsKey(c.getIdentifier())) { Result r = resultsfinal.get(c.getIdentifier()); if (ev.getType() == 0 || ev.getType() == 1 || ev.getType() == 3) { if (r.getType() != 2) { r.setValue(r.getValue() + ev.getWeight() * sepk.getValue()); } } else if (ev.getType() == 2) { r.setValue(ev.getWeight() * sepk.getValue()); r.setValue(ev.getWeight() * sepk.getValue()); r.setType(ev.getType()); } } else { Result r = new Result(c, sepk.getValue() * ev.getWeight(), ev.getType(), ev.getEdition().getId()); resultsfinal.put(c.getIdentifier(), r); } } } } } } } } return resultsfinal.values(); } public Map> getStudentsResultsByEdition(Edition edition) { Map> mapresults = new HashMap>(); Set evals = edition.getEvaluations(); Set students = edition.getStudents(); Iterable ses = studentEvalRep.findAll(); for (Evaluation eval : evals) { for (Student s : students) { for (StudentEvaluation se : ses) { if (se.getId().getEvaluation().getId() == eval.getId()) { if (se.getId().getStudent().getId() == s.getId()) { String key = String.valueOf(se.getId().getStudent().getNumber()); if (!mapresults.containsKey(key)) mapresults.put(String.valueOf(s.getNumber()), new LinkedList()); mapresults.get(key).add(new ResultByEdition(eval, s, se.getId().getValue())); } } } } } return mapresults; } public Student getStudentByAccount(Account ac) { return studentRep.getStudentByAccount(ac.getId()); } public Set getStudensFromDegree(Long did) { return studentRep.getStudensFromDegree(did); } public void saveGrade(Grade next) { studentEvalRep.updateStudentEvaluation(next.getValue(), next.getStudent(), 1, next.getEvaluation()); } }