private static Optional<Order> toOrder(String property, Optional<Direction> direction) { if (!StringUtils.hasText(property)) { return Optional.empty(); } return Optional.of(direction.map(it -> new Order(it, property)).orElseGet(() -> Order.by(property))); }
/** * Converts the given {@link OrderSpecifier} into an {@link Order}. * * @param orderSpecifier must not be {@literal null}. * @return */ private static Order toOrder(OrderSpecifier<?> orderSpecifier) { Assert.notNull(orderSpecifier, "Order specifier must not be null!"); Expression<?> target = orderSpecifier.getTarget(); Object targetElement = target instanceof Path ? preparePropertyPath((Path<?>) target) : target; Assert.notNull(targetElement, "Target element must not be null!"); return Order.by(targetElement.toString()).with(orderSpecifier.isAscending() ? Direction.ASC : Direction.DESC); }
/** * Creates an {@link Order} instance from the given property source, direction and domain class. If the domain class * is given, we will use it for nested property traversal checks. * * @param propertySource * @param direction must not be {@literal null}. * @param domainClass must not be {@literal null}. * @return * @see PropertyPath#from(String, Class) */ private Order createOrder(String propertySource, Optional<Direction> direction, Optional<Class<?>> domainClass) { return domainClass.map(type -> { PropertyPath propertyPath = PropertyPath.from(propertySource, type); return direction.map(it -> new Order(it, propertyPath.toDotPath())) .orElseGet(() -> Order.by(propertyPath.toDotPath())); }).orElseGet(() -> direction// .map(it -> new Order(it, StringUtils.uncapitalize(propertySource))) .orElseGet(() -> Order.by(StringUtils.uncapitalize(propertySource)))); }
@Override public Sort getMappedSort(Class<?> typeToRead, Sort sort) { RelationalPersistentEntity<?> entity = getPersistentEntity(typeToRead); if (entity == null) { return sort; } List<Order> mappedOrder = new ArrayList<>(); for (Order order : sort) { RelationalPersistentProperty persistentProperty = entity.getPersistentProperty(order.getProperty()); if (persistentProperty == null) { mappedOrder.add(order); } else { mappedOrder .add(Order.by(persistentProperty.getColumnName()).with(order.getNullHandling()).with(order.getDirection())); } } return Sort.by(mappedOrder); }
@Test public void getActiveUserStoriesByProjectNameTest() { DashboardDTO dashboard = TestObjectFactory.createDashboard(); Feature story1 = TestObjectFactory.createActiveStory(); Feature story2 = TestObjectFactory.createActiveStory(); List<Feature> stories = new ArrayList<>(); stories.add(story1); stories.add(story2); when(featureRepository.findActiveUserStoriesByBoards(Collections.singletonList(dashboard.getName()), Sort.by(Order.by("sStatus")))).thenReturn(stories); List<Feature> activeStoriesByDashboardName = featureService.getActiveUserStoriesByBoards(Collections.singletonList(dashboard.getName())); verify(featureRepository, times(1)) .findActiveUserStoriesByBoards(Collections.singletonList(dashboard.getName()), Sort.by(Order.by("sStatus"))); assertThat(activeStoriesByDashboardName.get(0)).isEqualTo(story1); assertThat(activeStoriesByDashboardName.get(1)).isEqualTo(story2); }
@Override public Sort getMappedSort(Class<?> typeToRead, Sort sort) { RelationalPersistentEntity<?> entity = getPersistentEntity(typeToRead); if (entity == null) { return sort; } List<Order> mappedOrder = new ArrayList<>(); for (Order order : sort) { RelationalPersistentProperty persistentProperty = entity.getPersistentProperty(order.getProperty()); if (persistentProperty == null) { mappedOrder.add(order); } else { mappedOrder .add(Order.by(persistentProperty.getColumnName()).with(order.getNullHandling()).with(order.getDirection())); } } return Sort.by(mappedOrder); }