/**
* 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.externalSupervision.consult;
import java.io.IOException;
import java.math.RoundingMode;
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.ExecutionDegree;
import org.fenixedu.academic.domain.ExecutionYear;
import org.fenixedu.academic.domain.Person;
import org.fenixedu.academic.domain.StudentCurricularPlan;
import org.fenixedu.academic.domain.student.Registration;
import org.fenixedu.academic.domain.student.RegistrationProtocol;
import org.fenixedu.academic.ui.struts.action.base.FenixDispatchAction;
import org.fenixedu.academic.ui.struts.action.externalSupervision.ExternalSupervisionApplication.ExternalSupervisionConsultApp;
import org.fenixedu.academic.util.Bundle;
import org.fenixedu.bennu.core.domain.User;
import org.fenixedu.bennu.core.i18n.BundleUtil;
import org.fenixedu.bennu.core.security.Authenticate;
import org.fenixedu.bennu.struts.annotations.Forward;
import org.fenixedu.bennu.struts.annotations.Forwards;
import org.fenixedu.bennu.struts.annotations.Mapping;
import org.fenixedu.bennu.struts.portal.EntryPoint;
import org.fenixedu.bennu.struts.portal.StrutsFunctionality;
import pt.ist.fenixWebFramework.renderers.utils.RenderUtils;
import pt.ist.fenixframework.FenixFramework;
import pt.utl.ist.fenix.tools.util.excel.Spreadsheet;
import pt.utl.ist.fenix.tools.util.excel.Spreadsheet.Row;
@StrutsFunctionality(app = ExternalSupervisionConsultApp.class, path = "degree", titleKey = "label.selectDegree.executionDegree")
@Mapping(path = "/viewDegree", module = "externalSupervision")
@Forwards({ @Forward(name = "selectDegree", path = "/externalSupervision/consult/selectDegree.jsp") })
public class ExternalSupervisorViewDegreeDA extends FenixDispatchAction {
@EntryPoint
public ActionForward beginTaskFlow(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
final User userView = Authenticate.getUser();
final Person supervisor = userView.getPerson();
RegistrationProtocol protocol = supervisor.getOnlyRegistrationProtocol();
ExternalSupervisorViewsBean bean;
if (protocol == null) {
bean = new ExternalSupervisorViewsBean(ExecutionYear.readCurrentExecutionYear());
bean.setMegavisor(true);
boolean selectProtocol = true;
request.setAttribute("selectProtocol", selectProtocol);
} else {
bean = new ExternalSupervisorViewsBean(ExecutionYear.readCurrentExecutionYear(), protocol);
}
request.setAttribute("sessionBean", bean);
return mapping.findForward("selectDegree");
}
public ActionForward degreePostback(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
ExternalSupervisorViewsBean bean = getRenderedObject("sessionBean");
if (bean.getMegavisor()) {
boolean selectProtocol = true;
request.setAttribute("selectProtocol", selectProtocol);
}
RenderUtils.invalidateViewState();
request.setAttribute("sessionBean", bean);
return mapping.findForward("selectDegree");
}
public ActionForward showStudents(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
ExternalSupervisorViewsBean bean = getRenderedObject("sessionBean");
RenderUtils.invalidateViewState();
if (bean == null) {
final String registrationProtocolId = request.getParameter("registrationProtocolId");
RegistrationProtocol registrationProtocol = FenixFramework.getDomainObject(registrationProtocolId);
final String executionYearId = request.getParameter("executionYearId");
ExecutionYear executionYear = FenixFramework.getDomainObject(executionYearId);
final String executionDegreeId = request.getParameter("executionDegreeId");
ExecutionDegree executionDegree = FenixFramework.getDomainObject(executionDegreeId);
Boolean megavisor = Boolean.valueOf(request.getParameter("megavisor"));
bean = new ExternalSupervisorViewsBean(executionYear, registrationProtocol);
bean.setExecutionDegree(executionDegree);
bean.setDegreeType(executionDegree.getDegreeType());
bean.setMegavisor(megavisor);
}
if (bean.getMegavisor()) {
boolean selectProtocol = true;
request.setAttribute("selectProtocol", selectProtocol);
}
bean.generateStudentsFromDegree();
Boolean degreeSelected = true;
request.setAttribute("sessionBean", bean);
request.setAttribute("hasChosenDegree", degreeSelected);
return mapping.findForward("selectDegree");
}
public ActionForward exportXLS(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException {
ExternalSupervisorViewsBean bean = getRenderedObject("sessionBean");
final Spreadsheet spreadsheet = generateSpreadsheet(bean);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename=" + getFilename(bean) + ".xls");
spreadsheet.exportToXLSSheet(response.getOutputStream());
response.getOutputStream().flush();
response.flushBuffer();
return null;
}
private String getFilename(ExternalSupervisorViewsBean bean) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(BundleUtil.getString(Bundle.APPLICATION, "label.students.lowercase"));
strBuilder.append("_");
strBuilder.append(bean.getProtocol().getCode());
strBuilder.append("_");
strBuilder.append(bean.getExecutionDegree().getDegree().getSigla());
strBuilder.append("_");
strBuilder.append(bean.getExecutionYear().getName());
return strBuilder.toString();
}
private Spreadsheet generateSpreadsheet(ExternalSupervisorViewsBean bean) {
final Spreadsheet spreadsheet = createSpreadSheet();
for (final StudentCurricularPlan studentCurricularPlan : bean.generateAllStudentCurricularPlans()) {
final Row row = spreadsheet.addRow();
row.setCell(studentCurricularPlan.getPerson().getUsername());
row.setCell(studentCurricularPlan.getRegistration().getNumber());
row.setCell(studentCurricularPlan.getPerson().getName());
row.setCell(studentCurricularPlan.getPerson().getInstitutionalOrDefaultEmailAddressValue());
row.setCell(studentCurricularPlan.getPerson().getIdDocumentType().getLocalizedName());
row.setCell(studentCurricularPlan.getPerson().getDocumentIdNumber());
row.setCell(studentCurricularPlan.getDegree().getSigla());
row.setCell(studentCurricularPlan.getName());
row.setCell(studentCurricularPlan.getStartDateYearMonthDay().toString());
row.setCell(studentCurricularPlan.getEndDate() == null ? "" : studentCurricularPlan.getEndDate().toString());
row.setCell(studentCurricularPlan.getRegistration().getActiveStateType().getDescription());
row.setCell(studentCurricularPlan.getRegistration().getNumberOfCurriculumEntries());
row.setCell(studentCurricularPlan.getRegistration().getEctsCredits());
row.setCell(getAverageInformation(studentCurricularPlan));
row.setCell(studentCurricularPlan.getRegistration().getCurricularYear());
}
return spreadsheet;
}
private String getAverageInformation(final StudentCurricularPlan studentCurricularPlan) {
final Registration registration = studentCurricularPlan.getRegistration();
if (registration.isConcluded()) {
if (registration.isRegistrationConclusionProcessed()
&& (!registration.isBolonha() || studentCurricularPlan.getInternalCycleCurriculumGroupsSize().intValue() == 1)) {
return registration.getAverage().setScale(2, RoundingMode.HALF_EVEN).toPlainString();
} else {
return " - ";
}
} else {
return registration.getAverage().setScale(2, RoundingMode.HALF_EVEN).toPlainString();
}
}
private Spreadsheet createSpreadSheet() {
final Spreadsheet spreadsheet = new Spreadsheet(BundleUtil.getString(Bundle.APPLICATION, "list.students"));
spreadsheet.setHeaders(new String[] {
BundleUtil.getString(Bundle.APPLICATION, "label.username"),
BundleUtil.getString(Bundle.APPLICATION, "label.number"),
BundleUtil.getString(Bundle.APPLICATION, "label.name"),
BundleUtil.getString(Bundle.APPLICATION, "label.email"),
BundleUtil.getString(Bundle.APPLICATION, "label.identificationDocumentType"),
BundleUtil.getString(Bundle.APPLICATION, "label.identificationDocumentNumber"),
BundleUtil.getString(Bundle.APPLICATION, "label.Degree"),
BundleUtil.getString(Bundle.APPLICATION, "label.curricularPlan"),
BundleUtil.getString(Bundle.APPLICATION, "label.net.sourceforge.fenixedu.domain.student.Registration.startDate"),
BundleUtil.getString(Bundle.APPLICATION, "label.net.sourceforge.fenixedu.domain.student.Registration.conclusionDate"),
BundleUtil.getString(Bundle.APPLICATION, "label.student.curricular.plan.state"),
BundleUtil.getString(Bundle.APPLICATION, "label.number.approved.curricular.courses"),
BundleUtil.getString(Bundle.APPLICATION, "label.ects"),
BundleUtil.getString(Bundle.APPLICATION, "label.average"),
BundleUtil.getString(Bundle.APPLICATION, "label.student.curricular.year"),
" ", " " });
return spreadsheet;
}
}