@Override
  public Boolean affecter(Dossier dossier, SessionExamen session)
      throws AffectationClosedException {
    DossierSession sameexist =
        dossierSessionRepos.findByDossierIdAndSessionExamenId(dossier.getId(), session.getId());
    if (sameexist != null) {
      return true;
    }

    DossierSession exist = dossierSessionRepos.findByDossierId(dossier.getId());
    if (exist != null && exist.getSessionExamen().getStatus() == SessionStatus.CLOSED) {
      GregorianCalendar calendar = new GregorianCalendar();
      calendar.add(Calendar.DAY_OF_MONTH, -1);

      Date dateSession = exist.getSessionExamen().getDateSession();
      Date yesterday = calendar.getTime();

      if (yesterday.before(dateSession)) {
        throw new AffectationClosedException();
      }
    }
    if (exist != null) {
      dossierSessionRepos.delete(exist);
    }

    Dossier affectedDossier = dossierRepos.findOne(dossier.getId());

    DossierHistoric dossierHistoric = new DossierHistoric();
    dossierHistoric.setStatus(affectedDossier.getStatus());
    dossierHistoric.setCreateDate(new Date());
    dossierHistoric.setDossier(affectedDossier);
    dossierHistoricRepo.save(dossierHistoric);

    affectedDossier.setStatus(DossierStatus.AFFECTED);

    SessionExamen examen = sessionExamenRepos.findOne(session.getId());
    examen.setCandidates(examen.getCandidates() + 1);

    DossierSession dossierSession = new DossierSession();
    dossierSession.setDateAffectation(new Date());
    dossierSession.setAssignedBy(securityContextProvider.getCurrentAdmin());
    dossierSession.setDossier(affectedDossier);
    dossierSession.setSessionExamen(examen);

    try {
      dossierRepos.save(affectedDossier);
      sessionExamenRepos.save(examen);
      dossierSessionRepos.save(dossierSession);
      return true;
    } catch (Exception e) {
      log.error(e.getMessage());
      return false;
    }
  }
  @Override
  public void sendEmailConvocation(DossierSession session, String link) {
    Dossier dossier = dossierRepos.findOne(session.getDossier().getId());
    DossierSession dossierSession = dossierSessionRepos.findOne(session.getId());

    if (Strings.isNullOrEmpty(dossierSession.getGeneratedConvocationPDFPath())) {
      String token =
          Base64.encode(
              dossierSession.getDossier().getGeneratedNumDossier() + "" + (new Date()).toString());
      token = token.replace("=", "E");
      dossierSession.setGeneratedConvocationPDFPath(token);
      dossierSessionRepos.save(dossierSession);
    }

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("gender", dossier.getOwner().getGender().toString());
    params.put("lastname", dossier.getOwner().getLastName());
    params.put("firstname", dossier.getOwner().getFirstName());
    params.put("nomEnfant", dossier.getCandidat().getLastname());
    params.put("prenomEnfant", dossier.getCandidat().getFirstname());
    params.put("refdossier", dossier.getGeneratedNumDossier());
    params.put(
        "link",
        link + "resource/convocation?number=" + dossierSession.getGeneratedConvocationPDFPath());

    try {
      EmailDTO email =
          emailService.populateEmail(
              EmailType.CONVOCATED_FOR_TEST,
              dossier.getOwner().getEmail(),
              sender,
              params,
              sender,
              "");
      emailService.prepare(email);

      InboxMessage message = new InboxMessage();
      message.setParentUser(dossier.getOwner());
      message.setSubject(email.getSubject());
      message.setContent(email.getMessage());
      message.setMsgDate(new Date());
      message.setMsgStatus(InboxMessageStatus.UNREAD);
      inboxMessageRepos.save(message);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @Override
  public Boolean launchSession(SessionExamen session, String link) {
    List<DossierSession> dossierSessions =
        dossierSessionRepos.findBySessionExamenId(session.getId());
    for (DossierSession dossiersession : dossierSessions) {
      Dossier dossier = dossierRepos.findOne(dossiersession.getDossier().getId());
      String token =
          Base64.encode(
              dossiersession.getDossier().getGeneratedNumDossier() + "" + (new Date()).toString());
      token = token.replace("=", "E");
      dossiersession.setGeneratedConvocationPDFPath(token);

      DossierHistoric dossierHistoric = new DossierHistoric();
      dossierHistoric.setStatus(dossier.getStatus());
      dossierHistoric.setCreateDate(new Date());
      dossierHistoric.setDossier(dossier);
      dossierHistoricRepo.save(dossierHistoric);

      dossier.setStatus(DossierStatus.INVITED_TO_TEST);

      dossierRepos.save(dossier);
      dossierSessionRepos.save(dossiersession);

      if (dossier.getNiveauEtude().getEmailConvocation() == null
          || !dossier.getNiveauEtude().getEmailConvocation()) continue;

      // preparing the email data
      Map<String, Object> params = new HashMap<String, Object>();
      params.put("gender", dossier.getOwner().getGender().toString());
      params.put("lastname", dossier.getOwner().getLastName());
      params.put("firstname", dossier.getOwner().getFirstName());
      params.put("nomEnfant", dossier.getCandidat().getLastname());
      params.put("prenomEnfant", dossier.getCandidat().getFirstname());
      params.put("refdossier", dossier.getGeneratedNumDossier());
      params.put("link", link + "resource/convocation?number=" + token);

      try {
        EmailDTO email =
            emailService.populateEmail(
                EmailType.CONVOCATED_FOR_TEST,
                dossier.getOwner().getEmail(),
                sender,
                params,
                "",
                "");
        emailService.prepare(email);

        InboxMessage message = new InboxMessage();
        message.setParentUser(dossier.getOwner());
        message.setSubject(email.getSubject());
        message.setContent(email.getMessage());
        message.setMsgDate(new Date());
        message.setMsgStatus(InboxMessageStatus.UNREAD);
        inboxMessageRepos.save(message);
      } catch (Exception e) {
        e.printStackTrace();
        return false;
      }
    }
    session = sessionExamenRepos.findOne(session.getId());
    session.setStatus(SessionStatus.CLOSED);
    sessionExamenRepos.save(session);
    return true;
  }