private boolean isTypeRestricting(Example example) { if (example.getMatcher() instanceof UntypedExampleMatcher) { return false; } if (example.getMatcher().getIgnoredPaths().isEmpty()) { return true; } for (String path : example.getMatcher().getIgnoredPaths()) { if (this.converter.getTypeMapper().isTypeKey(path)) { return false; } } return true; }
/** * 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; }
/** * 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); }
private boolean isTypeRestricting(Example example) { if (example.getMatcher() instanceof UntypedExampleMatcher) { return false; } if (example.getMatcher().getIgnoredPaths().isEmpty()) { return true; } for (String path : example.getMatcher().getIgnoredPaths()) { if (this.converter.getTypeMapper().isTypeKey(path)) { return false; } } return true; }
/** * 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; }
/** * 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; }
final String fullJavaPath, Object value) { final String delimiter = example.getMatcher().isAllMatching() ? " AND " : " OR "; if (predicateBuilder.length() > 0) { predicateBuilder.append(delimiter); final ExampleMatcher.PropertySpecifier specifier = example.getMatcher().getPropertySpecifiers() .getForPath(fullPath); if (specifier != null && value != null) { clause = String.format("e.%s == null", fullPath); } else if (String.class.isAssignableFrom(value.getClass())) { final boolean ignoreCase = specifier == null ? example.getMatcher().isIgnoreCaseEnabled() : (specifier.getIgnoreCase() == null ? false : specifier.getIgnoreCase()); final ExampleMatcher.StringMatcher stringMatcher = (specifier == null || specifier.getStringMatcher() == ExampleMatcher.StringMatcher.DEFAULT) ? example.getMatcher().getDefaultStringMatcher() : specifier.getStringMatcher(); final String string = (String) value;
switch (example.getMatcher().getDefaultStringMatcher()) { case EXACT: likeType = LikeType.EQUAL_TO; example.getMatcher().isIgnoreCaseEnabled(), likeType);
switch (example.getMatcher().getDefaultStringMatcher()) { case EXACT: likeType = LikeType.EQUAL_TO; example.getMatcher().isIgnoreCaseEnabled(), likeType);
private <T> void validateExample(Example<T> example) { Assert.notNull(example, "A non-null example is expected"); ExampleMatcher matcher = example.getMatcher(); if (!matcher.isAllMatching()) { throw new DatastoreDataException("Unsupported MatchMode. Only MatchMode.ALL is supported"); } if (matcher.isIgnoreCaseEnabled()) { throw new DatastoreDataException("Ignore case matching is not supported"); } if (!(matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.EXACT || matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.DEFAULT)) { throw new DatastoreDataException("Unsupported StringMatcher. Only EXACT and DEFAULT are supported"); } Optional<String> path = example.getMatcher().getIgnoredPaths().stream().filter((s) -> s.contains(".")).findFirst(); if (path.isPresent()) { throw new DatastoreDataException("Ignored paths deeper than 1 are not supported"); } if (matcher.getPropertySpecifiers().hasValues()) { throw new DatastoreDataException("Property matchers are not supported"); } } }
/** * 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); }
/** * This method will create an invocation that had it occurred on a query method would provide sufficient * data for a parsed query method expression to be evaluated * @param descriptor the query descriptor * @param example the example that is used for evaluation * @return the fake method invocation corresponding to the example probe */ private Invocation createInvocation(QueryDescriptor descriptor, Example example) { final List<Object> values = new ArrayList<>(); //since according to http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example //the matcher only supports AND condition, so, we expect to see only one branch final List<List<Parameter>> branches = descriptor.getBranches(); final List<Parameter> parameters = branches.get(0); for (Parameter parameter : parameters) { final String propertyPath = parameter.getPath(); final Object propertyValue = PropertyUtils.getPropertyValue(example.getProbe(), propertyPath); final ExampleMatcher.PropertySpecifier specifier = example.getMatcher().getPropertySpecifiers().getForPath(propertyPath); values.add(specifier == null ? propertyValue : specifier.getPropertyValueTransformer().convert(propertyValue)); } return new ImmutableInvocation(null, values.toArray()); }
/** * This method will create an invocation that had it occurred on a query method would provide sufficient * data for a parsed query method expression to be evaluated * @param descriptor the query descriptor * @param example the example that is used for evaluation * @return the fake method invocation corresponding to the example probe */ private Invocation createInvocation(QueryDescriptor descriptor, Example example) { final List<Object> values = new ArrayList<>(); //since according to http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example //the matcher only supports AND condition, so, we expect to see only one branch final List<List<Parameter>> branches = descriptor.getBranches(); final List<Parameter> parameters = branches.get(0); for (Parameter parameter : parameters) { final String propertyPath = parameter.getPath(); final Object propertyValue = PropertyUtils.getPropertyValue(example.getProbe(), propertyPath); final ExampleMatcher.PropertySpecifier specifier = example.getMatcher().getPropertySpecifiers().getForPath(propertyPath); values.add(specifier == null ? propertyValue : specifier.getPropertyValueTransformer().convert(propertyValue)); } return new ImmutableInvocation(null, values.toArray()); }
private void traversePropertyTree( final Example<T> example, final StringBuilder predicateBuilder, final Map<String, Object> bindVars, final String path, final String javaPath, final ArangoPersistentEntity<?> entity, final Object object) { final PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(object); entity.doWithProperties((final ArangoPersistentProperty property) -> { if (property.getFrom().isPresent() || property.getTo().isPresent() || property.getRelations().isPresent()) { return; } final String fullPath = path + (path.length() == 0 ? "" : ".") + property.getFieldName(); final String fullJavaPath = javaPath + (javaPath.length() == 0 ? "" : ".") + property.getName(); final Object value = accessor.getProperty(property); if (property.isEntity() && value != null) { final ArangoPersistentEntity<?> persistentEntity = context.getPersistentEntity(property.getType()); traversePropertyTree(example, predicateBuilder, bindVars, fullPath, fullJavaPath, persistentEntity, value); } else if (!example.getMatcher().isIgnoredPath(fullJavaPath) && (value != null || example.getMatcher().getNullHandler().equals(ExampleMatcher.NullHandler.INCLUDE))) { addPredicate(example, predicateBuilder, bindVars, fullPath, fullJavaPath, value); } }); }
private void traversePropertyTree( final Example<T> example, final StringBuilder predicateBuilder, final Map<String, Object> bindVars, final String path, final String javaPath, final ArangoPersistentEntity<?> entity, final Object object) { final PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(object); entity.doWithProperties((final ArangoPersistentProperty property) -> { if (property.getFrom().isPresent() || property.getTo().isPresent() || property.getRelations().isPresent()) { return; } final String fullPath = path + (path.length() == 0 ? "" : ".") + property.getFieldName(); final String fullJavaPath = javaPath + (javaPath.length() == 0 ? "" : ".") + property.getName(); final Object value = accessor.getProperty(property); if (property.isEntity() && value != null) { final ArangoPersistentEntity<?> persistentEntity = context.getPersistentEntity(property.getType()); traversePropertyTree(example, predicateBuilder, bindVars, fullPath, fullJavaPath, persistentEntity, value); } else if (!example.getMatcher().isIgnoredPath(fullJavaPath) && (value != null || example.getMatcher().getNullHandler().equals(ExampleMatcher.NullHandler.INCLUDE))) { addPredicate(example, predicateBuilder, bindVars, fullPath, fullJavaPath, value); } }); }
builder.setKind(persistentEntity.kindName()); ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); matcherAccessor.getPropertySpecifiers(); LinkedList<StructuredQuery.Filter> filters = new LinkedList<>(); persistentEntity.doWithColumnBackedProperties((persistentProperty) -> { if (!example.getMatcher().isIgnoredPath(persistentProperty.getName())) { String fieldName = persistentProperty.getFieldName(); Value<?> value = probeEntity.getValue(fieldName); if (value instanceof NullValue && example.getMatcher().getNullHandler() != ExampleMatcher.NullHandler.INCLUDE) {
final OperatorContext operatorContext = configuration.getDescriptionExtractor().getOperatorContext(); final Map<String, Object> values = extractValues(example.getProbe()); final ExampleMatcher matcher = example.getMatcher(); final List<Parameter> parameters = new ArrayList<>(); int index = 0;
final OperatorContext operatorContext = configuration.getDescriptionExtractor().getOperatorContext(); final Map<String, Object> values = extractValues(example.getProbe()); final ExampleMatcher matcher = example.getMatcher(); final List<Parameter> parameters = new ArrayList<>(); int index = 0;