UserFacadeEjb implements UserFacade { @Override public List<UserDto> getIndexList(UserCriteria userCriteria, int first, int max, List<SortProperty> sortProperties) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> cq = cb.createQuery(User.class); Root<User> user = cq.from(User.class); Join<User, District> district = user.join(User.DISTRICT, JoinType.LEFT); Join<User, Location> address = user.join(User.ADDRESS, JoinType.LEFT); Predicate filter = userService.buildCriteriaFilter(userCriteria, cb, user); if (filter != null) { cq.where(filter); } if (sortProperties != null && sortProperties.size() > 0) { List<Order> order = new ArrayList<Order>(sortProperties.size()); for (SortProperty sortProperty : sortProperties) { Expression<?> expression; switch (sortProperty.propertyName) { case UserDto.UUID: case UserDto.ACTIVE: case UserDto.USER_NAME: case UserDto.USER_EMAIL: expression = user.get(sortProperty.propertyName); break; case UserDto.NAME: expression = user.get(User.FIRST_NAME); order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression)); expression = user.get(User.LAST_NAME); break; case UserDto.DISTRICT: expression = district.get(District.NAME); break; case UserDto.ADDRESS: expression = address.get(Location.REGION); break; default: throw new IllegalArgumentException(sortProperty.propertyName); } order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression)); } cq.orderBy(order); } else { cq.orderBy(cb.desc(user.get(User.CHANGE_DATE))); } cq.select(user); List<User> resultList = em.createQuery(cq).setFirstResult(first).setMaxResults(max).getResultList(); return resultList.stream().map(u -> toDto(u)).collect(Collectors.toList()); }  @Override List<UserReferenceDto> getUsersByRegionAndRoles(RegionReferenceDto regionRef, UserRole... assignableRoles); @Override List<UserReferenceDto> getUserRefsByDistrict(DistrictReferenceDto districtRef, boolean includeSupervisors, UserRole... userRoles); @Override List<UserReferenceDto> getAllUserRefs(); @Override List<UserDto> getUsersByAssociatedOfficer(UserReferenceDto associatedOfficerRef, UserRole... userRoles); @Override List<UserDto> getAllAfter(Date date); @Override List<UserDto> getByUuids(List<String> uuids); @Override List<String> getAllUuids(); @Override UserDto getByUuid(String uuid); @Override UserDto getByUserName(String userName); @Override UserDto saveUser(UserDto dto); @Override List<UserDto> getIndexList(UserCriteria userCriteria, int first, int max, List<SortProperty> sortProperties); @Override long count(UserCriteria userCriteria); static UserDto toDto(User source); static UserReferenceDto toReferenceDto(User entity); @Override boolean isLoginUnique(String uuid, String userName); @Override String resetPassword(String uuid); @Override UserDto getCurrentUser(); @Override UserReferenceDto getCurrentUserAsReference(); @Override Set<UserRole> getValidLoginRoles(String userName, String password); @Override void removeUserAsSurveillanceAndContactOfficer(String userUuid);  }
@Test public void testGetIndexList() { RDCFEntities rdcf = creator.createRDCFEntities(); Region region = getBean(RegionService.class).getByUuid(rdcf.region.getUuid()); creator.createDistrict("district 2", region); creator.createUser(rdcf, "my", "User", UserRole.CASE_OFFICER, UserRole.CONTACT_OFFICER); creator.createUser(rdcf, UserRole.SURVEILLANCE_SUPERVISOR, UserRole.CONTACT_SUPERVISOR); creator.createUser(rdcf, UserRole.SURVEILLANCE_OFFICER, UserRole.DISTRICT_OBSERVER); List<UserDto> result; result = getUserFacade().getIndexList(new UserCriteria(), 0, 100, null); assertThat(result, hasSize(4)); assertThat(result.stream().map(e -> e.getUuid()).collect(Collectors.toSet()), hasSize(4)); result = getUserFacade().getIndexList(new UserCriteria().freeText("myUser"), 0, 100, null); assertThat(result, hasSize(1)); assertThat(result.get(0).getUserName(), equalTo("myUser")); }
@Test public void testGetIndexListFilteredAndOrderedByAddress() { List<UserDto> result = getUserFacade().getIndexList(new UserCriteria().freeText("min"), 0, 100, Collections.singletonList(new SortProperty(UserDto.ADDRESS))); assertThat(result, hasSize(1)); assertThat(result.get(0).getUserName(), equalTo("admin")); }