/**
* 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.administrativeOffice.student.candidacy.registrations;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import org.fenixedu.academic.domain.Degree;
import org.fenixedu.academic.domain.EntryPhase;
import org.fenixedu.academic.domain.ExecutionDegree;
import org.fenixedu.academic.domain.ExecutionYear;
import org.fenixedu.academic.domain.Person;
import org.fenixedu.academic.domain.Shift;
import org.fenixedu.academic.domain.candidacy.CandidacySituationType;
import org.fenixedu.academic.domain.candidacy.StudentCandidacy;
import org.fenixedu.academic.domain.organizationalStructure.Unit;
import org.fenixedu.academic.domain.student.Registration;
import org.fenixedu.commons.spreadsheet.Spreadsheet;
import org.fenixedu.commons.spreadsheet.Spreadsheet.Row;
import org.fenixedu.spaces.domain.Space;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
public class RegisteredDegreeCandidaciesSelectionBean implements Serializable {
private static final long serialVersionUID = 1L;
final static Comparator<StudentCandidacy> DEGREE_CANDIDACIES_COMPARATOR = new Comparator<StudentCandidacy>() {
@Override
public int compare(StudentCandidacy o1, StudentCandidacy o2) {
int result = o1.getEntryPhase().compareTo(o2.getEntryPhase());
if (result == 0) {
result =
o1.getActiveCandidacySituation().getSituationDate()
.compareTo(o2.getActiveCandidacySituation().getSituationDate());
}
if (result == 0) {
final ExecutionYear executionYear = ExecutionYear.readCurrentExecutionYear();
result =
o1.getExecutionDegree().getDegree().getNameFor(executionYear)
.compareTo(o2.getExecutionDegree().getDegree().getNameFor(executionYear));
}
if (result == 0) {
result = o1.getRegistration().getNumber().compareTo(o2.getRegistration().getNumber());
}
if (result == 0) {
result = o1.getPerson().getName().compareTo(o2.getPerson().getName());
}
if (result == 0) {
result = o1.getPerson().getDocumentIdNumber().compareTo(o2.getPerson().getDocumentIdNumber());
}
return (result != 0) ? result : o1.getExternalId().compareTo(o2.getExternalId());
}
};
private Space campus;
private ExecutionYear executionYear;
private EntryPhase entryPhase;
private DateTime beginDate;
private DateTime endDate;
public RegisteredDegreeCandidaciesSelectionBean() {
this.executionYear = ExecutionYear.readCurrentExecutionYear();
}
public Space getCampus() {
return campus;
}
public void setCampus(Space campus) {
this.campus = campus;
}
public ExecutionYear getExecutionYear() {
return executionYear;
}
public void setExecutionYear(ExecutionYear executionYear) {
this.executionYear = executionYear;
}
public EntryPhase getEntryPhase() {
return entryPhase;
}
public void setEntryPhase(EntryPhase entryPhase) {
this.entryPhase = entryPhase;
}
public DateTime getBeginDate() {
return beginDate;
}
public void setBeginDate(DateTime beginDate) {
this.beginDate = beginDate;
}
public DateTime getEndDate() {
return endDate;
}
public void setEndDate(DateTime endDate) {
this.endDate = endDate;
}
public List<StudentCandidacy> search(final Set<Degree> allowedPrograms) {
final List<StudentCandidacy> degreeCandidacies = new ArrayList<StudentCandidacy>();
for (final ExecutionDegree executionDegree : getExecutionYear().getExecutionDegreesSet()) {
if (executionDegree.getCampus() == getCampus()) {
for (final StudentCandidacy candidacy : executionDegree
.getFirstCycleCandidaciesBy(CandidacySituationType.REGISTERED)) {
DateTime situationDate = candidacy.getActiveCandidacySituation().getSituationDate();
if (getBeginDate() != null && situationDate.isBefore(getBeginDate())) {
continue;
}
if (getEndDate() != null && situationDate.isAfter(getEndDate())) {
continue;
}
if (candidacy.hasEntryPhase() && candidacy.getEntryPhase().equals(getEntryPhase())) {
if (candidacy.getRegistration() != null && candidacy.getRegistration().isCanceled()) {
continue;
}
if (allowedPrograms.contains(candidacy.getExecutionDegree().getDegree())) {
degreeCandidacies.add(candidacy);
}
}
}
}
}
Collections.sort(degreeCandidacies, DEGREE_CANDIDACIES_COMPARATOR);
return degreeCandidacies;
}
public Spreadsheet export(final Set<Degree> allowedPrograms) {
Spreadsheet spreadsheet;
spreadsheet = new Spreadsheet(getCampus().getName());
addHeaders(spreadsheet);
List<StudentCandidacy> result = search(allowedPrograms);
Collections.sort(result, DEGREE_CANDIDACIES_COMPARATOR);
for (final StudentCandidacy candidacy : result) {
addRow(spreadsheet, candidacy);
}
return spreadsheet;
}
public String getFilename() {
return new LocalDate().toString("ddMMyyyy") + "-Candidatos-" + campus.getName() + "-Fase"
+ this.entryPhase.getPhaseNumber() + ".xls";
}
private void addHeaders(Spreadsheet spreadsheet) {
spreadsheet.setHeader("Data de Matricula");
spreadsheet.setHeader("Tipo Ingresso");
spreadsheet.setHeader("Curso");
spreadsheet.setHeader("Nº de Aluno");
spreadsheet.setHeader("Nome");
spreadsheet.setHeader("B.I");
spreadsheet.setHeader("Aluno deslocado");
spreadsheet.setHeader("Localidade");
spreadsheet.setHeader("Email " + Unit.getInstitutionAcronym());
spreadsheet.setHeader("Email pessoal");
spreadsheet.setHeader("Telefone");
spreadsheet.setHeader("Telemovel");
spreadsheet.setHeader("Deslocado da residencia");
spreadsheet.setHeader("Turnos");
}
private void addRow(final Spreadsheet spreadsheet, final StudentCandidacy candidacy) {
final Row row = spreadsheet.addRow();
final Person person = candidacy.getPerson();
final ExecutionYear executionYear = ExecutionYear.readCurrentExecutionYear();
row.setCell(candidacy.getActiveCandidacySituation().getSituationDate().toString("dd/MM/yyyy HH:mm"));
row.setCell(candidacy.getRegistration().getIngressionType().getCode());
row.setCell(candidacy.getExecutionDegree().getDegree().getNameFor(executionYear).getContent());
row.setCell(candidacy.getRegistration().getNumber().toString());
row.setCell(person.getName());
row.setCell(person.getDocumentIdNumber());
row.setCell(candidacy.getDislocatedFromPermanentResidence() != null
&& candidacy.getDislocatedFromPermanentResidence().booleanValue() ? "Sim" : "Nao");
row.setCell(person.getArea());
row.setCell(person.getInstitutionalEmailAddressValue());
row.setCell(getPersonalEmailAddress(person));
row.setCell(getPhone(person));
row.setCell(getMobilePhone(person));
row.setCell(candidacy.getDislocatedFromPermanentResidence() != null
&& candidacy.getDislocatedFromPermanentResidence().booleanValue() ? "Sim" : "Nao");
row.setCell(getShiftNames(candidacy));
}
private String getPersonalEmailAddress(final Person person) {
return person.getEmailAddressStream()
.filter(e -> e.isPersonalType() && e.hasValue())
.map(e -> e.getValue())
.findAny().orElse("");
}
private String getPhone(final Person person) {
return person.hasDefaultPhone() && person.getDefaultPhone().hasNumber() ? person.getDefaultPhone().getNumber() : "";
}
private String getMobilePhone(final Person person) {
return person.hasDefaultMobilePhone() && person.getDefaultMobilePhone().hasNumber() ? person.getDefaultMobilePhone()
.getNumber() : "";
}
private String getShiftNames(final StudentCandidacy candidacy) {
if (candidacy.getRegistration() == null) {
return " ";
}
final Registration registration = candidacy.getRegistration();
final StringBuilder builder = new StringBuilder();
for (final Shift shift : registration.getShiftsFor(candidacy.getExecutionYear().getFirstExecutionPeriod())) {
builder.append(shift.getNome()).append(",");
}
if (builder.length() > 1) {
builder.deleteCharAt(builder.length() - 1);
}
return builder.toString();
}
}