@Override
  @Transactional
  public Enrollments getEnrollmentsByClientId(
      UUID clientId, String loginUser, Integer startIndex, Integer maxItems) {

    HmisUser hmisUser = daoFactory.getAccountDao().findByUsername(loginUser);
    if (hmisUser == null) throw new AccountNotFoundException();

    List<com.servinglynk.hmis.warehouse.model.v2014.Enrollment> pEnrollments =
        daoFactory.getEnrollmentDao().getEnrollmentsByClientId(clientId, startIndex, maxItems);
    List<com.servinglynk.hmis.warehouse.model.v2014.Enrollment> sharingEnrollments =
        daoFactory
            .getSharingRuleDao()
            .getSharedEnrollments(hmisUser.getId(), hmisUser.getOrganization().getId());
    if (sharingEnrollments.size() > 0) {
      pEnrollments.addAll(sharingEnrollments);
    }

    Enrollments enrollments = new Enrollments();

    for (com.servinglynk.hmis.warehouse.model.v2014.Enrollment pEnrollment : pEnrollments) {
      enrollments.addEnrollment(EnrollmentConveter.entityToModel(pEnrollment));
    }

    long count = daoFactory.getEnrollmentDao().getEnrollmentCount(clientId);
    SortedPagination pagination = new SortedPagination();

    pagination.setFrom(startIndex);
    pagination.setReturned(enrollments.getEnrollments().size());
    pagination.setTotal((int) count);
    enrollments.setPagination(pagination);

    return enrollments;
  }
  @Override
  @Transactional
  public com.servinglynk.hmis.warehouse.core.model.Enrollment
      getEnrollmentByClientIdAndEnrollmentId(UUID enrollmentId, UUID clientId) {
    com.servinglynk.hmis.warehouse.model.v2014.Enrollment pEnrollment =
        daoFactory
            .getEnrollmentDao()
            .getEnrollmentByClientIdAndEnrollmentId(enrollmentId, clientId);
    if (pEnrollment == null) throw new EnrollmentNotFound();

    return EnrollmentConveter.entityToModel(pEnrollment);
  }
  @Override
  @Transactional
  public com.servinglynk.hmis.warehouse.core.model.Enrollment createEnrollment(
      com.servinglynk.hmis.warehouse.core.model.Enrollment enrollment,
      UUID clientId,
      String caller) {
    com.servinglynk.hmis.warehouse.model.v2014.Client pClient =
        daoFactory.getClientDao().getClientById(clientId);
    if (pClient == null) throw new ClientNotFoundException();

    com.servinglynk.hmis.warehouse.model.v2014.Enrollment pEnrollment =
        EnrollmentConveter.modelToEntity(enrollment, null);
    pEnrollment.setClient(pClient);
    // pEnrollment.setUser(daoFactory.getHmisUserDao().findByUsername(caller));
    pEnrollment.setDateCreated(
        (new Date()).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
    daoFactory.getEnrollmentDao().createEnrollment(pEnrollment);

    enrollment.setEnrollmentId(pEnrollment.getId());
    return enrollment;
  }