Refine search
/** * Creates new {@link Sort} with potentially unsafe {@link Order} instances. * * @param properties must not be {@literal null}. * @return */ public Sort withUnsafe(String... properties) { Assert.notEmpty(properties, "Properties must not be empty!"); Assert.noNullElements(properties, "Properties must not contain null values!"); List<Order> orders = new ArrayList<>(properties.length); for (String property : properties) { orders.add(new JpaOrder(getDirection(), property, getNullHandling(), isIgnoreCase(), this.unsafe)); } return Sort.by(orders); }
/** * Adds a {@link Sort} to the {@link Query} instance. * * @param sort * @return */ public Query with(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); if (sort.isUnsorted()) { return this; } sort.stream().filter(Order::isIgnoreCase).findFirst().ifPresent(it -> { throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! " + "MongoDB does not support sorting ignoring case currently!", it.getProperty())); }); this.sort = this.sort.and(sort); return this; }
@Override public Optional<Launch> findLastLaunch(String projectId, String mode) { Query query = query(where(PROJECT_ID_REFERENCE).is(projectId)).addCriteria(where(STATUS).ne(IN_PROGRESS)) .addCriteria(where(MODE).is(mode)) .limit(1) .with(new Sort(DESC, START_TIME)); List<Launch> launches = mongoTemplate.find(query, Launch.class); return !launches.isEmpty() ? Optional.of(launches.get(0)) : Optional.empty(); }
public Sort getMappedSort(Sort sort, CassandraPersistentEntity<?> entity) { Assert.notNull(sort, "Sort must not be null"); Assert.notNull(entity, "CassandraPersistentEntity must not be null"); if (!sort.iterator().hasNext()) { return sort; } List<Order> mappedOrders = new ArrayList<>(); for (Order order : sort) { ColumnName columnName = ColumnName.from(order.getProperty()); Field field = createPropertyField(entity, columnName); Order mappedOrder = getCqlIdentifier(columnName, field) .map(cqlIdentifier -> new Order(order.getDirection(), cqlIdentifier.toCql())).orElse(order); mappedOrders.add(mappedOrder); } return Sort.by(mappedOrders); }
@Override public <R> Optional<R> findOne(Specification<T> spec, Class<R> projectionType) { TypedQuery<T> query = getQuery(spec, Sort.unsorted()); try { T result = query.getSingleResult(); return Optional.ofNullable(projectionFactory.createProjection(projectionType, result)); } catch (NoResultException e) { return Optional.empty(); } }
@Override public Optional<Action> findOldestActiveActionByTarget(final String controllerId) { if (!actionRepository.activeActionExistsForControllerId(controllerId)) { return Optional.empty(); } // used in favorite to findFirstByTargetAndActiveOrderByIdAsc due to // DATAJPA-841 issue. return actionRepository.findFirstByTargetControllerIdAndActive(new Sort(Direction.ASC, "id"), controllerId, true); }
/** * Static factory method to create a {@link Query} using the provided {@link CriteriaDefinition}. * * @param criteriaDefinitions must not be {@literal null}. * @return the {@link Query} for {@link CriteriaDefinition}s. */ public static Query query(Iterable<? extends CriteriaDefinition> criteriaDefinitions) { Assert.notNull(criteriaDefinitions, "CriteriaDefinitions must not be null"); List<CriteriaDefinition> collect = stream(criteriaDefinitions.spliterator(), false) .collect(Collectors.toList()); return new Query(collect, Columns.empty(), Sort.unsorted(), Optional.empty(), Optional.empty(), Optional.empty(), false); }
@Override public List<T> findAllById(Iterable<ID> ids) { Assert.notNull(ids, "The given Iterable of Id's must not be null!"); if (!ids.iterator().hasNext()) { return Collections.emptyList(); } if (entityInformation.hasCompositeId()) { List<T> results = new ArrayList<T>(); for (ID id : ids) { findById(id).ifPresent(results::add); } return results; } ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation); TypedQuery<T> query = getQuery(specification, Sort.unsorted()); return query.setParameter(specification.parameter, ids).getResultList(); }
@Override public Message<?> pollMessageFromGroup(final Object groupId) { Assert.notNull(groupId, "'groupId' must not be null"); Query query = whereGroupIdIs(groupId).with(Sort.by(GROUP_UPDATE_TIMESTAMP_KEY, SEQUENCE)); MessageWrapper messageWrapper = this.template.findAndRemove(query, MessageWrapper.class, this.collectionName); Message<?> message = null; if (messageWrapper != null) { message = messageWrapper.getMessage(); } updateGroup(groupId, lastModifiedUpdate()); return message; }
@Override public Message<?> pollMessageFromGroup(Object groupId) { Assert.notNull(groupId, "'groupId' must not be null"); Sort sort = Sort.by(MessageDocumentFields.LAST_MODIFIED_TIME, MessageDocumentFields.SEQUENCE); if (this.priorityEnabled) { sort = Sort.by(Sort.Direction.DESC, MessageDocumentFields.PRIORITY).and(sort); } Query query = groupIdQuery(groupId).with(sort); MessageDocument document = this.mongoTemplate.findAndRemove(query, MessageDocument.class, this.collectionName); Message<?> message = null; if (document != null) { message = document.getMessage(); } return message; }
/** * Creates a new {@link Sort} for the given properties. * * @param properties must not be {@literal null}. * @return */ public static Sort by(String... properties) { Assert.notNull(properties, "Properties must not be null!"); return properties.length == 0 ? Sort.unsorted() : new Sort(properties); }
/** * Translates {@link Sort} orders from Jackson-mapped field names to {@link PersistentProperty} names. Properties * that cannot be resolved are dropped. * * @param input must not be {@literal null}. * @param rootEntity must not be {@literal null}. * @return {@link Sort} with translated field names or {@literal null} if translation dropped all sort fields. */ public Sort translateSort(Sort input, PersistentEntity<?, ?> rootEntity) { Assert.notNull(input, "Sort must not be null!"); Assert.notNull(rootEntity, "PersistentEntity must not be null!"); List<Order> filteredOrders = new ArrayList<Order>(); for (Order order : input) { List<String> iteratorSource = new ArrayList<String>(); Matcher matcher = SPLITTER.matcher("_" + order.getProperty()); while (matcher.find()) { iteratorSource.add(matcher.group(1)); } String mappedPropertyPath = getMappedPropertyPath(rootEntity, iteratorSource); if (mappedPropertyPath != null) { filteredOrders.add(order.withProperty(mappedPropertyPath)); } } return filteredOrders.isEmpty() ? Sort.unsorted() : Sort.by(filteredOrders); }
@Override public Iterable<D> findAll(Sort sort) { Assert.notNull(sort, "sort must not be null"); Iterator<Sort.Order> iter = sort.iterator(); String query = findAllQuery + "ORDER BY "; while (iter.hasNext()) { Sort.Order order = iter.next(); query = query + order.getProperty() + " " + order.getDirection(); if (iter.hasNext()) { query = query + ", "; } } return jdbcTemplate.query(query, rowMapper); }
/** * Creates the actual query object applying the given {@link Sort} parameter. Use this method in case you haven't * provided a {@link ParameterAccessor} in the first place but want to apply dynamic sorting nevertheless. * * @param dynamicSort must not be {@literal null}. * @return */ public T createQuery(Sort dynamicSort) { Assert.notNull(dynamicSort, "DynamicSort must not be null!"); return complete(createCriteria(tree), tree.getSort().and(dynamicSort)); }
/** * Apply sorting for the given query. * * @param query the query * @param sort the sort * @return the string */ public static String applySorting(String query, Sort sort) { Assert.hasText(query); if (null == sort || !sort.iterator().hasNext()) { return query; } throw new UnsupportedOperationException("Not implemented"); }
/** * Creates a new unsorted {@link PageRequest}. * * @param page zero-based page index. * @param size the size of the page to be returned. * @throws IllegalArgumentException for page requests other than the first page. */ public static CassandraPageRequest of(int page, int size) { Assert.isTrue(page == 0, "Cannot create a Cassandra page request for an indexed page other than the first page (0)."); return of(page, size, Sort.unsorted()); }