private SortedSet<InternshipCandidacyBean> filterCandidates(CandidateSearchBean search) {
   SortedSet<InternshipCandidacyBean> candidates = new TreeSet<InternshipCandidacyBean>();
   if (search.getSession() != null) {
     for (InternshipCandidacy candidacy : search.getSession().getInternshipCandidacySet()) {
       if (isIncluded(candidacy, search)) {
         candidates.add(new InternshipCandidacyBean(candidacy));
       }
     }
   }
   return candidates;
 }
 public ActionForward searchCandidates(
     ActionMapping mapping,
     ActionForm actionForm,
     HttpServletRequest request,
     HttpServletResponse response) {
   CandidateSearchBean search = getRenderedObject("search");
   if (search.getCutStart() != null
       && search.getCutEnd() != null
       && search.getCutEnd().isBefore(search.getCutStart())) {
     addErrorMessage(
         request,
         "start",
         "error.internationalrelations.internship.candidacy.search.startafterend");
     return prepareCandidates(mapping, actionForm, request, response);
   }
   request.setAttribute("search", search);
   request.setAttribute("candidates", filterCandidates(search));
   return mapping.findForward("candidates");
 }
 public ActionForward sessionPostback(
     ActionMapping mapping,
     ActionForm actionForm,
     HttpServletRequest request,
     HttpServletResponse response) {
   CandidateSearchBean search = getRenderedObject("search");
   RenderUtils.invalidateViewState();
   Interval interval = search.getSession().getCandidacyInterval();
   if (search.getCutStart() == null
       || !interval.contains(search.getCutStart().toDateTimeAtStartOfDay())) {
     search.setCutStart(interval.getStart().toLocalDate());
   }
   if (search.getCutEnd() == null
       || !interval.contains(search.getCutEnd().toDateTimeAtStartOfDay())) {
     search.setCutEnd(interval.getEnd().toLocalDate());
   }
   if (interval.contains(new LocalDate().minusDays(1).toDateMidnight())) {
     search.setCutEnd(new LocalDate().minusDays(1));
   }
   request.setAttribute("search", search);
   return mapping.findForward("candidates");
 }
 private boolean isIncluded(InternshipCandidacy candidacy, CandidateSearchBean search) {
   if (search.getUniversity() != null
       && !candidacy.getUniversity().equals(search.getUniversity())) {
     return false;
   }
   if (search.getCutStart() != null
       && candidacy.getCandidacyDate().isBefore(search.getCutStart().toDateMidnight())) {
     return false;
   }
   if (search.getCutEnd() != null
       && !candidacy
           .getCandidacyDate()
           .isBefore(search.getCutEnd().plusDays(1).toDateMidnight())) {
     return false;
   }
   return true;
 }
  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;
  }