@Transactional(propagation = Propagation.REQUIRED)
  public Patient savePatient(Patient patient) throws ParseException {
    UserDetailEntity patientEntity = userDetailMapper.patientToUserDetailEntity(patient);

    if (patientEntity.getId() != null) {
      patientEntity = userDetailRepository.save(patientEntity);
    } else {
      UserCredentialsEntity userCredentialsEntity = new UserCredentialsEntity();
      userCredentialsEntity.setRoleId(roleRepository.findByRole("PATIENT").getId());
      userCredentialsEntity = userCredentialsRepository.save(userCredentialsEntity);

      patientEntity.setId(userCredentialsEntity.getId());
      patientEntity = userDetailRepository.save(patientEntity);

      UserDetailEntity doctor =
          userDetailRepository.findOne(iamService.getAuthenticatedUser().getId());
      DoctorPatientMappingId doctorPatientMappingId =
          new DoctorPatientMappingId(doctor.getId(), patientEntity.getId());
      DoctorPatientMappingEntity doctorPatientMappingEntity =
          new DoctorPatientMappingEntity(doctorPatientMappingId);

      doctorPatientMappingRepository.save(doctorPatientMappingEntity);
    }
    return userDetailMapper.userDetailEntityToPatient(patientEntity);
  }
  // TODO service should throw appropriate exception to controller, like not found for null pointer.
  public List<Patient> getAllPatient(
      final Integer pageNumber, final Integer pageSize, final Boolean loadTreatment) {

    Pageable pageable =
        new PageRequest(calculatePageNumber(pageNumber), calculatePageSize(pageSize));
    UserCredentialsEntity doctor = iamService.getAuthenticatedUser();
    Page<UserDetailEntity> userDetailEntities =
        userDetailRepository.findAllPatientOfLoggedInDoctor(doctor.getId(), pageable);

    List<Patient> patients = userDetailMapper.userDetailEntitiesToPatients(userDetailEntities);

    if (loadTreatment) {
      patients = loadTreatments(patients);
    }

    return patients;
  }
  public Patient getPatientDetails(Long patientId) {
    UserDetailEntity userDetailEntity = userDetailRepository.findOne(patientId);

    return userDetailMapper.userDetailEntityToPatient(userDetailEntity);
  }