package ciai.controller; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.fasterxml.jackson.annotation.JsonView; import ciai.exception.EditionNotFoundException; import ciai.exception.EvaluationNotAddedToEditionException; import ciai.exception.EvaluationNotEditedException; import ciai.exception.EvaluationNotFoundException; import ciai.exception.EvaluationNotRemovedFromEdition; import ciai.exception.ProfessorNotAddedToEditionException; import ciai.exception.ProfessorNotFoundException; import ciai.exception.ProfessorNotRemovedFromEdition; import ciai.exception.StudentNotAddedToEditionException; import ciai.exception.StudentNotFoundException; import ciai.exception.StudentNotRemovedFromEditionException; import ciai.model.Degree; import ciai.model.Edition; import ciai.model.Evaluation; import ciai.model.Grade; import ciai.model.Professor; import ciai.model.ResultByEdition; import ciai.model.Student; import ciai.service.AppService; import ciai.view.Views; import tools.AccountRole; import tools.Response; @Controller @RequestMapping(value = "/edition") public class EditionController { @Autowired AppService service; /** * Detalhes de uma edição */ @RequestMapping(value = "/view/{id}", method = RequestMethod.GET) @JsonView(Views.StudentView.class) public @ResponseBody ResponseEntity getEdition(@PathVariable Long id) { Edition edition; try { edition = service.getEdition(id); } catch (EditionNotFoundException e) { return new ResponseEntity(HttpStatus.NOT_FOUND); } return new ResponseEntity(edition, HttpStatus.OK); } /** * Adicionar professor a uma edição */ @RequestMapping(value = "/{id}/add/professor", method = RequestMethod.POST) @PreAuthorize("hasRole('PROFESSOR') and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity addProfessorToEdition(@PathVariable Long id, @RequestParam Map requestParams) { try { Long professor_id = Long.parseLong(requestParams.get("professor_id")); Professor p = service.getProfessor(professor_id); Edition e = service.getEdition(id); service.addProfessorToEdition(e, p); } catch (ProfessorNotFoundException e) { return new ResponseEntity(new Response("Professor not found"), HttpStatus.NOT_FOUND); } catch (EditionNotFoundException e) { return new ResponseEntity(new Response("Edition not found"), HttpStatus.NOT_FOUND); } catch (ProfessorNotAddedToEditionException e1) { return new ResponseEntity(new Response("Error adding professor to edition"), HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(new Response("Professor added to edition"), HttpStatus.OK); } /** * Listar professores de uma edição */ @RequestMapping(value = "/view/{id}/professors", method = RequestMethod.GET) @JsonView(Views.ProfessorView.class) public @ResponseBody ResponseEntity listProfessorsFromEdition(@PathVariable Long id) { Set list; try { list = service.getEditionProfessors(id); } catch (EditionNotFoundException e) { return new ResponseEntity(HttpStatus.NOT_FOUND); } return new ResponseEntity>(list, HttpStatus.OK); } /** * Remover professor de uma edição */ @RequestMapping(value = "/{id}/remove/professor", method = RequestMethod.POST) @PreAuthorize("hasRole('PROFESSOR') and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity removeProfessorFromEdition(@PathVariable Long id, @RequestParam Map requestParams) { try { Long professor_id = Long.parseLong(requestParams.get("number")); Professor professor = service.getProfessor(professor_id); if (professor.getAccount().getRole().getDescription().equals(AccountRole.PROFESSOR)) return new ResponseEntity(new Response("Only assistant professors can be remove"), HttpStatus.INTERNAL_SERVER_ERROR); Edition edition = service.getEdition(id); service.removeProfessorFromEdition(edition, professor); } catch (ProfessorNotRemovedFromEdition e) { return new ResponseEntity(new Response("Error removing professor from edition"), HttpStatus.INTERNAL_SERVER_ERROR); } catch (ProfessorNotFoundException e) { return new ResponseEntity(new Response("Professor not found"), HttpStatus.NOT_FOUND); } catch (EditionNotFoundException e) { return new ResponseEntity(new Response("Edition not found"), HttpStatus.NOT_FOUND); } return new ResponseEntity(new Response("Professor removed from edition"), HttpStatus.OK); } /** * Adicionar estudante a uma edição */ @RequestMapping(value = "/{id}/add/student", method = RequestMethod.POST) @PreAuthorize("hasRole('PROFESSOR') and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity addStudentToEdition(@PathVariable Long id, @RequestParam Map requestParams) { try { int number = Integer.parseInt(requestParams.get("number")); Edition edition = service.getEdition(id); Student student = service.getStudentByNumber(number); service.addStudentToEdition(edition, student); } catch (EditionNotFoundException e) { return new ResponseEntity(new Response("Edition not found"), HttpStatus.NOT_FOUND); } catch (StudentNotFoundException e) { return new ResponseEntity(new Response("Student not found"), HttpStatus.NOT_FOUND); } catch (StudentNotAddedToEditionException e) { return new ResponseEntity(new Response("Error adding student to edition"), HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(new Response("Student added to edition"), HttpStatus.OK); } /** * Remover estudante de uma edição */ @RequestMapping(value = "/{id}/remove/student", method = RequestMethod.POST) @PreAuthorize("hasRole('PROFESSOR') and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity removeStudentFromEdition(@PathVariable Long id, @RequestParam Map requestParams) { try { int number = Integer.parseInt(requestParams.get("number")); Edition edition = service.getEdition(id); Student student = service.getStudentByNumber(number); service.removeStudentFromEdition(edition, student); } catch (EditionNotFoundException e) { return new ResponseEntity(new Response("Edition not found"), HttpStatus.NOT_FOUND); } catch (StudentNotFoundException e) { return new ResponseEntity(new Response("Student not found"), HttpStatus.NOT_FOUND); } catch (StudentNotRemovedFromEditionException e) { return new ResponseEntity(new Response("Error removing student from edition"), HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(new Response("Student removed from edition"), HttpStatus.OK); } /** * Listar estudantes de uma edição */ @RequestMapping(value = "/view/{id}/students", method = RequestMethod.GET) @JsonView(Views.StudentView.class) @PreAuthorize("(hasRole('ASSISTANT') or hasRole('PROFESSOR')) and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity listStudentsFromEdition(@PathVariable Long id) { Set list; try { list = service.getEditionStudents(id); } catch (EditionNotFoundException e) { return new ResponseEntity(HttpStatus.NOT_FOUND); } return new ResponseEntity>(list, HttpStatus.OK); } /** * Adicionar avaliação a uma edição */ @RequestMapping(value = "/{id}/add/evaluation", method = RequestMethod.POST) @PreAuthorize("hasRole('PROFESSOR') and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity addEvaluationToEdition(@PathVariable Long id, @RequestParam Map requestParams) { String name = requestParams.get("name"); String date = requestParams.get("date"); String time = requestParams.get("time"); float weight = Float.parseFloat(requestParams.get("weight")); int type = Integer.parseInt(requestParams.get("type")); try { Edition edition = service.getEdition(id); service.addEvaluationToEdition(edition, name, type, weight, date, time); } catch (EditionNotFoundException e) { return new ResponseEntity(new Response("Edition not found"), HttpStatus.NOT_FOUND); } catch (EvaluationNotAddedToEditionException e) { return new ResponseEntity(new Response("Evaluation not added to edition"), HttpStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { System.out.println("add evaluation to edition error"); return new ResponseEntity(new Response("Evaluation not added to edition"), HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(new Response("Evaluation added to edition"), HttpStatus.OK); } /** * Editar avaliação de uma edição */ @RequestMapping(value = "/{id}/edit/evaluation", method = RequestMethod.POST) @PreAuthorize("hasRole('PROFESSOR') and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity editEvaluationFromEdition(@PathVariable Long id, @RequestParam Map requestParams) { Long eval_id = Long.valueOf(requestParams.get("evaluation_id")); String name = requestParams.get("name"); String date = requestParams.get("date"); String time = requestParams.get("time"); float weight = Float.parseFloat(requestParams.get("weight")); int type = Integer.parseInt(requestParams.get("type")); try { Edition edition = service.getEdition(id); Evaluation eval = service.getEvaluation(eval_id); service.editEvaluation(edition, eval, name, date, time, weight, type); } catch (EditionNotFoundException e) { return new ResponseEntity(new Response("Edition not found"), HttpStatus.NOT_FOUND); } catch (EvaluationNotFoundException e) { return new ResponseEntity(new Response("Evaluation not found"), HttpStatus.NOT_FOUND); } catch (EvaluationNotEditedException e) { return new ResponseEntity(new Response("Error editing the evaluation"), HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(new Response("Evaluation edited"), HttpStatus.OK); } /** * Apagar avaliação de uma edição */ @RequestMapping(value = "/{id}/remove/evaluation", method = RequestMethod.POST) @PreAuthorize("hasRole('PROFESSOR') and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity removeEvaluationFromEdition(@PathVariable Long id, @RequestParam Map requestParams) { try { Long eval_id = Long.valueOf(requestParams.get("number")); Edition edition = service.getEdition(id); Evaluation eval = service.getEvaluation(eval_id); service.removeEvaluationFromEdition(edition, eval); } catch (EditionNotFoundException e) { return new ResponseEntity(new Response("Edition not found"), HttpStatus.NOT_FOUND); } catch (EvaluationNotFoundException e) { return new ResponseEntity(new Response("Evaluation not found"), HttpStatus.NOT_FOUND); } catch (EvaluationNotRemovedFromEdition e) { return new ResponseEntity(new Response("Error removing the evaluation"), HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(new Response("Evaluation removed from edition"), HttpStatus.OK); } /** * Lista de avaliações por tipo de uma edição */ @RequestMapping(value = "/view/{id}/evaluations", method = RequestMethod.GET) @JsonView(Views.StudentView.class) public @ResponseBody Set getEditionEvaluations(@PathVariable Long id) { return service.getEditionEvaluations(id); } /** * Atribuir nota de uma avaliação de um estudante */ @RequestMapping(value = "/{id}/save/grades", method = RequestMethod.POST) @PreAuthorize("(hasRole('ASSISTANT') or hasRole('PROFESSOR')) and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity gradeStudent(@PathVariable Long id, @RequestBody List input) { List grades = input; Iterator it = grades.iterator(); List myGrades = new LinkedList(); while(it.hasNext()){ Grade c = it.next(); Grade n = new Grade(); n.setEvaluation(c.getEvaluation()); n.setStudent(c.getStudent()); n.setValue(c.getValue()); myGrades.add(n); } it = myGrades.iterator(); while(it.hasNext()){ service.saveGrade(it.next()); } return new ResponseEntity(new Response("Student graded"), HttpStatus.OK); } /** * Listar estudantes possiveis a adicionar a uma edição */ @RequestMapping(value = "/{id}/search/student", method = RequestMethod.POST) @JsonView(Views.ProfessorView.class) @PreAuthorize("(hasRole('ASSISTANT') or hasRole('PROFESSOR')) and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity searchStudentsToAddEdition(@PathVariable Long id, @RequestParam Map requestParams) { String value = requestParams.get("value"); Set find = new HashSet(); try { Edition edition = service.getEdition(id); Degree d = edition.getDegree(); for (Student student : service.getStudensFromDegree(d.getId())) { if (student.getEditions() != null) { if (!student.getEditions().contains(edition)) { if (student.getName().toLowerCase().contains(value.toLowerCase())) { find.add(student); } else { try { int number = Integer.parseInt(value); if (student.getNumber() == number) { find.add(student); } } catch (Exception e2) { } } } } } } catch (EditionNotFoundException e) { return new ResponseEntity(new Response("Edition not found"), HttpStatus.NOT_FOUND); } catch (Exception e) { e.printStackTrace(); return new ResponseEntity(new Response("Error getting students"), HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity>(find, HttpStatus.OK); } /** * Listar professores possiveis a adicionar a uma edição */ @RequestMapping(value = "/{id}/search/professor", method = RequestMethod.POST) @JsonView(Views.ProfessorView.class) @PreAuthorize("(hasRole('ASSISTANT') or hasRole('PROFESSOR')) and @securityService.isProfessorOfEdition(#id)") public @ResponseBody ResponseEntity searchProfessorsToAddEdition(@PathVariable Long id, @RequestParam Map requestParams) { String value = requestParams.get("value"); Set find = new HashSet(); try { Edition edition = service.getEdition(id); for (Professor professor : service.getAllProfessors()) { if (professor.getEditions() != null) { if (!professor.getEditions().contains(edition)) { if (professor.getName().toLowerCase().contains(value.toLowerCase())) { find.add(professor); } } } } } catch (EditionNotFoundException e) { return new ResponseEntity(new Response("Edition not found"), HttpStatus.NOT_FOUND); } catch (Exception e) { e.printStackTrace(); return new ResponseEntity(new Response("Error getting students"), HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity>(find, HttpStatus.OK); } /** * Lista de resultados de todos os estudantes de uma edição */ @RequestMapping(value = "/view/{id}/students/results", method = RequestMethod.GET) @PreAuthorize("(hasRole('ASSISTANT') or hasRole('PROFESSOR')) and @securityService.isProfessorOfEdition(#id)") @JsonView(Views.ProfessorView.class) public ResponseEntity getStudentsResultsByEdition(@PathVariable Long id) { Map> result = new HashMap>(); try { Edition e = service.getEdition(id); result = service.getStudentsResultsByEdition(e); } catch (EditionNotFoundException e) { return new ResponseEntity(HttpStatus.NOT_FOUND); } return new ResponseEntity>>(result,HttpStatus.OK); } }