@Override public <S extends T> List<S> findAll(Example<S> example, Sort sort) { return getQuery(new ExampleSpecification<S>(example), example.getProbeType(), sort).getResultList(); }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example) { Assert.notNull(example, "Example must not be null!"); return getMappedExample(example, mappingContext.getRequiredPersistentEntity(example.getProbeType())); }
@Override public <S extends T> Optional<S> findOne(Example<S> example) { try { return Optional.of( getQuery(new ExampleSpecification<S>(example), example.getProbeType(), Sort.unsorted()).getSingleResult()); } catch (NoResultException e) { return Optional.empty(); } }
private Set<Class<?>> getTypesToMatch(Example<?> example) { Set<Class<?>> types = new HashSet<>(); for (TypeInformation<?> reference : mappingContext.getManagedTypes()) { if (example.getProbeType().isAssignableFrom(reference.getType())) { types.add(reference.getType()); } } return types; }
@Override public <S extends T> boolean exists(Example<S> example) { return !getQuery(new ExampleSpecification<S>(example), example.getProbeType(), Sort.unsorted()).getResultList() .isEmpty(); }
@Override public <S extends T> List<S> findAll(Example<S> example) { return getQuery(new ExampleSpecification<S>(example), example.getProbeType(), Sort.unsorted()).getResultList(); }
@Override public <S extends T> long count(Example<S> example) { return executeCountQuery(getCountQuery(new ExampleSpecification<S>(example), example.getProbeType())); }
@Override public <S extends T> Optional<S> findOne(Example<S> example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); return Optional .ofNullable(mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName())); }
@Override public <S extends T> Mono<S> findOne(Example<S> example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); q.limit(2); return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()).buffer(2) .map(vals -> { if (vals.size() > 1) { throw new IncorrectResultSizeDataAccessException(1); } return vals.iterator().next(); }).next(); }
@Override public <S extends T> Page<S> findAll(final Example<S> example, Pageable pageable) { Assert.notNull(example, "Sample must not be null!"); Assert.notNull(pageable, "Pageable must not be null!"); Query q = new Query(new Criteria().alike(example)).with(pageable); List<S> list = mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()); return PageableExecutionUtils.getPage(list, pageable, () -> mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName())); }
@Override public <S extends T> List<S> findAll(Example<S> example, Sort sort) { Assert.notNull(example, "Sample must not be null!"); Assert.notNull(sort, "Sort must not be null!"); Query q = new Query(new Criteria().alike(example)).with(sort); return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()); }
@Override public <S extends T> Flux<S> findAll(Example<S> example, Sort sort) { Assert.notNull(example, "Sample must not be null!"); Assert.notNull(sort, "Sort must not be null!"); Query query = new Query(new Criteria().alike(example)).with(sort); return mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName()); }
@Override public <S extends T> long count(Example<S> example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); return mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName()); }
@Override public <S extends T> Mono<Boolean> exists(Example<S> example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); return mongoOperations.exists(q, example.getProbeType(), entityInformation.getCollectionName()); }
@Override public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) { ExampleSpecification<S> spec = new ExampleSpecification<>(example); Class<S> probeType = example.getProbeType(); TypedQuery<S> query = getQuery(new ExampleSpecification<>(example), probeType, pageable); return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec); }
@Override public <S extends T> boolean exists(Example<S> example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); return mongoOperations.exists(q, example.getProbeType(), entityInformation.getCollectionName()); }
@Override public <S extends T> Mono<Long> count(Example<S> example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); return mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName()); }
/** * Extract the {@link Predicate} representing the {@link Example}. * * @param root must not be {@literal null}. * @param cb must not be {@literal null}. * @param example must not be {@literal null}. * @return never {@literal null}. */ public static <T> Predicate getPredicate(Root<T> root, CriteriaBuilder cb, Example<T> example) { Assert.notNull(root, "Root must not be null!"); Assert.notNull(cb, "CriteriaBuilder must not be null!"); Assert.notNull(example, "Example must not be null!"); ExampleMatcher matcher = example.getMatcher(); List<Predicate> predicates = getPredicates("", cb, root, root.getModel(), example.getProbe(), example.getProbeType(), new ExampleMatcherAccessor(matcher), new PathNode("root", null, example.getProbe())); if (predicates.isEmpty()) { return cb.isTrue(cb.literal(true)); } if (predicates.size() == 1) { return predicates.iterator().next(); } Predicate[] array = predicates.toArray(new Predicate[0]); return matcher.isAllMatching() ? cb.and(array) : cb.or(array); }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @param entity must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) { Assert.notNull(example, "Example must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); Document reference = (Document) converter.convertToMongoType(example.getProbe()); if (entity.getIdProperty() != null && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) { Object identifier = entity.getIdentifierAccessor(example.getProbe()).getIdentifier(); if (identifier == null) { reference.remove(entity.getIdProperty().getFieldName()); } } ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor); Document flattened = ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new Document(SerializationUtils.flattenMap(reference)); Document result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened); return updateTypeRestrictions(result, example); }
/** * Retrieve a mapped {@link RedisOperationChain} to query secondary indexes given {@link Example}. * * @param example must not be {@literal null}. * @return the mapped {@link RedisOperationChain}. */ public RedisOperationChain getMappedExample(Example<?> example) { RedisOperationChain chain = new RedisOperationChain(); ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", example.getProbe(), mappingContext.getRequiredPersistentEntity(example.getProbeType()), matcherAccessor, example.getMatcher().getMatchMode(), chain); return chain; }