/** * Copyright © 2002 Instituto Superior Técnico * * This file is part of FenixEdu Academic. * * FenixEdu Academic 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 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. If not, see <http://www.gnu.org/licenses/>. */ package org.fenixedu.academic.ui.struts.action.coordinator.candidacy.secondCycle; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map.Entry; import java.util.SortedSet; import java.util.stream.Collectors; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.fenixedu.academic.domain.Coordinator; import org.fenixedu.academic.domain.Degree; import org.fenixedu.academic.domain.DegreeCurricularPlan; import org.fenixedu.academic.domain.ExecutionDegree; import org.fenixedu.academic.domain.ExecutionInterval; import org.fenixedu.academic.domain.ExecutionSemester; import org.fenixedu.academic.domain.ExecutionYear; import org.fenixedu.academic.domain.candidacyProcess.CandidacyProcess; import org.fenixedu.academic.domain.candidacyProcess.IndividualCandidacyProcess; import org.fenixedu.academic.domain.candidacyProcess.secondCycle.SecondCycleCandidacyProcess; import org.fenixedu.academic.domain.candidacyProcess.secondCycle.SecondCycleIndividualCandidacyProcess; import org.fenixedu.academic.domain.period.SecondCycleCandidacyPeriod; import org.fenixedu.academic.predicate.AccessControl; import org.fenixedu.academic.ui.struts.action.candidacy.CandidacyProcessDA; import org.fenixedu.academic.ui.struts.action.coordinator.DegreeCoordinatorIndex; import org.fenixedu.academic.util.Bundle; import org.fenixedu.bennu.core.i18n.BundleUtil; import org.fenixedu.bennu.struts.annotations.Forward; import org.fenixedu.bennu.struts.annotations.Forwards; import org.fenixedu.bennu.struts.annotations.Mapping; import org.fenixedu.commons.spreadsheet.Spreadsheet; import org.fenixedu.commons.spreadsheet.Spreadsheet.Row; import org.fenixedu.commons.spreadsheet.SpreadsheetXLSExporter; import pt.ist.fenixframework.FenixFramework; @Mapping(path = "/caseHandlingSecondCycleCandidacyProcess", module = "coordinator", formBeanClass = SecondCycleCandidacyProcessDA.SecondCycleCandidacyProcessForm.class, functionality = DegreeCoordinatorIndex.class) @Forwards(@Forward(name = "intro", path = "/coordinator/candidacy/secondCycle/mainCandidacyProcess.jsp")) public class SecondCycleCandidacyProcessDA extends CandidacyProcessDA { static public class SecondCycleCandidacyProcessForm extends CandidacyProcessForm { private String selectedProcessId; public String getSelectedProcessId() { return selectedProcessId; } public void setSelectedProcessId(String selectedProcessId) { this.selectedProcessId = selectedProcessId; } } @Override protected Class getProcessType() { return SecondCycleCandidacyProcess.class; } @Override protected Class getChildProcessType() { return SecondCycleIndividualCandidacyProcess.class; } @Override protected Class getCandidacyPeriodType() { return SecondCycleCandidacyPeriod.class; } @Override protected SecondCycleCandidacyProcess getProcess(HttpServletRequest request) { return (SecondCycleCandidacyProcess) super.getProcess(request); } @Override public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { DegreeCoordinatorIndex.setCoordinatorContext(request); return super.execute(mapping, actionForm, request, response); } List<ExecutionInterval> readExecutionIntervalFilteredByCoordinatorTeam(final HttpServletRequest request) { final List<ExecutionInterval> returnExecutionIntervals = new ArrayList<ExecutionInterval>(); final List<ExecutionInterval> executionIntervals = ExecutionInterval.readExecutionIntervalsWithCandidacyPeriod(getCandidacyPeriodType()); DegreeCurricularPlan degreeCurricularPlan = getDegreeCurricularPlan(request); for (ExecutionInterval interval : executionIntervals) { final ExecutionYear executionYear = (interval instanceof ExecutionYear) ? (ExecutionYear) interval : ((ExecutionSemester) interval) .getExecutionYear(); final ExecutionDegree executionDegree = degreeCurricularPlan.getExecutionDegreeByYear(executionYear); if (executionDegree == null) { continue; } for (Coordinator coordinator : executionDegree.getCoordinatorsListSet()) { if (coordinator.getPerson() == AccessControl.getPerson()) { returnExecutionIntervals.add(interval); break; } } } return returnExecutionIntervals; } @Override protected void setStartInformation(ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { if (!hasExecutionInterval(request)) { final List<ExecutionInterval> executionIntervals = readExecutionIntervalFilteredByCoordinatorTeam(request); request.setAttribute("executionIntervals", executionIntervals); if (executionIntervals.size() == 1) { final ExecutionInterval executionInterval = executionIntervals.iterator().next(); final List<SecondCycleCandidacyProcess> candidacyProcesses = getCandidacyProcesses(executionInterval); if (candidacyProcesses.size() == 1) { setCandidacyProcessInformation(request, candidacyProcesses.iterator().next()); setCandidacyProcessInformation(actionForm, getProcess(request)); request.setAttribute("candidacyProcesses", candidacyProcesses); return; } } request.setAttribute("canCreateProcess", canCreateProcess(getProcessType().getName())); request.setAttribute("executionIntervals", executionIntervals); } else { final ExecutionInterval executionInterval = getExecutionInterval(request); final SecondCycleCandidacyProcess candidacyProcess = getCandidacyProcess(request, executionInterval); if (candidacyProcess != null) { setCandidacyProcessInformation(request, candidacyProcess); setCandidacyProcessInformation(actionForm, getProcess(request)); } else { final List<SecondCycleCandidacyProcess> candidacyProcesses = getCandidacyProcesses(executionInterval); if (candidacyProcesses.size() == 1) { setCandidacyProcessInformation(request, candidacyProcesses.iterator().next()); setCandidacyProcessInformation(actionForm, getProcess(request)); request.setAttribute("candidacyProcesses", candidacyProcesses); return; } request.setAttribute("canCreateProcess", canCreateProcess(getProcessType().getName())); request.setAttribute("executionIntervals", readExecutionIntervalFilteredByCoordinatorTeam(request)); } request.setAttribute("candidacyProcesses", getCandidacyProcesses(executionInterval)); } } private List<ExecutionInterval> getExecutionIntervalsWithCandidacyPeriod() { return ExecutionInterval.readExecutionIntervalsWithCandidacyPeriod(getCandidacyPeriodType()); } private List<SecondCycleCandidacyProcess> getCandidacyProcesses(final ExecutionInterval executionInterval) { final List<SecondCycleCandidacyProcess> result = new ArrayList<SecondCycleCandidacyProcess>(); for (final SecondCycleCandidacyPeriod period : executionInterval.getSecondCycleCandidacyPeriods()) { result.add(period.getSecondCycleCandidacyProcess()); } return result; } @Override public ActionForward listProcessAllowedActivities(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { setCandidacyProcessInformation(request, getProcess(request)); setCandidacyProcessInformation(form, getProcess(request)); request.setAttribute("candidacyProcesses", getCandidacyProcesses(getProcess(request).getCandidacyExecutionInterval())); return introForward(mapping); } private void setCandidacyProcessInformation(final ActionForm actionForm, final SecondCycleCandidacyProcess process) { final SecondCycleCandidacyProcessForm form = (SecondCycleCandidacyProcessForm) actionForm; form.setSelectedProcessId(process.getExternalId()); form.setExecutionIntervalId(process.getCandidacyExecutionInterval().getExternalId()); } @Override protected ActionForward introForward(ActionMapping mapping) { return mapping.findForward("intro"); } @Override protected SecondCycleCandidacyProcess getCandidacyProcess(final HttpServletRequest request, final ExecutionInterval executionInterval) { final String selectedProcessId = getStringFromRequest(request, "selectedProcessId"); if (selectedProcessId != null) { for (final SecondCycleCandidacyPeriod candidacyPeriod : executionInterval.getSecondCycleCandidacyPeriods()) { if (candidacyPeriod.getSecondCycleCandidacyProcess().getExternalId().equals(selectedProcessId)) { return candidacyPeriod.getSecondCycleCandidacyProcess(); } } } return null; } public ActionForward prepareExecutePrintCandidacies(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment; filename=" + getReportFilename()); final ServletOutputStream writer = response.getOutputStream(); writeReport(getProcess(request), writer); writer.flush(); response.flushBuffer(); return null; } private void writeReport(final SecondCycleCandidacyProcess process, final ServletOutputStream writer) throws IOException { final List<Spreadsheet> spreadsheets = new ArrayList<Spreadsheet>(); for (final Entry<Degree, SortedSet<SecondCycleIndividualCandidacyProcess>> entry : process .getValidSecondCycleIndividualCandidaciesByDegree().entrySet()) { spreadsheets.add(buildReport(entry.getKey(), entry.getValue())); } new SpreadsheetXLSExporter().exportToXLSSheets(writer, spreadsheets); } private Spreadsheet buildReport(final Degree degree, final SortedSet<SecondCycleIndividualCandidacyProcess> name) { final Spreadsheet spreadsheet = new Spreadsheet(degree.getSigla(), getHeader()); for (final SecondCycleIndividualCandidacyProcess process : name) { final Row row = spreadsheet.addRow(); row.setCell(process.getPersonalDetails().getName()); row.setCell(process.getPrecedentDegreeInformation().getConclusionGrade()); row.setCell(process.getCandidacyProfessionalExperience()); row.setCell(process.getPrecedentDegreeInformation().getDegreeAndInstitutionName()); row.setCell(process.getCandidacyAffinity()); row.setCell(process.getCandidacyDegreeNature()); row.setCell(process.getCandidacyGrade()); row.setCell(process.getCandidacyInterviewGrade() != null ? process.getCandidacyInterviewGrade() : " "); row.setCell(process.getCandidacySeriesGrade()); if (process.isCandidacyAccepted() || process.isCandidacyRejected()) { row.setCell(BundleUtil.getString(Bundle.ENUMERATION, process.getCandidacyState().getQualifiedName())); } else { row.setCell(" "); } } return spreadsheet; } private List<Object> getHeader() { final List<Object> result = new ArrayList<Object>(); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.name")); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.candidacy.mfc")); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.candidacy.professionalExperience")); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.candidacy.degree.and.school")); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.candidacy.affinity")); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.candidacy.degreeNature")); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.candidacy.grade")); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.candidacy.interviewGrade")); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.candidacy.seriesGrade")); result.add(BundleUtil.getString(Bundle.APPLICATION, "label.candidacy.result")); return result; } static public class SecondCycleCandidacyDegreeBean extends CandidacyDegreeBean { private String notes; public SecondCycleCandidacyDegreeBean(final SecondCycleIndividualCandidacyProcess process) { setPersonalDetails(process.getPersonalDetails()); setDegree(process.getCandidacySelectedDegree()); setState(process.getCandidacyState()); setRegistrationCreated(process.hasRegistrationForCandidacy()); setNotes(process.getCandidacyNotes()); } public String getNotes() { return notes; } private void setNotes(String notes) { this.notes = notes; } } @Override protected List<CandidacyDegreeBean> createCandidacyDegreeBeans(HttpServletRequest request) { final SecondCycleCandidacyProcess process = getProcess(request); final List<CandidacyDegreeBean> candidacyDegreeBeans = new ArrayList<CandidacyDegreeBean>(); for (final SecondCycleIndividualCandidacyProcess child : process.getAcceptedSecondCycleIndividualCandidacies()) { candidacyDegreeBeans.add(new SecondCycleCandidacyDegreeBean(child)); } Collections.sort(candidacyDegreeBeans); return candidacyDegreeBeans; } @Override protected List<IndividualCandidacyProcess> getChildProcesses(final CandidacyProcess process, HttpServletRequest request) { Collection<IndividualCandidacyProcess> processes = process.getChildProcessesSet(); List<IndividualCandidacyProcess> selectedDegreesIndividualCandidacyProcesses = new ArrayList<IndividualCandidacyProcess>(); DegreeCurricularPlan degreeCurricularPlan = getDegreeCurricularPlan(request); for (IndividualCandidacyProcess child : processes) { if (((SecondCycleIndividualCandidacyProcess) child).getSelectedDegrees().contains(degreeCurricularPlan.getDegree())) { selectedDegreesIndividualCandidacyProcesses.add(child); } } return selectedDegreesIndividualCandidacyProcesses; } public DegreeCurricularPlan getDegreeCurricularPlan(HttpServletRequest request) { final String degreeCurricularPlanOID = DegreeCoordinatorIndex.findDegreeCurricularPlanID(request); request.setAttribute("degreeCurricularPlanID", degreeCurricularPlanOID); if (degreeCurricularPlanOID != null) { return FenixFramework.getDomainObject(degreeCurricularPlanOID); } return null; } @Override protected Spreadsheet buildIndividualCandidacyReport(Spreadsheet spreadsheet, final IndividualCandidacyProcess individualCandidacyProcess) { SecondCycleIndividualCandidacyProcess secondCycleIndividualCandidacyProcess = (SecondCycleIndividualCandidacyProcess) individualCandidacyProcess; final Row row = spreadsheet.addRow(); row.setCell(secondCycleIndividualCandidacyProcess.getProcessCode()); row.setCell(secondCycleIndividualCandidacyProcess.getPersonalDetails().getName()); row.setCell(secondCycleIndividualCandidacyProcess.getPersonalDetails().getIdDocumentType().getLocalizedName()); row.setCell(secondCycleIndividualCandidacyProcess.getPersonalDetails().getDocumentIdNumber()); row.setCell(secondCycleIndividualCandidacyProcess.getPersonalDetails().getCountry() != null ? secondCycleIndividualCandidacyProcess .getPersonalDetails().getCountry().getCountryNationality().getContent() : ""); row.setCell(secondCycleIndividualCandidacyProcess.getPrecedentDegreeInformation().getDegreeAndInstitutionName()); row.setCell(secondCycleIndividualCandidacyProcess.getPrecedentDegreeInformation().getDegreeDesignation()); row.setCell(secondCycleIndividualCandidacyProcess.getPrecedentDegreeInformation().getConclusionDate() != null ? secondCycleIndividualCandidacyProcess .getPrecedentDegreeInformation().getConclusionDate().toString(dateFormat) : ""); row.setCell(secondCycleIndividualCandidacyProcess.getPrecedentDegreeInformation().getConclusionGrade()); String degrees = String.join( "\n", secondCycleIndividualCandidacyProcess .getCandidacy() .getSelectedDegreesSet() .stream() .map(d -> d.getNameI18N(secondCycleIndividualCandidacyProcess.getCandidacyExecutionInterval()) .getContent()).collect(Collectors.toList())); row.setCell(degrees); row.setCell(BundleUtil.getString(Bundle.ENUMERATION, individualCandidacyProcess.getCandidacyState().getQualifiedName())); row.setCell(BundleUtil.getString(Bundle.CANDIDATE, secondCycleIndividualCandidacyProcess.getProcessChecked() != null && secondCycleIndividualCandidacyProcess.getProcessChecked() ? MESSAGE_YES : MESSAGE_NO)); return spreadsheet; } @Override protected void setCandidacyProcessInformation(final HttpServletRequest request, final CandidacyProcess process) { super.setCandidacyProcessInformation(request, process); request.setAttribute("executionIntervals", readExecutionIntervalFilteredByCoordinatorTeam(request)); } }