Java tutorial
/** * Copyright 2014 Instituto Superior Tcnico * * This file is part of FenixEdu Academic Thesis. * * FenixEdu Academic Thesis is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * FenixEdu Academic Thesis 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with FenixEdu Academic Thesis. If not, see <http://www.gnu.org/licenses/>. */ package org.fenixedu.academic.thesis.ui.controller; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import org.fenixedu.academic.domain.student.Registration; import org.fenixedu.academic.domain.student.Student; import org.fenixedu.academic.thesis.domain.StudentThesisCandidacy; import org.fenixedu.academic.thesis.domain.ThesisProposal; import org.fenixedu.academic.thesis.domain.ThesisProposalsConfiguration; import org.fenixedu.academic.thesis.ui.exception.MaxNumberStudentThesisCandidaciesException; import org.fenixedu.academic.thesis.ui.exception.OutOfCandidacyPeriodException; import org.fenixedu.academic.thesis.ui.exception.ThesisProposalException; import org.fenixedu.academic.thesis.ui.service.ParticipantLabelService; import org.fenixedu.academic.thesis.ui.service.StudentCandidaciesService; import org.fenixedu.bennu.core.security.Authenticate; import org.fenixedu.bennu.spring.portal.SpringFunctionality; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; 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.servlet.mvc.support.RedirectAttributes; import com.google.gson.JsonArray; import com.google.gson.JsonParser; @SpringFunctionality(app = ThesisProposalsController.class, title = "title.studentThesisCandidacy.management", accessGroup = "activeStudents") @RequestMapping("/studentCandidacies") public class StudentCandidaciesController { @Autowired StudentCandidaciesService service; @Autowired(required = false) ParticipantLabelService participantLabelService; @RequestMapping(value = "", method = RequestMethod.GET) public String listProposals(Model model) { Student student = Authenticate.getUser().getPerson().getStudent(); Set<ThesisProposalsConfiguration> suggestedConfigs = service.getSuggestedConfigs(student); HashMap<Registration, Set<ThesisProposal>> proposalsByReg = service.getOpenProposalsByReg(student); Map<ThesisProposalsConfiguration, List<StudentThesisCandidacy>> candidaciesByConfig = service .getCandidaciesByConfig(student); int proposalsSize = proposalsByReg.values().stream().map(Set::size).reduce(0, (a, b) -> a + b); int candidaciesSize = candidaciesByConfig.values().stream().map(List::size).reduce(0, (a, b) -> a + b); if (participantLabelService != null) { model.addAttribute("participantLabelService", participantLabelService); } model.addAttribute("suggestedConfigs", suggestedConfigs); model.addAttribute("proposalsSize", proposalsSize); model.addAttribute("candidaciesSize", candidaciesSize); model.addAttribute("candidaciesByConfig", candidaciesByConfig); model.addAttribute("proposalsByReg", proposalsByReg); Map<ThesisProposal, Integer> applicationCountByProposalConfig = candidaciesByConfig.values().stream() .flatMap(List::stream).collect(Collectors.toMap(StudentThesisCandidacy::getThesisProposal, c -> c.getThesisProposal().getStudentThesisCandidacySet().size())); model.addAttribute("applicationCountByProposalConfig", applicationCountByProposalConfig); Map<ThesisProposal, Integer> applicationCountByProposalReg = proposalsByReg.values().stream() .flatMap(Set::stream) .collect(Collectors.toMap(tp -> tp, tp -> tp.getStudentThesisCandidacySet().size())); model.addAttribute("applicationCountByProposalReg", applicationCountByProposalReg); Set<ThesisProposal> acceptedProposals = proposalsByReg.values().stream().flatMap(Set::stream) .flatMap(tp -> tp.getStudentThesisCandidacySet().stream()) .filter(candidacy -> candidacy.getAcceptedByAdvisor()) .map(candidacy -> candidacy.getThesisProposal()).collect(Collectors.toSet()); model.addAttribute("acceptedProposals", acceptedProposals); return "studentCandidacies/list"; } @RequestMapping(value = "/candidate/{oid}", method = RequestMethod.POST) public String createThesisCandidacyForm(@PathVariable("oid") ThesisProposal thesisProposal, @RequestParam Registration registration, Model model) throws MaxNumberStudentThesisCandidaciesException, OutOfCandidacyPeriodException { try { service.createStudentThesisCandidacy(registration, thesisProposal); } catch (ThesisProposalException exception) { model.addAttribute("error", exception.getClass().getSimpleName()); return listProposals(model); } return "redirect:/studentCandidacies"; } @RequestMapping(value = "/delete/{oid}", method = RequestMethod.GET) public String deleteThesisCandidacy(@PathVariable("oid") StudentThesisCandidacy studentThesisCandidacy, Model model) { boolean result = service.delete(studentThesisCandidacy, model); if (result) { return "redirect:/studentCandidacies"; } else { return listProposals(model); } } @RequestMapping(value = "/updatePreferences", method = RequestMethod.POST) public String updateStudentThesisCandidaciesWeights(@RequestParam String json, Model model, RedirectAttributes redirectAttrs) { JsonParser parser = new JsonParser(); JsonArray jsonArray = (JsonArray) parser.parse(json); try { service.updateStudentThesisCandidaciesWeights(jsonArray); } catch (ThesisProposalException e) { redirectAttrs.addAttribute("error", e.getClass().getSimpleName()); } return "redirect:/studentCandidacies"; } }