private Spreadsheet buildReport(
      final Degree degree, final SortedSet<SecondCycleIndividualCandidacyProcess> name) {
    final Spreadsheet spreadsheet = new Spreadsheet(degree.getSigla(), getHeader());

    for (final SecondCycleIndividualCandidacyProcess process : name) {
      if (!process.canExecuteActivity(Authenticate.getUser())) {
        continue;
      }
      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;
  }
  public ActionForward exportToCandidatesToXls(
      ActionMapping mapping,
      ActionForm actionForm,
      HttpServletRequest request,
      HttpServletResponse response)
      throws FenixActionException {
    CandidateSearchBean search = getRenderedObject("search");
    if (search.getCutEnd().isBefore(search.getCutStart())) {
      addErrorMessage(
          request,
          "start",
          "error.internationalrelations.internship.candidacy.search.startafterend");
      return prepareCandidates(mapping, actionForm, request, response);
    }
    if (search.getCutEnd().plusDays(1).toDateMidnight().isAfterNow()) {
      addErrorMessage(
          request, "end", "error.internationalrelations.internship.candidacy.export.todaywontwork");
      return prepareCandidates(mapping, actionForm, request, response);
    }
    Spreadsheet sheet = new Spreadsheet(search.getName());
    if (search.getUniversity() == null) {
      sheet.setHeaders(HEADERS);
    } else {
      sheet.setHeaders(HEADERS_NO_UNIV);
    }
    for (InternshipCandidacyBean bean : filterCandidates(search)) {
      Row row = sheet.addRow();
      row.setCell(bean.getCandidacy().getCandidacyCode());
      if (search.getUniversity() == null) {
        row.setCell(bean.getUniversity().getFullPresentationName());
      }
      row.setCell(bean.getStudentNumber());
      row.setCell(bean.getStudentYear().ordinal() + 1);
      row.setCell(bean.getDegree());
      row.setCell(bean.getBranch());
      row.setCell(bean.getName());
      row.setCell(bean.getGender().toLocalizedString());
      row.setCell(bean.getBirthday().toString("dd-MM-yyyy"));
      row.setCell(bean.getParishOfBirth());
      row.setCell(
          StringUtils.capitalize(
              bean.getCountryOfBirth().getCountryNationality().getPreferedContent().toLowerCase()));
      row.setCell(bean.getDocumentIdNumber());
      row.setCell(
          bean.getEmissionLocationOfDocumentId() != null
              ? bean.getEmissionLocationOfDocumentId()
              : null);
      row.setCell(
          bean.getEmissionDateOfDocumentId() != null
              ? bean.getEmissionDateOfDocumentId().toString("dd-MM-yyyy")
              : null);
      row.setCell(
          bean.getExpirationDateOfDocumentId() != null
              ? bean.getExpirationDateOfDocumentId().toString("dd-MM-yyyy")
              : null);
      row.setCell(bean.getPassportIdNumber() != null ? bean.getPassportIdNumber() : "");
      row.setCell(
          bean.getEmissionLocationOfPassport() != null ? bean.getEmissionLocationOfPassport() : "");
      row.setCell(
          bean.getEmissionDateOfPassport() != null
              ? bean.getEmissionDateOfPassport().toString("dd-MM-yyyy")
              : "");
      row.setCell(
          bean.getExpirationDateOfPassport() != null
              ? bean.getExpirationDateOfPassport().toString("dd-MM-yyyy")
              : "");
      row.setCell(bean.getStreet());
      row.setCell(bean.getAreaCode());
      row.setCell(bean.getArea());
      row.setCell(bean.getTelephone());
      row.setCell(bean.getMobilePhone());
      row.setCell(bean.getEmail());
      row.setCell(
          StringUtils.capitalize(
              bean.getFirstDestination() != null
                  ? bean.getFirstDestination().getName().toLowerCase()
                  : ""));
      row.setCell(
          StringUtils.capitalize(
              bean.getSecondDestination() != null
                  ? bean.getSecondDestination().getName().toLowerCase()
                  : ""));
      row.setCell(
          StringUtils.capitalize(
              bean.getThirdDestination() != null
                  ? bean.getThirdDestination().getName().toLowerCase()
                  : ""));
      row.setCell(BundleUtil.getString(Bundle.ENUMERATION, bean.getEnglish().getQualifiedKey()));
      row.setCell(BundleUtil.getString(Bundle.ENUMERATION, bean.getFrench().getQualifiedKey()));
      row.setCell(BundleUtil.getString(Bundle.ENUMERATION, bean.getSpanish().getQualifiedKey()));
      row.setCell(BundleUtil.getString(Bundle.ENUMERATION, bean.getGerman().getQualifiedKey()));
      row.setCell(bean.getPreviousCandidacy() ? "Sim" : "Não");
    }

    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment; filename=" + search.getName() + ".xls");

    try {
      OutputStream outputStream = response.getOutputStream();
      sheet.exportToXLSSheet(outputStream);
      outputStream.close();
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
    }
    return null;
  }
  @Override
  public void renderReport(final Spreadsheet spreadsheet) throws Exception {
    spreadsheet.setHeader("Número");
    spreadsheet.setHeader("Sexo");
    spreadsheet.setHeader("Média");
    spreadsheet.setHeader("Média Anual");
    spreadsheet.setHeader("Número Inscrições");
    spreadsheet.setHeader("Número Aprovações");
    spreadsheet.setHeader("Nota de Seriação");
    spreadsheet.setHeader("Local de Origem");

    final ExecutionYear executionYear = getExecutionYear();
    for (final Degree degree : Degree.readNotEmptyDegrees()) {
      if (checkDegreeType(getDegreeType(), degree)) {
        if (isActive(degree)) {
          for (final Registration registration : degree.getRegistrationsSet()) {
            if (registration.isRegistered(getExecutionYear())) {

              int enrolmentCounter = 0;
              int aprovalCounter = 0;
              BigDecimal bigDecimal = null;
              double totalCredits = 0;

              for (final Registration otherRegistration :
                  registration.getStudent().getRegistrationsSet()) {
                if (otherRegistration.getDegree() == registration.getDegree()) {
                  for (final StudentCurricularPlan studentCurricularPlan :
                      otherRegistration.getStudentCurricularPlansSet()) {
                    for (final Enrolment enrolment : studentCurricularPlan.getEnrolmentsSet()) {
                      final ExecutionSemester executionSemester = enrolment.getExecutionPeriod();
                      if (executionSemester.getExecutionYear() == executionYear) {
                        enrolmentCounter++;
                        if (enrolment.isApproved()) {
                          aprovalCounter++;
                          final Grade grade = enrolment.getGrade();
                          if (grade.isNumeric()) {
                            final double credits =
                                enrolment.getEctsCreditsForCurriculum().doubleValue();
                            totalCredits += credits;
                            bigDecimal =
                                bigDecimal == null
                                    ? grade.getNumericValue().multiply(new BigDecimal(credits))
                                    : bigDecimal.add(
                                        grade.getNumericValue().multiply(new BigDecimal(credits)));
                          }
                        }
                      }
                    }
                  }
                }
              }

              final Row row = spreadsheet.addRow();
              row.setCell(registration.getNumber().toString());
              row.setCell(registration.getPerson().getGender().toLocalizedString());
              row.setCell(registration.getAverage(executionYear));
              if (bigDecimal == null) {
                row.setCell("");
              } else {
                row.setCell(
                    bigDecimal.divide(new BigDecimal(totalCredits), 5, RoundingMode.HALF_UP));
              }
              row.setCell(Integer.toString(enrolmentCounter));
              row.setCell(Integer.toString(aprovalCounter));
              row.setCell(
                  registration.getEntryGrade() != null
                      ? registration.getEntryGrade().toString()
                      : StringUtils.EMPTY);
              Boolean dislocated = null;
              if (registration.hasStudentCandidacy()) {
                dislocated =
                    registration.getStudentCandidacy().getDislocatedFromPermanentResidence();
              }

              final String dislocatedString =
                  dislocated == null
                      ? ""
                      : (dislocated.booleanValue() ? "Deslocado" : "Não Deslocado");
              row.setCell(dislocatedString);
            }
          }
        }
      }
    }
  }
  @Override
  protected Spreadsheet buildIndividualCandidacyReport(
      final 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());

    StringBuilder degreesSb = new StringBuilder();
    for (Degree degree :
        secondCycleIndividualCandidacyProcess.getCandidacy().getSelectedDegreesSet()) {
      degreesSb.append(degree.getName()).append("\n");
    }

    row.setCell(degreesSb.toString());
    row.setCell(
        BundleUtil.getString(
            Bundle.ENUMERATION, individualCandidacyProcess.getCandidacyState().getQualifiedName()));
    row.setCell(
        BundleUtil.getString(
            Bundle.CANDIDATE,
            secondCycleIndividualCandidacyProcess.getProcessChecked() != null
                    && secondCycleIndividualCandidacyProcess.getProcessChecked()
                ? MESSAGE_YES
                : MESSAGE_NO));
    row.setCell(secondCycleIndividualCandidacyProcess.getCandidacyNotes());
    return spreadsheet;
  }