@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 Boolean desaffecter(Dossier dossier) throws AffectationClosedException {
    DossierSession exist = dossierSessionRepos.findByDossierId(dossier.getId());
    if (exist == null || exist.getSessionExamen() == null || exist.getDossier() == null) {
      return false;
    }
    if (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();
      }
    }

    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.ACCEPTED_FOR_TEST);

    SessionExamen examen = sessionExamenRepos.findOne(exist.getSessionExamen().getId());
    examen.setCandidates(examen.getCandidates() - 1);

    try {
      dossierRepos.save(affectedDossier);
      sessionExamenRepos.save(examen);
      dossierSessionRepos.delete(exist);
      return true;
    } catch (Exception e) {
      log.error(e.getMessage());
      return false;
    }
  }