@Transactional
 @Override
 public CareSessionEntity updateCareSession(CareSessionEntity careSession) {
   careSessionDao.update(careSession);
   careSessionDao.flush();
   return careSession;
 }
 @Transactional
 @Override
 public boolean cancelCareSession(int idCareSession) {
   CareSessionEntity cse = careSessionDao.findById(idCareSession);
   if (cse != null && cse.getValid()) {
     cse.setValid(false);
     careSessionDao.save(cse);
     careSessionDao.flush();
     return true;
   }
   return false;
 }
  @Transactional
  @Override
  public void saveScheduleForProfessional(
      int idProfessional, int idCareSession, List<ScheduleEntity> schedules) {
    CareSessionEntity careSession = careSessionDao.findById(idCareSession);
    ProfessionalEntity professional = professionalDao.findById(idProfessional);

    Validator.shouldBeFound(careSession);
    Validator.shouldBeFound(professional);

    List<ScheduleEntity> alreadyInDB =
        scheduleDao.findByProfessionalByCareSession(idProfessional, idCareSession);

    // Remove the schedules (with its appointments) no longer present
    List<Integer> idsToRemove = getIdsFromAnotContainedInB(alreadyInDB, schedules);
    if (!idsToRemove.isEmpty()) {
      scheduleDao.deleteSchedulesWithAppointments(idsToRemove);
      scheduleDao.flush();
    }

    // get list of IDs on alreadyInDB but schedules, to erase them
    for (ScheduleEntity se : schedules) {
      se.setProfessional(professional);
      se.setCareSession(careSession);
    }
    // create or update
    scheduleDao.saveAll(schedules);
    scheduleDao.flush();
  }
  private ProfessionalCalendarBO buildProfessionalCalendar(
      int idProfessional, Integer idCareSession) {
    ProfessionalEntity professional = professionalDao.findById(idProfessional);
    Validator.shouldBeFound(professional);

    CareSessionEntity careSession = null;

    if (idCareSession != null) {
      careSession = careSessionDao.findById(idCareSession);
      Validator.shouldBeFound(careSession);
    }

    List<ScheduleEntity> frees = new ArrayList();
    List<Pair<ScheduleEntity, AppointmentEntity>> taken = new ArrayList();

    List<ScheduleEntity> allSchedules =
        (idCareSession == null
            ? scheduleDao.findByProfessional(idProfessional)
            : scheduleDao.findByProfessionalByCareSession(idProfessional, idCareSession));
    List<AppointmentEntity> allAppointments =
        (idCareSession == null
            ? appointmentDao.findByProfessional(idProfessional)
            : appointmentDao.findByProfessionalByCareSession(idProfessional, idCareSession));

    // allAppointments should be a subset of allSchedules

    // put all the schedules into a hashmap (by id)
    HashMap<Integer, ScheduleEntity> sch = new HashMap();
    for (ScheduleEntity se : allSchedules) {
      sch.put(se.getId(), se);
    }

    // get the schedules related to appointments and put them in taken
    for (AppointmentEntity ae : allAppointments) {
      ScheduleEntity se = sch.remove(ae.getSchedule().getId());
      Pair<ScheduleEntity, AppointmentEntity> pair = new Pair(se, ae);
      taken.add(pair);
    }

    // put the remaining schedules from the hash into frees
    for (Map.Entry<Integer, ScheduleEntity> entry : sch.entrySet()) {
      frees.add(entry.getValue());
    }

    return new ProfessionalCalendarBO(careSession, professional, frees, taken);
  }
 @Transactional
 @Override
 public CareSessionEntity findCareSessionById(int idCareSession) {
   return careSessionDao.findById(idCareSession);
 }
 @Transactional
 @Override
 public List<CareSessionEntity> findPendingCareSessions(int ongId, Date currentDate) {
   return careSessionDao.findPending(ongId, currentDate);
 }
 @Transactional
 @Override
 public List<CareSessionEntity> findAllCareSessions(int ongId) {
   return careSessionDao.findByOng(ongId);
 }
 @Transactional
 @Override
 public CareSessionEntity findCurrentCareSession(OngEntity ong, Date currentTime) {
   return careSessionDao.findByDate(ong.getId(), currentTime);
 }