@RequestMapping(
      value = {"/offers/apply/{offerId}"},
      method = RequestMethod.GET)
  public ModelAndView applyOffer(@PathVariable String offerId, HttpServletRequest request)
      throws IOException {

    ModelAndView mv = new ModelAndView("displayOffers");
    super.setFooterDisPlayON(mv);
    mv.addObject("mainClass", mainClass);

    String candidatId = (String) request.getSession().getAttribute(AController.USER_ID);
    super.setAuth();

    ClientOffers byId = clientsJobsService.getById(offerId);
    mv.addObject("offers", byId);
    if (super.authName != null
        && !super.authName.isEmpty()
        && !"anonymousUser".equals(super.authName)
        && !"ROLE_CLIENT".equals(super.getAuthType())) {

      ArrayList<CandidatOffers> byOfferId = candidatOffersService.getByOfferId(offerId);
      boolean alreadyApply = false;
      for (CandidatOffers offer : byOfferId) {

        if (candidatId == null
            ? offer.getPartialCandidat().getId() == null
            : candidatId.equals(offer.getPartialCandidat().getId())) {
          alreadyApply = true;
          break;
        }
      }
      if (!alreadyApply) {

        CandidatOffers candidatOffers = new CandidatOffers();
        candidatOffers.setId(UUID.randomUUID().toString());
        candidatOffers.setDate(new Date().toString());
        PartialCandidat partialCandidat = new PartialCandidat();
        partialCandidat.setId(candidatId);
        candidatOffers.setOfferId(offerId);
        candidatOffers.setPartialCandidat(partialCandidat);
        candidatOffersService.addCandidatOffers(candidatOffers);
        this.sendApplicationMail(candidatOffers, byId);
        mv.addObject("applyOk", true);
      } else {
        mv.addObject("alreadyApply", alreadyApply);
      }

    } else {
      mv.addObject("noAuth", true);
    }

    return mv;
  }
  /**
   * send an email to the client in case of a new application
   *
   * @param candidatOffers
   * @param clientOffers
   * @throws IOException
   */
  private void sendApplicationMail(CandidatOffers candidatOffers, ClientOffers clientOffers)
      throws IOException {

    String offerTitle = clientOffers.getTitle();
    String idClient = clientOffers.getPartialsClients().getId();
    Clients client = clientService.getById(idClient);
    String email = client.getEmail();
    String candidatID = candidatOffers.getPartialCandidat().getId();
    Candidat candidat = candidatService.getById(candidatID);

    String cvContends = candidat.getCvContends();

    mailService.send(email, cvContends, CerebrosController.NEW_APPLICATION + offerTitle);
  }